0
0
mirror of https://github.com/rkd77/elinks.git synced 2025-06-30 22:19:29 -04:00

952, 954: Add spidermonkey_empty_context

This commit is contained in:
Kalle Olavi Niemitalo 2008-07-07 15:57:47 +03:00 committed by Kalle Olavi Niemitalo
parent 5ad675e244
commit 8f2f9e7265
2 changed files with 33 additions and 15 deletions

View File

@ -17,36 +17,50 @@
* perhaps others. */ * perhaps others. */
JSRuntime *spidermonkey_runtime; JSRuntime *spidermonkey_runtime;
/** A JSContext that can be used in JS_SetPrivate and JS_GetPrivate
* when no better one is available. This context has no global
* object, so scripts cannot be evaluated in it.
*
* XXX: This also works around 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. */
JSContext *spidermonkey_empty_context;
/** A reference count for ::spidermonkey_runtime so that modules using /** A reference count for ::spidermonkey_runtime so that modules using
* it can be initialized and shut down in arbitrary order. */ * it can be initialized and shut down in arbitrary order. */
static int spidermonkey_runtime_refcount; static int spidermonkey_runtime_refcount;
/** Add a reference to ::spidermonkey_runtime, and initialize it if /** Initialize ::spidermonkey_runtime and ::spidermonkey_empty_context.
* that is the first reference. * If already initialized, just increment the reference count.
*
* @return 1 if successful or 0 on error. If this succeeds, the * @return 1 if successful or 0 on error. If this succeeds, the
* caller must eventually call spidermonkey_runtime_release(). */ * caller must eventually call spidermonkey_runtime_release(). */
int int
spidermonkey_runtime_addref(void) spidermonkey_runtime_addref(void)
{ {
if (!spidermonkey_runtime) { if (spidermonkey_runtime_refcount == 0) {
JSContext *dummy; assert(spidermonkey_runtime == NULL);
assert(spidermonkey_empty_context == NULL);
assert(spidermonkey_runtime_refcount == 0);
if_assert_failed return 0; if_assert_failed return 0;
spidermonkey_runtime = JS_NewRuntime(4L * 1024L * 1024L); spidermonkey_runtime = JS_NewRuntime(4L * 1024L * 1024L);
if (!spidermonkey_runtime) return 0; if (!spidermonkey_runtime) return 0;
/* XXX: This is a hack to avoid a crash on exit. spidermonkey_empty_context = JS_NewContext(spidermonkey_runtime,
* SMJS will crash on JS_DestroyRuntime if the given 0);
* runtime has never had any context created, which if (!spidermonkey_empty_context) {
* will be the case if one closes ELinks without /* Perhaps JS_DestroyRuntime will now crash
* having loaded any documents. */ * because no context was created, but there's
dummy = JS_NewContext(spidermonkey_runtime, 0); * not much else to do. */
if (dummy) JS_DestroyContext(dummy); JS_DestroyRuntime(spidermonkey_runtime);
/* Else hope it works anyway. */ spidermonkey_runtime = NULL;
JS_ShutDown();
}
} }
assert(spidermonkey_runtime);
assert(spidermonkey_empty_context);
spidermonkey_runtime_refcount++; spidermonkey_runtime_refcount++;
assert(spidermonkey_runtime_refcount > 0); assert(spidermonkey_runtime_refcount > 0);
if_assert_failed { spidermonkey_runtime_refcount--; return 0; } if_assert_failed { spidermonkey_runtime_refcount--; return 0; }
@ -61,10 +75,13 @@ spidermonkey_runtime_release(void)
{ {
assert(spidermonkey_runtime_refcount > 0); assert(spidermonkey_runtime_refcount > 0);
assert(spidermonkey_runtime); assert(spidermonkey_runtime);
assert(spidermonkey_empty_context);
if_assert_failed return; if_assert_failed return;
--spidermonkey_runtime_refcount; --spidermonkey_runtime_refcount;
if (spidermonkey_runtime_refcount == 0) { if (spidermonkey_runtime_refcount == 0) {
JS_DestroyContext(spidermonkey_empty_context);
spidermonkey_empty_context = NULL;
JS_DestroyRuntime(spidermonkey_runtime); JS_DestroyRuntime(spidermonkey_runtime);
spidermonkey_runtime = NULL; spidermonkey_runtime = NULL;
JS_ShutDown(); JS_ShutDown();

View File

@ -19,6 +19,7 @@
#include "util/string.h" #include "util/string.h"
extern JSRuntime *spidermonkey_runtime; extern JSRuntime *spidermonkey_runtime;
extern JSContext *spidermonkey_empty_context;
int spidermonkey_runtime_addref(void); int spidermonkey_runtime_addref(void);
void spidermonkey_runtime_release(void); void spidermonkey_runtime_release(void);