From 7fc035fd8ababac55c034aec8e5f45fd5ea2fbe5 Mon Sep 17 00:00:00 2001 From: davmac314 Date: Thu, 9 Feb 2017 13:29:50 +0000 Subject: [PATCH] Fix asynchronous instantiation via reflection. (#238) Fix asynchronous instantiation via reflection. The generated code resumes from a thread suspension event, but won't appropriately re-suspend if the constructor suspends. --- .../platform/plugin/PlatformGenerator.java | 3 ++ .../teavm/classlib/java/lang/ClassTest.java | 8 ++++ .../classlib/java/lang/TestObjectAsync.java | 42 +++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 tests/src/test/java/org/teavm/classlib/java/lang/TestObjectAsync.java diff --git a/platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java b/platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java index 1ac230379..bd9172362 100644 --- a/platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java +++ b/platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java @@ -128,6 +128,9 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin writer.append("if").ws().append("($rt_resuming())").ws().append("{").indent().softNewLine(); writer.append("var $r = $rt_nativeThread().pop();").softNewLine(); writer.append(cls + ".$$constructor$$($r);").softNewLine(); + writer.append("if").ws().append("($rt_suspending())").ws().append("{").indent().softNewLine(); + writer.append("return").ws().append("$rt_nativeThread().push($r);").softNewLine(); + writer.outdent().append("}").softNewLine(); writer.append("return $r;").softNewLine(); writer.outdent().append("}").softNewLine(); diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/ClassTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/ClassTest.java index 3cc1d2223..12b090f8e 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/ClassTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/ClassTest.java @@ -117,6 +117,14 @@ public class ClassTest { assertEquals(1, ((TestObject)instance).getCounter()); } + @Test + public void instanceCreatedThroughReflectionAsync() throws Exception { + Runnable instance = TestObjectAsync.class.newInstance(); + instance.run(); + assertEquals(TestObjectAsync.class, instance.getClass()); + assertEquals(2, ((TestObjectAsync)instance).getCounter()); + } + @Test public void declaringClassFound() { assertEquals(ClassTest.class, new A().getClass().getDeclaringClass()); diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/TestObjectAsync.java b/tests/src/test/java/org/teavm/classlib/java/lang/TestObjectAsync.java new file mode 100644 index 000000000..84fe8c9b4 --- /dev/null +++ b/tests/src/test/java/org/teavm/classlib/java/lang/TestObjectAsync.java @@ -0,0 +1,42 @@ +/* + * Copyright 2017 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.lang; + +public class TestObjectAsync extends Object implements Runnable { + private int counter; + + public static Runnable r; + + public TestObjectAsync() { + try { + Thread.sleep(1); + Thread.sleep(1); + counter = 1; + } + catch (InterruptedException ie) { + throw new RuntimeException(ie); + } + } + + @Override + public void run() { + ++counter; + } + + public int getCounter() { + return counter; + } +}