[gjs/mozjs91: 12/25] Adapt to new Maybe-based property descriptor API




commit be9491ac05f0309108b499ad6ca126ad2fc49f9a
Author: Evan Welsh <contact evanwelsh com>
Date:   Sat Jul 10 20:38:39 2021 -0700

    Adapt to new Maybe-based property descriptor API
    
    object() was previously used to indicate whether a property descriptor
    was found. Now it's indicated by whether the returned Maybe is Some or
    Nothing and the descriptor object is returned separately in the holder
    out-parameter.
    
    See https://bugzilla.mozilla.org/show_bug.cgi?id=1706404

 gi/gobject.cpp   | 29 ++++++++++++++++++++---------
 gi/object.cpp    |  8 +++++---
 gjs/gjs_pch.hh   |  1 +
 gjs/importer.cpp | 12 ++++++++----
 gjs/module.cpp   | 13 ++++++++-----
 5 files changed, 42 insertions(+), 21 deletions(-)
---
diff --git a/gi/gobject.cpp b/gi/gobject.cpp
index b9247781..95b03378 100644
--- a/gi/gobject.cpp
+++ b/gi/gobject.cpp
@@ -17,6 +17,7 @@
 #include <js/Value.h>
 #include <js/ValueArray.h>
 #include <jsapi.h>  // for JS_SetProperty, JS_DefineProperty
+#include <mozilla/Maybe.h>
 
 #include "gi/gobject.h"
 #include "gi/object.h"
