[gjs] boxed, param, union: add $gtype to constructors



commit 1fc375c124dc493e987907503391cef05f8db6a2
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Sat Feb 11 16:05:41 2012 +0100

    boxed, param, union: add $gtype to constructors
    
    Various places assume that they can grab the GType for a class
    by retrieving the $gtype property. This extends this convention
    to boxed types, unions and GParamSpec.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=669848

 gi/boxed.c |   12 +++++++++---
 gi/param.c |   18 ++++++++++++++++--
 gi/union.c |   31 +++++++++++++++++--------------
 3 files changed, 42 insertions(+), 19 deletions(-)
---
diff --git a/gi/boxed.c b/gi/boxed.c
index 7e627a3..c8bc3ad 100644
--- a/gi/boxed.c
+++ b/gi/boxed.c
@@ -32,6 +32,7 @@
 #include <gjs/compat.h>
 #include "repo.h"
 #include "function.h"
+#include "gtype.h"
 
 #include <util/log.h>
 
@@ -1093,6 +1094,7 @@ gjs_define_boxed_class(JSContext    *context,
     JSObject *constructor;
     jsval value;
     Boxed *priv;
+    GType gtype;
 
     /* See the comment in gjs_define_object_class() for an
      * explanation of how this all works; Boxed is pretty much the
@@ -1179,10 +1181,14 @@ gjs_define_boxed_class(JSContext    *context,
         }
     }
 
+    gtype = g_registered_type_info_get_g_type((GIRegisteredTypeInfo *) priv->info);
+
     constructor = JSVAL_TO_OBJECT(value);
-    gjs_define_static_methods (context, constructor,
-                               g_registered_type_info_get_g_type (priv->info),
-                               priv->info);
+    gjs_define_static_methods (context, constructor, gtype, priv->info);
+
+    value = OBJECT_TO_JSVAL(gjs_gtype_create_gtype_wrapper(context, gtype));
+    JS_DefineProperty(context, constructor, "$gtype", value,
+                      NULL, NULL, JSPROP_PERMANENT);
 
     if (constructor_p)
         *constructor_p = constructor;
diff --git a/gi/param.c b/gi/param.c
index 2e2c201..11adb6b 100644
--- a/gi/param.c
+++ b/gi/param.c
@@ -485,12 +485,12 @@ gjs_define_param_class(JSContext    *context,
     const char *constructor_name;
     JSObject *prototype;
     jsval value;
+    JSObject *constructor;
 
     constructor_name = "ParamSpec";
 
     gjs_object_get_property(context, in_object, constructor_name, &value);
     if (value != JSVAL_VOID) {
-        JSObject *constructor;
 
         if (!JSVAL_IS_OBJECT(value)) {
             gjs_throw(context, "Existing property '%s' does not look like a constructor",
@@ -544,8 +544,22 @@ gjs_define_param_class(JSContext    *context,
     if (prototype == NULL)
         gjs_fatal("Can't init class %s", constructor_name);
 
-    g_assert(gjs_object_has_property(context, in_object, constructor_name));
+    constructor = NULL;
+    gjs_object_get_property(context, in_object, constructor_name, &value);
+    if (value != JSVAL_VOID) {
+        if (!JSVAL_IS_OBJECT(value)) {
+            gjs_throw(context, "Property '%s' does not look like a constructor",
+                      constructor_name);
+            return JS_FALSE;
+        }
+    }
+
+    constructor = JSVAL_TO_OBJECT(value);
 
+    value = OBJECT_TO_JSVAL(gjs_gtype_create_gtype_wrapper(context, G_TYPE_PARAM));
+    JS_DefineProperty(context, constructor, "$gtype", value,
+                      NULL, NULL, JSPROP_PERMANENT);
+    
     if (prototype_p)
         *prototype_p = prototype;
 
diff --git a/gi/union.c b/gi/union.c
index 63f1bb0..cc4f84e 100644
--- a/gi/union.c
+++ b/gi/union.c
@@ -35,7 +35,7 @@
 #include <gjs/compat.h>
 #include "repo.h"
 #include "function.h"
-
+#include "gtype.h"
 
 #include <jsapi.h>
 
@@ -380,6 +380,7 @@ gjs_define_union_class(JSContext    *context,
     jsval value;
     Union *priv;
     GType gtype;
+    JSObject *constructor;
 
     /* For certain unions, we may be able to relax this in the future by
      * directly allocating union memory, as we do for structures in boxed.c
@@ -398,8 +399,6 @@ gjs_define_union_class(JSContext    *context,
     constructor_name = g_base_info_get_name( (GIBaseInfo*) info);
 
     if (gjs_object_get_property(context, in_object, constructor_name, &value)) {
-        JSObject *constructor;
-
         if (!JSVAL_IS_OBJECT(value)) {
             gjs_throw(context, "Existing property '%s' does not look like a constructor",
                          constructor_name);
@@ -459,20 +458,24 @@ gjs_define_union_class(JSContext    *context,
     gjs_debug(GJS_DEBUG_GBOXED, "Defined class %s prototype is %p class %p in object %p",
               constructor_name, prototype, JS_GET_CLASS(context, prototype), in_object);
 
-    if (constructor_p) {
-        *constructor_p = NULL;
-        gjs_object_get_property(context, in_object, constructor_name, &value);
-        if (value != JSVAL_VOID) {
-            if (!JSVAL_IS_OBJECT(value)) {
-                gjs_throw(context, "Property '%s' does not look like a constructor",
-                          constructor_name);
-                return JS_FALSE;
-            }
+    gjs_object_get_property(context, in_object, constructor_name, &value);
+    if (value != JSVAL_VOID) {
+        if (!JSVAL_IS_OBJECT(value)) {
+            gjs_throw(context, "Property '%s' does not look like a constructor",
+                      constructor_name);
+            return JS_FALSE;
         }
-
-        *constructor_p = JSVAL_TO_OBJECT(value);
     }
 
+    constructor = JSVAL_TO_OBJECT(value);
+    
+    value = OBJECT_TO_JSVAL(gjs_gtype_create_gtype_wrapper(context, gtype));
+    JS_DefineProperty(context, constructor, "$gtype", value,
+                      NULL, NULL, JSPROP_PERMANENT);
+
+    if (constructor_p)
+        *constructor_p = constructor;
+
     if (prototype_p)
         *prototype_p = prototype;
 



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