[gjs] object: Split out gparam aspect of get_prop hook for GObject wrapper



commit b8214734628187620b82546f0ff2179c508825cf
Author: Havoc Pennington <hp pobox com>
Date:   Fri Dec 5 21:55:00 2008 +0000

    object: Split out gparam aspect of get_prop hook for GObject wrapper
    
    Split out the code that tries to resolve properties from the gparams of
    an object.
    
    The idea is to add a similar function fetching properties from fields.
    
    (Stale patch, rebased and expanded to include the set_prop hook by
    Philip Chimento eight years later.)
    
    https://bugzilla.gnome.org/show_bug.cgi?id=563391

 gi/object.cpp |  111 ++++++++++++++++++++++++++++++++++----------------------
 1 files changed, 67 insertions(+), 44 deletions(-)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index f0c67a1..f742fc2 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -253,38 +253,16 @@ proto_priv_from_js(JSContext       *context,
     return priv_from_js(context, proto);
 }
 
-/* a hook on getting a property; set value_p to override property's value.
- * Return value is false on OOM/exception.
- */
 static bool
-object_instance_get_prop(JSContext              *context,
-                         JS::HandleObject        obj,
-                         JS::HandleId            id,
-                         JS::MutableHandleValue  value_p)
+get_prop_from_g_param(JSContext             *context,
+                      JS::HandleObject       obj,
+                      ObjectInstance        *priv,
+                      const char            *name,
+                      JS::MutableHandleValue value_p)
 {
-    ObjectInstance *priv;
-    char *name;
     char *gname;
     GParamSpec *param;
     GValue gvalue = { 0, };
-    bool ret = true;
-
-    if (!gjs_get_string_id(context, id, &name))
-        return true; /* not resolved, but no error */
-
-    priv = priv_from_js(context, obj);
-    gjs_debug_jsprop(GJS_DEBUG_GOBJECT,
-                     "Get prop '%s' hook obj %p priv %p",
-                     name, obj.get(), priv);
-
-    if (priv == NULL) {
-        /* If we reach this point, either object_instance_new_resolve
-         * did not throw (so name == "_init"), or the property actually
-         * exists and it's not something we should be concerned with */
-        goto out;
-    }
-    if (priv->gobj == NULL) /* prototype, not an instance. */
-        goto out;
 
     gname = gjs_hyphen_from_camel(name);
     param = g_object_class_find_property(G_OBJECT_GET_CLASS(priv->gobj),
@@ -293,16 +271,16 @@ object_instance_get_prop(JSContext              *context,
 
     if (param == NULL) {
         /* leave value_p as it was */
-        goto out;
+        return true;
     }
 
     /* Do not fetch JS overridden properties from GObject, to avoid
      * infinite recursion. */
     if (g_param_spec_get_qdata(param, gjs_is_custom_property_quark()))
-        goto out;
+        return true;
 
     if ((param->flags & G_PARAM_READABLE) == 0)
-        goto out;
+        return true;
 
     gjs_debug_jsprop(GJS_DEBUG_GOBJECT,
                      "Overriding %s with GObject prop %s",
@@ -313,29 +291,24 @@ object_instance_get_prop(JSContext              *context,
                           &gvalue);
     if (!gjs_value_from_g_value(context, value_p, &gvalue)) {
         g_value_unset(&gvalue);
-        ret = false;
-        goto out;
+        return false;
     }
     g_value_unset(&gvalue);
 
- out:
-    g_free(name);
-    return ret;
+    return true;
 }
 
-/* a hook on setting a property; set value_p to override property value to
- * be set. Return value is false on OOM/exception.
+/* a hook on getting a property; set value_p to override property's value.
+ * Return value is false on OOM/exception.
  */
 static bool
-object_instance_set_prop(JSContext              *context,
+object_instance_get_prop(JSContext              *context,
                          JS::HandleObject        obj,
                          JS::HandleId            id,
-                         bool                    strict,
                          JS::MutableHandleValue  value_p)
 {
     ObjectInstance *priv;
     char *name;
-    GParameter param = { NULL, { 0, }};
     bool ret = true;
 
     if (!gjs_get_string_id(context, id, &name))
@@ -343,25 +316,42 @@ object_instance_set_prop(JSContext              *context,
 
     priv = priv_from_js(context, obj);
     gjs_debug_jsprop(GJS_DEBUG_GOBJECT,
-                     "Set prop '%s' hook obj %p priv %p",
+                     "Get prop '%s' hook obj %p priv %p",
                      name, obj.get(), priv);
 
     if (priv == NULL) {
-        /* see the comment in object_instance_get_prop() on this */
+        /* If we reach this point, either object_instance_new_resolve
+         * did not throw (so name == "_init"), or the property actually
+         * exists and it's not something we should be concerned with */
         goto out;
     }
     if (priv->gobj == NULL) /* prototype, not an instance. */
         goto out;
 
+    ret = get_prop_from_g_param(context, obj, priv, name, value_p);
+
+ out:
+    g_free(name);
+    return ret;
+}
+
+static bool
+set_g_param_from_prop(JSContext      *context,
+                      ObjectInstance *priv,
+                      const char     *name,
+                      JS::HandleValue value_p)
+{
+    GParameter param = { NULL, { 0, }};
+
     switch (init_g_param_from_property(context, name,
                                        value_p,
                                        G_TYPE_FROM_INSTANCE(priv->gobj),
                                        &param,
                                        false /* constructing */)) {
     case SOME_ERROR_OCCURRED:
-        ret = false;
+        return false;
     case NO_SUCH_G_PROPERTY:
-        goto out;
+        return true;
     case VALUE_WAS_SET:
     default:
         break;
@@ -371,6 +361,39 @@ object_instance_set_prop(JSContext              *context,
                           &param.value);
 
     g_value_unset(&param.value);
+    return true;
+}
+
+/* a hook on setting a property; set value_p to override property value to
+ * be set. Return value is false on OOM/exception.
+ */
+static bool
+object_instance_set_prop(JSContext              *context,
+                         JS::HandleObject        obj,
+                         JS::HandleId            id,
+                         bool                    strict,
+                         JS::MutableHandleValue  value_p)
+{
+    ObjectInstance *priv;
+    char *name;
+    bool ret = true;
+
+    if (!gjs_get_string_id(context, id, &name))
+        return true; /* not resolved, but no error */
+
+    priv = priv_from_js(context, obj);
+    gjs_debug_jsprop(GJS_DEBUG_GOBJECT,
+                     "Set prop '%s' hook obj %p priv %p",
+                     name, obj.get(), priv);
+
+    if (priv == NULL) {
+        /* see the comment in object_instance_get_prop() on this */
+        goto out;
+    }
+    if (priv->gobj == NULL) /* prototype, not an instance. */
+        goto out;
+
+    ret = set_g_param_from_prop(context, priv, name, value_p);
 
     /* note that the prop will also have been set in JS, which I think
      * is OK, since we hook get and set so will always override that


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