diff --git a/core/src/main/java/org/teavm/backend/c/CTarget.java b/core/src/main/java/org/teavm/backend/c/CTarget.java index 5bf0c2134..3716eeb72 100644 --- a/core/src/main/java/org/teavm/backend/c/CTarget.java +++ b/core/src/main/java/org/teavm/backend/c/CTarget.java @@ -152,6 +152,7 @@ public class CTarget implements TeaVMTarget, TeaVMCHost { private List generatorFactories = new ArrayList<>(); private Characteristics characteristics; private Set asyncMethods; + private boolean hasThreads; private MethodNodeCache astCache = EmptyMethodNodeCache.INSTANCE; private boolean incremental; private boolean lineNumbersGenerated; @@ -285,11 +286,11 @@ public class CTarget implements TeaVMTarget, TeaVMCHost { @Override public void analyzeBeforeOptimizations(ListableClassReaderSource classSource) { - AsyncMethodFinder asyncFinder = new AsyncMethodFinder(controller.getDependencyInfo().getCallGraph(), - controller.getDiagnostics()); + AsyncMethodFinder asyncFinder = new AsyncMethodFinder(controller.getDependencyInfo().getCallGraph()); asyncFinder.find(classSource); asyncMethods = new HashSet<>(asyncFinder.getAsyncMethods()); asyncMethods.addAll(asyncFinder.getAsyncFamilyMethods()); + hasThreads = asyncFinder.hasAsyncMethods(); } @Override @@ -302,7 +303,7 @@ public class CTarget implements TeaVMTarget, TeaVMCHost { classInitializerEliminator.apply(program); classInitializerTransformer.transform(program); nullCheckTransformation.apply(program, method.getResultType()); - new CoroutineTransformation(controller.getUnprocessedClassSource(), asyncMethods) + new CoroutineTransformation(controller.getUnprocessedClassSource(), asyncMethods, hasThreads) .apply(program, method.getReference()); ShadowStackTransformer shadowStackTransformer = !incremental ? this.shadowStackTransformer diff --git a/core/src/main/java/org/teavm/backend/javascript/JavaScriptTarget.java b/core/src/main/java/org/teavm/backend/javascript/JavaScriptTarget.java index 4f640dc01..0bf35c72c 100644 --- a/core/src/main/java/org/teavm/backend/javascript/JavaScriptTarget.java +++ b/core/src/main/java/org/teavm/backend/javascript/JavaScriptTarget.java @@ -470,8 +470,7 @@ public class JavaScriptTarget implements TeaVMTarget, TeaVMJavaScriptHost { } private List modelToAst(ListableClassHolderSource classes) { - AsyncMethodFinder asyncFinder = new AsyncMethodFinder(controller.getDependencyInfo().getCallGraph(), - controller.getDiagnostics()); + AsyncMethodFinder asyncFinder = new AsyncMethodFinder(controller.getDependencyInfo().getCallGraph()); asyncFinder.find(classes); asyncMethods.addAll(asyncFinder.getAsyncMethods()); asyncFamilyMethods.addAll(asyncFinder.getAsyncFamilyMethods()); diff --git a/core/src/main/java/org/teavm/backend/lowlevel/transform/CoroutineTransformation.java b/core/src/main/java/org/teavm/backend/lowlevel/transform/CoroutineTransformation.java index 796e4daa4..6688a373e 100644 --- a/core/src/main/java/org/teavm/backend/lowlevel/transform/CoroutineTransformation.java +++ b/core/src/main/java/org/teavm/backend/lowlevel/transform/CoroutineTransformation.java @@ -79,10 +79,13 @@ public class CoroutineTransformation { private SwitchInstruction resumeSwitch; private int parameterCount; private ValueType returnType; + private boolean hasThreads; - public CoroutineTransformation(ClassReaderSource classSource, Set asyncMethods) { + public CoroutineTransformation(ClassReaderSource classSource, Set asyncMethods, + boolean hasThreads) { this.classSource = classSource; this.asyncMethods = asyncMethods; + this.hasThreads = hasThreads; } public void apply(Program program, MethodReference methodReference) { @@ -233,7 +236,7 @@ public class CoroutineTransformation { } else if (instruction instanceof InitClassInstruction) { return isSplittingClassInitializer(((InitClassInstruction) instruction).getClassName()); } else { - return instruction instanceof MonitorEnterInstruction; + return hasThreads && instruction instanceof MonitorEnterInstruction; } } diff --git a/core/src/main/java/org/teavm/model/util/AsyncMethodFinder.java b/core/src/main/java/org/teavm/model/util/AsyncMethodFinder.java index 02cd66156..7299aba07 100644 --- a/core/src/main/java/org/teavm/model/util/AsyncMethodFinder.java +++ b/core/src/main/java/org/teavm/model/util/AsyncMethodFinder.java @@ -25,7 +25,6 @@ import java.util.Set; import org.teavm.callgraph.CallGraph; import org.teavm.callgraph.CallGraphNode; import org.teavm.callgraph.CallSite; -import org.teavm.diagnostics.Diagnostics; import org.teavm.interop.Async; import org.teavm.model.ClassReader; import org.teavm.model.ElementModifier; @@ -44,13 +43,11 @@ public class AsyncMethodFinder { private Set readonlyAsyncMethods = Collections.unmodifiableSet(asyncMethods); private Set readonlyAsyncFamilyMethods = Collections.unmodifiableSet(asyncFamilyMethods.keySet()); private CallGraph callGraph; - private Diagnostics diagnostics; private ListableClassReaderSource classSource; private boolean hasAsyncMethods; - public AsyncMethodFinder(CallGraph callGraph, Diagnostics diagnostics) { + public AsyncMethodFinder(CallGraph callGraph) { this.callGraph = callGraph; - this.diagnostics = diagnostics; } public Set getAsyncMethods() { @@ -63,7 +60,7 @@ public class AsyncMethodFinder { public void find(ListableClassReaderSource classSource) { this.classSource = classSource; - hasAsyncMethods = hasAsyncMethods(); + hasAsyncMethods = findAsyncMethods(); for (String clsName : classSource.getClassNames()) { ClassReader cls = classSource.get(clsName); for (MethodReader method : cls.getMethods()) { @@ -104,7 +101,7 @@ public class AsyncMethodFinder { } } - private boolean hasAsyncMethods() { + private boolean findAsyncMethods() { boolean result = false; loop: for (String clsName : classSource.getClassNames()) { ClassReader cls = classSource.get(clsName); @@ -123,6 +120,10 @@ public class AsyncMethodFinder { return result && method != null; } + public boolean hasAsyncMethods() { + return hasAsyncMethods; + } + private boolean hasMonitor(MethodReader method) { if (method.hasModifier(ElementModifier.SYNCHRONIZED)) { return true;