From 4ce1031c0cd33722b55b3dd58429a7fd3a12946b Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Sat, 14 Feb 2015 01:18:43 +0400 Subject: [PATCH] Trying to fix https://github.com/konsoletyper/teavm/issues/58 --- .../org/teavm/classlib/java/lang/TThread.java | 2 +- .../java/org/teavm/javascript/Decompiler.java | 10 ++++- .../teavm/model/util/AsyncMethodFinder.java | 38 +++++++++++-------- .../src/main/java/org/teavm/vm/TeaVM.java | 2 +- .../resources/org/teavm/javascript/runtime.js | 2 +- .../platform/plugin/PlatformGenerator.java | 4 +- 6 files changed, 35 insertions(+), 23 deletions(-) diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java index 5a66d1575..43db2182f 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java @@ -36,7 +36,7 @@ public class TThread extends TObject implements TRunnable { private long id; private TString name; - private TRunnable target; + TRunnable target; public TThread() { this(null, null); diff --git a/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java b/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java index ff34cdb26..9567a7060 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java +++ b/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java @@ -46,11 +46,14 @@ public class Decompiler { private Set methodsToPass = new HashSet<>(); private RegularMethodNodeCache regularMethodCache; private Set asyncMethods; + private Set asyncFamilyMethods; - public Decompiler(ClassHolderSource classSource, ClassLoader classLoader, Set asyncMethods) { + public Decompiler(ClassHolderSource classSource, ClassLoader classLoader, Set asyncMethods, + Set asyncFamilyMethods) { this.classSource = classSource; this.classLoader = classLoader; this.asyncMethods = asyncMethods; + this.asyncFamilyMethods = asyncFamilyMethods; } public RegularMethodNodeCache getRegularMethodCache() { @@ -196,7 +199,10 @@ public class Decompiler { public AsyncMethodNode decompileAsync(MethodHolder method) { AsyncMethodNode node = new AsyncMethodNode(method.getReference()); - AsyncProgramSplitter splitter = new AsyncProgramSplitter(asyncMethods); + Set splitMethods = new HashSet<>(); + splitMethods.addAll(asyncMethods); + splitMethods.addAll(asyncFamilyMethods); + AsyncProgramSplitter splitter = new AsyncProgramSplitter(splitMethods); splitter.split(method.getProgram()); for (int i = 0; i < splitter.size(); ++i) { Integer input = null; diff --git a/teavm-core/src/main/java/org/teavm/model/util/AsyncMethodFinder.java b/teavm-core/src/main/java/org/teavm/model/util/AsyncMethodFinder.java index d494d66bc..76718099f 100644 --- a/teavm-core/src/main/java/org/teavm/model/util/AsyncMethodFinder.java +++ b/teavm-core/src/main/java/org/teavm/model/util/AsyncMethodFinder.java @@ -76,6 +76,9 @@ public class AsyncMethodFinder { } } } + for (MethodReference method : asyncMethods) { + addOverridenToFamily(method); + } for (String clsName : classSource.getClassNames()) { ClassReader cls = classSource.get(clsName); for (MethodReader method : cls.getMethods()) { @@ -114,16 +117,16 @@ public class AsyncMethodFinder { for (CallSite callSite : node.getCallerCallSites()) { add(callSite.getCaller().getMethod()); } - Set visited = new HashSet<>(); - Set overriden = new HashSet<>(); - if (cls.getParent() != null && !cls.getParent().equals(cls.getName())) { - findOverridenMethods(new MethodReference(cls.getParent(), methodRef.getDescriptor()), overriden, visited); + } + + private void addOverridenToFamily(MethodReference methodRef) { + asyncFamilyMethods.put(methodRef, true); + ClassReader cls = classSource.get(methodRef.getClassName()); + if (cls == null) { + return; } - for (String iface : cls.getInterfaces()) { - findOverridenMethods(new MethodReference(iface, methodRef.getDescriptor()), overriden, visited); - } - for (MethodReference overridenMethod : overriden) { - add(overridenMethod); + for (MethodReference overridenMethod : findOverridenMethods(cls, methodRef)) { + addOverridenToFamily(overridenMethod); } } @@ -145,6 +148,15 @@ public class AsyncMethodFinder { if (cls == null) { return false; } + for (MethodReference overridenMethod : findOverridenMethods(cls, methodRef)) { + if (addToFamily(overridenMethod)) { + return true; + } + } + return false; + } + + private Set findOverridenMethods(ClassReader cls, MethodReference methodRef) { List parents = new ArrayList<>(); if (cls.getParent() != null && !cls.getParent().equals(cls.getName())) { parents.add(cls.getParent()); @@ -156,13 +168,7 @@ public class AsyncMethodFinder { for (String parent : parents) { findOverridenMethods(new MethodReference(parent, methodRef.getDescriptor()), overriden, visited); } - - for (MethodReference overridenMethod : overriden) { - if (addToFamily(overridenMethod)) { - return true; - } - } - return false; + return overriden; } private void findOverridenMethods(MethodReference methodRef, Set result, 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 fde6d1549..8b8258e59 100644 --- a/teavm-core/src/main/java/org/teavm/vm/TeaVM.java +++ b/teavm-core/src/main/java/org/teavm/vm/TeaVM.java @@ -562,7 +562,7 @@ public class TeaVM implements TeaVMHost, ServiceRepository { asyncFamilyMethods.addAll(asyncFinder.getAsyncFamilyMethods()); progressListener.phaseStarted(TeaVMPhase.DECOMPILATION, classes.getClassNames().size()); - Decompiler decompiler = new Decompiler(classes, classLoader, asyncFinder.getAsyncMethods()); + Decompiler decompiler = new Decompiler(classes, classLoader, asyncMethods, asyncFamilyMethods); decompiler.setRegularMethodCache(incremental ? astCache : null); for (Map.Entry entry : methodGenerators.entrySet()) { diff --git a/teavm-core/src/main/resources/org/teavm/javascript/runtime.js b/teavm-core/src/main/resources/org/teavm/javascript/runtime.js index 53cf7a43a..66c7691ee 100644 --- a/teavm-core/src/main/resources/org/teavm/javascript/runtime.js +++ b/teavm-core/src/main/resources/org/teavm/javascript/runtime.js @@ -472,7 +472,7 @@ function $rt_asyncAdapter(f) { return $return($rt_asyncResult(result)); } } -function $rt_rootInvocationAdapter(f, extraArgs) { +function $rt_rootInvocationAdapter(f) { return function() { var args = Array.prototype.slice.apply(arguments); args.push(function(result) { diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java index 808e304bb..fb5d9bfc9 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java @@ -132,8 +132,8 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin private void generateStartThread(GeneratorContext context, SourceWriter writer) throws IOException { String runnable = context.getParameterName(1); writer.append("window.setTimeout(function()").ws().append("{").indent().softNewLine(); - writer.appendMethodBody(Platform.class, "launchThread", Runnable.class, void.class).append("(") - .append(runnable).append(");").softNewLine(); + writer.append("$rt_rootInvocationAdapter(").appendMethodBody(Platform.class, "launchThread", Runnable.class, + void.class).append(")(").append(runnable).append(");").softNewLine(); writer.outdent().append("},").ws().append("0);").softNewLine(); } }