diff options
author | Jasper St. Pierre <jstpierre@mecheye.net> | 2011-12-07 10:37:41 -0500 |
---|---|---|
committer | Jasper St. Pierre <jstpierre@mecheye.net> | 2011-12-15 17:16:05 -0500 |
commit | ebc844929d90fb2f86be6713b52074432e183aed (patch) | |
tree | 0e83dc2a4b20a9816f17ccec1543cc6102d4ff0a | |
parent | 378ff475f1510192974c72709c2cdd9844de85e1 (diff) | |
download | gjs-ebc844929d90fb2f86be6713b52074432e183aed.tar.gz |
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
-rw-r--r-- | gi/enumeration.c | 6 | ||||
-rw-r--r-- | gi/object.c | 32 | ||||
-rw-r--r-- | gi/object.h | 2 |
3 files changed, 40 insertions, 0 deletions
diff --git a/gi/enumeration.c b/gi/enumeration.c index cda00a5d..c7734b47 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 37435969..79a921db 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", >ype_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 e0dc3a43..d6b93758 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 |