diff options
author | Michael Drake <tlsa@netsurf-browser.org> | 2015-08-13 12:23:59 +0100 |
---|---|---|
committer | Michael Drake <tlsa@netsurf-browser.org> | 2015-08-13 12:23:59 +0100 |
commit | 16fd36eb9d13a71deba2d7a737efa16e1f953138 (patch) | |
tree | 41aabec842296aecf820d3b2cfcd64060357c150 | |
parent | c7a32294940c54294b96c0d0d789e0cffb1c03b3 (diff) | |
download | netsurf-16fd36eb9d13a71deba2d7a737efa16e1f953138.tar.gz netsurf-16fd36eb9d13a71deba2d7a737efa16e1f953138.tar.bz2 |
Proper error handling for JS context creation.
-rw-r--r-- | desktop/browser.c | 9 | ||||
-rw-r--r-- | javascript/dukky.c | 11 | ||||
-rw-r--r-- | javascript/js.h | 8 | ||||
-rw-r--r-- | javascript/jsapi.c | 11 | ||||
-rw-r--r-- | javascript/none.c | 6 |
5 files changed, 29 insertions, 16 deletions
diff --git a/desktop/browser.c b/desktop/browser.c index 18c7fa247..21b6c8815 100644 --- a/desktop/browser.c +++ b/desktop/browser.c @@ -854,11 +854,10 @@ nserror browser_window_initialise_common(enum browser_window_create_flags flags, assert(bw); /* new javascript context for each window/(i)frame */ - bw->jsctx = js_newcontext(nsoption_int(script_timeout), - slow_script, - NULL); - /* If bw->jsctx == NULL, it might be because we built with no JS. - * TODO: Error handling in the with JS case. */ + err = js_newcontext(nsoption_int(script_timeout), + slow_script, NULL, &bw->jsctx); + if (err != NSERROR_OK) + return err; if (flags & BW_CREATE_CLONE) { assert(existing != NULL); diff --git a/javascript/dukky.c b/javascript/dukky.c index 2fb7e5466..0358a572e 100644 --- a/javascript/dukky.c +++ b/javascript/dukky.c @@ -325,14 +325,16 @@ void js_finalise(void) #define DUKKY_NEW_PROTOTYPE(klass, uklass, klass_name) \ dukky_create_prototype(ctx, dukky_##klass##___proto, PROTO_NAME(uklass), klass_name) -jscontext *js_newcontext(int timeout, jscallback *cb, void *cbctx) +nserror js_newcontext(int timeout, jscallback *cb, void *cbctx, + jscontext **jsctx) { duk_context *ctx; jscontext *ret = calloc(1, sizeof(*ret)); + *jsctx = NULL; LOG("Creating new duktape javascript context"); - if (ret == NULL) return NULL; + if (ret == NULL) return NSERROR_NOMEM; ctx = ret->ctx = duk_create_heap_default(); - if (ret->ctx == NULL) { free(ret); return NULL; } + if (ret->ctx == NULL) { free(ret); return NSERROR_NOMEM; } /* Create the prototype stuffs */ duk_push_global_object(ctx); duk_push_boolean(ctx, true); @@ -341,7 +343,8 @@ jscontext *js_newcontext(int timeout, jscallback *cb, void *cbctx) /* Create prototypes here */ dukky_create_prototypes(ctx); - return ret; + *jsctx = ret; + return NSERROR_OK; } void js_destroycontext(jscontext *ctx) diff --git a/javascript/js.h b/javascript/js.h index 7102fcf0e..6f7de920f 100644 --- a/javascript/js.h +++ b/javascript/js.h @@ -23,6 +23,9 @@ #ifndef _NETSURF_JAVASCRIPT_JS_H_ #define _NETSURF_JAVASCRIPT_JS_H_ +#include "utils/errors.h" + + typedef struct jscontext jscontext; typedef struct jsobject jsobject; @@ -45,8 +48,11 @@ void js_finalise(void); * \param timeout elapsed wallclock time (in seconds) before \a callback is called * \param cb the callback when the runtime exceeds the timeout * \param cbctx The context to pass to the callback + * \param jsctx Updated to the created JS context + * \return NSERROR_OK on success, appropriate error otherwise. */ -jscontext *js_newcontext(int timeout, jscallback *cb, void *cbctx); +nserror js_newcontext(int timeout, jscallback *cb, void *cbctx, + jscontext **jsctx); /** Destroy a previously created context */ void js_destroycontext(jscontext *ctx); diff --git a/javascript/jsapi.c b/javascript/jsapi.c index 7780e77e6..5bac27510 100644 --- a/javascript/jsapi.c +++ b/javascript/jsapi.c @@ -313,17 +313,19 @@ disable_heartbeat(struct heartbeat *hb) #endif -jscontext *js_newcontext(int timeout, jscallback *cb, void *cbctx) +nserror js_newcontext(int timeout, jscallback *cb, void *cbctx, + jscontext **jsctx) { JSContext *cx; + *jsctx = NULL; if (rt == NULL) { - return NULL; + return NSERROR_OK; } cx = JS_NewContext(rt, 8192); if (cx == NULL) { - return NULL; + return NSERROR_NOMEM; } /* set options on context */ @@ -339,7 +341,8 @@ jscontext *js_newcontext(int timeout, jscallback *cb, void *cbctx) JSLOG("New Context %p", cx); - return (jscontext *)cx; + r*jsctx = (jscontext *)cx; + return NSERROR_OK; } void js_destroycontext(jscontext *ctx) diff --git a/javascript/none.c b/javascript/none.c index d4b8ce565..3ae1f4f54 100644 --- a/javascript/none.c +++ b/javascript/none.c @@ -35,9 +35,11 @@ void js_finalise(void) { } -jscontext *js_newcontext(int timeout, jscallback *cb, void *cbctx) +nserror js_newcontext(int timeout, jscallback *cb, void *cbctx, + jscontext **jsctx) { - return NULL; + *jsctx = NULL; + return NSERROR_OK; } void js_destroycontext(jscontext *ctx) |