From 816a17dfc097526e236271ed47a51f3d198fd15e Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 10 Feb 2017 23:43:04 +0300 Subject: [PATCH] When Value.get() called in metaprogramming lambda and not assigned anywhere, capture instance anyway so that if it's a lazy value, it will be computed --- .../impl/CompositeMethodGenerator.java | 2 ++ .../test/MetaprogrammingTest.java | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/CompositeMethodGenerator.java b/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/CompositeMethodGenerator.java index c1e91e688..054c7d0c5 100644 --- a/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/CompositeMethodGenerator.java +++ b/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/CompositeMethodGenerator.java @@ -824,6 +824,8 @@ public class CompositeMethodGenerator { insn.setReceiver(var(receiver)); insn.setAssignee(var(instance)); add(insn); + } else { + var(instance); } return; } else { diff --git a/tests/src/test/java/org/teavm/metaprogramming/test/MetaprogrammingTest.java b/tests/src/test/java/org/teavm/metaprogramming/test/MetaprogrammingTest.java index 337b35621..ca80fccd7 100644 --- a/tests/src/test/java/org/teavm/metaprogramming/test/MetaprogrammingTest.java +++ b/tests/src/test/java/org/teavm/metaprogramming/test/MetaprogrammingTest.java @@ -36,6 +36,7 @@ import org.teavm.metaprogramming.Value; import org.teavm.metaprogramming.reflect.ReflectField; import org.teavm.metaprogramming.reflect.ReflectMethod; import org.teavm.metaprogramming.test.subpackage.MetaprogrammingGenerator; +import org.teavm.model.ValueType; @CompileTime @RunWith(TeaVMTestRunner.class) @@ -395,6 +396,24 @@ public class MetaprogrammingTest { emit(() -> type); } + @Test + public void unassignedLazyEvaluated() { + withUnassignedLazy(Object.class); + assertEquals(23, counter); + } + + @Meta + private static native void withUnassignedLazy(Class cls); + private static void withUnassignedLazy(ReflectClass cls) { + emit(() -> counter = 42); + Value value = lazy(() -> counter = 23); + emit(() -> { + value.get(); + }); + } + + private static int counter = 0; + static class Context { public int a; public int b;