[gjs/mozjs78: 6/21] Refactor Array-related JSAPI calls. - Use <js/Array.h> for... JS::NewArrayObject, JS::GetArrayLeng



commit c42e427fdea19e61a7ca779af274b4ef0a87f875
Author: Evan Welsh <noreply evanwelsh com>
Date:   Sat Jul 4 22:09:47 2020 -0500

    Refactor Array-related JSAPI calls.
    - Use <js/Array.h> for...
      JS::NewArrayObject, JS::GetArrayLength, and JS::IsArrayObject
    - JS::AutoValueArray is now JS::RootedValueArray

 gi/arg.cpp                |  15 ++---
 gi/boxed.cpp              |   2 +-
 gi/function.cpp           |   5 +-
 gi/gerror.cpp             |   2 +-
 gi/gobject.cpp            |   2 +-
 gi/private.cpp            |  13 +++--
 gjs/importer.cpp          |   9 +--
 gjs/jsapi-util-error.cpp  |   2 +-
 gjs/jsapi-util.cpp        |   3 +-
 modules/cairo-context.cpp | 136 +++++++++++++++++++++++++---------------------
 test/gjs-tests.cpp        |   5 +-
 11 files changed, 106 insertions(+), 88 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index ba706021..4f1796f0 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -34,6 +34,7 @@
 #include <glib-object.h>
 #include <glib.h>
 
+#include <js/Array.h>
 #include <js/CharacterEncoding.h>
 #include <js/Conversions.h>
 #include <js/GCVector.h>            // for RootedVector, MutableWrappedPtrOp...
@@ -778,7 +779,7 @@ gjs_array_from_strv(JSContext             *context,
             return false;
     }
 
-    JS::RootedObject obj(context, JS_NewArrayObject(context, elems));
+    JS::RootedObject obj(context, JS::NewArrayObject(context, elems));
     if (!obj)
         return false;
 
