[gjs] Stop exposing GType structures as such



commit 18e82fd8bd08171b7c0807c29b3d8367e62ec0ec
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Tue May 14 22:07:15 2013 +0200

    Stop exposing GType structures as such
    
    Having methods exposed in GType structures like they were boxeds
    forces one to use a horrible hack through prototype to call them.
    Instead, have those methods installed on the JS classes like
    static methods. A hack is still required to call them on subclasses,
    but it's smaller.
    
    Note that this commit fixes the testGObjectClass.js test broken in
    f3c9b6e33b9d316349911a881a5f7ff5d4c102c4
    
    https://bugzilla.gnome.org/show_bug.cgi?id=700347

 gi/ns.cpp                            |    6 ++++--
 gi/object.cpp                        |   21 +++++++++++++++++++++
 gi/repo.cpp                          |   16 +++++++++++++++-
 gi/repo.h                            |    3 ++-
 installed-tests/js/testGTypeClass.js |    2 +-
 5 files changed, 43 insertions(+), 5 deletions(-)
---
diff --git a/gi/ns.cpp b/gi/ns.cpp
index 5c84281..12ec27e 100644
--- a/gi/ns.cpp
+++ b/gi/ns.cpp
@@ -67,6 +67,7 @@ ns_new_resolve(JSContext *context,
     GIRepository *repo;
     GIBaseInfo *info;
     JSBool ret = JS_FALSE;
+    gboolean defined;
 
     if (!gjs_get_string_id(context, id, &name))
         return JS_TRUE; /* not resolved, but no error */
@@ -104,9 +105,10 @@ ns_new_resolve(JSContext *context,
               g_base_info_get_name(info),
               g_base_info_get_namespace(info));
 
-    if (gjs_define_info(context, obj, info)) {
+    if (gjs_define_info(context, obj, info, &defined)) {
         g_base_info_unref(info);
-        objp.set(obj); /* we defined the property in this object */
+        if (defined)
+            objp.set(obj); /* we defined the property in this object */
         ret = JS_TRUE;
     } else {
         gjs_debug(GJS_DEBUG_GNAMESPACE,
diff --git a/gi/object.cpp b/gi/object.cpp
index 4cffc40..b571878 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1871,6 +1871,7 @@ gjs_object_define_static_methods(JSContext    *context,
                                  GType         gtype,
                                  GIObjectInfo *object_info)
 {
+    GIStructInfo *gtype_struct;
     int i;
     int n_methods;
 
@@ -1897,6 +1898,26 @@ gjs_object_define_static_methods(JSContext    *context,
 
         g_base_info_unref((GIBaseInfo*) meth_info);
     }
+
+    gtype_struct = g_object_info_get_class_struct(object_info);
+
+    if (gtype_struct == NULL)
+        return JS_TRUE;
+
+    n_methods = g_struct_info_get_n_methods(gtype_struct);
+
+    for (i = 0; i < n_methods; i++) {
+        GIFunctionInfo *meth_info;
+
+        meth_info = g_struct_info_get_method(gtype_struct, i);
+
+        gjs_define_function(context, constructor, gtype,
+                            (GICallableInfo*)meth_info);
+
+        g_base_info_unref((GIBaseInfo*) meth_info);
+    }
+
+    g_base_info_unref((GIBaseInfo*) gtype_struct);
     return JS_TRUE;
 }
 
diff --git a/gi/repo.cpp b/gi/repo.cpp
index 2516fc8..a085a96 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -453,11 +453,15 @@ _gjs_log_info_usage(GIBaseInfo *info)
 JSBool
 gjs_define_info(JSContext  *context,
                 JSObject   *in_object,
-                GIBaseInfo *info)
+                GIBaseInfo *info,
+                gboolean   *defined)
 {
 #if GJS_VERBOSE_ENABLE_GI_USAGE
     _gjs_log_info_usage(info);
 #endif
+
+    *defined = TRUE;
+
     switch (g_base_info_get_type(info)) {
     case GI_INFO_TYPE_FUNCTION:
         {
@@ -492,6 +496,16 @@ gjs_define_info(JSContext  *context,
         }
         break;
     case GI_INFO_TYPE_STRUCT:
+        /* We don't want GType structures in the namespace,
+           we expose their fields as vfuncs and their methods
+           as static methods
+        */
+        if (g_struct_info_is_gtype_struct((GIStructInfo*) info)) {
+            *defined = FALSE;
+            break;
+        }
+        /* Fall through */
+
     case GI_INFO_TYPE_BOXED:
         gjs_define_boxed_class(context, in_object, (GIBoxedInfo*) info);
         break;
diff --git a/gi/repo.h b/gi/repo.h
index 918d528..ba1fd90 100644
--- a/gi/repo.h
+++ b/gi/repo.h
@@ -49,7 +49,8 @@ JSObject *  gjs_lookup_generic_prototype        (JSContext      *context,
                                                  GIBaseInfo     *info);
 JSBool      gjs_define_info                     (JSContext      *context,
                                                  JSObject       *in_object,
-                                                 GIBaseInfo     *info);
+                                                 GIBaseInfo     *info,
+                                                 gboolean       *defined);
 char*       gjs_camel_from_hyphen               (const char     *hyphen_name);
 char*       gjs_hyphen_from_camel               (const char     *camel_name);
 
diff --git a/installed-tests/js/testGTypeClass.js b/installed-tests/js/testGTypeClass.js
index 0db60e0..b0a877a 100644
--- a/installed-tests/js/testGTypeClass.js
+++ b/installed-tests/js/testGTypeClass.js
@@ -10,7 +10,7 @@ const Gio = imports.gi.Gio;
 const GObject = imports.gi.GObject;
 
 function testGObjectClass() {
-    let find_property = GObject.ObjectClass.prototype.find_property;
+    let find_property = GObject.Object.find_property;
 
     let p1 = find_property.call(Gio.ThemedIcon, 'name');
 


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