From 46235ec8cfad7f4b601a19ac7874727bfac8a84b Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Wed, 6 Jan 2016 14:02:33 +0100 Subject: js: Fix error handling bugs and one scoping bug. js_pcall was leaving crap on the stack in case of errors. AFSimple_Calculate needs access to the doc object, but cannot use the usual 'this' binding since it's a function. Add a global reference to the doc object MuPDF_Doc. --- source/pdf/js/pdf-js.c | 15 +++++++++++++-- source/pdf/js/pdf-util.js | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) (limited to 'source/pdf/js') diff --git a/source/pdf/js/pdf-js.c b/source/pdf/js/pdf-js.c index c03429a7..bc9fcae0 100644 --- a/source/pdf/js/pdf-js.c +++ b/source/pdf/js/pdf-js.c @@ -541,6 +541,9 @@ static void declare_dom(pdf_js *js) addmethod(J, "Doc.mailDoc", doc_mailDoc, 6); } js_setregistry(J, "Doc"); + + js_getregistry(J, "Doc"); + js_setglobal(J, "MuPDF_Doc"); /* for pdf-util.js use */ } static void preload_helpers(pdf_js *js) @@ -676,11 +679,19 @@ void pdf_js_execute(pdf_js *js, char *source) { if (js) { - puts(source); if (js_ploadstring(js->imp, "[pdf]", source)) + { + fz_warn(js->ctx, "%s", js_tostring(js->imp, -1)); + js_pop(js->imp, 1); return; + } js_getregistry(js->imp, "Doc"); /* set 'this' to the Doc object */ - js_pcall(js->imp, 0); + if (js_pcall(js->imp, 0)) + { + fz_warn(js->ctx, "%s", js_tostring(js->imp, -1)); + js_pop(js->imp, 1); + return; + } js_pop(js->imp, 1); } } diff --git a/source/pdf/js/pdf-util.js b/source/pdf/js/pdf-util.js index 9a62add0..299f80ac 100644 --- a/source/pdf/js/pdf-util.js +++ b/source/pdf/js/pdf-util.js @@ -819,7 +819,7 @@ function AFSimple_Calculate(op, list) for (i = 0; i < list.length; i++) { - var field = getField(list[i]); + var field = MuPDF_Doc.getField(list[i]); var value = Number(field.value); switch (op) -- cgit v1.2.3