@@ -61,28 +62,38 @@ static bool jsobj_set_gproperty(JSContext* cx, JS::HandleObject object,
         GjsAutoChar camel_name = gjs_hyphen_to_camel(pspec->name);
 
         if (g_param_spec_get_qdata(pspec, ObjectBase::custom_property_quark())) {
-            JS::Rooted<JS::PropertyDescriptor> jsprop(cx);
+            JS::Rooted<mozilla::Maybe<JS::PropertyDescriptor>> jsprop(cx);
+            JS::RootedObject holder(cx);
 
             // Ensure to call any associated setter method
             if (!g_str_equal(underscore_name.get(), pspec->name)) {
-                if (!JS_GetPropertyDescriptor(cx, object, underscore_name, &jsprop))
+                if (!JS_GetPropertyDescriptor(cx, object, underscore_name,
+                                              &jsprop, &holder)) {
                     return false;
-                if (jsprop.setter() &&
-                    !JS_SetProperty(cx, object, underscore_name, jsvalue))
+                }
+
+                if (jsprop.isSome() && jsprop->setter() &&
+                    !JS_SetProperty(cx, object, underscore_name, jsvalue)) {
                     return false;
+                }
             }
 
             if (!g_str_equal(camel_name.get(), pspec->name)) {
-                if (!JS_GetPropertyDescriptor(cx, object, camel_name, &jsprop))
+                if (!JS_GetPropertyDescriptor(cx, object, camel_name, &jsprop,
+                                              &holder)) {
                     return false;
-                if (jsprop.setter() &&
-                    !JS_SetProperty(cx, object, camel_name, jsvalue))
+                }
+
+                if (jsprop.isSome() && jsprop.value().setter() &&
+                    !JS_SetProperty(cx, object, camel_name, jsvalue)) {
                     return false;
+                }
             }
 
-            if (!JS_GetPropertyDescriptor(cx, object, pspec->name, &jsprop))
+            if (!JS_GetPropertyDescriptor(cx, object, pspec->name, &jsprop,
+                                          &holder))
                 return false;
-            if (jsprop.setter() &&
+            if (jsprop.isSome() && jsprop.value().setter() &&
                 !JS_SetProperty(cx, object, pspec->name, jsvalue))
                 return false;
         }
diff --git a/gi/object.cpp b/gi/object.cpp
index 6dd47807..fa9409f4 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -42,6 +42,7 @@
 #include <jsapi.h>        // for IsCallable
 #include <jsfriendapi.h>  // for JS_GetObjectFunction, IsFunctionO...
 #include <mozilla/HashTable.h>
+#include <mozilla/Maybe.h>
 
 #include "gi/arg-inl.h"
 #include "gi/arg.h"
@@ -700,14 +701,15 @@ static bool resolve_on_interface_prototype(JSContext* cx,
     if (!interface_prototype)
         return false;
 
-    JS::Rooted<JS::PropertyDescriptor> desc(cx);
+    JS::Rooted<mozilla::Maybe<JS::PropertyDescriptor>> desc(cx);
+    JS::RootedObject holder(cx);
     if (!JS_GetPropertyDescriptorById(cx, interface_prototype, identifier,
-                                      &desc))
+                                      &desc, &holder))
         return false;
 
     // If the property doesn't exist on the interface prototype, we don't need
     // to perform this trick.
-    if (!desc.object()) {
+    if (desc.isNothing()) {
         *found = false;
         return true;
     }
diff --git a/gjs/gjs_pch.hh b/gjs/gjs_pch.hh
index 4b0d8a29..46bea8a9 100644
--- a/gjs/gjs_pch.hh
+++ b/gjs/gjs_pch.hh
@@ -109,6 +109,7 @@
 #include <mozilla/HashFunctions.h>
 #include <mozilla/HashTable.h>
 #include <mozilla/Likely.h>
+#include <mozilla/Maybe.h>
 #include <mozilla/UniquePtr.h>
 #include <mozilla/Unused.h>
 #ifdef HAVE_READLINE_READLINE_H
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index 4145aae3..273ab1bc 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -11,7 +11,6 @@
 #endif
 
 #include <string>
-#include <utility>  // for move
 #include <vector>   // for vector
 
 #include <gio/gio.h>
@@ -37,6 +36,7 @@
 #include <js/Value.h>
 #include <jsapi.h>    // for JS_DefinePropertyById, JS_DefineP...
 #include <jspubtd.h>  // for JSProto_Error
+#include <mozilla/Maybe.h>
 #include <mozilla/UniquePtr.h>
 
 #include "gjs/atoms.h"
@@ -201,17 +201,21 @@ seal_import(JSContext       *cx,
             JS::HandleId     id,
             const char      *name)
 {
-    JS::Rooted<JS::PropertyDescriptor> descr(cx);
+    JS::Rooted<mozilla::Maybe<JS::PropertyDescriptor>> maybe_descr(cx);
 
-    if (!JS_GetOwnPropertyDescriptorById(cx, obj, id, &descr) || !descr.object()) {
+    if (!JS_GetOwnPropertyDescriptorById(cx, obj, id, &maybe_descr) ||
+        maybe_descr.isNothing()) {
         gjs_debug(GJS_DEBUG_IMPORTER,
                   "Failed to get attributes to seal '%s' in importer",
                   name);
         return false;
     }
 
+    JS::Rooted<JS::PropertyDescriptor> descr(cx, maybe_descr.value());
+
     descr.setConfigurable(false);
-    if (!JS_DefinePropertyById(cx, descr.object(), id, descr)) {
+
+    if (!JS_DefinePropertyById(cx, obj, id, descr)) {
         gjs_debug(GJS_DEBUG_IMPORTER,
                   "Failed to redefine attributes to seal '%s' in importer",
                   name);
diff --git a/gjs/module.cpp b/gjs/module.cpp
index 51884958..c7e9e1e8 100644
--- a/gjs/module.cpp
+++ b/gjs/module.cpp
@@ -37,6 +37,7 @@
 #include <js/ValueArray.h>
 #include <jsapi.h>  // for JS_DefinePropertyById, ...
 #include <jsfriendapi.h>  // for SetFunctionNativeReserved
+#include <mozilla/Maybe.h>
 
 #include "gjs/atoms.h"
 #include "gjs/context-private.h"
@@ -183,9 +184,12 @@ class GjsScriptModule {
             return true;  /* nothing imported yet */
         }
 
-        if (!JS_HasPropertyById(cx, lexical, id, resolved))
+        JS::Rooted<mozilla::Maybe<JS::PropertyDescriptor>> maybe_desc(cx);
+        JS::RootedObject holder(cx);
+        if (!JS_GetPropertyDescriptorById(cx, lexical, id, &maybe_desc,
+                                          &holder))
             return false;
-        if (!*resolved)
+        if (maybe_desc.isNothing())
             return true;
 
         /* The property is present in the lexical environment. This should not
@@ -201,9 +205,8 @@ class GjsScriptModule {
                   "please fix your code anyway.",
                   gjs_debug_id(id).c_str(), m_name);
 
-        JS::Rooted<JS::PropertyDescriptor> desc(cx);
-        return JS_GetPropertyDescriptorById(cx, lexical, id, &desc) &&
-            JS_DefinePropertyById(cx, module, id, desc);
+        JS::Rooted<JS::PropertyDescriptor> desc(cx, maybe_desc.value());
+        return JS_DefinePropertyById(cx, module, id, desc);
     }
 
     GJS_JSAPI_RETURN_CONVENTION


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