[gjs/wip/ptomato/mozjs31prep: 6/8] js: Root misc functions



commit 28bf1e60c23eea790146d37f8fe067bc6cbf42de
Author: Philip Chimento <philip endlessm com>
Date:   Thu Nov 3 16:44:16 2016 -0700

    js: Root misc functions
    
    This ports the rest of the main codebase to use exact GC rooting for
    everything that would otherwise have caused a compile error in mozjs31.
    
    There were only a few modifications per file still to do so I stuck them
    all in one commit.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=742249

 gi/gerror.cpp             |    5 +--
 gi/gtype.cpp              |   24 +++++++++------------
 gi/interface.cpp          |    4 +-
 gjs/byteArray.cpp         |   16 ++++++++------
 gjs/context.cpp           |    3 +-
 gjs/coverage.cpp          |    6 +---
 gjs/importer.cpp          |    5 ++-
 gjs/jsapi-util.h          |    3 +-
 modules/cairo-context.cpp |   50 ++++++++++++++++++++++----------------------
 modules/cairo-region.cpp  |   13 ++++++-----
 modules/system.cpp        |    4 +-
 11 files changed, 66 insertions(+), 67 deletions(-)
---
diff --git a/gi/gerror.cpp b/gi/gerror.cpp
index 6e1d214..70ea7c1 100644
--- a/gi/gerror.cpp
+++ b/gi/gerror.cpp
@@ -458,11 +458,10 @@ gjs_error_from_gerror(JSContext             *context,
                       g_base_info_get_name((GIBaseInfo *)info));
 
     JS::RootedObject proto(context, gjs_lookup_generic_prototype(context, info));
+    JS::RootedObject global(context, gjs_get_import_global(context));
     proto_priv = priv_from_js(context, proto);
 
-    obj = JS_NewObjectWithGivenProto(context,
-                                     JS_GetClass(proto), proto,
-                                     gjs_get_import_global (context));
+    obj = JS_NewObjectWithGivenProto(context, JS_GetClass(proto), proto, global);
 
     GJS_INC_COUNTER(gerror);
     priv = g_slice_new0(Error);
