[gjs/ewlsh/fix-closures: 4/5] gi: Allow GObject.Value boxed type in all contexts




commit 2c81c47ac37a1196dcc035bdd3ff15d2693fb984
Author: Evan Welsh <contact evanwelsh com>
Date:   Thu Aug 26 21:28:26 2021 -0700

    gi: Allow GObject.Value boxed type in all contexts
    
    Previously we only handled this conversion in the arg cache.

 gi/value.cpp | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
---
diff --git a/gi/value.cpp b/gi/value.cpp
index af1f6702..cc85b654 100644
--- a/gi/value.cpp
+++ b/gi/value.cpp
@@ -355,6 +355,39 @@ gjs_value_to_g_value_internal(JSContext      *context,
                               GValue         *gvalue,
                               bool            no_copy)
 {
+    if (value.isObject()) {
+        JS::RootedObject obj(context, &value.toObject());
+        GType gtype;
+
+        if (!gjs_gtype_get_actual_gtype(context, obj, &gtype))
+            return false;
+
+        // Don't unbox GValue if the GValue's gtype is GObject.Value
+        if (gtype == G_TYPE_VALUE && G_VALUE_TYPE(gvalue) != G_TYPE_VALUE) {
+            GValue* source = BoxedBase::to_c_ptr<GValue>(context, obj);
+            // Only initialize the value if it doesn't have a type
+            // and our source GValue has been initialized
+            GType source_gtype = G_VALUE_TYPE(source);
+            if (G_VALUE_TYPE(gvalue) == 0) {
+                if (source_gtype == 0) {
+                    gjs_throw(context, "GObject.Value is not initialized with a type");
+                    return false;
+                }
+
+                g_value_init(gvalue, source_gtype);
+            }
+            
+            GType dest_gtype = G_VALUE_TYPE(gvalue);
+            if (!g_value_type_compatible(source_gtype, dest_gtype)) {
+                gjs_throw(context, "GObject.Value expected GType %s, found %s", g_type_name(dest_gtype), 
g_type_name(source_gtype));
+                return false;
+            }
+
+            g_value_copy(source, gvalue);
+            return true;
+        }
+    }
+
     GType gtype;
     bool out_of_range = false;
 


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