[vala/staging] codegen: Pass lambda expression of variable initializer to signal connect



commit 056daaae8131f1e3b1759c4d08ba890ce501097e
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Thu Jan 31 20:09:07 2019 +0100

    codegen: Pass lambda expression of variable initializer to signal connect

 codegen/valagsignalmodule.vala             | 11 ++++++---
 tests/Makefile.am                          |  1 +
 tests/objects/signals-lambda-delegate.vala | 39 ++++++++++++++++++++++++++++++
 3 files changed, 47 insertions(+), 4 deletions(-)
---
diff --git a/codegen/valagsignalmodule.vala b/codegen/valagsignalmodule.vala
index 3d0d19c2c..80f02f849 100644
--- a/codegen/valagsignalmodule.vala
+++ b/codegen/valagsignalmodule.vala
@@ -595,11 +595,14 @@ public class Vala.GSignalModule : GObjectModule {
                string connect_func;
 
                DelegateType? dt = null;
-               var p = handler.symbol_reference as Parameter;
-               if (p != null) {
-                       dt = p.variable_type as DelegateType;
+               if (handler.symbol_reference is Variable) {
+                       dt = ((Variable) handler.symbol_reference).variable_type as DelegateType;
                        if (dt != null && !context.experimental) {
-                               Report.warning (dt.source_reference, "Connecting delegates to signals is 
experimental");
+                               Report.warning (handler.source_reference, "Connecting delegates to signals is 
experimental");
+                       }
+                       // Use actual lambda expression if available for proper target/destroy handling
+                       if (((Variable) handler.symbol_reference).initializer is LambdaExpression) {
+                               handler = ((Variable) handler.symbol_reference).initializer;
                        }
                }
                var m = handler.symbol_reference as Method;
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 27f18f030..cfbe1f4a6 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -287,6 +287,7 @@ TESTS = \
        objects/signals.vala \
        objects/signals-delegate.vala \
        objects/signals-delegate-parameter.vala \
+       objects/signals-lambda-delegate.vala \
        objects/singleton.vala \
        objects/test-025.vala \
        objects/test-026.vala \
diff --git a/tests/objects/signals-lambda-delegate.vala b/tests/objects/signals-lambda-delegate.vala
new file mode 100644
index 000000000..5a8a34368
--- /dev/null
+++ b/tests/objects/signals-lambda-delegate.vala
@@ -0,0 +1,39 @@
+class Foo : Object {
+       public signal void bar (string s);
+       public signal void baz (string s);
+}
+
+delegate void FooFunc (Foo foo, string s);
+
+void main () {
+       var foo = new Foo ();
+       ulong bar_id = 0U;
+       ulong baz_id = 0U;
+       {
+               FooFunc callback = (f,s) => {
+                       assert (s == "bar" || s == "baz");
+
+                       if (s == "bar") {
+                               assert (bar_id > 0U);
+                               f.disconnect (bar_id);
+                               bar_id = 0U;
+                       }
+
+                       if (s == "baz") {
+                               assert (baz_id > 0U);
+                               f.disconnect (baz_id);
+                               baz_id = 0U;
+                       }
+               };
+
+               bar_id = foo.bar.connect (callback);
+               baz_id = foo.baz.connect (callback);
+       }
+       {
+               foo.bar ("bar");
+               assert (bar_id == 0U);
+
+               foo.baz ("baz");
+               assert (baz_id == 0U);
+       }
+}


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