[gjs/wip/xulrunner-1.9.3: 6/6] 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: 6/6] xulrunner 1.9.3: Use JS_NewGlobalObject if available
- Date: Sat, 18 Sep 2010 00:08:31 +0000 (UTC)
commit 9aac3fb737013703004579fc3e59d2eba77d89f6
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 | 23 ++---------------------
gjs/jsapi-util-array.c | 5 ++---
gjs/jsapi-util-error.c | 6 +++---
gjs/jsapi-util.c | 36 ++++++++++++++++++++++++++++++++++++
gjs/jsapi-util.h | 1 +
6 files changed, 52 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 01ab41e..fc88197 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)
{
@@ -603,13 +596,8 @@ 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");
if (!JS_DefineProperty(js_context->context, js_context->global,
"window", OBJECT_TO_JSVAL(js_context->global),
@@ -617,13 +605,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-array.c b/gjs/jsapi-util-array.c
index c3b6914..9961d8e 100644
--- a/gjs/jsapi-util-array.c
+++ b/gjs/jsapi-util-array.c
@@ -314,9 +314,8 @@ gjstest_test_func_gjs_jsapi_util_array(void)
JS_BeginRequest(context);
- global = JS_NewObject(context, NULL, NULL, NULL);
- JS_SetGlobalObject(context, global);
- JS_InitStandardClasses(context, global);
+ if (!gjs_init_context_standard(context))
+ gjs_fatal("failed to initialize context");
JS_SetErrorReporter(context, test_error_reporter);
diff --git a/gjs/jsapi-util-error.c b/gjs/jsapi-util-error.c
index f5fa228..7a71fc0 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>
@@ -219,9 +220,8 @@ gjstest_test_func_gjs_jsapi_util_error_throw(void)
JS_BeginRequest(context);
- global = JS_NewObject(context, NULL, NULL, NULL);
- JS_SetGlobalObject(context, global);
- JS_InitStandardClasses(context, global);
+ if (!gjs_init_context_standard(context))
+ gjs_fatal("failed to initialize context");
JS_SetErrorReporter(context, test_error_reporter);
diff --git a/gjs/jsapi-util.c b/gjs/jsapi-util.c
index 2abe49e..619dc42 100644
--- a/gjs/jsapi-util.c
+++ b/gjs/jsapi-util.c
@@ -66,6 +66,42 @@ 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;
+}
+
+
/* The "load context" is the one we use for loading
* modules and initializing classes.
*/
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index 39b782a..78a0ca7 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);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]