[gjs] Don't use JS_SetGlobalObject/GetGlobalObject



commit e35bbd4e8ad11fd4575ab3e43e0f9bd4f63b3e3e
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Wed Jan 15 16:22:33 2014 -0500

    Don't use JS_SetGlobalObject/GetGlobalObject
    
    These are deprecated (and removed!) upstream, as they're just an
    opaque pointer. JS_GetGlobalForScopeChain is what we want here, as
    that looks up the global on the current compartment.

 gi/closure.cpp           |    4 +---
 gi/function.cpp          |    6 +++---
 gi/object.cpp            |    9 ---------
 gi/value.cpp             |    6 +++---
 gjs/context.cpp          |   29 +++++++++++++++++++++++++++--
 gjs/jsapi-util-error.cpp |    3 +--
 gjs/jsapi-util.cpp       |   38 ++++++++------------------------------
 gjs/jsapi-util.h         |    3 ++-
 gjs/stack.cpp            |    5 ++---
 9 files changed, 47 insertions(+), 56 deletions(-)
---
diff --git a/gi/closure.cpp b/gi/closure.cpp
index 6ec9a51..274f06f 100644
--- a/gi/closure.cpp
+++ b/gi/closure.cpp
@@ -253,7 +253,6 @@ gjs_closure_invoke(GClosure *closure,
 {
     Closure *c;
     JSContext *context;
-    JSObject *global;
 
     c = (Closure*) closure;
 
@@ -267,8 +266,7 @@ gjs_closure_invoke(GClosure *closure,
 
     context = c->context;
     JS_BeginRequest(context);
-    global = JS_GetGlobalObject(context);
-    JSAutoCompartment ac(context, global);
+    JSAutoCompartment ac(context, c->obj);
 
     if (JS_IsExceptionPending(context)) {
         gjs_debug_closure("Exception was pending before invoking callback??? "
diff --git a/gi/function.cpp b/gi/function.cpp
index 7994d04..686aaee 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -172,7 +172,7 @@ gjs_callback_closure(ffi_cif *cif,
 {
     JSContext *context;
     JSRuntime *runtime;
-    JSObject *global;
+    JSObject *func_obj;
     GjsCallbackTrampoline *trampoline;
     int i, n_args, n_jsargs, n_outargs;
     jsval *jsargs, rval;
@@ -203,8 +203,8 @@ gjs_callback_closure(ffi_cif *cif,
     }
 
     JS_BeginRequest(context);
-    global = JS_GetGlobalObject(context);
-    JSAutoCompartment ac(context, global);
+    func_obj = &trampoline->js_function.toObject();
+    JSAutoCompartment ac(context, func_obj);
 
     n_args = g_callable_info_get_n_args(trampoline->info);
 
diff --git a/gi/object.cpp b/gi/object.cpp
index 0771234..2903a37 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1998,9 +1998,6 @@ gjs_define_object_class(JSContext      *context,
         constructor_name = g_type_name(gtype);
     }
 
-    global = JS_GetGlobalObject(context);
-    JSAutoCompartment ac(context, global);
-
     if (!gjs_init_class_dynamic(context, in_object,
                                 parent_proto,
                                 ns, constructor_name,
@@ -2088,16 +2085,10 @@ gjs_object_from_g_object(JSContext    *context,
         gtype = G_TYPE_FROM_INSTANCE(gobj);
         proto = gjs_lookup_object_prototype(context, gtype);
 
-        JS_BeginRequest(context);
-        global = JS_GetGlobalObject(context);
-        JSAutoCompartment ac(context, global);
-
         obj = JS_NewObjectWithGivenProto(context,
                                          JS_GetClass(proto), proto,
                                          gjs_get_import_global (context));
 
-        JS_EndRequest(context);
-
         if (obj == NULL)
             goto out;
 
diff --git a/gi/value.cpp b/gi/value.cpp
index 4a2e6ca..ca50cdd 100644
--- a/gi/value.cpp
+++ b/gi/value.cpp
@@ -57,8 +57,8 @@ closure_marshal(GClosure        *closure,
                 gpointer         marshal_data)
 {
     JSContext *context;
-    JSObject *global;
     JSRuntime *runtime;
+    JSObject *obj;
     int argc;
     jsval *argv;
     jsval rval;
@@ -100,9 +100,9 @@ closure_marshal(GClosure        *closure,
         return;
     }
 
+    obj = gjs_closure_get_callable(closure);
     JS_BeginRequest(context);
-    global = JS_GetGlobalObject(context);
-    JSAutoCompartment ac(context, global);
+    JSAutoCompartment ac(context, obj);
 
     argc = n_param_values;
     rval = JSVAL_VOID;
diff --git a/gjs/context.cpp b/gjs/context.cpp
index d8e3785..39c852a 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -429,10 +429,9 @@ gjs_context_constructed(GObject *object)
     /* set ourselves as the private data */
     JS_SetContextPrivate(js_context->context, js_context);
 
-    if (!gjs_init_context_standard(js_context->context))
+    if (!gjs_init_context_standard(js_context->context, &js_context->global))
         g_error("Failed to initialize context");
 
-    js_context->global = JS_GetGlobalObject(js_context->context);
     JSAutoCompartment ac(js_context->context, js_context->global);
 
     if (!JS_DefineProperty(js_context->context, js_context->global,
@@ -764,3 +763,29 @@ gjs_object_get_property_const(JSContext      *context,
     pname = gjs_context_get_const_string(context, property_name);
     return JS_GetPropertyById(context, obj, pname, value_p);
 }
+
+/**
+ * gjs_get_import_global:
+ * @context: a #JSContext
+ *
+ * Gets the "import global" for the context's runtime. The import
+ * global object is the global object for the context. It is used
+ * as the root object for the scope of modules loaded by GJS in this
+ * runtime, and should also be used as the globals 'obj' argument passed
+ * to JS_InitClass() and the parent argument passed to JS_ConstructObject()
+ * when creating a native classes that are shared between all contexts using
+ * the runtime. (The standard JS classes are not shared, but we share
+ * classes such as GObject proxy classes since objects of these classes can
+ * easily migrate between contexts and having different classes depending
+ * on the context where they were first accessed would be confusing.)
+ *
+ * Return value: the "import global" for the context's
+ *  runtime. Will never return %NULL while GJS has an active context
+ *  for the runtime.
+ */
+JSObject*
+gjs_get_import_global(JSContext *context)
+{
+    GjsContext *gjs_context = (GjsContext *) JS_GetContextPrivate(context);
+    return gjs_context->global;
+}
diff --git a/gjs/jsapi-util-error.cpp b/gjs/jsapi-util-error.cpp
index 91b68e4..36a5852 100644
--- a/gjs/jsapi-util-error.cpp
+++ b/gjs/jsapi-util-error.cpp
@@ -82,8 +82,7 @@ gjs_throw_valist(JSContext       *context,
         goto out;
     }
 
-    if (!JS_GetProperty(context, JS_GetGlobalObject(context),
-                        error_class, &v_constructor) ||
+    if (!JS_GetProperty(context, JS_GetGlobalForScopeChain(context), error_class, &v_constructor) ||
         !JSVAL_IS_OBJECT(v_constructor)) {
         JS_ReportError(context, "??? Missing Error constructor in global object?");
         goto out;
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 9cb7929..2ad4339 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -46,31 +46,6 @@ gjs_util_error_quark (void)
     return g_quark_from_static_string ("gjs-util-error-quark");
 }
 
-/**
- * gjs_get_import_global:
- * @context: a #JSContext
- *
- * Gets the "import global" for the context's runtime. The import
- * global object is the global object for the context. It is used
- * as the root object for the scope of modules loaded by GJS in this
- * runtime, and should also be used as the globals 'obj' argument passed
- * to JS_InitClass() and the parent argument passed to JS_ConstructObject()
- * when creating a native classes that are shared between all contexts using
- * the runtime. (The standard JS classes are not shared, but we share
- * classes such as GObject proxy classes since objects of these classes can
- * easily migrate between contexts and having different classes depending
- * on the context where they were first accessed would be confusing.)
- *
- * Return value: the "import global" for the context's
- *  runtime. Will never return %NULL while GJS has an active context
- *  for the runtime.
- */
-JSObject*
-gjs_get_import_global(JSContext *context)
-{
-    return JS_GetGlobalObject(context);
-}
-
 static JSClass global_class = {
     "GjsGlobal", JSCLASS_GLOBAL_FLAGS_WITH_SLOTS(GJS_GLOBAL_SLOT_LAST),
     JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
@@ -82,14 +57,16 @@ static JSClass global_class = {
 /**
  * gjs_init_context_standard:
  * @context: a #JSContext
- *
- * This function creates a default global object for @context,
- * and calls JS_InitStandardClasses using it.
+ * @global_out: (out): The created global object.
+
+ * This function creates a global object given the context,
+ * and initializes it with the default API.
  *
  * Returns: %TRUE on success, %FALSE otherwise
  */
 gboolean
-gjs_init_context_standard (JSContext *context)
+gjs_init_context_standard (JSContext  *context,
+                           JSObject  **global_out)
 {
     JSObject *global;
     JS::CompartmentOptions options;
@@ -123,7 +100,6 @@ gjs_init_context_standard (JSContext *context)
 
     /* Set the context's global */
     JSAutoCompartment ac(context, global);
-    JS_SetGlobalObject(context, global);
 
     if (!JS_InitStandardClasses(context, global))
         return FALSE;
@@ -134,6 +110,8 @@ gjs_init_context_standard (JSContext *context)
     if (!JS_DefineDebuggerObject(context, global))
         return FALSE;
 
+    *global_out = global;
+
     return TRUE;
 }
 
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index f1182af..6e98713 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -202,7 +202,8 @@ jsval gjs_##cname##_create_proto(JSContext *context, JSObject *module, const cha
     return rval; \
 }
 
-gboolean    gjs_init_context_standard        (JSContext       *context);
+gboolean    gjs_init_context_standard        (JSContext       *context,
+                                              JSObject       **global_out);
 
 JSObject*   gjs_get_import_global            (JSContext       *context);
 
diff --git a/gjs/stack.cpp b/gjs/stack.cpp
index df0afb3..40b2064 100644
--- a/gjs/stack.cpp
+++ b/gjs/stack.cpp
@@ -59,11 +59,10 @@ gjs_context_get_frame_info (JSContext  *context,
     JSBool ret = JS_FALSE;
 
     JS_BeginRequest(context);
-    global = JS_GetGlobalObject(context);
+    global = JS_GetGlobalForScopeChain(context);
     JSAutoCompartment ac(context, global);
 
-    if (!JS_GetProperty(context, JS_GetGlobalObject(context),
-                        "Error", &v_constructor) ||
+    if (!JS_GetProperty(context, global, "Error", &v_constructor) ||
         !JSVAL_IS_OBJECT(v_constructor)) {
         g_error("??? Missing Error constructor in global object?");
         goto out;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]