[gjs: 1/6] Convert JS_New usage to JS::Construct




commit fa5e60146337fc9b4861d40034b68857c06c6459
Author: Evan Welsh <contact evanwelsh com>
Date:   Sat Jul 10 20:21:17 2021 -0700

    Convert JS_New usage to JS::Construct
    
    JS_New was removed in favor of JS::Construct.
    
    See https://bugzilla.mozilla.org/show_bug.cgi?id=1491055

 doc/SpiderMonkey_Memory.md  |  4 ++--
 gi/gerror.cpp               |  8 +++++++-
 gi/gobject.cpp              | 13 +++++++------
 gjs/jsapi-dynamic-class.cpp |  7 ++++++-
 gjs/jsapi-util-error.cpp    |  5 ++++-
 5 files changed, 26 insertions(+), 11 deletions(-)
---
diff --git a/doc/SpiderMonkey_Memory.md b/doc/SpiderMonkey_Memory.md
index 3e30a7c7..99f893f7 100644
--- a/doc/SpiderMonkey_Memory.md
+++ b/doc/SpiderMonkey_Memory.md
@@ -69,7 +69,7 @@ Note that the wrapped T in `JS::PersistentRooted<T>` is the location of your val
 Here is the trickier part. If you create an object, say:
 
 ```c++
-JSObject *obj = JS_New(cx, whatever, ...);
+JSObject* obj = JS_NewPlainObject(cx);
 ```
 
 `obj` is NOT now referenced by any other object. If the GC ran right away, `obj` would be collected.
@@ -84,7 +84,7 @@ Any SpiderMonkey APIs that can cause a garbage collection will force you to use
 So instead of the above code, you would write
 
 ```c++
-JS::RootedObject obj(cx, JS_New(cx, whatever, ...));
+JS::RootedObject obj(cx, JS_NewPlainObject(cx));
 ```
 
 ### JSFunctionSpec and extra local roots ###
diff --git a/gi/gerror.cpp b/gi/gerror.cpp
index 08745a59..2e62cddb 100644
--- a/gi/gerror.cpp
+++ b/gi/gerror.cpp
@@ -336,7 +336,13 @@ gjs_error_from_js_gerror(JSContext *cx,
     if (!JS_GetClassObject(cx, error_kind, &error_constructor))
         return nullptr;
 
-    return JS_New(cx, error_constructor, error_args);
+    JS::RootedValue v_error_constructor(cx,
+                                        JS::ObjectValue(*error_constructor));
+    JS::RootedObject error(cx);
+    if (!JS::Construct(cx, v_error_constructor, error_args, &error))
+        return nullptr;
+
+    return error;
 }
 
 JSObject* ErrorInstance::object_for_c_ptr(JSContext* context, GError* gerror) {
diff --git a/gi/gobject.cpp b/gi/gobject.cpp
index 8263068b..d6501624 100644
--- a/gi/gobject.cpp
+++ b/gi/gobject.cpp
@@ -137,7 +137,8 @@ static GObject* gjs_object_constructor(
     if (!constructor)
         return nullptr;
 
-    JSObject* object;
+    JS::RootedValue v_constructor(cx, JS::ObjectValue(*constructor));
+    JS::RootedObject object(cx);
     if (n_construct_properties) {
         JS::RootedObject props_hash(cx, JS_NewPlainObject(cx));
 
@@ -149,13 +150,13 @@ static GObject* gjs_object_constructor(
 
         JS::RootedValueArray<1> args(cx);
         args[0].set(JS::ObjectValue(*props_hash));
-        object = JS_New(cx, constructor, args);
-    } else {
-        object = JS_New(cx, constructor, JS::HandleValueArray::empty());
-    }
 
-    if (!object)
+        if (!JS::Construct(cx, v_constructor, args, &object))
+            return nullptr;
+    } else if (!JS::Construct(cx, v_constructor, JS::HandleValueArray::empty(),
+                              &object)) {
         return nullptr;
+    }
 
     auto* priv = ObjectBase::for_js_nocheck(object);
     /* Should have been set in init_impl() and pushed into object_init_list,
diff --git a/gjs/jsapi-dynamic-class.cpp b/gjs/jsapi-dynamic-class.cpp
index fa75a436..bf7d8db9 100644
--- a/gjs/jsapi-dynamic-class.cpp
+++ b/gjs/jsapi-dynamic-class.cpp
@@ -141,7 +141,12 @@ gjs_construct_object_dynamic(JSContext                  *context,
                                      atoms.constructor(), &constructor))
         return NULL;
 
-    return JS_New(context, constructor, args);
+    JS::RootedValue v_constructor(context, JS::ObjectValue(*constructor));
+    JS::RootedObject object(context);
+    if (!JS::Construct(context, v_constructor, args, &object))
+        return nullptr;
+
+    return object;
 }
 
 GJS_JSAPI_RETURN_CONVENTION
diff --git a/gjs/jsapi-util-error.cpp b/gjs/jsapi-util-error.cpp
index a966f0f8..70a060c4 100644
--- a/gjs/jsapi-util-error.cpp
+++ b/gjs/jsapi-util-error.cpp
@@ -71,8 +71,11 @@
     if (!JS_GetClassObject(context, error_kind, &constructor))
         goto out;
 
+    v_constructor.setObject(*constructor);
+
     /* throw new Error(message) */
-    new_exc = JS_New(context, constructor, error_args);
+    if (!JS::Construct(context, v_constructor, error_args, &new_exc))
+        goto out;
 
     if (!new_exc)
         goto out;


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