]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Handle non-default AsyncResult parameter position
authorRico Tzschichholz <ricotz@ubuntu.com>
Mon, 10 Dec 2018 15:07:46 +0000 (16:07 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 11 Dec 2018 13:18:35 +0000 (14:18 +0100)
Unfortunately gdbus-codegen puts the AsyncResult parameter after possible
out-parameters therefore there is an "async_result_pos" attribute required
to handle this correctly.

Vala supposely follows the common practice to put the AsyncResult before
out-parameters by default.

Fixes https://gitlab.gnome.org/GNOME/vala/issues/709

codegen/valaccode.vala
codegen/valaccodebasemodule.vala
codegen/valaccodemethodcallmodule.vala
codegen/valagasyncmodule.vala
codegen/valagdbusclientmodule.vala
tests/Makefile.am
tests/asynchronous/result-pos.vala [new file with mode: 0644]
tests/gir/async-result-pos.test [new file with mode: 0644]
vala/valagirparser.vala
vala/valamethod.vala
vala/valausedattr.vala

index d4d9abee5d726e428f31671b5e203eb667ae7c76..2954b3e37cebb6ff0a2afcb07694373c8ebc1bf2 100644 (file)
@@ -380,6 +380,11 @@ namespace Vala {
                return get_ccode_attribute(m).vfunc_name;
        }
 
+       public static double get_ccode_async_result_pos (Method m) {
+               assert (m.coroutine);
+               return m.get_attribute_double ("CCode", "async_result_pos", 0.1);
+       }
+
        public static string get_ccode_finish_name (Method m) {
                return get_ccode_attribute(m).finish_name;
        }
index 9012567c4d00f459789245d871ac84b45a5002e3..6662e1c45ce889056345c75748014838f7ac49e8 100644 (file)
@@ -4835,7 +4835,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                // output arguments used separately
                                out_arg_map = new HashMap<int,CCodeExpression> (direct_hash, direct_equal);
                                // pass GAsyncResult stored in closure to finish function
-                               out_arg_map.set (get_param_pos (0.1), new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data_"), "_res_"));
+                               out_arg_map.set (get_param_pos (get_ccode_async_result_pos (m)), new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data_"), "_res_"));
                        }
 
                        if (cl != null && !cl.is_compact) {
index 18505c247a8d005122aa503601b8835ac9a97a15..90da65c881755fad25865591021f59df49d609a1 100644 (file)
@@ -129,7 +129,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
                                // output arguments used separately
                                out_arg_map = new HashMap<int,CCodeExpression> (direct_hash, direct_equal);
                                // pass GAsyncResult stored in closure to finish function
-                               out_arg_map.set (get_param_pos (0.1), new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data_"), "_res_"));
+                               out_arg_map.set (get_param_pos (get_ccode_async_result_pos (m)), new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data_"), "_res_"));
                        }
                }
 
index a3022532ac918499c58afce802e6359eadad6df5..29c38fdddfe8637fb8e1aaa34c6ea32321e195ea 100644 (file)
@@ -586,7 +586,7 @@ public class Vala.GAsyncModule : GtkModule {
 
                var cparam_map = new HashMap<int,CCodeParameter> (direct_hash, direct_equal);
 
-               cparam_map.set (get_param_pos (0.1), new CCodeParameter ("_res_", "GAsyncResult*"));
+               cparam_map.set (get_param_pos (get_ccode_async_result_pos (m)), new CCodeParameter ("_res_", "GAsyncResult*"));
 
                generate_cparameters (m, cfile, cparam_map, finishfunc, null, null, null, 2);
 
@@ -832,9 +832,9 @@ public class Vala.GAsyncModule : GtkModule {
                                        carg_map.set (get_param_pos (-0.9), new CCodeIdentifier ("_user_data_"));
                                }
                        } else if (direction == 2) {
-                               cparam_map.set (get_param_pos (0.1), new CCodeParameter ("_res_", "GAsyncResult*"));
+                               cparam_map.set (get_param_pos (get_ccode_async_result_pos (m)), new CCodeParameter ("_res_", "GAsyncResult*"));
                                if (carg_map != null) {
-                                       carg_map.set (get_param_pos (0.1), new CCodeIdentifier ("_res_"));
+                                       carg_map.set (get_param_pos (get_ccode_async_result_pos (m)), new CCodeIdentifier ("_res_"));
                                }
                        }
                }
index 57051b2c482adf613e04e0863d098c1376aa59e8..398c17f450385d93f3ba99a7301494754f0d62f9 100644 (file)
@@ -936,7 +936,7 @@ public class Vala.GDBusClientModule : GDBusModule {
 
                var cparam_map = new HashMap<int,CCodeParameter> (direct_hash, direct_equal);
 
-               cparam_map.set (get_param_pos (0.1), new CCodeParameter ("_res_", "GAsyncResult*"));
+               cparam_map.set (get_param_pos (get_ccode_async_result_pos (m)), new CCodeParameter ("_res_", "GAsyncResult*"));
 
                generate_cparameters (m, cfile, cparam_map, function, null, null, null, 2);
 
index a4b93c1c56c5cbec2d0e2975a2f8b16cc725e020..380703da2da6498aecb34e1e516e59ff78951427 100644 (file)
@@ -403,6 +403,7 @@ TESTS = \
        asynchronous/bug793158.vala \
        asynchronous/closures.vala \
        asynchronous/generator.vala \
+       asynchronous/result-pos.vala \
        asynchronous/yield.vala \
        generics/bug640330.test \
        generics/bug640330.vala \
@@ -434,6 +435,7 @@ TESTS = \
        gir/bug788775.test \
        gir/bug792998.test \
        gir/array-fixed-length.test \
+       gir/async-result-pos.test \
        gir/class.test \
        gir/delegate-alias-without-target.test \
        gir/delegate-closure-destroy-index-conflict.test \
diff --git a/tests/asynchronous/result-pos.vala b/tests/asynchronous/result-pos.vala
new file mode 100644 (file)
index 0000000..3c86f75
--- /dev/null
@@ -0,0 +1,48 @@
+[CCode (async_result_pos = 2.1)]
+async void foo (int in_i, out int out_i) {
+       out_i = in_i;
+}
+
+[CCode (async_result_pos = 2.1)]
+async void bar (int in_i, out int out_i) throws Error {
+       out_i = in_i;
+}
+
+async void run () {
+       int i;
+       yield foo (323, out i);
+       assert (i == 323);
+       try {
+               yield bar (742, out i);
+               assert (i == 742);
+       } catch {
+               assert_not_reached ();
+       }
+       loop.quit ();
+}
+
+MainLoop loop;
+
+void main () {
+       loop = new MainLoop ();
+
+       foo.begin (23, (o,r) => {
+               int i;
+               foo.end (r, out i);
+               assert (i == 23);
+       });
+
+       bar.begin (42, (o,r) => {
+               try {
+                       int i;
+                       bar.end (r, out i);
+                       assert (i == 42);
+               } catch {
+                       assert_not_reached ();
+               }
+       });
+
+       run.begin ();
+
+       loop.run ();
+}
diff --git a/tests/gir/async-result-pos.test b/tests/gir/async-result-pos.test
new file mode 100644 (file)
index 0000000..2a17f53
--- /dev/null
@@ -0,0 +1,56 @@
+GIR
+
+Input:
+
+<class name="Foo" c:type="TestFoo" glib:type-name="TestFoo" glib:get-type="test_foo_get_type" glib:type-struct="FooClass" parent="GObject.Object">
+  <method name="method_async" c:identifier="test_foo_method_async">
+    <return-value transfer-ownership="none">
+      <type name="none"/>
+    </return-value>
+    <parameters>
+      <instance-parameter name="self" transfer-ownership="none">
+        <type name="Foo" c:type="TestFoo*"/>
+      </instance-parameter>
+      <parameter name="input" transfer-ownership="none">
+        <type name="utf8" c:type="const gchar*"/>
+      </parameter>
+      <parameter name="_callback_" transfer-ownership="none" allow-none="1" closure="2" scope="async">
+        <type name="Gio.AsyncReadyCallback" c:type="GAsyncReadyCallback"/>
+      </parameter>
+      <parameter name="_callback__target" transfer-ownership="none" allow-none="1">
+        <type name="gpointer" c:type="void*"/>
+      </parameter>
+    </parameters>
+  </method>
+  <method name="method_finish" c:identifier="test_foo_method_finish" throws="1">
+    <return-value transfer-ownership="full">
+      <type name="none"/>
+    </return-value>
+    <parameters>
+      <instance-parameter name="self" transfer-ownership="none">
+        <type name="Foo" c:type="TestFoo*"/>
+      </instance-parameter>
+      <parameter name="output" direction="out" transfer-ownership="full">
+        <type name="utf8" c:type="gchar**"/>
+      </parameter>
+      <parameter name="_res_" transfer-ownership="none">
+        <type name="Gio.AsyncResult" c:type="GAsyncResult*"/>
+      </parameter>
+    </parameters>
+  </method>
+  <constructor name="new" c:identifier="test_foo_new">
+    <return-value transfer-ownership="full">
+      <type name="Test.Foo" c:type="TestFoo*"/>
+    </return-value>
+  </constructor>
+</class>
+
+Output:
+
+[CCode (cheader_filename = "test.h", type_id = "test_foo_get_type ()")]
+public class Foo : GLib.Object {
+       [CCode (has_construct_function = false)]
+       public Foo ();
+       [CCode (async_result_pos = 2.1)]
+       public async void method_async (string input, out string output) throws GLib.Error;
+}
index 4a22841f0b42a5fdb2c439cb1910104616f61145..061eb17c5518bfa00208ad372f7e20b06b979fa2 100644 (file)
@@ -3854,6 +3854,7 @@ public class Vala.GirParser : CodeVisitor {
 
                int i = 0, j=1;
 
+               int first_out = -1;
                int last = -1;
                foreach (ParameterInfo info in parameters) {
                        if (s is Delegate && info.closure_idx == i) {
@@ -3885,6 +3886,16 @@ public class Vala.GirParser : CodeVisitor {
                                // hidden parameters at the end of the parameter list
                                info.vala_idx = (j - 1) + (i - last) * 0.1F;
                        }
+                       if (first_out < 0 && info.param.direction == ParameterDirection.OUT) {
+                               first_out = i;
+                       }
+                       if (s is Method && first_out >= 0 && info.param.variable_type != null) {
+                               var type_name = info.param.variable_type.to_string ();
+                               if (type_name == "GLib.AsyncResult" || type_name == "Gio.AsyncResult") {
+                                       var shift = ((Method) s).binding == MemberBinding.INSTANCE ? 1.1 : 0.1;
+                                       s.set_attribute_double ("CCode", "async_result_pos", i + shift);
+                               }
+                       }
                        i++;
                }
 
@@ -4124,6 +4135,8 @@ public class Vala.GirParser : CodeVisitor {
                                }
                        }
 
+                       method.copy_attribute_double (finish_method, "CCode", "async_result_pos");
+
                        foreach (var param in finish_method.get_parameters ()) {
                                if (param.direction == ParameterDirection.OUT) {
                                        var async_param = param.copy ();
index 7ec91729da96ca2d0ee2b47667684fa25494d940..18f043f07386439078346e33932b8b52d85f8d05 100644 (file)
@@ -1061,6 +1061,7 @@ public class Vala.Method : Subroutine, Callable {
                        foreach (var param in get_type_parameters ()) {
                                end_method.add_type_parameter (param);
                        }
+                       end_method.copy_attribute_double (this, "CCode", "async_result_pos");
                }
                return end_method;
        }
@@ -1125,7 +1126,7 @@ public class Vala.Method : Subroutine, Callable {
                var result_type = new ObjectType ((ObjectTypeSymbol) glib_ns.scope.lookup ("AsyncResult"));
 
                var result_param = new Parameter ("_res_", result_type);
-               result_param.set_attribute_double ("CCode", "pos", 0.1);
+               result_param.set_attribute_double ("CCode", "pos", get_attribute_double ("CCode", "async_result_pos", 0.1));
                params.add (result_param);
 
                foreach (var param in parameters) {
index 28047daf38ec8f5c87e7b517138741156a17d054..97e9e9da9727162b1224cb98b6046765026b5c2d 100644 (file)
@@ -40,7 +40,7 @@ public class Vala.UsedAttr : CodeVisitor {
                "array_length_type", "array_length", "array_length_cname", "array_length_cexpr", "array_null_terminated",
                "vfunc_name", "finish_vfunc_name", "finish_name", "free_function_address_of", "pos", "delegate_target", "delegate_target_cname",
                "array_length_pos", "delegate_target_pos", "destroy_notify_pos", "ctype", "has_new_function", "notify", "finish_instance",
-               "use_inplace", "feature_test_macro", "default_value_on_error", "",
+               "use_inplace", "feature_test_macro", "default_value_on_error", "async_result_pos", "",
 
                "Immutable", "",
                "SingleInstance", "",