[gjs/mozjs91: 25/25] object: Don't fetch property descriptor on interface




commit 8163638d24a5453d319f63a202da3da78143f455
Author: Philip Chimento <philip chimento gmail com>
Date:   Fri Jan 14 16:55:00 2022 -0800

    object: Don't fetch property descriptor on interface
    
    It seems that nothing is actually used from the property descriptor, we
    only check if it exists, which can be done with JS_HasProperty.
    
    The previous code looked like we were copying the flags from the original
    descriptor, but I believe that was incorrect; they were all overwritten
    after calling setValue(), setGetterObject(), and setSetterObject(). In any
    case it seems like the new property must be configurable even if the
    previous property descriptor wasn't, because otherwise
    GObject.registerClass() can't redefine it. This seems to me like a good
    indication that the flags actually weren't being copied.

 gi/object.cpp | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index fa9409f4..d5964223 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -42,7 +42,6 @@
 #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"
@@ -701,15 +700,13 @@ static bool resolve_on_interface_prototype(JSContext* cx,
     if (!interface_prototype)
         return false;
 
-    JS::Rooted<mozilla::Maybe<JS::PropertyDescriptor>> desc(cx);
-    JS::RootedObject holder(cx);
-    if (!JS_GetPropertyDescriptorById(cx, interface_prototype, identifier,
-                                      &desc, &holder))
+    bool exists = false;
+    if (!JS_HasPropertyById(cx, interface_prototype, identifier, &exists))
         return false;
 
     // If the property doesn't exist on the interface prototype, we don't need
     // to perform this trick.
-    if (desc.isNothing()) {
+    if (!exists) {
         *found = false;
         return true;
     }
@@ -755,14 +752,15 @@ static bool resolve_on_interface_prototype(JSContext* cx,
     if (!JS_SetPropertyById(cx, accessor, atoms.prototype(), v_prototype))
         return false;
 
-    // Copy the original descriptor and remove any value, instead
-    // adding our getter and setter.
-    JS::Rooted<JS::PropertyDescriptor> target_desc(cx, desc);
-    target_desc.setValue(JS::UndefinedHandleValue);
-    target_desc.setGetterObject(getter);
-    target_desc.setSetterObject(setter);
+    // Create a new descriptor with our getter and setter, that is configurable
+    // and enumerable, because GObject may need to redefine it later.
+    JS::Rooted<JS::PropertyDescriptor> desc(
+        cx,
+        JS::PropertyDescriptor::Accessor(getter, setter,
+                                         {JS::PropertyAttribute::Configurable,
+                                          JS::PropertyAttribute::Enumerable}));
 
-    if (!JS_DefinePropertyById(cx, class_prototype, identifier, target_desc))
+    if (!JS_DefinePropertyById(cx, class_prototype, identifier, desc))
         return false;
 
     *found = true;


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