diff --git a/gi/gtype.cpp b/gi/gtype.cpp
index d7900e6..9421fcd 100644
--- a/gi/gtype.cpp
+++ b/gi/gtype.cpp
@@ -149,34 +149,30 @@ _gjs_gtype_get_actual_gtype(JSContext       *context,
                             JS::HandleObject object,
                             int              recurse)
 {
-    GType gtype = G_TYPE_INVALID;
-    JS::Value gtype_val = JS::UndefinedValue();
+    JSAutoRequest ar(context);
 
-    JS_BeginRequest(context);
-    if (JS_InstanceOf(context, object, &gjs_gtype_class, NULL)) {
-        gtype = GPOINTER_TO_SIZE(priv_from_js(context, object));
-        goto out;
-    }
+    if (JS_InstanceOf(context, object, &gjs_gtype_class, NULL))
+        return GPOINTER_TO_SIZE(priv_from_js(context, object));
+
+    JS::RootedValue gtype_val(context);
 
     /* OK, we don't have a GType wrapper object -- grab the "$gtype"
      * property on that and hope it's a GType wrapper object */
-    if (!JS_GetProperty(context, object, "$gtype", &gtype_val) ||
+    if (!JS_GetProperty(context, object, "$gtype", gtype_val.address()) ||
         !gtype_val.isObject()) {
 
         /* OK, so we're not a class. But maybe we're an instance. Check
            for "constructor" and recurse on that. */
-        if (!JS_GetProperty(context, object, "constructor", &gtype_val))
-            goto out;
+        if (!JS_GetProperty(context, object, "constructor", gtype_val.address()))
+            return G_TYPE_INVALID;
     }
 
     if (recurse > 0 && gtype_val.isObject()) {
         JS::RootedObject gtype_obj(context, &gtype_val.toObject());
-        gtype = _gjs_gtype_get_actual_gtype(context, gtype_obj, recurse - 1);
+        return _gjs_gtype_get_actual_gtype(context, gtype_obj, recurse - 1);
     }
 
- out:
-    JS_EndRequest(context);
-    return gtype;
+    return G_TYPE_INVALID;
 }
 
 GType
diff --git a/gi/interface.cpp b/gi/interface.cpp
index 699a7fc..a753792 100644
--- a/gi/interface.cpp
+++ b/gi/interface.cpp
@@ -191,7 +191,6 @@ gjs_define_interface_class(JSContext              *context,
     const char *constructor_name;
     const char *ns;
     JS::RootedObject prototype(context);
-    JS::Value value;
 
     ns = gjs_get_names_from_gtype_and_gi_info(gtype, (GIBaseInfo *) info,
                                               &constructor_name);
@@ -227,7 +226,8 @@ gjs_define_interface_class(JSContext              *context,
     if (priv->info)
         gjs_define_static_methods(context, constructor, priv->gtype, priv->info);
 
-    value = JS::ObjectOrNullValue(gjs_gtype_create_gtype_wrapper(context, priv->gtype));
+    JS::RootedValue value(context,
+        JS::ObjectOrNullValue(gjs_gtype_create_gtype_wrapper(context, priv->gtype)));
     JS_DefineProperty(context, constructor, "$gtype", value,
                       NULL, NULL, JSPROP_PERMANENT);
 
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index 20a329a..d4b77d8 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -534,9 +534,9 @@ byte_array_new(JSContext *context)
 {
     ByteArrayInstance *priv;
 
+    JS::RootedObject proto(context, byte_array_get_prototype(context));
     JS::RootedObject array(context,
-                           JS_NewObject(context, &gjs_byte_array_class,
-                                        byte_array_get_prototype(context), NULL));
+        JS_NewObject(context, &gjs_byte_array_class, proto, NULL));
 
     priv = g_slice_new0(ByteArrayInstance);
 
@@ -664,13 +664,14 @@ from_array_func(JSContext *context,
 
     priv->array = gjs_g_byte_array_new(0);
 
-    if (!JS_IsArrayObject(context, &argv[0].toObject())) {
+    JS::RootedObject array_obj(context, &argv[0].toObject());
+    if (!JS_IsArrayObject(context, array_obj)) {
         gjs_throw(context,
                   "byteArray.fromArray() called with non-array as first arg");
         return false;
     }
 
-    if (!JS_GetArrayLength(context, &argv[0].toObject(), &len)) {
+    if (!JS_GetArrayLength(context, array_obj, &len)) {
         gjs_throw(context,
                   "byteArray.fromArray() can't get length of first array arg");
         return false;
@@ -683,7 +684,7 @@ from_array_func(JSContext *context,
         guint8 b;
 
         elem = JS::UndefinedValue();
-        if (!JS_GetElement(context, &argv[0].toObject(), i, elem.address())) {
+        if (!JS_GetElement(context, array_obj, i, elem.address())) {
             /* this means there was an exception, while elem.isUndefined()
              * means no element found
              */
@@ -743,9 +744,10 @@ gjs_byte_array_from_byte_array (JSContext *context,
     g_return_val_if_fail(context != NULL, NULL);
     g_return_val_if_fail(array != NULL, NULL);
 
+    JS::RootedObject proto(context, byte_array_get_prototype(context));
     JS::RootedObject object(context,
-                            JS_NewObject(context, &gjs_byte_array_class,
-                                         byte_array_get_prototype(context), NULL));
+        JS_NewObject(context, &gjs_byte_array_class, proto, NULL));
+
     if (!object) {
         gjs_throw(context, "failed to create byte array");
         return NULL;
diff --git a/gjs/context.cpp b/gjs/context.cpp
index a8189eb..87d9960 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -723,8 +723,9 @@ gjs_context_define_string_array(GjsContext  *js_context,
                                 GError       **error)
 {
     JSAutoCompartment ac(js_context->context, js_context->global);
+    JS::RootedObject global_root(js_context->context, js_context->global);
     if (!gjs_define_string_array(js_context->context,
-                                 js_context->global,
+                                 global_root,
                                  array_name, array_length, array_values,
                                  JSPROP_READONLY | JSPROP_PERMANENT)) {
         gjs_log_exception(js_context->context);
diff --git a/gjs/coverage.cpp b/gjs/coverage.cpp
index 565ee94..f016137 100644
--- a/gjs/coverage.cpp
+++ b/gjs/coverage.cpp
@@ -1495,10 +1495,8 @@ gjs_run_script_in_coverage_compartment(GjsCoverage *coverage,
     JSContext          *js_context = (JSContext *) gjs_context_get_native_context(priv->context);
     JSAutoCompartment ac(js_context, priv->coverage_statistics);
     JS::RootedValue rval(js_context);
-    if (!gjs_eval_with_scope(js_context,
-                             priv->coverage_statistics,
-                             script,
-                             strlen(script),
+    JS::RootedObject rooted_priv(js_context, priv->coverage_statistics);
+    if (!gjs_eval_with_scope(js_context, rooted_priv, script, strlen(script),
                              "<coverage_modifier>",
                              &rval)) {
         gjs_log_exception(js_context);
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index 70ca39f..1b7f9cc 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -263,6 +263,7 @@ import_file(JSContext       *context,
     GError *error = NULL;
 
     JS::CompileOptions options(context);
+    JS::RootedValue ignored(context);
 
     if (!(g_file_load_contents(file, NULL, &script, &script_len, NULL, &error))) {
         if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY) &&
@@ -280,7 +281,7 @@ import_file(JSContext       *context,
     full_path = g_file_get_parse_name (file);
 
     if (!gjs_eval_with_scope(context, module_obj, script, script_len,
-                             full_path, NULL))
+                             full_path, &ignored))
         goto out;
 
     ret = true;
@@ -762,7 +763,7 @@ importer_new_enumerate(JSContext  *context,
             g_free(dirname);
         }
 
-        statep.get().setPrivate(iter);
+        statep.set(JS::PrivateValue(context));
 
         idp.set(INT_TO_JSID(iter->elements->len));
 
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index 172f5e3..2d699cd 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -227,10 +227,11 @@ gjs_##cname##_create_proto(JSContext *context,                                 \
                                rval, NULL, NULL, GJS_MODULE_PROP_FLAGS)) \
             return JS::NullValue(); \
         if (gtype != G_TYPE_NONE) { \
+            JS::RootedObject rval_obj(context, &rval.toObject());              \
             JS::RootedValue value(context,                                     \
                 JS::ObjectOrNullValue(gjs_gtype_create_gtype_wrapper(context,  \
                                                                      gtype))); \
-            JS_DefineProperty(context, &rval.toObject(), "$gtype", value, \
+            JS_DefineProperty(context, rval_obj, "$gtype", value,              \
                               NULL, NULL, JSPROP_PERMANENT);            \
         } \
     } \
diff --git a/modules/cairo-context.cpp b/modules/cairo-context.cpp
index 8ed57e0..288b74e 100644
--- a/modules/cairo-context.cpp
+++ b/modules/cairo-context.cpp
@@ -81,14 +81,14 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
         return false;                                                      \
     cfunc(cr, &arg1, &arg2);                                               \
     if (cairo_status(cr) == CAIRO_STATUS_SUCCESS) {                        \
-      JSObject *array = JS_NewArrayObject(context, 0, NULL);               \
+      JS::RootedObject array(context,                                      \
+          JS_NewArrayObject(context, 0, NULL));                            \
       if (!array)                                                          \
         return false;                                                      \
-      JS::Value r;                                                         \
-      r = JS::NumberValue(arg1);                                           \
-      if (!JS_SetElement(context, array, 0, &r)) return false;             \
-      r = JS::NumberValue(arg2);                                           \
-      if (!JS_SetElement(context, array, 1, &r)) return false;             \
+      JS::RootedValue r(context, JS::NumberValue(arg1));                   \
+      if (!JS_SetElement(context, array, 0, r.address())) return false;    \
+      r.setNumber(arg2);                                                   \
+      if (!JS_SetElement(context, array, 1, r.address())) return false;    \
       argv.rval().setObject(*array);                                       \
     }                                                                      \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
@@ -99,14 +99,14 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
    _GJS_CAIRO_CONTEXT_CHECK_NO_ARGS(method)                                \
     cfunc(cr, &arg1, &arg2);                                               \
     if (cairo_status(cr) == CAIRO_STATUS_SUCCESS) {                        \
-      JSObject *array = JS_NewArrayObject(context, 0, NULL);               \
+      JS::RootedObject array(context,                                      \
+          JS_NewArrayObject(context, 0, NULL));                            \
       if (!array)                                                          \
         return false;                                                      \
-      JS::Value r;                                                         \
-      r = JS::NumberValue(arg1);                                           \
-      if (!JS_SetElement(context, array, 0, &r)) return false;             \
-      r = JS::NumberValue(arg2);                                           \
-      if (!JS_SetElement(context, array, 1, &r)) return false;             \
+      JS::RootedValue r(context, JS::NumberValue(arg1));                   \
+      if (!JS_SetElement(context, array, 0, r.address())) return false;    \
+      r.setNumber(arg2);                                                   \
+      if (!JS_SetElement(context, array, 1, r.address())) return false;    \
       argv.rval().setObject(*array);                                       \
     }                                                                      \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
@@ -117,18 +117,18 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
    _GJS_CAIRO_CONTEXT_CHECK_NO_ARGS(method)                                \
     cfunc(cr, &arg1, &arg2, &arg3, &arg4);                                 \
     {                                                                      \
-      JSObject *array = JS_NewArrayObject(context, 0, NULL);               \
+      JS::RootedObject array(context,                                      \
+          JS_NewArrayObject(context, 0, NULL));                            \
       if (!array)                                                          \
         return false;                                                      \
-      JS::Value r;                                                         \
-      r = JS::NumberValue(arg1);                                           \
-      if (!JS_SetElement(context, array, 0, &r)) return false;             \
-      r = JS::NumberValue(arg2);                                           \
-      if (!JS_SetElement(context, array, 1, &r)) return false;             \
-      r = JS::NumberValue(arg3);                                           \
-      if (!JS_SetElement(context, array, 2, &r)) return false;             \
-      r = JS::NumberValue(arg4);                                           \
-      if (!JS_SetElement(context, array, 3, &r)) return false;             \
+      JS::RootedValue r(context, JS::NumberValue(arg1));                   \
+      if (!JS_SetElement(context, array, 0, r.address())) return false;    \
+      r.setNumber(arg2);                                                   \
+      if (!JS_SetElement(context, array, 1, r.address())) return false;    \
+      r.setNumber(arg3);                                                   \
+      if (!JS_SetElement(context, array, 2, r.address())) return false;    \
+      r.setNumber(arg4);                                                   \
+      if (!JS_SetElement(context, array, 3, r.address())) return false;    \
       argv.rval().setObject(*array);                                       \
     }                                                                      \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
@@ -553,12 +553,12 @@ setDash_func(JSContext *context,
 
     std::vector<double> dashes_c;
     dashes_c.reserve(len);
+    JS::RootedValue elem(context);
     for (i = 0; i < len; ++i) {
-        JS::Value elem;
         double b;
 
-        elem = JS::UndefinedValue();
-        if (!JS_GetElement(context, dashes, i, &elem)) {
+        elem.setUndefined();
+        if (!JS_GetElement(context, dashes, i, elem.address())) {
             return false;
         }
         if (elem.isUndefined())
diff --git a/modules/cairo-region.cpp b/modules/cairo-region.cpp
index a0c7d97..5aa5f39 100644
--- a/modules/cairo-region.cpp
+++ b/modules/cairo-region.cpp
@@ -147,20 +147,21 @@ static JSObject *
 make_rectangle(JSContext *context,
                cairo_rectangle_int_t *rect)
 {
-    JSObject *rect_obj = JS_NewObject(context, NULL, NULL, NULL);
-    JS::Value val;
+    JS::RootedObject rect_obj(context,
+        JS_NewObject(context, NULL, NULL, NULL));
+    JS::RootedValue val(context);
 
     val = JS::Int32Value(rect->x);
-    JS_SetProperty(context, rect_obj, "x", &val);
+    JS_SetProperty(context, rect_obj, "x", val.address());
 
     val = JS::Int32Value(rect->y);
-    JS_SetProperty(context, rect_obj, "y", &val);
+    JS_SetProperty(context, rect_obj, "y", val.address());
 
     val = JS::Int32Value(rect->width);
-    JS_SetProperty(context, rect_obj, "width", &val);
+    JS_SetProperty(context, rect_obj, "width", val.address());
 
     val = JS::Int32Value(rect->height);
-    JS_SetProperty(context, rect_obj, "height", &val);
+    JS_SetProperty(context, rect_obj, "height", val.address());
 
     return rect_obj;
 }
diff --git a/modules/system.cpp b/modules/system.cpp
index db3e143..f898166 100644
--- a/modules/system.cpp
+++ b/modules/system.cpp
@@ -170,6 +170,7 @@ gjs_js_define_system_stuff(JSContext              *context,
                  NULL);
 
     JS::RootedValue value(context);
+    JS::RootedValue gjs_version(context, JS::Int32Value(GJS_VERSION));
     if (!gjs_string_from_utf8(context, program_name,
                               -1, &value))
         goto out;
@@ -185,8 +186,7 @@ gjs_js_define_system_stuff(JSContext              *context,
         goto out;
 
     if (!JS_DefineProperty(context, module,
-                           "version",
-                           JS::Int32Value(GJS_VERSION),
+                           "version", gjs_version,
                            JS_PropertyStub,
                            JS_StrictPropertyStub,
                            GJS_MODULE_PROP_FLAGS | JSPROP_READONLY))


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