diff options
author | Paul Gardiner <paul@glidos.net> | 2012-05-16 12:20:00 +0100 |
---|---|---|
committer | Paul Gardiner <paul@glidos.net> | 2012-05-16 12:20:00 +0100 |
commit | 9b0edbc781b6dcb4db140d83468d4a3a305f61c5 (patch) | |
tree | 48b8a4966a15dc5a5030cdae12b7dd7535509a87 /pdf | |
parent | 967f42c877cd9a732761957e1329776600505119 (diff) | |
download | mupdf-9b0edbc781b6dcb4db140d83468d4a3a305f61c5.tar.xz |
JavaScript: provide mechanism via which C++ js engines can fz_throw errors
Diffstat (limited to 'pdf')
-rw-r--r-- | pdf/pdf_js.c | 25 | ||||
-rw-r--r-- | pdf/pdf_jsimp_cpp.c | 118 | ||||
-rw-r--r-- | pdf/pdf_jsimp_cpp.h | 18 | ||||
-rw-r--r-- | pdf/pdf_jsimp_v8.cpp | 65 |
4 files changed, 204 insertions, 22 deletions
diff --git a/pdf/pdf_js.c b/pdf/pdf_js.c index 6af7c672..ba5cc524 100644 --- a/pdf/pdf_js.c +++ b/pdf/pdf_js.c @@ -126,6 +126,7 @@ pdf_js *pdf_new_js(pdf_document *doc) fz_catch(ctx) { pdf_drop_js(js); + js = NULL; } return js; @@ -145,10 +146,30 @@ void pdf_drop_js(pdf_js *js) void pdf_js_execute(pdf_js *js, char *code) { - pdf_jsimp_execute(js->imp, code); + if (js) + { + fz_context *ctx = js->doc->ctx; + fz_try(ctx) + { + pdf_jsimp_execute(js->imp, code); + } + fz_catch(ctx) + { + } + } } void pdf_js_execute_count(pdf_js *js, char *code, int count) { - pdf_jsimp_execute_count(js->imp, code, count); + if (js) + { + fz_context *ctx = js->doc->ctx; + fz_try(ctx) + { + pdf_jsimp_execute_count(js->imp, code, count); + } + fz_catch(ctx) + { + } + } }
\ No newline at end of file diff --git a/pdf/pdf_jsimp_cpp.c b/pdf/pdf_jsimp_cpp.c new file mode 100644 index 00000000..1e98d680 --- /dev/null +++ b/pdf/pdf_jsimp_cpp.c @@ -0,0 +1,118 @@ +/* This file contains wrapper functions for pdf_jsimp functions implemented + * in C++, from which calls to fz_throw aren't safe. The C++ versions + * return errors explicitly, and these wrappers then throw them. */ + +#include "fitz-internal.h" +#include "mupdf-internal.h" +#include "pdf_jsimp_cpp.h" + + +pdf_jsimp *pdf_new_jsimp(fz_context *ctx, void *jsctx) +{ + pdf_jsimp *jsi = NULL; + char *err = pdf_new_jsimp_cpp(ctx, jsctx, &jsi); + if (err != NULL) + fz_throw(ctx, "%s", err); + + return jsi; +} + +void pdf_drop_jsimp(pdf_jsimp *imp) +{ + if (imp) + { + fz_context *ctx = pdf_jsimp_ctx_cpp(imp); + char *err = pdf_drop_jsimp_cpp(imp); + if (err != NULL) + fz_warn(ctx, "%s", err); + } +} + +pdf_jsimp_type *pdf_jsimp_new_type(pdf_jsimp *imp, pdf_jsimp_dtr *dtr) +{ + pdf_jsimp_type *type = NULL; + char *err = pdf_jsimp_new_type_cpp(imp, dtr, &type); + if (err != NULL) + fz_throw(pdf_jsimp_ctx_cpp(imp), "%s", err); + + return type; +} + +void pdf_jsimp_drop_type(pdf_jsimp *imp, pdf_jsimp_type *type) +{ + char *err = pdf_jsimp_drop_type_cpp(imp, type); + if (err != NULL) + fz_warn(pdf_jsimp_ctx_cpp(imp), "%s", err); +} + +void pdf_jsimp_addmethod(pdf_jsimp *imp, pdf_jsimp_type *type, char *name, pdf_jsimp_method *meth) +{ + char *err = pdf_jsimp_addmethod_cpp(imp, type, name, meth); + if (err != NULL) + fz_throw(pdf_jsimp_ctx_cpp(imp), "%s", err); +} + +void pdf_jsimp_addproperty(pdf_jsimp *imp, pdf_jsimp_type *type, char *name, pdf_jsimp_getter *get, pdf_jsimp_setter *set) +{ + char *err = pdf_jsimp_addproperty_cpp(imp, type, name, get, set); + if (err != NULL) + fz_throw(pdf_jsimp_ctx_cpp(imp), "%s", err); +} + +void pdf_jsimp_set_global_type(pdf_jsimp *imp, pdf_jsimp_type *type) +{ + char *err = pdf_jsimp_set_global_type_cpp(imp, type); + if (err != NULL) + fz_throw(pdf_jsimp_ctx_cpp(imp), "%s", err); +} + +pdf_jsimp_obj *pdf_jsimp_new_obj(pdf_jsimp *imp, pdf_jsimp_type *type, void *natobj) +{ + pdf_jsimp_obj *obj = NULL; + char *err = pdf_jsimp_new_obj_cpp(imp, type, natobj, &obj); + if (err != NULL) + fz_throw(pdf_jsimp_ctx_cpp(imp), "%s", err); + + return obj; +} + +void pdf_jsimp_drop_obj(pdf_jsimp *imp, pdf_jsimp_obj *obj) +{ + char *err = pdf_jsimp_drop_obj_cpp(imp, obj); + if (err != NULL) + fz_warn(pdf_jsimp_ctx_cpp(imp), "%s", err); +} + +pdf_jsimp_obj *pdf_jsimp_fromString(pdf_jsimp *imp, char *str) +{ + pdf_jsimp_obj *obj = NULL; + char *err = pdf_jsimp_fromString_cpp(imp, str, &obj); + if (err != NULL) + fz_throw(pdf_jsimp_ctx_cpp(imp), "%s", err); + + return obj; +} + +char *pdf_jsimp_toString(pdf_jsimp *imp, pdf_jsimp_obj *obj) +{ + char *str = NULL; + char *err = pdf_jsimp_toString_cpp(imp, obj, &str); + if (err != NULL) + fz_throw(pdf_jsimp_ctx_cpp(imp), "%s", err); + + return str; +} + +void pdf_jsimp_execute(pdf_jsimp *imp, char *code) +{ + char *err = pdf_jsimp_execute_cpp(imp, code); + if (err != NULL) + fz_throw(pdf_jsimp_ctx_cpp(imp), "%s", err); +} + +void pdf_jsimp_execute_count(pdf_jsimp *imp, char *code, int count) +{ + char *err = pdf_jsimp_execute_count_cpp(imp, code, count); + if (err != NULL) + fz_throw(pdf_jsimp_ctx_cpp(imp), "%s", err); +} diff --git a/pdf/pdf_jsimp_cpp.h b/pdf/pdf_jsimp_cpp.h new file mode 100644 index 00000000..8b5de640 --- /dev/null +++ b/pdf/pdf_jsimp_cpp.h @@ -0,0 +1,18 @@ +/* C++ version of the pdf_jsimp api. C++ cannot safely call fz_throw, + * so C++ implementations return explicit errors in char * form. */ + + +fz_context *pdf_jsimp_ctx_cpp(pdf_jsimp *imp); +char *pdf_new_jsimp_cpp(fz_context *ctx, void *jsctx, pdf_jsimp **imp); +char *pdf_drop_jsimp_cpp(pdf_jsimp *imp); +char *pdf_jsimp_new_type_cpp(pdf_jsimp *imp, pdf_jsimp_dtr *dtr, pdf_jsimp_type **type); +char *pdf_jsimp_drop_type_cpp(pdf_jsimp *imp, pdf_jsimp_type *type); +char *pdf_jsimp_addmethod_cpp(pdf_jsimp *imp, pdf_jsimp_type *type, char *name, pdf_jsimp_method *meth); +char *pdf_jsimp_addproperty_cpp(pdf_jsimp *imp, pdf_jsimp_type *type, char *name, pdf_jsimp_getter *get, pdf_jsimp_setter *set); +char *pdf_jsimp_set_global_type_cpp(pdf_jsimp *imp, pdf_jsimp_type *type); +char *pdf_jsimp_new_obj_cpp(pdf_jsimp *imp, pdf_jsimp_type *type, void *natobj, pdf_jsimp_obj **obj); +char *pdf_jsimp_drop_obj_cpp(pdf_jsimp *imp, pdf_jsimp_obj *obj); +char *pdf_jsimp_fromString_cpp(pdf_jsimp *imp, char *str, pdf_jsimp_obj **obj); +char *pdf_jsimp_toString_cpp(pdf_jsimp *imp, pdf_jsimp_obj *obj, char **str); +char *pdf_jsimp_execute_cpp(pdf_jsimp *imp, char *code); +char *pdf_jsimp_execute_count_cpp(pdf_jsimp *imp, char *code, int count); diff --git a/pdf/pdf_jsimp_v8.cpp b/pdf/pdf_jsimp_v8.cpp index 837a4919..e30ffdb3 100644 --- a/pdf/pdf_jsimp_v8.cpp +++ b/pdf/pdf_jsimp_v8.cpp @@ -7,6 +7,7 @@ extern "C" { #include "fitz-internal.h" #include "mupdf-internal.h" +#include "pdf_jsimp_cpp.h" } #include <vector> @@ -161,28 +162,37 @@ public: } }; +extern "C" fz_context *pdf_jsimp_ctx_cpp(pdf_jsimp *imp) +{ + return reinterpret_cast<PDFJSImp *>(imp)->ctx; +} -extern "C" pdf_jsimp *pdf_new_jsimp(fz_context *ctx, void *jsctx) +extern "C" char *pdf_new_jsimp_cpp(fz_context *ctx, void *jsctx, pdf_jsimp **imp) { - return reinterpret_cast<pdf_jsimp *>(new PDFJSImp(ctx, jsctx)); + *imp = reinterpret_cast<pdf_jsimp *>(new PDFJSImp(ctx, jsctx)); + + return NULL; } -extern "C" void pdf_drop_jsimp(pdf_jsimp *imp) +extern "C" char *pdf_drop_jsimp_cpp(pdf_jsimp *imp) { delete reinterpret_cast<PDFJSImp *>(imp); + return NULL; } -extern "C" pdf_jsimp_type *pdf_jsimp_new_type(pdf_jsimp *imp, pdf_jsimp_dtr *dtr) +extern "C" char *pdf_jsimp_new_type_cpp(pdf_jsimp *imp, pdf_jsimp_dtr *dtr, pdf_jsimp_type **type) { PDFJSImp *vImp = reinterpret_cast<PDFJSImp *>(imp); PDFJSImpType *vType = new PDFJSImpType(vImp, dtr); vImp->types.push_back(vType); - return reinterpret_cast<pdf_jsimp_type *>(vType); + *type = reinterpret_cast<pdf_jsimp_type *>(vType); + return NULL; } -extern "C" void pdf_jsimp_drop_type(pdf_jsimp *imp, pdf_jsimp_type *type) +extern "C" char *pdf_jsimp_drop_type_cpp(pdf_jsimp *imp, pdf_jsimp_type *type) { /* Types are recorded and destroyed as part of PDFJSImp */ + return NULL; } static Handle<Value> callMethod(const Arguments &args) @@ -217,7 +227,7 @@ static Handle<Value> callMethod(const Arguments &args) return scope.Close(val); } -extern "C" void pdf_jsimp_addmethod(pdf_jsimp *imp, pdf_jsimp_type *type, char *name, pdf_jsimp_method *meth) +extern "C" char *pdf_jsimp_addmethod_cpp(pdf_jsimp *imp, pdf_jsimp_type *type, char *name, pdf_jsimp_method *meth) { PDFJSImpType *vType = reinterpret_cast<PDFJSImpType *>(type); HandleScope scope; @@ -225,6 +235,7 @@ extern "C" void pdf_jsimp_addmethod(pdf_jsimp *imp, pdf_jsimp_type *type, char * PDFJSImpMethod *pmeth = new PDFJSImpMethod(vType->imp->jsctx, meth); vType->templ->Set(String::New(name), FunctionTemplate::New(callMethod, External::New(pmeth))); vType->methods.push_back(pmeth); + return NULL; } static Handle<Value> getProp(Local<String> property, const AccessorInfo &info) @@ -269,7 +280,7 @@ static void setProp(Local<String> property, Local<Value> value, const AccessorIn delete obj; } -extern "C" void pdf_jsimp_addproperty(pdf_jsimp *imp, pdf_jsimp_type *type, char *name, pdf_jsimp_getter *get, pdf_jsimp_setter *set) +extern "C" char *pdf_jsimp_addproperty_cpp(pdf_jsimp *imp, pdf_jsimp_type *type, char *name, pdf_jsimp_getter *get, pdf_jsimp_setter *set) { PDFJSImpType *vType = reinterpret_cast<PDFJSImpType *>(type); HandleScope scope; @@ -277,15 +288,17 @@ extern "C" void pdf_jsimp_addproperty(pdf_jsimp *imp, pdf_jsimp_type *type, char PDFJSImpProperty *prop = new PDFJSImpProperty(vType->imp->jsctx, get, set); vType->templ->SetAccessor(String::New(name), getProp, setProp, External::New(prop)); vType->properties.push_back(prop); + return NULL; } -extern "C" void pdf_jsimp_set_global_type(pdf_jsimp *imp, pdf_jsimp_type *type) +extern "C" char *pdf_jsimp_set_global_type_cpp(pdf_jsimp *imp, pdf_jsimp_type *type) { PDFJSImp *vImp = reinterpret_cast<PDFJSImp *>(imp); PDFJSImpType *vType = reinterpret_cast<PDFJSImpType *>(type); HandleScope scope; vImp->context = Persistent<Context>::New(Context::New(NULL, vType->templ)); + return NULL; } static void gcCallback(Persistent<Value> val, void *parm) @@ -301,7 +314,7 @@ static void gcCallback(Persistent<Value> val, void *parm) delete gco; /* Disposes of the persistent handle */ } -extern "C" pdf_jsimp_obj *pdf_jsimp_new_obj(pdf_jsimp *imp, pdf_jsimp_type *type, void *natobj) +extern "C" char *pdf_jsimp_new_obj_cpp(pdf_jsimp *imp, pdf_jsimp_type *type, void *natobj, pdf_jsimp_obj **robj) { PDFJSImpType *vType = reinterpret_cast<PDFJSImpType *>(type); HandleScope scope; @@ -317,36 +330,48 @@ extern "C" pdf_jsimp_obj *pdf_jsimp_new_obj(pdf_jsimp *imp, pdf_jsimp_type *type gco->pobj.MakeWeak(gco, gcCallback); } - return reinterpret_cast<pdf_jsimp_obj *>(new PDFJSImpObject(obj)); + *robj = reinterpret_cast<pdf_jsimp_obj *>(new PDFJSImpObject(obj)); + return NULL; } -extern "C" void pdf_jsimp_drop_obj(pdf_jsimp *imp, pdf_jsimp_obj *obj) +extern "C" char *pdf_jsimp_drop_obj_cpp(pdf_jsimp *imp, pdf_jsimp_obj *obj) { delete reinterpret_cast<PDFJSImpObject *>(obj); + return NULL; } -extern "C" pdf_jsimp_obj *pdf_jsimp_fromString(pdf_jsimp *imp, char *str) +extern "C" char *pdf_jsimp_fromString_cpp(pdf_jsimp *imp, char *str, pdf_jsimp_obj **obj) { - return reinterpret_cast<pdf_jsimp_obj *>(new PDFJSImpObject(str)); + *obj = reinterpret_cast<pdf_jsimp_obj *>(new PDFJSImpObject(str)); + return NULL; } -extern "C" char *pdf_jsimp_toString(pdf_jsimp *imp, pdf_jsimp_obj *obj) +extern "C" char *pdf_jsimp_toString_cpp(pdf_jsimp *imp, pdf_jsimp_obj *obj, char **str) { - return reinterpret_cast<PDFJSImpObject *>(obj)->toString(); + *str = reinterpret_cast<PDFJSImpObject *>(obj)->toString(); + return NULL; } -extern "C" void pdf_jsimp_execute(pdf_jsimp *imp, char *code) +extern "C" char *pdf_jsimp_execute_cpp(pdf_jsimp *imp, char *code) { PDFJSImp *vImp = reinterpret_cast<PDFJSImp *>(imp); HandleScope scope; Context::Scope context_scope(vImp->context); - Script::Compile(String::New(code))->Run(); + Handle<Script> script = Script::Compile(String::New(code)); + if (script.IsEmpty()) + return "compile failed in pdf_jsimp_execute"; + script->Run(); + return NULL; } -extern "C" void pdf_jsimp_execute_count(pdf_jsimp *imp, char *code, int count) +extern "C" char *pdf_jsimp_execute_count_cpp(pdf_jsimp *imp, char *code, int count) { PDFJSImp *vImp = reinterpret_cast<PDFJSImp *>(imp); HandleScope scope; Context::Scope context_scope(vImp->context); - Script::Compile(String::New(code, count))->Run(); + Handle<Script> script = Script::Compile(String::New(code, count)); + if (script.IsEmpty()) + return "compile failed in pdf_jsimp_execute_count"; + script->Run(); + return NULL; }
\ No newline at end of file |