[gjs] value: Special case GValues in signals and properties



commit 6bebc3aeaa5c522b8037f8d9e607bcc2390e57cf
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Thu Sep 6 17:03:11 2012 +0200

    value: Special case GValues in signals and properties
    
    If gjs is handling a GValue that itself holds a GValue, such as a
    signal argument or a property, it should marshal the JS value into
    a GValue and then set it as a boxed type, as otherwise there is no
    way to build a JS boxed wrapping a GValue.
    
    (Original patch by Giovanni Campagna, reverse path and tests added later
    by Philip Chimento)
    
    https://bugzilla.gnome.org/show_bug.cgi?id=688128

 gi/value.cpp                            |   24 +++++++++++++++++++++---
 installed-tests/js/testGIMarshalling.js |   14 ++++++++++++++
 2 files changed, 35 insertions(+), 3 deletions(-)
---
diff --git a/gi/value.cpp b/gi/value.cpp
index 3e5d55f..186bed5 100644
--- a/gi/value.cpp
+++ b/gi/value.cpp
@@ -521,9 +521,21 @@ gjs_value_to_g_value_internal(JSContext      *context,
         void *gboxed;
 
         gboxed = NULL;
-        if (value.isNull()) {
-            /* nothing to do */
-        } else if (value.isObject()) {
+        if (value.isNull())
+            return true;
+
+        /* special case GValue */
+        if (g_type_is_a(gtype, G_TYPE_VALUE)) {
+            GValue nested_gvalue = G_VALUE_INIT;
+
+            if (!gjs_value_to_g_value(context, value, &nested_gvalue))
+                return false;
+
+            g_value_set_boxed(gvalue, &nested_gvalue);
+            return true;
+        }
+
+        if (value.isObject()) {
             JS::RootedObject obj(context, &value.toObject());
 
             if (g_type_is_a(gtype, G_TYPE_ERROR)) {
@@ -863,6 +875,12 @@ gjs_value_from_g_value_internal(JSContext             *context,
             return true;
         }
 
+        /* special case GValue */
+        if (g_type_is_a(gtype, G_TYPE_VALUE)) {
+            return gjs_value_from_g_value(context, value_p,
+                                          static_cast<GValue *>(gboxed));
+        }
+
         /* The only way to differentiate unions and structs is from
          * their g-i info as both GBoxed */
         info = g_irepository_find_by_gtype(g_irepository_get_default(),
diff --git a/installed-tests/js/testGIMarshalling.js b/installed-tests/js/testGIMarshalling.js
index e71edff..bf144d9 100644
--- a/installed-tests/js/testGIMarshalling.js
+++ b/installed-tests/js/testGIMarshalling.js
@@ -644,3 +644,17 @@ describe('Interface', function () {
         expect(ifaceImpl).toEqual(itself);
     });
 });
+
+describe('GObject properties', function () {
+    let obj;
+    beforeEach(function () {
+        obj = new GIMarshallingTests.PropertiesObject();
+    });
+
+    it('can handle GValues', function () {
+        obj.some_gvalue = 42;
+        expect(obj.some_gvalue).toEqual(42);
+        obj.some_gvalue = 'foo';
+        expect(obj.some_gvalue).toEqual('foo');
+    });
+});


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