From 031c1e6143093d731477fb4f266f466477975544 Mon Sep 17 00:00:00 2001 From: Kalle Olavi Niemitalo Date: Wed, 16 Jul 2008 14:50:41 +0300 Subject: [PATCH] 1031: Replace jsrt with spidermonkey_runtime src/ecmascript/spidermonkey/ now uses a JSRuntime managed by spidermonkey-shared.c. --- src/ecmascript/spidermonkey-shared.c | 16 +++++++++++++++- src/ecmascript/spidermonkey.c | 21 +++++++++++---------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/ecmascript/spidermonkey-shared.c b/src/ecmascript/spidermonkey-shared.c index 161b99004..6d8875519 100644 --- a/src/ecmascript/spidermonkey-shared.c +++ b/src/ecmascript/spidermonkey-shared.c @@ -29,11 +29,24 @@ int spidermonkey_runtime_addref(void) { if (!spidermonkey_runtime) { + JSContext *dummy; + assert(spidermonkey_runtime_refcount == 0); if_assert_failed return 0; - spidermonkey_runtime = JS_NewRuntime(1L * 1024L * 1024L); + + spidermonkey_runtime = JS_NewRuntime(4L * 1024L * 1024L); if (!spidermonkey_runtime) return 0; + + /* XXX: This is a hack to avoid a crash on exit. + * SMJS will crash on JS_DestroyRuntime if the given + * runtime has never had any context created, which + * will be the case if one closes ELinks without + * having loaded any documents. */ + dummy = JS_NewContext(spidermonkey_runtime, 0); + if (dummy) JS_DestroyContext(dummy); + /* Else hope it works anyway. */ } + spidermonkey_runtime_refcount++; assert(spidermonkey_runtime_refcount > 0); if_assert_failed { spidermonkey_runtime_refcount--; return 0; } @@ -54,6 +67,7 @@ spidermonkey_runtime_release(void) if (spidermonkey_runtime_refcount == 0) { JS_DestroyRuntime(spidermonkey_runtime); spidermonkey_runtime = NULL; + JS_ShutDown(); } } diff --git a/src/ecmascript/spidermonkey.c b/src/ecmascript/spidermonkey.c index 6dae80096..267f81c58 100644 --- a/src/ecmascript/spidermonkey.c +++ b/src/ecmascript/spidermonkey.c @@ -55,7 +55,7 @@ /* TODO? Are there any which need to be implemented? */ -static JSRuntime *jsrt; +static int js_module_init_ok; static void error_reporter(JSContext *ctx, const char *message, JSErrorReport *report) @@ -137,19 +137,14 @@ setup_safeguard(struct ecmascript_interpreter *interpreter, static void spidermonkey_init(struct module *xxx) { - jsrt = JS_NewRuntime(0x400000UL); - /* XXX: This is a hack to avoid a crash on exit. SMJS will crash - * on JS_DestroyRuntime if the given runtime has never had any context - * created, which will be the case if one closes ELinks without having - * loaded any documents. */ - JS_DestroyContext(JS_NewContext(jsrt, 0)); + js_module_init_ok = spidermonkey_runtime_addref(); } static void spidermonkey_done(struct module *xxx) { - JS_DestroyRuntime(jsrt); - JS_ShutDown(); + if (js_module_init_ok) + spidermonkey_runtime_release(); } @@ -161,8 +156,10 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter) *statusbar_obj, *menubar_obj, *navigator_obj; assert(interpreter); + if (!js_module_init_ok) return NULL; - ctx = JS_NewContext(jsrt, 8192 /* Stack allocation chunk size */); + ctx = JS_NewContext(spidermonkey_runtime, + 8192 /* Stack allocation chunk size */); if (!ctx) return NULL; interpreter->backend_data = ctx; @@ -236,6 +233,7 @@ spidermonkey_put_interpreter(struct ecmascript_interpreter *interpreter) JSContext *ctx; assert(interpreter); + if (!js_module_init_ok) return; ctx = interpreter->backend_data; JS_DestroyContext(ctx); interpreter->backend_data = NULL; @@ -250,6 +248,7 @@ spidermonkey_eval(struct ecmascript_interpreter *interpreter, jsval rval; assert(interpreter); + if (!js_module_init_ok) return; ctx = interpreter->backend_data; setup_safeguard(interpreter, ctx); interpreter->ret = ret; @@ -266,6 +265,7 @@ spidermonkey_eval_stringback(struct ecmascript_interpreter *interpreter, jsval rval; assert(interpreter); + if (!js_module_init_ok) return NULL; ctx = interpreter->backend_data; setup_safeguard(interpreter, ctx); interpreter->ret = NULL; @@ -293,6 +293,7 @@ spidermonkey_eval_boolback(struct ecmascript_interpreter *interpreter, int ret; assert(interpreter); + if (!js_module_init_ok) return 0; ctx = interpreter->backend_data; setup_safeguard(interpreter, ctx); interpreter->ret = NULL;