[gjs/wip/ptomato/mozjs31prep: 10/13] js: Remove most of JS_Add*Root and JS_Remove*Root



commit f43953d0dabe9601536ee42b412a002bd85d5606
Author: Philip Chimento <philip endlessm com>
Date:   Thu Oct 13 17:25:23 2016 -0700

    js: Remove most of JS_Add*Root and JS_Remove*Root
    
    For rooting regular stack-allocated GC things, we should use
    JS::Rooted<T> instead of JS_Add*Root and JS_Remove*Root. This requires
    changing a few more function signatures to take JS::MutableHandleValue.
    
    In a few cases, it is possible to encapsulate argc, argv, rval in a
    JS::CallArgs& in cases where we know the function's caller is going to
    have a JS::CallArgs anyway.
    
    A few uses of JS_Add*Root and JS_Remove*Root remain, but those will be
    removed in mozjs31 in favour of JS::PersistentRooted.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=742249

 gi/arg.cpp         |    6 +-----
 gi/boxed.cpp       |   52 ++++++++++++++++++++--------------------------------
 gi/closure.cpp     |    8 ++++----
 gi/closure.h       |   10 ++++++----
 gi/object.cpp      |   11 +++++------
 gi/value.cpp       |    6 +-----
 gjs/jsapi-util.cpp |   14 +++++++-------
 gjs/jsapi-util.h   |   14 ++++++++------
 8 files changed, 52 insertions(+), 69 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index ec80121..a00c68f 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -2366,7 +2366,6 @@ gjs_object_from_g_hash (JSContext             *context,
 {
     GHashTableIter iter;
     JSObject *obj;
-    JSString *keystr;
     char     *keyutf8 = NULL;
     GArgument keyarg, valarg;
     bool result;
@@ -2384,9 +2383,7 @@ gjs_object_from_g_hash (JSContext             *context,
     value_p.setObject(*obj);
 
     JS::RootedValue keyjs(context), valjs(context);
-
-    keystr = NULL;
-    JS_AddStringRoot(context, &keystr);
+    JS::RootedString keystr(context);
 
     result = false;
 
@@ -2422,7 +2419,6 @@ gjs_object_from_g_hash (JSContext             *context,
 
  out:
     if (keyutf8) g_free(keyutf8);
-    JS_RemoveStringRoot(context, &keystr);
 
     return result;
 }
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index dc17f09..a05a490 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -325,12 +325,10 @@ boxed_init_from_props(JSContext   *context,
 }
 
 static bool
-boxed_invoke_constructor(JSContext   *context,
-                         JSObject    *obj,
-                         jsid         constructor_name,
-                         unsigned     argc,
-                         JS::Value   *argv,
-                         JS::Value   *rval)
+boxed_invoke_constructor(JSContext             *context,
+                         JSObject              *obj,
+                         jsid                   constructor_name,
+                         JS::CallArgs&          args)
 {
     JS::Value js_constructor, js_constructor_func;
     jsid constructor_const;
@@ -343,16 +341,15 @@ boxed_invoke_constructor(JSContext   *context,
                                      constructor_name, &js_constructor_func))
         return false;
 
-    return gjs_call_function_value(context, NULL, js_constructor_func, argc, argv, rval);
+    return gjs_call_function_value(context, NULL, js_constructor_func,
+                                   args.length(), args.array(), args.rval());
 }
 
 static bool
-boxed_new(JSContext   *context,
-          JSObject    *obj, /* "this" for constructor */
-          Boxed       *priv,
-          unsigned     argc,
-          JS::Value   *argv,
-          JS::Value   *rval)
+boxed_new(JSContext             *context,
+          JSObject              *obj, /* "this" for constructor */
+          Boxed                 *priv,
+          JS::CallArgs&          args)
 {
     if (priv->gtype == G_TYPE_VARIANT) {
         jsid constructor_name;
@@ -360,7 +357,7 @@ boxed_new(JSContext   *context,
         /* Short-circuit construction for GVariants by calling into the JS packing
            function */
         constructor_name = gjs_context_get_const_string(context, GJS_STRING_NEW_INTERNAL);
-        return boxed_invoke_constructor (context, obj, constructor_name, argc, argv, rval);
+        return boxed_invoke_constructor(context, obj, constructor_name, args);
     }
 
     /* If the structure is registered as a boxed, we can create a new instance by
@@ -401,7 +398,8 @@ boxed_new(JSContext   *context,
         /* for simplicity, we simply delegate all the work to the actual JS constructor
            function (which we retrieve from the JS constructor, that is, Namespace.BoxedType,
            or object.constructor, given that object was created with the right prototype */
-        retval = boxed_invoke_constructor(context, obj, priv->default_constructor_name, argc, argv, rval);
+        retval = boxed_invoke_constructor(context, obj,
+                                          priv->default_constructor_name, args);
         return retval;
     } else {
         gjs_throw(context, "Unable to construct struct type %s since it has no default constructor and 
cannot be allocated directly",
@@ -411,16 +409,16 @@ boxed_new(JSContext   *context,
 
     /* If we reach this code, we need to init from a map of fields */
 
-    if (argc == 0)
+    if (args.length() == 0)
         return true;
 
-    if (argc > 1) {
+    if (args.length() > 1) {
         gjs_throw(context, "Constructor with multiple arguments not supported for %s",
                   g_base_info_get_name((GIBaseInfo *)priv->info));
         return false;
     }
 
-    return boxed_init_from_props (context, obj, priv, argv[0]);
+    return boxed_init_from_props(context, obj, priv, args[0]);
 }
 
 GJS_NATIVE_CONSTRUCTOR_DECLARE(boxed)
@@ -430,7 +428,6 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(boxed)
     Boxed *proto_priv;
     JS::RootedObject proto(context);
     Boxed *source_priv;
-    JS::Value actual_rval;
     bool retval;
 
     GJS_NATIVE_CONSTRUCTOR_PRELUDE(boxed);
@@ -484,22 +481,13 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(boxed)
 
     /* we may need to return a value different from object
        (for example because we delegate to another constructor)
-       prepare the location for that
     */
 
-    actual_rval = JS::UndefinedValue();
-    JS_AddValueRoot(context, &actual_rval);
+    argv.rval().setUndefined();
+    retval = boxed_new(context, object, priv, argv);
 
-    retval = boxed_new(context, object, priv, argc, argv.array(), &actual_rval);
-
-    if (retval) {
-        if (!actual_rval.isUndefined())
-            argv.rval().set(actual_rval);
-        else
-            GJS_NATIVE_CONSTRUCTOR_FINISH(boxed);
-    }
-
-    JS_RemoveValueRoot(context, &actual_rval);
+    if (argv.rval().isUndefined())
+        GJS_NATIVE_CONSTRUCTOR_FINISH(boxed);
 
     return retval;
 }
diff --git a/gi/closure.cpp b/gi/closure.cpp
index 4540c09..0457780 100644
--- a/gi/closure.cpp
+++ b/gi/closure.cpp
@@ -246,10 +246,10 @@ closure_set_invalid(gpointer  data,
 }
 
 void
-gjs_closure_invoke(GClosure  *closure,
-                   int        argc,
-                   JS::Value *argv,
-                   JS::Value *retval)
+gjs_closure_invoke(GClosure              *closure,
+                   int                    argc,
+                   JS::Value             *argv,
+                   JS::MutableHandleValue retval)
 {
     Closure *c;
     JSContext *context;
diff --git a/gi/closure.h b/gi/closure.h
index 7991747..39cd54c 100644
--- a/gi/closure.h
+++ b/gi/closure.h
@@ -35,10 +35,12 @@ GClosure*  gjs_closure_new           (JSContext    *context,
                                       JSObject     *callable,
                                       const char   *description,
                                       bool          root_function);
-void       gjs_closure_invoke        (GClosure     *closure,
-                                      int           argc,
-                                      JS::Value    *argv,
-                                      JS::Value    *retval);
+
+void gjs_closure_invoke(GClosure              *closure,
+                        int                    argc,
+                        JS::Value             *argv,
+                        JS::MutableHandleValue retval);
+
 JSContext* gjs_closure_get_context   (GClosure     *closure);
 bool       gjs_closure_is_valid      (GClosure     *closure);
 JSObject*  gjs_closure_get_callable  (GClosure     *closure);
diff --git a/gi/object.cpp b/gi/object.cpp
index 6231f62..0fc1c9e 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1340,7 +1340,6 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(object_instance)
     GJS_NATIVE_CONSTRUCTOR_VARIABLES(object_instance)
     bool ret;
     JS::Value initer;
-    JS::Value rval;
     jsid object_init_name;
 
     GJS_NATIVE_CONSTRUCTOR_PRELUDE(object_instance);
@@ -1354,13 +1353,13 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(object_instance)
     if (!gjs_object_require_property(context, object, "GObject instance", object_init_name, &initer))
         return false;
 
-    rval.setUndefined();
-    ret = gjs_call_function_value(context, object, initer, argc, argv.array(), &rval);
+    argv.rval().setUndefined();
+    ret = gjs_call_function_value(context, object, initer,
+                                  argc, argv.array(), argv.rval());
 
-    if (rval.isUndefined())
-        rval.setObject(*object);
+    if (argv.rval().isUndefined())
+        argv.rval().setObject(*object);
 
-    argv.rval().set(rval);
     return ret;
 }
 
diff --git a/gi/value.cpp b/gi/value.cpp
index f13b2e4..7b778e6 100644
--- a/gi/value.cpp
+++ b/gi/value.cpp
@@ -121,7 +121,6 @@ closure_marshal(GClosure        *closure,
     JSRuntime *runtime;
     JSObject *obj;
     int argc;
-    JS::Value rval;
     int i;
     GSignalQuery signal_query = { 0, };
     GISignalInfo *signal_info;
@@ -170,12 +169,10 @@ closure_marshal(GClosure        *closure,
     JSAutoCompartment ac(context, obj);
 
     argc = n_param_values;
-    rval = JS::UndefinedValue();
+    JS::RootedValue rval(context);
     JS::AutoValueVector argv(context);
     argv.resize(argc);
 
-    JS_AddValueRoot(context, &rval);
-
     if (marshal_data) {
         /* we are used for a signal handler */
         guint signal_id;
@@ -294,7 +291,6 @@ closure_marshal(GClosure        *closure,
     }
 
  cleanup:
-    JS_RemoveValueRoot(context, &rval);
     JS_EndRequest(context);
 }
 
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 6b6e95d..bb26009 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -706,19 +706,19 @@ gjs_move_exception(JSContext      *src_context,
 }
 
 bool
-gjs_call_function_value(JSContext      *context,
-                        JSObject       *obj,
-                        JS::Value       fval,
-                        unsigned        argc,
-                        JS::Value      *argv,
-                        JS::Value      *rval)
+gjs_call_function_value(JSContext             *context,
+                        JSObject              *obj,
+                        JS::Value              fval,
+                        unsigned               argc,
+                        JS::Value             *argv,
+                        JS::MutableHandleValue rval)
 {
     bool result;
 
     JS_BeginRequest(context);
 
     result = JS_CallFunctionValue(context, obj, fval,
-                                  argc, argv, rval);
+                                  argc, argv, rval.address());
 
     if (result)
         gjs_schedule_gc_if_needed(context);
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index 9a7eac2..1b944a4 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -373,12 +373,14 @@ char*       gjs_value_debug_string           (JSContext       *context,
                                               JS::Value        value);
 void        gjs_explain_scope                (JSContext       *context,
                                               const char      *title);
-bool        gjs_call_function_value          (JSContext       *context,
-                                              JSObject        *obj,
-                                              JS::Value        fval,
-                                              unsigned         argc,
-                                              JS::Value       *argv,
-                                              JS::Value       *rval);
+
+bool gjs_call_function_value(JSContext             *context,
+                             JSObject              *obj,
+                             JS::Value              fval,
+                             unsigned               argc,
+                             JS::Value             *argv,
+                             JS::MutableHandleValue rval);
+
 void        gjs_error_reporter               (JSContext       *context,
                                               const char      *message,
                                               JSErrorReport   *report);


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