[gjs/mozjs91: 46/64] JS_New -> JS::Construct




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

    JS_New -> JS::Construct

 doc/SpiderMonkey_Memory.md |  6 ++++--
 gi/gerror.cpp              |  8 +++++++-
 gi/gobject.cpp             | 14 ++++++++++----
 gjs/jsapi-util-error.cpp   |  7 +++++--
 4 files changed, 26 insertions(+), 9 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-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]