[seed] Improving GVariant support using gjs syntax.



commit 170fea769cc887ad9cac8339f686a6ea7a56ca6d
Author: Danilo Cesar Lemes de Paula <danilo cesar collabora co uk>
Date:   Tue Aug 16 11:56:46 2016 -0300

    Improving GVariant support using gjs syntax.
    
    GJS syntax allows the creation of GVariant using a custom constructor.

 jsextensions/gjs/GLib.js |    2 ++
 libseed/seed-engine.c    |   14 +++++++++++---
 libseed/seed-types.c     |    9 +++++++++
 3 files changed, 22 insertions(+), 3 deletions(-)
---
diff --git a/jsextensions/gjs/GLib.js b/jsextensions/gjs/GLib.js
index f4161a2..96193d7 100644
--- a/jsextensions/gjs/GLib.js
+++ b/jsextensions/gjs/GLib.js
@@ -250,6 +250,7 @@ function _init() {
 
     GLib = this;
 
+
     // small HACK: we add a matches() method to standard Errors so that
     // you can do "catch(e if e.matches(Ns.FooError, Ns.FooError.SOME_CODE))"
     // without checking instanceof
@@ -272,6 +273,7 @@ function _init() {
     this.Variant.prototype.unpack = function() {
        return _unpack_variant(this, false);
     };
+
     this.Variant.prototype.deep_unpack = function() {
        return _unpack_variant(this, true);
     };
diff --git a/libseed/seed-engine.c b/libseed/seed-engine.c
index a9a9b64..0ee1618 100644
--- a/libseed/seed-engine.c
+++ b/libseed/seed-engine.c
@@ -129,13 +129,21 @@ seed_struct_constructor_invoked(JSContextRef ctx,
     GIBaseInfo* info = JSObjectGetPrivate(constructor);
     JSValueRef ret;
     JSObjectRef parameters = 0;
-
+    GType gtype = g_registered_type_info_get_g_type((GIRegisteredTypeInfo*) info);
+    if (gtype == G_TYPE_VARIANT) {
+        JSObjectRef new_internal = (JSObjectRef) seed_object_get_property(ctx,
+                                                                          constructor,
+                                                                          "_new_internal");
+
+        if (JSObjectIsFunction(ctx, new_internal)) {
+            ret = JSObjectCallAsFunction(ctx, new_internal, NULL, argumentCount, arguments, exception);
+            return (JSObjectRef) ret;
+        }
+    }
     if (argumentCount == 1) {
         if (!JSValueIsObject(ctx, arguments[0])) {
 
             // new GObject.GValue()  can accept anything as a argument...
-            GType gtype
-              = g_registered_type_info_get_g_type((GIRegisteredTypeInfo*) info);
             if (!g_type_is_a(gtype, G_TYPE_VALUE)) {
                 seed_make_exception(ctx, exception, "ArgumentError",
                                     "Constructor expects object as argument");
diff --git a/libseed/seed-types.c b/libseed/seed-types.c
index 7cfc479..9f5449b 100644
--- a/libseed/seed-types.c
+++ b/libseed/seed-types.c
@@ -1540,6 +1540,15 @@ seed_value_to_gvalue(JSContextRef ctx,
                 g_base_info_unref(info);
             }
         }
+    } else if (g_type_is_a(type, G_TYPE_VARIANT)) {
+        gpointer p = seed_pointer_get_pointer(ctx, val);
+        if (p) {
+            g_value_init(ret, type);
+            g_value_set_variant(ret, p);
+            return TRUE;
+        } else {
+            g_critical("Couldn't not convert to GVariant. Probably something is not implemented");
+        }
     }
 
     switch (type) {


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