[gjs/mozjs78] Fix proto properties for dynamic classes. - Flags is no longer writable, so edit it inline.



commit 199913ea7fdc678324de0502176994f442fb8ce9
Author: Evan Welsh <noreply evanwelsh com>
Date:   Sun Jul 5 18:58:11 2020 -0500

    Fix proto properties for dynamic classes.
    - Flags is no longer writable, so edit it inline.

 gjs/jsapi-dynamic-class.cpp | 48 ++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 43 insertions(+), 5 deletions(-)
---
diff --git a/gjs/jsapi-dynamic-class.cpp b/gjs/jsapi-dynamic-class.cpp
index 200c3c59..1ab89e27 100644
--- a/gjs/jsapi-dynamic-class.cpp
+++ b/gjs/jsapi-dynamic-class.cpp
@@ -50,6 +50,46 @@ enum {
     DYNAMIC_PROPERTY_PRIVATE_SLOT,
 };
 
+static bool DefineLazyProperties(JSContext* cx, JS::HandleObject obj,
+                                 const JSPropertySpec* ps) {
+    auto prop = ps;
+
+    for (; prop->name; prop++) {
+        g_assert(!prop->isSelfHosted() &&
+                 "Prototype properties cannot be self hosted.");
+
+        auto name = prop->name;
+
+        g_assert(!name.isSymbol() &&
+                 "Prototype properties cannot be set with symbols.");
+        g_assert(name.isString() && "Prototype properties must be strings.");
+
+        auto id = name.string();
+
+        if (prop->isAccessor()) {
+            if (!JS_DefineProperty(cx, obj, id,
+                                   prop->u.accessors.getter.native.op,
+                                   prop->u.accessors.setter.native.op,
+                                   JSPROP_RESOLVING | prop->attributes())) {
+                return false;
+            }
+        } else {
+            JS::RootedValue v(cx);
+
+            if (!prop->getValue(cx, &v)) {
+                return false;
+            }
+
+            if (!JS_DefineProperty(cx, obj, id, v,
+                                   JSPROP_RESOLVING | prop->attributes())) {
+                return false;
+            }
+        }
+    }
+
+    return true;
+}
+
 bool gjs_init_class_dynamic(JSContext* context, JS::HandleObject in_object,
                             JS::HandleObject parent_proto, const char* ns_name,
                             const char* class_name, const JSClass* clasp,
@@ -88,17 +128,15 @@ bool gjs_init_class_dynamic(JSContext* context, JS::HandleObject in_object,
 
     /* Bypass resolve hooks when defining the initial properties */
     if (clasp->cOps->resolve) {
-        JSPropertySpec *ps_iter;
         JSFunctionSpec *fs_iter;
-        // COMPAT(mozjs78): https://phabricator.services.mozilla.com/D72528
-        // for (ps_iter = proto_ps; ps_iter && ps_iter->name; ps_iter++)
-        //    ps_iter->flags |= JSPROP_RESOLVING;
         for (fs_iter = proto_fs; fs_iter && fs_iter->name; fs_iter++)
             fs_iter->flags |= JSPROP_RESOLVING;
     }
 
-    if (proto_ps && !JS_DefineProperties(context, prototype, proto_ps))
+    if (proto_ps && !DefineLazyProperties(context, prototype, proto_ps)) {
         return false;
+    }
+
     if (proto_fs && !JS_DefineFunctions(context, prototype, proto_fs))
         return false;
 


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