From ce2c625f53e331414ce3e4b634f8be3cecd1bd63 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 6 Feb 2015 19:02:14 +0400 Subject: [PATCH] Add test to check async virtual call resolution. Fix bug in async unit tests support --- .../teavm/classlib/java/lang/ThreadTest.java | 30 ++++++++++++++++++- .../java/org/teavm/javascript/Renderer.java | 4 ++- .../src/main/java/org/teavm/vm/TeaVM.java | 2 +- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/ThreadTest.java b/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/ThreadTest.java index 22f7230bd..72e4b697c 100644 --- a/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/ThreadTest.java +++ b/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/ThreadTest.java @@ -16,6 +16,8 @@ package org.teavm.classlib.java.lang; import static org.junit.Assert.*; +import java.util.ArrayList; +import java.util.List; import org.junit.Test; /** @@ -28,7 +30,7 @@ public class ThreadTest { long start = System.currentTimeMillis(); Thread.sleep(100); long duration = System.currentTimeMillis() - start; - assertTrue("Thread.sleed did not wait enogh", duration < 100); + assertTrue("Thread.sleed did not wait enogh", duration >= 100); } @Test @@ -41,8 +43,34 @@ public class ThreadTest { } } + private void throwException() { Thread.yield(); throw new IllegalStateException(); } + + @Test + public void asyncVirtualCallsSupported() { + List alist = new ArrayList<>(); + alist.add(new A() { + @Override int foo() { + return 3; + } + }); + alist.add(new A() { + @Override int foo() { + Thread.yield(); + return 5; + } + }); + int sum = 0; + for (A a : alist) { + sum += a.foo(); + } + assertEquals(8, sum); + } + + abstract class A { + abstract int foo(); + } } diff --git a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java index 490aa00b2..d71d3455b 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java +++ b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java @@ -923,7 +923,9 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext writer.append(" $return($rt_asyncResult("); } if (statement.getResult() != null) { - writer.append(' '); + if (!async) { + writer.append(' '); + } prevCallSite = debugEmitter.emitCallSite(); statement.getResult().acceptVisitor(this); debugEmitter.emitCallSite(); diff --git a/teavm-core/src/main/java/org/teavm/vm/TeaVM.java b/teavm-core/src/main/java/org/teavm/vm/TeaVM.java index 6f974e1e1..d7ae3665e 100644 --- a/teavm-core/src/main/java/org/teavm/vm/TeaVM.java +++ b/teavm-core/src/main/java/org/teavm/vm/TeaVM.java @@ -537,7 +537,7 @@ public class TeaVM implements TeaVMHost, ServiceRepository { private List modelToAst(ListableClassHolderSource classes) { AsyncMethodFinder asyncFinder = new AsyncMethodFinder(dependencyChecker.getCallGraph(), diagnostics); asyncFinder.find(classes); - asyncMethods.addAll(asyncMethods); + asyncMethods.addAll(asyncFinder.getAsyncMethods()); progressListener.phaseStarted(TeaVMPhase.DECOMPILATION, classes.getClassNames().size()); Decompiler decompiler = new Decompiler(classes, classLoader, asyncFinder.getAsyncMethods());