[gjs/gnome-40] GObject: Call only setter for JS-defined construct-only properties



commit b46278740cdc631ca7419271a5abb63ed6ca41d7
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon Jun 21 00:18:40 2021 +0200

    GObject: Call only setter for JS-defined construct-only properties
    
    Only construct-only properties defined in JS GObject subclasses need
    to poke into the several property accessors. This is notably superfluous
    if the construct-only property comes from a parent GObject in C-land
    (since then the JS object should not define the JS properties for it).
    
    Avoid doing this with construct-only properties not defined by JS
    objects.

 gi/gobject.cpp | 41 ++++++++++++++++++++++-------------------
 1 file changed, 22 insertions(+), 19 deletions(-)
---
diff --git a/gi/gobject.cpp b/gi/gobject.cpp
index 1de4b469..b86872c2 100644
--- a/gi/gobject.cpp
+++ b/gi/gobject.cpp
@@ -58,31 +58,34 @@ static bool jsobj_set_gproperty(JSContext* cx, JS::HandleObject object,
     if (pspec->flags & G_PARAM_CONSTRUCT_ONLY) {
         unsigned flags = GJS_MODULE_PROP_FLAGS | JSPROP_READONLY;
         GjsAutoChar camel_name = gjs_hyphen_to_camel(pspec->name);
-        JS::Rooted<JS::PropertyDescriptor> jsprop(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 (g_param_spec_get_qdata(pspec, ObjectBase::custom_property_quark())) {
+            JS::Rooted<JS::PropertyDescriptor> jsprop(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))
+                    return false;
+                if (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))
+                    return false;
+                if (jsprop.setter() &&
+                    !JS_SetProperty(cx, object, camel_name, jsvalue))
+                    return false;
+            }
+
+            if (!JS_GetPropertyDescriptor(cx, object, pspec->name, &jsprop))
                 return false;
             if (jsprop.setter() &&
-                !JS_SetProperty(cx, object, underscore_name, jsvalue))
+                !JS_SetProperty(cx, object, pspec->name, jsvalue))
                 return false;
         }
 
-        if (!g_str_equal(camel_name.get(), pspec->name)) {
-            if (!JS_GetPropertyDescriptor(cx, object, camel_name, &jsprop))
-                return false;
-            if (jsprop.setter() &&
-                !JS_SetProperty(cx, object, camel_name, jsvalue))
-                return false;
-        }
-
-        if (!JS_GetPropertyDescriptor(cx, object, pspec->name, &jsprop))
-            return false;
-        if (jsprop.setter() &&
-            !JS_SetProperty(cx, object, pspec->name, jsvalue))
-            return false;
-
         return JS_DefineProperty(cx, object, underscore_name, jsvalue, flags) &&
                JS_DefineProperty(cx, object, camel_name, jsvalue, flags) &&
                JS_DefineProperty(cx, object, pspec->name, jsvalue, flags);


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