[gjs] importer: Overwrite property rather than change attrs



commit 685f9e42d064d84a8702fd2cfdc752f3e49388ae
Author: Philip Chimento <philip endlessm com>
Date:   Mon Nov 7 12:28:44 2016 -0800

    importer: Overwrite property rather than change attrs
    
    JS_GetPropertyAttributes() and JS_SetPropertyAttributes() are going away
    in mozjs31. Previously seal_import() added the "permanent" flag in the
    property descriptor. Now, we simply overwrite the existing property with
    a new one with the "permanent" flag set. This has the same effect.
    
    Until mozjs31 we have to use JS_GetPropertyDescriptorById() which can
    return properties from higher up the prototype chain. However, on the
    importer object, there will not be any properties of the same name higher
    up the prototype chain, because we already defined the property in
    define_import().
    
    https://bugzilla.gnome.org/show_bug.cgi?id=742249

 gjs/importer.cpp |   27 +++++++++++++++++----------
 1 files changed, 17 insertions(+), 10 deletions(-)
---
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index 12323e3..291c52c 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -153,27 +153,34 @@ define_import(JSContext       *context,
  * we do this after the import succesfully completes.
  */
 static bool
-seal_import(JSContext       *context,
+seal_import(JSContext       *cx,
             JS::HandleObject obj,
             const char      *name)
 {
-    JSBool found;
-    unsigned attrs;
-
-    if (!JS_GetPropertyAttributes(context, obj, name,
-                                  &attrs, &found) || !found) {
+    JS::Rooted<JSPropertyDescriptor> descr(cx);
+    JS::RootedId prop_id(cx, gjs_intern_string_to_id(cx, name));
+
+    /* COMPAT: To be replaced with JS_GetOwnPropertyDescriptor() in mozjs31.
+     * This also looks for properties higher up the prototype chain, but in
+     * practice this will always be an own property because we defined it in
+     * define_import(). */
+    if (!JS_GetPropertyDescriptorById(cx, obj, prop_id, 0 /* flags */,
+                                      descr.address()) ||
+        descr.object() == NULL) {
         gjs_debug(GJS_DEBUG_IMPORTER,
                   "Failed to get attributes to seal '%s' in importer",
                   name);
         return false;
     }
 
-    attrs |= JSPROP_PERMANENT;
+    /* COMPAT: in mozjs45 use .setConfigurable(false) and the form of
+     * JS_DefineProperty that takes the JSPropertyDescriptor directly */
 
-    if (!JS_SetPropertyAttributes(context, obj, name,
-                                  attrs, &found) || !found) {
+    if (!JS_DefineProperty(cx, descr.object(), name, descr.value(),
+                           descr.getter(), descr.setter(),
+                           descr.attributes() | JSPROP_PERMANENT)) {
         gjs_debug(GJS_DEBUG_IMPORTER,
-                  "Failed to set attributes to seal '%s' in importer",
+                  "Failed to redefine attributes to seal '%s' in importer",
                   name);
         return false;
     }


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