[gjs/wip/ptomato/mozjs31prep] jsapi-util: Root several utility functions



commit 6788c724b3f0a19ec4267d3da1e673ed6e691005
Author: Philip Chimento <philip endlessm com>
Date:   Wed Oct 26 16:50:29 2016 -0700

    jsapi-util: Root several utility functions
    
    This adds GC rooting to the signatures of:
    
    - gjs_define_string_array()
    - gjs_log_exception_full()
    - gjs_value_debug_string()
    - gjs_call_function_value()
    - gjs_eval_with_scope()
    
    All fairly simple cases that do not really cascade into other code.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=742249

 gi/boxed.cpp        |    2 +-
 gi/closure.cpp      |    9 +++----
 gjs/byteArray.cpp   |    7 ++---
 gjs/context.cpp     |   13 ++++++-----
 gjs/coverage.cpp    |   13 +++++------
 gjs/importer.cpp    |   11 ++++-----
 gjs/jsapi-util.cpp  |   53 +++++++++++++++++++++++---------------------------
 gjs/jsapi-util.h    |   40 ++++++++++++++++++++------------------
 modules/console.cpp |    7 ++---
 9 files changed, 74 insertions(+), 81 deletions(-)
---
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index 3cf4964..7fd594b 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -341,7 +341,7 @@ boxed_invoke_constructor(JSContext             *context,
                                      constructor_name, &js_constructor_func))
         return false;
 
-    return gjs_call_function_value(context, NULL, js_constructor_func,
+    return gjs_call_function_value(context, JS::NullPtr(), js_constructor_func,
                                    args.length(), args.array(), args.rval());
 }
 
diff --git a/gi/closure.cpp b/gi/closure.cpp
index babd6ac..8e8e7b5 100644
--- a/gi/closure.cpp
+++ b/gi/closure.cpp
@@ -274,12 +274,11 @@ gjs_closure_invoke(GClosure              *closure,
         gjs_log_exception(context);
     }
 
