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

This commit is contained in:
Alexey Andreev 2017-02-10 23:43:04 +03:00
parent da3802d9c5
commit 816a17dfc0
2 changed files with 21 additions and 0 deletions

View File

@ -824,6 +824,8 @@ public class CompositeMethodGenerator {
insn.setReceiver(var(receiver));
insn.setAssignee(var(instance));
add(insn);
} else {
var(instance);
}
return;
} else {

View File

@ -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<Object> cls);
private static void withUnassignedLazy(ReflectClass<Object> cls) {
emit(() -> counter = 42);
Value<Object> value = lazy(() -> counter = 23);
emit(() -> {
value.get();
});
}
private static int counter = 0;
static class Context {
public int a;
public int b;