[gjs] param: use accessors to read name, nick, blurb and default value



commit c57a4d71e32808ad20d598022cd534af85fce04b
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Wed Feb 26 22:50:03 2014 +0100

    param: use accessors to read name, nick, blurb and default value
    
    The GParamSpec API provides binding friendly accessors for
    name, nick, blurb and default, so we don't need to recurse the
    hierarchy looking for a field.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=725282

 gi/param.cpp                 |  106 +-----------------------------------------
 libgjs-private/gjs-util.cpp  |   18 +++++++
 libgjs-private/gjs-util.h    |    6 ++
 modules/overrides/GObject.js |   32 +++++++++++++
 4 files changed, 57 insertions(+), 105 deletions(-)
---
diff --git a/gi/param.cpp b/gi/param.cpp
index 11cf355..efd8d4e 100644
--- a/gi/param.cpp
+++ b/gi/param.cpp
@@ -118,110 +118,6 @@ param_new_resolve(JSContext *context,
     return ret;
 }
 
-
-static GIFieldInfo *
-find_field_info(GIObjectInfo *info,
-                gchar        *name)
-{
-    int i;
-    GIFieldInfo *field_info;
-
-    /* GParamSpecs aren't very big. We could optimize this so that it isn't
-     * O(N), but for the biggest GParamSpec, N=5, so it doesn't really matter. */
-    for (i = 0; i < g_object_info_get_n_fields((GIObjectInfo*)info); i++) {
-        field_info = g_object_info_get_field((GIObjectInfo*)info, i);
-        if (g_str_equal(name, g_base_info_get_name((GIBaseInfo*)field_info)))
-            return field_info;
-
-        g_base_info_unref((GIBaseInfo*)field_info);
-    }
-
-    return NULL;
-}
-
-/* a hook on getting a property; set value_p to override property's value.
- * Return value is JS_FALSE on OOM/exception.
- */
-static JSBool
-param_get_prop(JSContext              *context,
-               JS::HandleObject        obj,
-               JS::HandleId            id,
-               JS::MutableHandleValue  value_p)
-{
-    JSBool success;
-    Param *priv;
-    GParamSpec *pspec;
-    char *name;
-    GType gtype;
-    GIObjectInfo *info = NULL, *parent_info = NULL;
-    GIFieldInfo *field_info = NULL;
-    GITypeInfo *type_info = NULL;
-    GIArgument arg;
-
-    priv = priv_from_js(context, obj);
-    if (priv == NULL) {
-        /* prototype */
-        return JS_TRUE;
-    }
-
-    if (!gjs_get_string_id(context, id, &name))
-        return JS_TRUE; /* not something we affect, but no error */
-
-    success = JS_FALSE;
-    pspec = priv->gparam;
-
-    gtype = G_TYPE_FROM_INSTANCE(pspec);
-    info = (GIObjectInfo*)g_irepository_find_by_gtype(g_irepository_get_default(), gtype);
-
-    if (info == NULL) {
-        /* We may have a non-introspectable GParamSpec subclass here. Just return VOID. */
-        value_p.set(JSVAL_VOID);
-        success = JS_TRUE;
-        goto out;
-    }
-
-    parent_info = g_object_info_get_parent(info);
-
-    field_info = find_field_info(info, name);
-
-    if (field_info == NULL) {
-        /* Try it on the parent GParamSpec for generic GParamSpec properties. */
-        field_info = find_field_info(parent_info, name);
-    }
-
-    if (field_info == NULL) {
-        success = JS_TRUE;
-        goto out;
-    }
-
-    type_info = g_field_info_get_type(field_info);
-
-    if (!g_field_info_get_field(field_info, priv->gparam, &arg)) {
-        gjs_throw(context, "Reading field %s.%s is not supported",
-                  g_base_info_get_name(info),
-                  g_base_info_get_name((GIBaseInfo*)field_info));
-        goto out;
-    }
-
-    if (!gjs_value_from_g_argument(context, value_p.address(), type_info, &arg, TRUE))
-        goto out;
-
-    success = JS_TRUE;
-
- out:
-    if (field_info != NULL)
-        g_base_info_unref((GIBaseInfo*)field_info);
-    if (type_info != NULL)
-        g_base_info_unref((GIBaseInfo*)type_info);
-    if (info != NULL)
-        g_base_info_unref((GIBaseInfo*)info);
-    if (parent_info != NULL)
-        g_base_info_unref((GIBaseInfo*)parent_info);
-    g_free(name);
-
-    return success;
-}
-
 GJS_NATIVE_CONSTRUCTOR_DECLARE(param)
 {
     GJS_NATIVE_CONSTRUCTOR_VARIABLES(param)
@@ -264,7 +160,7 @@ struct JSClass gjs_param_class = {
     JSCLASS_BACKGROUND_FINALIZE,
     JS_PropertyStub,
     JS_DeletePropertyStub,
-    param_get_prop,
+    JS_PropertyStub,
     JS_StrictPropertyStub,
     JS_EnumerateStub,
     (JSResolveOp) param_new_resolve,
diff --git a/libgjs-private/gjs-util.cpp b/libgjs-private/gjs-util.cpp
index 0fb0f2e..645a34d 100644
--- a/libgjs-private/gjs-util.cpp
+++ b/libgjs-private/gjs-util.cpp
@@ -48,3 +48,21 @@ gjs_bindtextdomain(const char *domain,
     /* Always use UTF-8; we assume it internally here */
     bind_textdomain_codeset(domain, "UTF-8");
 }
+
+GParamFlags
+gjs_param_spec_get_flags(GParamSpec *pspec)
+{
+    return pspec->flags;
+}
+
+GType
+gjs_param_spec_get_value_type(GParamSpec *pspec)
+{
+    return pspec->value_type;
+}
+
+GType
+gjs_param_spec_get_owner_type(GParamSpec *pspec)
+{
+    return pspec->owner_type;
+}
diff --git a/libgjs-private/gjs-util.h b/libgjs-private/gjs-util.h
index 0eb55f6..18ca87e 100644
--- a/libgjs-private/gjs-util.h
+++ b/libgjs-private/gjs-util.h
@@ -24,6 +24,7 @@
 #define __GJS_PRIVATE_UTIL_H__
 
 #include <glib.h>
+#include <glib-object.h>
 
 G_BEGIN_DECLS
 
@@ -35,6 +36,11 @@ void gjs_textdomain     (const char *domain);
 void gjs_bindtextdomain (const char *domain,
                          const char *location);
 
+/* For imports.overrides.GObject */
+GParamFlags gjs_param_spec_get_flags (GParamSpec *pspec);
+GType       gjs_param_spec_get_value_type (GParamSpec *pspec);
+GType       gjs_param_spec_get_owner_type (GParamSpec *pspec);
+
 G_END_DECLS
 
 #endif
diff --git a/modules/overrides/GObject.js b/modules/overrides/GObject.js
index 3b5cd68..f66a0c1 100644
--- a/modules/overrides/GObject.js
+++ b/modules/overrides/GObject.js
@@ -20,6 +20,7 @@
 
 const Lang = imports.lang;
 const Gi = imports._gi;
+const GjsPrivate = imports.gi.GjsPrivate;
 
 let GObject;
 
@@ -256,6 +257,37 @@ function _init() {
         return GObject.param_spec_param(name, nick, blurb, param_type, flags);
     };
 
+    Object.defineProperties(this.ParamSpec.prototype, {
+        'name': { configurable: false,
+                  enumerable: false,
+                  get: function() { return this.get_name() } },
+        '_nick': { configurable: false,
+                   enumerable: false,
+                   get: function() { return this.get_nick() } },
+        'nick': { configurable: false,
+                  enumerable: false,
+                  get: function() { return this.get_nick() } },
+        '_blurb': { configurable: false,
+                    enumerable: false,
+                    get: function() { return this.get_blurb() } },
+        'blurb': { configurable: false,
+                   enumerable: false,
+                   get: function() { return this.get_blurb() } },
+        'default_value': { configurable: false,
+                           enumerable: false,
+                           get: function() { return this.get_default_value() } },
+        'flags':  { configurable: false,
+                    enumerable: false,
+                    get: function() { return GjsPrivate.param_spec_get_flags(this) } },
+        'value_type':  { configurable: false,
+                         enumerable: false,
+                         get: function() { return GjsPrivate.param_spec_get_value_type(this) } },
+        'owner_type':  { configurable: false,
+                         enumerable: false,
+                         get: function() { return GjsPrivate.param_spec_get_owner_type(this) } },
+    });
+
+
     this.Class = GObjectMeta;
     this.Object.prototype.__metaclass__ = this.Class;
 


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