[gjs/wip/xulrunner-1.9.3: 5/5] xulrunner 1.9.3: Use JS_NewGlobalObject if available



commit 27cea61c1a9893303df83cd7cbab90d5112d9936
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 |    5 ++---
 gjs/jsapi-util.c       |   36 ++++++++++++++++++++++++++++++++++++
 gjs/jsapi-util.h       |    1 +
 6 files changed, 51 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 96f7bb5..1d4d3dd 100644
--- a/gjs/context.c
+++ b/gjs/context.c
@@ -281,13 +281,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)
 {
@@ -602,13 +595,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_initialialize_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),
@@ -616,13 +604,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 ecd6ea3..2a89a09 100644
--- a/gjs/jsapi-util-array.c
+++ b/gjs/jsapi-util-array.c
@@ -313,9 +313,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_initialize_standard_context(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..43155f4 100644
--- a/gjs/jsapi-util-error.c
+++ b/gjs/jsapi-util-error.c
@@ -219,9 +219,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_initialize_standard_context(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 b8ffb64..74fc395 100644
--- a/gjs/jsapi-util.c
+++ b/gjs/jsapi-util.c
@@ -65,6 +65,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_initialialize_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_initialialize_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_class))
+        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..ecd40f1 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_initialialize_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]