[gjs/wip/xulrunner-1.9.3-rebase5: 23/26] xulrunner 1.9.3: Use JS_NewGlobalObject if available
- From: Colin Walters <walters src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/wip/xulrunner-1.9.3-rebase5: 23/26] xulrunner 1.9.3: Use JS_NewGlobalObject if available
- Date: Thu, 23 Sep 2010 00:05:58 +0000 (UTC)
commit d01b5863e98caa3c8d92c0689e47efda5363a778
Author: Colin Walters <walters verbum org>
Date: Fri Sep 17 11:58:43 2010 -0400
xulrunner 1.9.3: Use JS_NewGlobalObject if available
In 1.9.3, we need to explicitly say when we're making the global
object. Clean this up by introducing a wrapper function; while
we're at it, also call JS_InitStandardClasses here since everything
else creating a global did too.
Note that rooting the global object is not necessary, so remove
that. From a grep of the sources (going back to at least 1.9.2):
./src/jsgc.cpp: if (acx->globalObject && !JS_HAS_OPTION(acx, JSOPTION_UNROOTED_GLOBAL))
./src/jsgc.cpp: JS_CALL_OBJECT_TRACER(trc, acx->globalObject, "global object");
configure.ac | 9 ++++++++-
gjs/context.c | 27 +++------------------------
gjs/jsapi-util-error.c | 1 +
gjs/jsapi-util.c | 35 +++++++++++++++++++++++++++++++++++
gjs/jsapi-util.h | 1 +
gjs/unit-test-utils.c | 5 ++---
6 files changed, 50 insertions(+), 28 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index a6a381f..6d99f81 100644
--- a/configure.ac
+++ b/configure.ac
@@ -147,9 +147,16 @@ AC_CHECK_LIB([mozjs], [JS_DefinePropertyById], :,
AC_CHECK_LIB([mozjs], [JS_AddValueRoot], [have_js_addvalueroot=true],
[have_js_addvalueroot=false],
[$JS_LIBS])
+AC_CHECK_LIB([mozjs], [JS_NewGlobalObject], [have_js_newglobalobject=true],
+ [have_js_newglobalobject=false],
+ [$JS_LIBS])
if test "x$have_js_addvalueroot" = xtrue; then
AC_DEFINE(HAVE_JS_ADDVALUEROOT, 1,
- [Define if using xulrunner 1.9.3 or later])
+ [Define if -lmozjs has AddValueRoot])
+fi
+if test "x$have_js_newglobalobject" = xtrue; then
+ AC_DEFINE(HAVE_JS_NEWGLOBALOBJECT, 1,
+ [Define if -lmozjs has NewGlobalObject])
fi
## workaround for Ubuntu Hardy bug where mozilla-js.pc gives CFLAGS
diff --git a/gjs/context.c b/gjs/context.c
index d374533..025036e 100644
--- a/gjs/context.c
+++ b/gjs/context.c
@@ -282,13 +282,6 @@ gjs_printerr(JSContext *context,
return JS_TRUE;
}
-static JSClass global_class = {
- "GjsGlobal", JSCLASS_GLOBAL_FLAGS,
- JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
- JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
- JSCLASS_NO_OPTIONAL_MEMBERS
-};
-
static void
gjs_context_init(GjsContext *js_context)
{
@@ -352,9 +345,6 @@ gjs_context_dispose(GObject *object)
}
if (js_context->global != NULL) {
- JS_BeginRequest(js_context->context);
- JS_RemoveObjectRoot(js_context->context, &js_context->global);
- JS_EndRequest(js_context->context);
js_context->global = NULL;
}
@@ -600,13 +590,9 @@ gjs_context_constructor (GType type,
JS_SetVersion(js_context->context, OUR_JS_VERSION);
}
- js_context->global = JS_NewObject(js_context->context, &global_class, NULL, NULL);
- if (js_context->global == NULL)
- gjs_fatal("Failed to create javascript global object");
-
- /* Sets global object and adds builtins to it */
- if (!JS_InitStandardClasses(js_context->context, js_context->global))
- gjs_fatal("Failed to init standard javascript classes");
+ if (!gjs_init_context_standard(js_context->context))
+ gjs_fatal("Failed to initialize context");
+ js_context->global = JS_GetGlobalObject(js_context->context);
if (!JS_DefineProperty(js_context->context, js_context->global,
"window", OBJECT_TO_JSVAL(js_context->global),
@@ -614,13 +600,6 @@ gjs_context_constructor (GType type,
JSPROP_READONLY | JSPROP_PERMANENT))
gjs_fatal("No memory to export global object as 'window'");
- /* this is probably not necessary, having it as global object in
- * context already roots it presumably? Could not find where it
- * does in a quick glance through spidermonkey source though.
- */
- if (!JS_AddObjectRoot(js_context->context, &js_context->global))
- gjs_fatal("No memory to add global object as GC root");
-
/* Define a global function called log() */
if (!JS_DefineFunction(js_context->context, js_context->global,
"log",
diff --git a/gjs/jsapi-util-error.c b/gjs/jsapi-util-error.c
index 7a4c0b6..b59e182 100644
--- a/gjs/jsapi-util-error.c
+++ b/gjs/jsapi-util-error.c
@@ -24,6 +24,7 @@
#include <config.h>
#include "jsapi-util.h"
+#include "compat.h"
#include <util/log.h>
diff --git a/gjs/jsapi-util.c b/gjs/jsapi-util.c
index a1f7df9..885408c 100644
--- a/gjs/jsapi-util.c
+++ b/gjs/jsapi-util.c
@@ -66,6 +66,41 @@ gjs_runtime_set_data(JSRuntime *runtime,
g_dataset_set_data_full(runtime, name, data, dnotify);
}
+static JSClass global_class = {
+ "GjsGlobal", JSCLASS_GLOBAL_FLAGS,
+ JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
+ JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
+ JSCLASS_NO_OPTIONAL_MEMBERS
+};
+
+/**
+ * gjs_init_context_standard:
+ * @context: a #JSContext
+ *
+ * This function creates a default global object for @context,
+ * and calls JS_InitStandardClasses using it.
+ *
+ * Returns: %TRUE on success, %FALSE otherwise
+ */
+gboolean
+gjs_init_context_standard (JSContext *context)
+{
+ JSObject *global;
+#ifdef HAVE_JS_NEWGLOBALOBJECT
+ global = JS_NewGlobalObject(context, &global_class);
+ if (global == NULL)
+ return FALSE;
+#else
+ global = JS_NewObject(context, &global_class, NULL, NULL);
+ if (global == NULL)
+ return FALSE;
+ JS_SetGlobalObject(context, global);
+#endif
+ if (!JS_InitStandardClasses(context, global))
+ return FALSE;
+ return TRUE;
+}
+
/* Historical note: There is no separate load context anymore. */
JSContext*
gjs_runtime_get_load_context(JSContext *context)
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index bfc8190..0e65df2 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -194,6 +194,7 @@ jsval cname##_create_proto(JSContext *context, JSObject *module, const char *pro
return rval; \
}
+gboolean gjs_init_context_standard (JSContext *context);
void* gjs_runtime_get_data (JSRuntime *runtime,
const char *name);
diff --git a/gjs/unit-test-utils.c b/gjs/unit-test-utils.c
index 20fde06..5e0e785 100644
--- a/gjs/unit-test-utils.c
+++ b/gjs/unit-test-utils.c
@@ -39,9 +39,8 @@ _gjs_unit_test_fixture_begin (GjsUnitTestFixture *fixture)
fixture->runtime = JS_NewRuntime(1024*1024 /* max bytes */);
fixture->context = JS_NewContext(fixture->runtime, 8192);
JS_BeginRequest(fixture->context);
- global = JS_NewObject(fixture->context, NULL, NULL, NULL);
- JS_SetGlobalObject(fixture->context, global);
- JS_InitStandardClasses(fixture->context, global);
+ if (!gjs_init_context_standard(fixture->context))
+ g_error("failed to init context");
JS_SetErrorReporter(fixture->context, test_error_reporter);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]