[gjs/mozjs91: 5/17] Convert JS_New usage to JS::Construct




commit 32ed1b2ec41b28d7ce949557983ac14c3f4c3d83
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  |  6 ++++--
 gi/gerror.cpp               |  8 +++++++-
 gi/gobject.cpp              | 14 ++++++++++----
 gjs/jsapi-dynamic-class.cpp | 10 +++++++++-
 gjs/jsapi-util-error.cpp    |  7 +++++--
 5 files changed, 35 insertions(+), 10 deletions(-)
---
diff --git a/doc/SpiderMonkey_Memory.md b/doc/SpiderMonkey_Memory.md
index 3e30a7c7..5348e852 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::Construct(cx, whatever, ...);
 ```
 
 `obj` is NOT now referenced by any other object. If the GC ran right away, `obj` would be collected.
@@ -84,7 +84,9 @@ 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);
+if (!JS::Construct(cx, whatever, ..., &obj))
+  ...
 ```
 
 ### JSFunctionSpec and extra local roots ###
diff --git a/gi/gerror.cpp b/gi/gerror.cpp
index 08745a59..d3a2fb60 100644
--- a/gi/gerror.cpp
+++ b/gi/gerror.cpp
@@ -335,8 +335,14 @@ gjs_error_from_js_gerror(JSContext *cx,
     JS::RootedObject error_constructor(cx);
     if (!JS_GetClassObject(cx, error_kind, &error_constructor))
         return nullptr;
+    JS::RootedValue error_constructorv(cx);
+    error_constructorv.setObject(*error_constructor);
 
-    return JS_New(cx, error_constructor, error_args);
+    JS::RootedObject object(cx);
+    if (!JS::Construct(cx, error_constructorv, error_args, &object))
+        return nullptr;
+
+    return object;
 }
 
 JSObject* ErrorInstance::object_for_c_ptr(JSContext* context, GError* gerror) {
diff --git a/gi/gobject.cpp b/gi/gobject.cpp
index 8263068b..c589ce31 100644
--- a/gi/gobject.cpp
+++ b/gi/gobject.cpp
@@ -137,7 +137,10 @@ static GObject* gjs_object_constructor(
     if (!constructor)
         return nullptr;
 
-    JSObject* object;
+    JS::RootedValue constructorv(cx);
+    constructorv.setObject(*constructor);
+
+    JS::RootedObject object(cx);
     if (n_construct_properties) {
         JS::RootedObject props_hash(cx, JS_NewPlainObject(cx));
 
@@ -149,9 +152,12 @@ 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 (!JS::Construct(cx, constructorv, args, &object))
+            return nullptr;
+    } else if (!JS::Construct(cx, constructorv, JS::HandleValueArray::empty(),
+                              &object)) {
+        return nullptr;
     }
 
     if (!object)
diff --git a/gjs/jsapi-dynamic-class.cpp b/gjs/jsapi-dynamic-class.cpp
index 3b23a5c3..cb206d22 100644
--- a/gjs/jsapi-dynamic-class.cpp
+++ b/gjs/jsapi-dynamic-class.cpp
@@ -141,7 +141,15 @@ gjs_construct_object_dynamic(JSContext                  *context,
                                      atoms.constructor(), &constructor))
         return NULL;
 
-    return JS_New(context, constructor, args);
+    JS::RootedValue constructorv(context);
+    constructorv.setObject(*constructor);
+
+    JS::RootedObject object(context);
+
+    if (!JS::Construct(context, constructorv, 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..835a81b3 100644
--- a/gjs/jsapi-util-error.cpp
+++ b/gjs/jsapi-util-error.cpp
@@ -68,11 +68,14 @@
         goto out;
     }
 
-    if (!JS_GetClassObject(context, error_kind, &constructor))
+    if (!JS_GetClassObject(context, error_kind, &constructor) || !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]