+    JS::RootedValue v_closure(context, JS::ObjectValue(*c->obj));
     if (!gjs_call_function_value(context,
-                                 NULL, /* "this" object; NULL is some kind of default presumably */
-                                 JS::ObjectValue(*c->obj),
-                                 argc,
-                                 argv,
-                                 retval)) {
+                                 /* "this" object; null is some kind of default presumably */
+                                 JS::NullPtr(),
+                                 v_closure, argc, argv, retval)) {
         /* Exception thrown... */
         gjs_debug_closure("Closure invocation failed (exception should "
                           "have been thrown) closure %p callable %p",
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index 9028f3c..20a329a 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -516,12 +516,11 @@ to_gbytes_func(JSContext *context,
 static JSObject *
 byte_array_get_prototype(JSContext *context)
 {
-    JS::Value retval;
-
-    retval = gjs_get_global_slot (context, GJS_GLOBAL_SLOT_BYTE_ARRAY_PROTOTYPE);
+    JS::RootedValue retval(context,
+        gjs_get_global_slot(context, GJS_GLOBAL_SLOT_BYTE_ARRAY_PROTOTYPE));
 
     if (!retval.isObject()) {
-        if (!gjs_eval_with_scope(context, NULL,
+        if (!gjs_eval_with_scope(context, JS::NullPtr(),
                                  "imports.byteArray.ByteArray.prototype;", -1,
                                  "<internal>", &retval))
             g_error ("Could not import byte array prototype\n");
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 1f72f1e..a8189eb 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -153,7 +153,6 @@ gjs_log_error(JSContext *context,
 {
     JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
     JSExceptionState *exc_state;
-    JSString *jstr;
 
     if ((argc != 1 && argc != 2) || !argv[0].isObject()) {
         gjs_throw(context, "Must pass an exception and optionally a message to logError()");
@@ -162,6 +161,8 @@ gjs_log_error(JSContext *context,
 
     JS_BeginRequest(context);
 
+    JS::RootedString jstr(context);
+
     if (argc == 2) {
         /* JS_ValueToString might throw, in which we will only
          *log that the value could be converted to string */
@@ -170,11 +171,10 @@ gjs_log_error(JSContext *context,
         if (jstr != NULL)
             argv[1] = JS::StringValue(jstr);    // GC root
         JS_RestoreExceptionState(context, exc_state);
-    } else {
-        jstr = NULL;
     }
 
-    gjs_log_exception_full(context, argv[0], jstr);
+    /* COMPAT: JS::CallArgs::operator[] will yield Handle in mozjs31 */
+    gjs_log_exception_full(context, argv.handleOrUndefinedAt(0), jstr);
 
     JS_EndRequest(context);
     argv.rval().setUndefined();
@@ -646,14 +646,15 @@ gjs_context_eval(GjsContext   *js_context,
                  GError      **error)
 {
     bool ret = false;
-    JS::Value retval;
 
     JSAutoCompartment ac(js_context->context, js_context->global);
     JSAutoRequest ar(js_context->context);
 
     g_object_ref(G_OBJECT(js_context));
 
-    if (!gjs_eval_with_scope(js_context->context, NULL, script, script_len, filename, &retval)) {
+    JS::RootedValue retval(js_context->context);
+    if (!gjs_eval_with_scope(js_context->context, JS::NullPtr(), script,
+                             script_len, filename, &retval)) {
         gjs_log_exception(js_context->context);
         g_set_error(error,
                     GJS_ERROR,
diff --git a/gjs/coverage.cpp b/gjs/coverage.cpp
index 9c1c666..565ee94 100644
--- a/gjs/coverage.cpp
+++ b/gjs/coverage.cpp
@@ -1263,10 +1263,10 @@ static JSClass coverage_global_class = {
 };
 
 static bool
-gjs_context_eval_file_in_compartment(GjsContext *context,
-                                     const char *filename,
-                                     JSObject   *compartment_object,
-                                     GError     **error)
+gjs_context_eval_file_in_compartment(GjsContext      *context,
+                                     const char      *filename,
+                                     JS::HandleObject compartment_object,
+                                     GError         **error)
 {
     char  *script = NULL;
     gsize script_len = 0;
@@ -1285,12 +1285,11 @@ gjs_context_eval_file_in_compartment(GjsContext *context,
 
     g_object_unref(file);
 
-    JS::Value return_value;
-
     JSContext *js_context = (JSContext *) gjs_context_get_native_context(context);
 
     JSAutoCompartment compartment(js_context, compartment_object);
 
+    JS::RootedValue return_value(js_context);
     if (!gjs_eval_with_scope(js_context,
                              compartment_object,
                              script, script_len, filename,
@@ -1495,7 +1494,7 @@ gjs_run_script_in_coverage_compartment(GjsCoverage *coverage,
     GjsCoveragePrivate *priv = (GjsCoveragePrivate *) gjs_coverage_get_instance_private(coverage);
     JSContext          *js_context = (JSContext *) gjs_context_get_native_context(priv->context);
     JSAutoCompartment ac(js_context, priv->coverage_statistics);
-    JS::Value rval;
+    JS::RootedValue rval(js_context);
     if (!gjs_eval_with_scope(js_context,
                              priv->coverage_statistics,
                              script,
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index 8b548fe..70ca39f 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -251,10 +251,10 @@ create_module_object(JSContext *context)
 }
 
 static bool
-import_file(JSContext  *context,
-            const char *name,
-            GFile      *file,
-            JSObject   *module_obj)
+import_file(JSContext       *context,
+            const char      *name,
+            GFile           *file,
+            JS::HandleObject module_obj)
 {
     bool ret = false;
     char *script = NULL;
@@ -296,7 +296,6 @@ load_module_init(JSContext       *context,
                  JS::HandleObject in_object,
                  const char      *full_path)
 {
-    JSObject *module_obj;
     JSBool found;
     GFile *file;
 
@@ -313,7 +312,7 @@ load_module_init(JSContext       *context,
         }
     }
 
-    module_obj = create_module_object (context);
+    JS::RootedObject module_obj(context, create_module_object(context));
     file = g_file_new_for_commandline_arg(full_path);
     if (!import_file (context, "__init__", file, module_obj))
         goto out;
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index da5bf0a..b0acd77 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -320,12 +320,12 @@ gjs_build_string_array(JSContext   *context,
 }
 
 JSObject*
-gjs_define_string_array(JSContext   *context,
-                        JSObject    *in_object,
-                        const char  *array_name,
-                        gssize       array_length,
-                        const char **array_values,
-                        unsigned     attrs)
+gjs_define_string_array(JSContext       *context,
+                        JS::HandleObject in_object,
+                        const char      *array_name,
+                        ssize_t          array_length,
+                        const char     **array_values,
+                        unsigned         attrs)
 {
     JSObject *array;
 
@@ -436,7 +436,7 @@ _gjs_g_utf8_make_valid (const char *name)
  */
 char*
 gjs_value_debug_string(JSContext      *context,
-                       JS::Value       value)
+                       JS::HandleValue value)
 {
     JSString *str;
     char *bytes;
@@ -507,9 +507,9 @@ utf8_exception_from_non_gerror_value(JSContext *cx,
 }
 
 bool
-gjs_log_exception_full(JSContext *context,
-                       JS::Value  exc,
-                       JSString  *message)
+gjs_log_exception_full(JSContext       *context,
+                       JS::HandleValue  exc,
+                       JS::HandleString message)
 {
     char *utf8_exception, *utf8_message;
     bool is_syntax;
@@ -620,13 +620,13 @@ gjs_log_exception(JSContext  *context)
 
     JS_BeginRequest(context);
 
-    JS::RootedValue exc(context, JS::UndefinedValue());
+    JS::RootedValue exc(context);
     if (!JS_GetPendingException(context, exc.address()))
         goto out;
 
     JS_ClearPendingException(context);
 
-    gjs_log_exception_full(context, exc, NULL);
+    gjs_log_exception_full(context, exc, JS::NullPtr());
 
     retval = true;
 
@@ -638,8 +638,8 @@ gjs_log_exception(JSContext  *context)
 
 bool
 gjs_call_function_value(JSContext             *context,
-                        JSObject              *obj,
-                        JS::Value              fval,
+                        JS::HandleObject       obj,
+                        JS::HandleValue        fval,
                         unsigned               argc,
                         JS::Value             *argv,
                         JS::MutableHandleValue rval)
@@ -839,15 +839,14 @@ gjs_strip_unix_shebang(const char  *script,
 }
 
 bool
-gjs_eval_with_scope(JSContext    *context,
-                    JSObject     *object,
-                    const char   *script,
-                    gssize        script_len,
-                    const char   *filename,
-                    JS::Value    *retval_p)
+gjs_eval_with_scope(JSContext             *context,
+                    JS::HandleObject       object,
+                    const char            *script,
+                    ssize_t                script_len,
+                    const char            *filename,
+                    JS::MutableHandleValue retval)
 {
     int start_line_number = 1;
-    JS::Value retval = JS::UndefinedValue();
     JSAutoRequest ar(context);
 
     if (script_len < 0)
@@ -863,17 +862,16 @@ gjs_eval_with_scope(JSContext    *context,
         return false;
     }
 
-    if (!object)
-        object = JS_NewObject(context, NULL, NULL, NULL);
+    JS::RootedObject eval_obj(context, object);
+    if (!eval_obj)
+        eval_obj = JS_NewObject(context, NULL, NULL, NULL);
 
     JS::CompileOptions options(context);
     options.setUTF8(true)
            .setFileAndLine(filename, start_line_number)
            .setSourcePolicy(JS::CompileOptions::LAZY_SOURCE);
 
-    js::RootedObject rootedObj(context, object);
-
-    if (!JS::Evaluate(context, rootedObj, options, script, script_len, &retval))
+    if (!JS::Evaluate(context, eval_obj, options, script, script_len, retval.address()))
         return false;
 
     gjs_schedule_gc_if_needed(context);
@@ -887,8 +885,5 @@ gjs_eval_with_scope(JSContext    *context,
     gjs_debug(GJS_DEBUG_CONTEXT,
               "Script evaluation succeeded");
 
-    if (retval_p)
-        *retval_p = retval;
-
     return true;
 }
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index ef14f17..41d0f20 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -350,12 +350,14 @@ JSObject *gjs_construct_object_dynamic(JSContext       *context,
 JSObject*   gjs_build_string_array           (JSContext       *context,
                                               gssize           array_length,
                                               char           **array_values);
-JSObject*   gjs_define_string_array          (JSContext       *context,
-                                              JSObject        *obj,
-                                              const char      *array_name,
-                                              gssize           array_length,
-                                              const char     **array_values,
-                                              unsigned         attrs);
+
+JSObject *gjs_define_string_array(JSContext       *context,
+                                  JS::HandleObject obj,
+                                  const char      *array_name,
+                                  ssize_t          array_length,
+                                  const char     **array_values,
+                                  unsigned         attrs);
+
 void        gjs_throw                        (JSContext       *context,
                                               const char      *format,
                                               ...)  G_GNUC_PRINTF (2, 3);
@@ -370,16 +372,16 @@ void        gjs_throw_g_error                (JSContext       *context,
 
 bool        gjs_log_exception                (JSContext       *context);
 
-bool        gjs_log_exception_full           (JSContext       *context,
-                                              JS::Value        exc,
-                                              JSString        *message);
+bool gjs_log_exception_full(JSContext       *context,
+                            JS::HandleValue  exc,
+                            JS::HandleString message);
 
-char*       gjs_value_debug_string           (JSContext       *context,
-                                              JS::Value        value);
+char *gjs_value_debug_string(JSContext      *context,
+                             JS::HandleValue value);
 
 bool gjs_call_function_value(JSContext             *context,
-                             JSObject              *obj,
-                             JS::Value              fval,
+                             JS::HandleObject       obj,
+                             JS::HandleValue        fval,
                              unsigned               argc,
                              JS::Value             *argv,
                              JS::MutableHandleValue rval);
@@ -439,12 +441,12 @@ bool gjs_context_get_frame_info(JSContext                              *context,
                                 mozilla::Maybe<JS::MutableHandleValue>& fileName,
                                 mozilla::Maybe<JS::MutableHandleValue>& lineNumber);
 
-bool              gjs_eval_with_scope        (JSContext    *context,
-                                              JSObject     *object,
-                                              const char   *script,
-                                              gssize        script_len,
-                                              const char   *filename,
-                                              JS::Value    *retval_p);
+bool gjs_eval_with_scope(JSContext             *context,
+                         JS::HandleObject       object,
+                         const char            *script,
+                         ssize_t                script_len,
+                         const char            *filename,
+                         JS::MutableHandleValue retval);
 
 typedef enum {
   GJS_STRING_CONSTRUCTOR,
diff --git a/modules/console.cpp b/modules/console.cpp
index e754c2f..f183269 100644
--- a/modules/console.cpp
+++ b/modules/console.cpp
@@ -160,7 +160,7 @@ gjs_console_interact(JSContext *context,
 {
     GJS_GET_THIS(context, argc, vp, argv, object);
     bool eof = false;
-    JS::Value result;
+    JS::RootedValue result(context);
     JSString *str;
     GString *buffer = NULL;
     char *temp_buf = NULL;
@@ -195,12 +195,11 @@ gjs_console_interact(JSContext *context,
         JS::CompileOptions options(context);
         options.setUTF8(true)
                .setFileAndLine("typein", startline);
-        js::RootedObject rootedObj(context, object);
-        JS::Evaluate(context, rootedObj, options, buffer->str, buffer->len,  &result);
+        JS::Evaluate(context, object, options, buffer->str, buffer->len, result.address());
 
         gjs_schedule_gc_if_needed(context);
 
-        if (JS_GetPendingException(context, &result)) {
+        if (JS_GetPendingException(context, result.address())) {
             str = JS_ValueToString(context, result);
             JS_ClearPendingException(context);
         } else if (result.isUndefined()) {


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