[gjs] Add a common way to grab a GType for an object



commit ebc844929d90fb2f86be6713b52074432e183aed
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Wed Dec 7 10:37:41 2011 -0500

    Add a common way to grab a GType for an object
    
    This is only hooked up for objects, enums and flags right now, but it's
    absolutely possible that more will be hooked up in the future.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=665432

 gi/enumeration.c |    6 ++++++
 gi/object.c      |   32 ++++++++++++++++++++++++++++++++
 gi/object.h      |    2 ++
 3 files changed, 40 insertions(+), 0 deletions(-)
---
diff --git a/gi/enumeration.c b/gi/enumeration.c
index cda00a5..c7734b4 100644
--- a/gi/enumeration.c
+++ b/gi/enumeration.c
@@ -108,6 +108,7 @@ gjs_define_enumeration(JSContext    *context,
                        JSObject    **enumeration_p)
 {
     const char *enum_name;
+    GType gtype;
     JSObject *enum_obj;
     jsval value;
     int i;
@@ -165,6 +166,11 @@ gjs_define_enumeration(JSContext    *context,
         }
     }
 
+    gtype = g_registered_type_info_get_g_type((GIRegisteredTypeInfo*)info);
+    value = INT_TO_JSVAL(gtype);
+    JS_DefineProperty(context, enum_obj, "$gtype", value,
+                      NULL, NULL, JSPROP_PERMANENT);
+
     gjs_debug(GJS_DEBUG_GENUM,
               "Defining %s.%s as %p",
               g_base_info_get_namespace( (GIBaseInfo*) info),
diff --git a/gi/object.c b/gi/object.c
index 3743596..79a921d 100644
--- a/gi/object.c
+++ b/gi/object.c
@@ -1376,6 +1376,10 @@ gjs_define_object_class(JSContext     *context,
        gjs_define_static_methods(context, constructor, info);
     }
 
+    value = INT_TO_JSVAL(gtype);
+    JS_DefineProperty(context, constructor, "$gtype", value,
+                      NULL, NULL, JSPROP_PERMANENT);
+
     if (prototype_p)
         *prototype_p = prototype;
 
@@ -1528,3 +1532,31 @@ gjs_g_object_from_object(JSContext    *context,
 
     return priv->gobj;
 }
+
+GType
+gjs_gtype_from_value(JSContext *context,
+                     jsval      val)
+{
+    GType gtype = G_TYPE_NONE;
+
+    if (JSVAL_IS_INT(val))
+        return JSVAL_TO_INT(val);
+
+    if (JSVAL_IS_OBJECT(val)) {
+        JS_BeginRequest(context);
+
+        JSObject *obj = JSVAL_TO_OBJECT(val);
+        jsval gtype_val;
+        if (!JS_GetProperty(context, obj, "$gtype", &gtype_val))
+            goto out;
+
+        if (!JSVAL_IS_INT(gtype_val))
+            goto out;
+
+        gtype = JSVAL_TO_INT(gtype_val);
+    }
+
+ out:
+    JS_EndRequest(context);
+    return gtype;
+}
diff --git a/gi/object.h b/gi/object.h
index e0dc3a4..d6b9375 100644
--- a/gi/object.h
+++ b/gi/object.h
@@ -44,6 +44,8 @@ JSObject* gjs_object_from_g_object      (JSContext     *context,
                                          GObject       *gobj);
 GObject*  gjs_g_object_from_object      (JSContext     *context,
                                          JSObject      *obj);
+GType     gjs_gtype_from_value         (JSContext     *context,
+                                        jsval          value);
 
 G_END_DECLS
 



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