[gjs] xulrunner 1.9.3: Use JS_NewGlobalObject if available



commit 0d81ec48c99412fbac9a2b77ae85409367698670
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  |    6 ++----
 6 files changed, 50 insertions(+), 29 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 6e0b92a..8fdcba5 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 8a9a4a4..3efda19 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;
     }
 
@@ -603,13 +593,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),
@@ -617,13 +603,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 2abe49e..a2a7420 100644
--- a/gjs/jsapi-util.c
+++ b/gjs/jsapi-util.c
@@ -172,6 +172,41 @@ gjs_runtime_clear_call_context(JSRuntime *runtime)
     gjs_debug(GJS_DEBUG_CONTEXT, "Call context cleared");
 }
 
+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;
+}
+
 static void
 runtime_data_destroy_notify(void *data)
 {
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);
diff --git a/gjs/unit-test-utils.c b/gjs/unit-test-utils.c
index 2c3f5fb..26dea22 100644
--- a/gjs/unit-test-utils.c
+++ b/gjs/unit-test-utils.c
@@ -35,13 +35,11 @@ test_error_reporter(JSContext     *context,
 void
 _gjs_unit_test_fixture_begin (GjsUnitTestFixture *fixture)
 {
-    JSObject *global;
     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]