@@ -1198,7 +1199,7 @@ gjs_array_from_flat_gvalue_array(JSContext             *context,
 
     // a null array pointer takes precedence over whatever `length` says
     if (!values) {
-        JSObject* jsarray = JS_NewArrayObject(context, 0);
+        JSObject* jsarray = JS::NewArrayObject(context, 0);
         if (!jsarray)
             return false;
         value.setObject(*jsarray);
@@ -1223,7 +1224,7 @@ gjs_array_from_flat_gvalue_array(JSContext             *context,
 
     if (result) {
         JSObject *jsarray;
-        jsarray = JS_NewArrayObject(context, elems);
+        jsarray = JS::NewArrayObject(context, elems);
         value.setObjectOrNull(jsarray);
     }
 
@@ -2435,7 +2436,7 @@ gjs_array_from_g_list (JSContext             *context,
         }
     }
 
-    JS::RootedObject obj(context, JS_NewArrayObject(context, elems));
+    JS::RootedObject obj(context, JS::NewArrayObject(context, elems));
     if (!obj)
         return false;
 
@@ -2491,7 +2492,7 @@ gjs_array_from_carray_internal (JSContext             *context,
 
     // a null array pointer takes precedence over whatever `length` says
     if (!array) {
-        JSObject* jsarray = JS_NewArrayObject(context, 0);
+        JSObject* jsarray = JS::NewArrayObject(context, 0);
         if (!jsarray)
             return false;
         value_p.setObject(*jsarray);
@@ -2592,7 +2593,7 @@ gjs_array_from_carray_internal (JSContext             *context,
 
 #undef ITERATE
 
-    JS::RootedObject obj(context, JS_NewArrayObject(context, elems));
+    JS::RootedObject obj(context, JS::NewArrayObject(context, elems));
     if (!obj)
         return false;
 
@@ -2799,7 +2800,7 @@ gjs_array_from_zero_terminated_c_array (JSContext             *context,
 
 #undef ITERATE
 
-    JS::RootedObject obj(context, JS_NewArrayObject(context, elems));
+    JS::RootedObject obj(context, JS::NewArrayObject(context, elems));
     if (!obj)
         return false;
 
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index 9c1bd8a7..a7d8253a 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -608,7 +608,7 @@ bool BoxedInstance::set_nested_interface_object(JSContext* context,
      */
     BoxedBase* source_priv = get_copy_source(context, value);
     if (!source_priv) {
-        JS::AutoValueArray<1> args(context);
+        JS::RootedValueArray<1> args(context);
         args[0].set(value);
         JS::RootedObject tmp_object(context,
             gjs_construct_object_dynamic(context, proto, args));
diff --git a/gi/function.cpp b/gi/function.cpp
index 15f14caa..28bd183e 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -36,6 +36,7 @@
 #include <glib-object.h>
 #include <glib.h>
 
+#include <js/Array.h>
 #include <js/CallArgs.h>
 #include <js/Class.h>
 #include <js/GCVector.h>
@@ -392,7 +393,7 @@ static void gjs_callback_closure(ffi_cif* cif G_GNUC_UNUSED, void* result,
         }
     } else {
         bool is_array = rval.isObject();
-        if (!JS_IsArrayObject(context, rval, &is_array))
+        if (!JS::IsArrayObject(context, rval, &is_array))
             goto out;
 
         if (!is_array) {
@@ -1413,7 +1414,7 @@ release:
                 args.rval().set(return_values[0]);
             } else {
                 JSObject *array;
-                array = JS_NewArrayObject(context, return_values);
+                array = JS::NewArrayObject(context, return_values);
                 if (array == NULL) {
                     failed = true;
                 } else {
diff --git a/gi/gerror.cpp b/gi/gerror.cpp
index 38e1e577..81f07c57 100644
--- a/gi/gerror.cpp
+++ b/gi/gerror.cpp
@@ -349,7 +349,7 @@ static JSObject *
 gjs_error_from_js_gerror(JSContext *cx,
                          GError    *gerror)
 {
-    JS::AutoValueArray<1> error_args(cx);
+    JS::RootedValueArray<1> error_args(cx);
     if (!gjs_string_from_utf8(cx, gerror->message, error_args[0]))
         return nullptr;
 
diff --git a/gi/gobject.cpp b/gi/gobject.cpp
index 5e16d77c..fdd78c10 100644
--- a/gi/gobject.cpp
+++ b/gi/gobject.cpp
@@ -128,7 +128,7 @@ static GObject* gjs_object_constructor(
                                      construct_properties[i].pspec))
                 return nullptr;
 
-        JS::AutoValueArray<1> args(cx);
+        JS::RootedValueArray<1> args(cx);
         args[0].set(JS::ObjectValue(*props_hash));
         object = JS_New(cx, constructor, args);
     } else {
diff --git a/gi/private.cpp b/gi/private.cpp
index 9159776d..44c73cc3 100644
--- a/gi/private.cpp
+++ b/gi/private.cpp
@@ -29,13 +29,14 @@
 #include <glib-object.h>
 #include <glib.h>
 
+#include <js/Array.h>  // for JS::GetArrayLength,
 #include <js/CallArgs.h>
 #include <js/Id.h>  // for JSID_TO_SYMBOL
 #include <js/PropertySpec.h>
 #include <js/RootingAPI.h>
 #include <js/TypeDecls.h>
 #include <js/Utility.h>  // for UniqueChars
-#include <jsapi.h>       // for JS_GetArrayLength, JS_GetElement
+#include <jsapi.h>       // for JS_GetElement
 
 #include "gi/gobject.h"
 #include "gi/gtype.h"
@@ -106,7 +107,7 @@ static bool validate_interfaces_and_properties_args(JSContext* cx,
                                                     uint32_t* n_interfaces,
                                                     uint32_t* n_properties) {
     bool is_array;
-    if (!JS_IsArrayObject(cx, interfaces, &is_array))
+    if (!JS::IsArrayObject(cx, interfaces, &is_array))
         return false;
     if (!is_array) {
         gjs_throw(cx, "Invalid parameter interfaces (expected Array)");
@@ -114,10 +115,10 @@ static bool validate_interfaces_and_properties_args(JSContext* cx,
     }
 
     uint32_t n_int;
-    if (!JS_GetArrayLength(cx, interfaces, &n_int))
+    if (!JS::GetArrayLength(cx, interfaces, &n_int))
         return false;
 
-    if (!JS_IsArrayObject(cx, properties, &is_array))
+    if (!JS::IsArrayObject(cx, properties, &is_array))
         return false;
     if (!is_array) {
         gjs_throw(cx, "Invalid parameter properties (expected Array)");
@@ -125,7 +126,7 @@ static bool validate_interfaces_and_properties_args(JSContext* cx,
     }
 
     uint32_t n_prop;
-    if (!JS_GetArrayLength(cx, properties, &n_prop))
+    if (!JS::GetArrayLength(cx, properties, &n_prop))
         return false;
 
     if (n_interfaces)
@@ -384,7 +385,7 @@ static bool gjs_signal_new(JSContext* cx, unsigned argc, JS::Value* vp) {
     }
 
     uint32_t n_parameters;
-    if (!JS_GetArrayLength(cx, params_obj, &n_parameters))
+    if (!JS::GetArrayLength(cx, params_obj, &n_parameters))
         return false;
 
     GType* params = g_newa(GType, n_parameters);
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index 628d3cad..d43130d9 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -36,6 +36,7 @@
 #include <gio/gio.h>
 #include <glib.h>
 
+#include <js/Array.h>
 #include <js/CallArgs.h>
 #include <js/CharacterEncoding.h>
 #include <js/Class.h>
@@ -478,14 +479,14 @@ static bool do_import(JSContext* context, JS::HandleObject obj, Importer* priv,
                                      atoms.search_path(), &search_path))
         return false;
 
-    if (!JS_IsArrayObject(context, search_path, &is_array))
+    if (!JS::IsArrayObject(context, search_path, &is_array))
         return false;
     if (!is_array) {
         gjs_throw(context, "searchPath property on importer is not an array");
         return false;
     }
 
-    if (!JS_GetArrayLength(context, search_path, &search_path_len)) {
+    if (!JS::GetArrayLength(context, search_path, &search_path_len)) {
         gjs_throw(context, "searchPath array has no length");
         return false;
     }
@@ -633,14 +634,14 @@ static bool importer_new_enumerate(JSContext* context, JS::HandleObject object,
                                      atoms.search_path(), &search_path))
         return false;
 
-    if (!JS_IsArrayObject(context, search_path, &is_array))
+    if (!JS::IsArrayObject(context, search_path, &is_array))
         return false;
     if (!is_array) {
         gjs_throw(context, "searchPath property on importer is not an array");
         return false;
     }
 
-    if (!JS_GetArrayLength(context, search_path, &search_path_len)) {
+    if (!JS::GetArrayLength(context, search_path, &search_path_len)) {
         gjs_throw(context, "searchPath array has no length");
         return false;
     }
diff --git a/gjs/jsapi-util-error.cpp b/gjs/jsapi-util-error.cpp
index 509e3c92..0546ca66 100644
--- a/gjs/jsapi-util-error.cpp
+++ b/gjs/jsapi-util-error.cpp
@@ -83,7 +83,7 @@ gjs_throw_valist(JSContext       *context,
     JS::RootedObject constructor(context);
     JS::RootedValue v_constructor(context), exc_val(context);
     JS::RootedObject new_exc(context);
-    JS::AutoValueArray<1> error_args(context);
+    JS::RootedValueArray<1> error_args(context);
     result = false;
 
     if (!gjs_string_from_utf8(context, s, error_args[0])) {
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 4d7193b2..26622a3b 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -38,6 +38,7 @@
 #include <utility>  // for move
 #include <vector>
 
+#include <js/Array.h>
 #include <js/CallArgs.h>
 #include <js/CharacterEncoding.h>
 #include <js/Class.h>
@@ -236,7 +237,7 @@ JSObject* gjs_build_string_array(JSContext* context,
         elems.infallibleAppend(element);
     }
 
-    return JS_NewArrayObject(context, elems);
+    return JS::NewArrayObject(context, elems);
 }
 
 JSObject* gjs_define_string_array(JSContext* context,
diff --git a/modules/cairo-context.cpp b/modules/cairo-context.cpp
index 52b7734c..a6e8bd2d 100644
--- a/modules/cairo-context.cpp
+++ b/modules/cairo-context.cpp
@@ -29,6 +29,7 @@
 #include <girepository.h>
 #include <glib.h>
 
+#include <js/Array.h>  // for JS::NewArrayObject
 #include <js/CallArgs.h>
 #include <js/Class.h>
 #include <js/Conversions.h>
@@ -37,7 +38,7 @@
 #include <js/TypeDecls.h>
 #include <js/Utility.h>  // for UniqueChars
 #include <js/Value.h>
-#include <jsapi.h>  // for JS_SetElement, JS_NewArrayObject
+#include <jsapi.h>  // for JS_SetElement
 
 #include "gi/arg.h"
 #include "gi/foreign.h"
@@ -87,65 +88,76 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     argv.rval().setBoolean(ret);                                           \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
 
-#define _GJS_CAIRO_CONTEXT_DEFINE_FUNC2FFAFF(method, cfunc, n1, n2)        \
-_GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
-    double arg1, arg2;                                                     \
-    if (!gjs_parse_call_args(context, #method, argv, "ff",                 \
-                             #n1, &arg1, #n2, &arg2))                      \
-        return false;                                                      \
-    cfunc(cr, &arg1, &arg2);                                               \
-    if (cairo_status(cr) == CAIRO_STATUS_SUCCESS) {                        \
-      JS::RootedObject array(context,                                      \
-          JS_NewArrayObject(context, JS::HandleValueArray::empty()));      \
-      if (!array)                                                          \
-        return false;                                                      \
-      JS::RootedValue r(context, JS::NumberValue(arg1));                   \
-      if (!JS_SetElement(context, array, 0, r)) return false;              \
-      r.setNumber(arg2);                                                   \
-      if (!JS_SetElement(context, array, 1, r)) return false;              \
-      argv.rval().setObject(*array);                                       \
-    }                                                                      \
-_GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
-
-#define _GJS_CAIRO_CONTEXT_DEFINE_FUNC0AFF(method, cfunc)                  \
-_GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
-    double arg1, arg2;                                                     \
-   _GJS_CAIRO_CONTEXT_CHECK_NO_ARGS(method)                                \
-    cfunc(cr, &arg1, &arg2);                                               \
-    if (cairo_status(cr) == CAIRO_STATUS_SUCCESS) {                        \
-      JS::RootedObject array(context,                                      \
-          JS_NewArrayObject(context, JS::HandleValueArray::empty()));      \
-      if (!array)                                                          \
-        return false;                                                      \
-      JS::RootedValue r(context, JS::NumberValue(arg1));                   \
-      if (!JS_SetElement(context, array, 0, r)) return false;              \
-      r.setNumber(arg2);                                                   \
-      if (!JS_SetElement(context, array, 1, r)) return false;              \
-      argv.rval().setObject(*array);                                       \
-    }                                                                      \
-_GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
-
-#define _GJS_CAIRO_CONTEXT_DEFINE_FUNC0AFFFF(method, cfunc)                \
-_GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
-    double arg1, arg2, arg3, arg4;                                         \
-   _GJS_CAIRO_CONTEXT_CHECK_NO_ARGS(method)                                \
-    cfunc(cr, &arg1, &arg2, &arg3, &arg4);                                 \
-    {                                                                      \
-      JS::RootedObject array(context,                                      \
-          JS_NewArrayObject(context, JS::HandleValueArray::empty()));      \
-      if (!array)                                                          \
-        return false;                                                      \
-      JS::RootedValue r(context, JS::NumberValue(arg1));                   \
-      if (!JS_SetElement(context, array, 0, r)) return false;              \
-      r.setNumber(arg2);                                                   \
-      if (!JS_SetElement(context, array, 1, r)) return false;              \
-      r.setNumber(arg3);                                                   \
-      if (!JS_SetElement(context, array, 2, r)) return false;              \
-      r.setNumber(arg4);                                                   \
-      if (!JS_SetElement(context, array, 3, r)) return false;              \
-      argv.rval().setObject(*array);                                       \
-    }                                                                      \
-_GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
+#define _GJS_CAIRO_CONTEXT_DEFINE_FUNC2FFAFF(method, cfunc, n1, n2)         \
+    _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                            \
+    double arg1, arg2;                                                      \
+    if (!gjs_parse_call_args(context, #method, argv, "ff", #n1, &arg1, #n2, \
+                             &arg2))                                        \
+        return false;                                                       \
+    cfunc(cr, &arg1, &arg2);                                                \
+    if (cairo_status(cr) == CAIRO_STATUS_SUCCESS) {                         \
+        JS::RootedObject array(                                             \
+            context,                                                        \
+            JS::NewArrayObject(context, JS::HandleValueArray::empty()));    \
+        if (!array)                                                         \
+            return false;                                                   \
+        JS::RootedValue r(context, JS::NumberValue(arg1));                  \
+        if (!JS_SetElement(context, array, 0, r))                           \
+            return false;                                                   \
+        r.setNumber(arg2);                                                  \
+        if (!JS_SetElement(context, array, 1, r))                           \
+            return false;                                                   \
+        argv.rval().setObject(*array);                                      \
+    }                                                                       \
+    _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
+
+#define _GJS_CAIRO_CONTEXT_DEFINE_FUNC0AFF(method, cfunc)                \
+    _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                         \
+    double arg1, arg2;                                                   \
+    _GJS_CAIRO_CONTEXT_CHECK_NO_ARGS(method)                             \
+    cfunc(cr, &arg1, &arg2);                                             \
+    if (cairo_status(cr) == CAIRO_STATUS_SUCCESS) {                      \
+        JS::RootedObject array(                                          \
+            context,                                                     \
+            JS::NewArrayObject(context, JS::HandleValueArray::empty())); \
+        if (!array)                                                      \
+            return false;                                                \
+        JS::RootedValue r(context, JS::NumberValue(arg1));               \
+        if (!JS_SetElement(context, array, 0, r))                        \
+            return false;                                                \
+        r.setNumber(arg2);                                               \
+        if (!JS_SetElement(context, array, 1, r))                        \
+            return false;                                                \
+        argv.rval().setObject(*array);                                   \
+    }                                                                    \
+    _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
+
+#define _GJS_CAIRO_CONTEXT_DEFINE_FUNC0AFFFF(method, cfunc)              \
+    _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                         \
+    double arg1, arg2, arg3, arg4;                                       \
+    _GJS_CAIRO_CONTEXT_CHECK_NO_ARGS(method)                             \
+    cfunc(cr, &arg1, &arg2, &arg3, &arg4);                               \
+    {                                                                    \
+        JS::RootedObject array(                                          \
+            context,                                                     \
+            JS::NewArrayObject(context, JS::HandleValueArray::empty())); \
+        if (!array)                                                      \
+            return false;                                                \
+        JS::RootedValue r(context, JS::NumberValue(arg1));               \
+        if (!JS_SetElement(context, array, 0, r))                        \
+            return false;                                                \
+        r.setNumber(arg2);                                               \
+        if (!JS_SetElement(context, array, 1, r))                        \
+            return false;                                                \
+        r.setNumber(arg3);                                               \
+        if (!JS_SetElement(context, array, 2, r))                        \
+            return false;                                                \
+        r.setNumber(arg4);                                               \
+        if (!JS_SetElement(context, array, 3, r))                        \
+            return false;                                                \
+        argv.rval().setObject(*array);                                   \
+    }                                                                    \
+    _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
 
 #define _GJS_CAIRO_CONTEXT_DEFINE_FUNC0F(method, cfunc)                    \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
@@ -543,14 +555,14 @@ setDash_func(JSContext *context,
                              "offset", &offset))
         return false;
 
-    if (!JS_IsArrayObject(context, dashes, &is_array))
+    if (!JS::IsArrayObject(context, dashes, &is_array))
         return false;
     if (!is_array) {
         gjs_throw(context, "dashes must be an array");
         return false;
     }
 
-    if (!JS_GetArrayLength(context, dashes, &len)) {
+    if (!JS::GetArrayLength(context, dashes, &len)) {
         gjs_throw(context, "Can't get length of dashes");
         return false;
     }
diff --git a/test/gjs-tests.cpp b/test/gjs-tests.cpp
index 8c2e596c..eea68366 100644
--- a/test/gjs-tests.cpp
+++ b/test/gjs-tests.cpp
@@ -31,6 +31,7 @@
 #include <glib.h>
 #include <glib/gstdio.h>  // for g_unlink
 
+#include <js/Array.h>
 #include <js/CharacterEncoding.h>
 #include <js/RootingAPI.h>
 #include <js/TypeDecls.h>
@@ -309,10 +310,10 @@ static void test_jsapi_util_debug_string_object_with_complicated_to_string(
         0xd83c, 0xdf6a,  /* cookie */
         0xd83c, 0xdf69,  /* doughnut */
     };
-    JS::AutoValueArray<2> contents(fx->cx);
+    JS::RootedValueArray<2> contents(fx->cx);
     contents[0].setString(JS_NewUCStringCopyN(fx->cx, desserts, 2));
     contents[1].setString(JS_NewUCStringCopyN(fx->cx, desserts + 2, 2));
-    JS::RootedObject array(fx->cx, JS_NewArrayObject(fx->cx, contents));
+    JS::RootedObject array(fx->cx, JS::NewArrayObject(fx->cx, contents));
     JS::RootedValue v_array(fx->cx, JS::ObjectValue(*array));
     char *debug_output = gjs_value_debug_string(fx->cx, v_array);
 


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