[gjs] gtype: fix name and toString() on the prototype



commit fbb840121e77cbc3d2fa207707b6819180ed6a41
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Mon Feb 24 15:59:12 2014 +0100

    gtype: fix name and toString() on the prototype
    
    Due to limitations of JS_InitClass, the prototype of GType objects
    is exposed as an object on the global, and people might accidentally
    call stuff on it. Let's not crash in that case.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=724925

 gi/gtype.cpp                            |   20 ++++++++++++++------
 installed-tests/js/testGIMarshalling.js |    5 +++++
 2 files changed, 19 insertions(+), 6 deletions(-)
---
diff --git a/gi/gtype.cpp b/gi/gtype.cpp
index a13b4f4..6ce4ca8 100644
--- a/gi/gtype.cpp
+++ b/gi/gtype.cpp
@@ -74,8 +74,11 @@ to_string_func(JSContext *context,
     
     gtype = GPOINTER_TO_SIZE(priv_from_js(context, obj));
 
-    strval = g_strdup_printf("[object GType for '%s']",
-                             g_type_name(gtype));
+    if (gtype == 0)
+        strval = g_strdup("[object GType prototype]");
+    else
+        strval = g_strdup_printf("[object GType for '%s']",
+                                 g_type_name(gtype));
     ret = gjs_string_from_utf8(context, strval, -1, &retval);
     if (ret)
         JS_SET_RVAL(context, vp, retval);
@@ -95,10 +98,15 @@ get_name_func (JSContext *context,
 
     gtype = GPOINTER_TO_SIZE(priv_from_js(context, *obj));
 
-    ret = gjs_string_from_utf8(context, g_type_name(gtype), -1, &retval);
-    if (ret)
-        JS_SET_RVAL(context, vp, retval);
-    return ret;
+    if (gtype == 0) {
+        JS_SET_RVAL(context, vp, JSVAL_NULL);
+        return TRUE;
+    } else {
+        ret = gjs_string_from_utf8(context, g_type_name(gtype), -1, &retval);
+        if (ret)
+            JS_SET_RVAL(context, vp, retval);
+        return ret;
+    }
 }
 
 /* Properties */
diff --git a/installed-tests/js/testGIMarshalling.js b/installed-tests/js/testGIMarshalling.js
index da8af79..c63a6d5 100644
--- a/installed-tests/js/testGIMarshalling.js
+++ b/installed-tests/js/testGIMarshalling.js
@@ -273,6 +273,11 @@ function testGType() {
     assertEquals(GObject.TYPE_INT, GIMarshallingTests.gtype_inout(GObject.TYPE_NONE));
 }
 
+function testGTypePrototype() {
+    assertNull(GIRepositoryGType.name);
+    assertEquals("[object GType prototype]", GIRepositoryGType.toString());
+}
+
 function testGValueGType() {
     // test that inferring the GType for a primitive value or an object works
 


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