]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Implement silent-cast for GLib.Variant eb3b8f9830b69502e381deea0d59f4660c1d6738 55/head
authorRico Tzschichholz <ricotz@ubuntu.com>
Thu, 21 Mar 2019 10:38:28 +0000 (11:38 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Thu, 28 Mar 2019 21:28:15 +0000 (22:28 +0100)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/767

codegen/valagvariantmodule.vala
tests/Makefile.am
tests/basic-types/gvariants-unboxing-safe.vala [new file with mode: 0644]
tests/semantic/cast-gvariant-unsupported.test [new file with mode: 0644]
vala/valacastexpression.vala

index edd98b702b9a4609c52e414ec767a17ec9da1369..ddbab1b885e70108359cb441bbb577263426670d 100644 (file)
@@ -160,8 +160,57 @@ public class Vala.GVariantModule : GAsyncModule {
 
                push_function (cfunc);
 
+               CCodeExpression type_expr = null;
+               BasicTypeInfo basic_type = {};
+               bool is_basic_type = false;
+               if (expr.is_silent_cast) {
+                       var signature = target_type.get_type_signature ();
+                       is_basic_type = get_basic_type_info (signature, out basic_type);
+                       var ccheck = new CCodeFunctionCall (new CCodeIdentifier ("g_variant_is_of_type"));
+                       ccheck.add_argument (new CCodeIdentifier ("value"));
+                       if (is_basic_type) {
+                               type_expr = new CCodeIdentifier ("G_VARIANT_TYPE_" + basic_type.type_name.ascii_up ());
+                       } else {
+                               var gvariant_type_type = new ObjectType ((Class) root_symbol.scope.lookup ("GLib").scope.lookup ("VariantType"));
+                               var type_temp = get_temp_variable (gvariant_type_type, true, expr, true);
+                               emit_temp_var (type_temp);
+                               type_expr = new CCodeFunctionCall (new CCodeIdentifier ("g_variant_type_new"));
+                               ((CCodeFunctionCall) type_expr).add_argument (new CCodeIdentifier ("\"%s\"".printf (signature)));
+                               store_value (get_local_cvalue (type_temp), new GLibValue (gvariant_type_type, type_expr), expr.source_reference);
+                               type_expr = get_variable_cexpression (type_temp.name);
+                       }
+                       ccheck.add_argument (type_expr);
+                       ccode.open_if (new CCodeBinaryExpression (CCodeBinaryOperator.AND, new CCodeIdentifier ("value"), ccheck));
+               }
+
                CCodeExpression func_result = deserialize_expression (target_type, new CCodeIdentifier ("value"), new CCodeIdentifier ("*result"));
-               if (target_type.is_real_non_null_struct_type ()) {
+
+               if (expr.is_silent_cast) {
+                       if (is_basic_type && basic_type.is_string) {
+                               ccode.add_return (func_result);
+                       } else {
+                               if (!is_basic_type) {
+                                       var type_free = new CCodeFunctionCall (new CCodeIdentifier ("g_variant_type_free"));
+                                       type_free.add_argument (type_expr);
+                                       ccode.add_expression (type_free);
+                               }
+                               var temp_type = expr.target_type.copy ();
+                               if (!expr.target_type.is_real_struct_type ()) {
+                                       temp_type.nullable = false;
+                               }
+                               var temp_value = create_temp_value (temp_type, false, expr);
+                               store_value (temp_value, new GLibValue (temp_type, func_result), expr.source_reference);
+                               ccode.add_return (get_cvalue_ (transform_value (temp_value, expr.target_type, expr)));
+                       }
+                       ccode.add_else ();
+                       if (!is_basic_type) {
+                               var type_free = new CCodeFunctionCall (new CCodeIdentifier ("g_variant_type_free"));
+                               type_free.add_argument (type_expr);
+                               ccode.add_expression (type_free);
+                       }
+                       ccode.add_return (new CCodeConstant ("NULL"));
+                       ccode.close ();
+               } else if (target_type.is_real_non_null_struct_type ()) {
                        ccode.add_assignment (new CCodeIdentifier ("*result"), func_result);
                } else {
                        ccode.add_return (func_result);
index 0e1472ffbd8ccb2e8b07423e36baddc78c278454..ce19144f74805e81eb071fa4e24acff3a2f9ca2a 100644 (file)
@@ -34,6 +34,7 @@ TESTS = \
        basic-types/glists.vala \
        basic-types/gptrarray.vala \
        basic-types/gvariants.vala \
+       basic-types/gvariants-unboxing-safe.vala \
        basic-types/bug570846.test \
        basic-types/bug571486.vala \
        basic-types/bug591552.vala \
@@ -596,6 +597,7 @@ TESTS = \
        semantic/assignment-same-variable.vala \
        semantic/assignment-signal-incompatible-method.test \
        semantic/assignment-signal-incompatible-type.test \
+       semantic/cast-gvariant-unsupported.test \
        semantic/chainup-gobject-incompatible-type-property.test \
        semantic/chainup-gobject-unknown-property.test \
        semantic/chainup-gobject-unsupported-type-property.test \
diff --git a/tests/basic-types/gvariants-unboxing-safe.vala b/tests/basic-types/gvariants-unboxing-safe.vala
new file mode 100644 (file)
index 0000000..03cc73b
--- /dev/null
@@ -0,0 +1,74 @@
+struct Foo {
+       public string s;
+       public uint64 u64;
+       public bool b;
+}
+
+void main () {
+       Variant v;
+
+       v = new Variant.int32 (4711);
+       {
+               bool? b = v as bool;
+               assert (b == null);
+       }
+       {
+               int16? i16 = v as int16;
+               assert (i16 == null);
+       }
+       {
+               int32? i32 = v as int32;
+               assert (i32 == 4711);
+       }
+       {
+               string? s = v as string;
+               assert (s == null);
+       }
+
+       v = new Variant.boolean (true);
+       {
+               bool? b = v as bool;
+               assert (b == true);
+       }
+       {
+               int32? i32 = v as int32;
+               assert (i32 == null);
+       }
+
+       v = new Variant.strv ({ "foo", "bar", "manam" });
+       {
+               string[]? sa = v as string[];
+               assert (sa != null);
+               assert (sa[2] == "manam");
+       }
+
+       Foo vsrc = { "foo", uint64.MAX, true };
+       v = vsrc;
+       assert ("(stb)" == v.get_type_string ());
+       {
+               Foo real_st = (Foo) v;
+               assert (real_st.s == "foo");
+               assert (real_st.u64 == uint64.MAX);
+               assert (real_st.b == true);
+
+               Foo? st = v as Foo;
+               assert (st != null);
+               assert (st.s == "foo");
+               assert (st.u64 == uint64.MAX);
+               assert (st.b == true);
+       }
+
+       HashTable<string,string> vsrc2 = new HashTable<string,string> (str_hash, str_equal);
+       vsrc2.insert ("foo", "bar");
+       vsrc2.insert ("bar", "manam");
+       v = vsrc2;
+       {
+               HashTable<string,string> dict = v as HashTable<string,string>;
+               assert (dict.lookup ("foo") == "bar");
+               assert (dict.lookup ("bar") == "manam");
+       }
+       {
+               HashTable<int,string>? dict = v as HashTable<int,string>;
+               assert (dict == null);
+       }
+}
diff --git a/tests/semantic/cast-gvariant-unsupported.test b/tests/semantic/cast-gvariant-unsupported.test
new file mode 100644 (file)
index 0000000..2e4ed2d
--- /dev/null
@@ -0,0 +1,6 @@
+Invalid Code
+
+void main () {
+       Variant? v = null;
+       Object? array = v as Object;
+}
index 1dc357764646599b77e7a9086588f6587614f1af..63e3561557c2d268b3dadc18479df11da6e1cf9e 100644 (file)
@@ -184,6 +184,9 @@ public class Vala.CastExpression : Expression {
                    && is_gvariant (context, inner.value_type) && !is_gvariant (context, value_type)) {
                        // GVariant unboxing returns owned value
                        value_type.value_owned = true;
+                       if (value_type.get_type_signature () == null) {
+                               Report.error (source_reference, "Casting of `GLib.Variant' to `%s' is not supported".printf (value_type.to_qualified_string ()));
+                       }
                }
 
                inner.target_type = inner.value_type.copy ();