From ed627459c89da937d14e43cfda79d028721163a9 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 23 Aug 2019 13:44:03 +0300 Subject: [PATCH] Fix issue with async methods in code server --- .../javascript/rendering/RuntimeRenderer.java | 13 +++++++++---- core/src/main/java/org/teavm/dependency/Linker.java | 5 ++--- .../org/teavm/model/util/AsyncMethodFinder.java | 2 +- core/src/main/java/org/teavm/vm/TeaVM.java | 5 ++--- .../main/java/org/teavm/devserver/CodeServlet.java | 2 +- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/RuntimeRenderer.java b/core/src/main/java/org/teavm/backend/javascript/rendering/RuntimeRenderer.java index 698ab36f2..d9cc67595 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/RuntimeRenderer.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/RuntimeRenderer.java @@ -28,6 +28,7 @@ import org.teavm.model.ClassReader; import org.teavm.model.ClassReaderSource; import org.teavm.model.FieldReference; import org.teavm.model.MethodDescriptor; +import org.teavm.model.MethodReader; import org.teavm.model.MethodReference; import org.teavm.model.ValueType; import org.teavm.vm.RenderingException; @@ -174,7 +175,8 @@ public class RuntimeRenderer { if (cls == null) { return false; } - return cls.getMethod(STRING_INTERN_METHOD) != null; + MethodReader method = cls.getMethod(STRING_INTERN_METHOD); + return method != null && method.getProgram() != null; } private void renderRuntimeObjcls() throws IOException { @@ -183,7 +185,8 @@ public class RuntimeRenderer { private void renderRuntimeThreads() throws IOException { ClassReader threadCls = classSource.get(THREAD_CLASS); - boolean threadUsed = threadCls != null && threadCls.getMethod(CURRENT_THREAD_METHOD) != null; + MethodReader currentThreadMethod = threadCls != null ? threadCls.getMethod(CURRENT_THREAD_METHOD) : null; + boolean threadUsed = currentThreadMethod != null && currentThreadMethod.getProgram() != null; writer.append("function $rt_getThread()").ws().append("{").indent().softNewLine(); if (threadUsed) { @@ -212,7 +215,8 @@ public class RuntimeRenderer { private void renderCreateStackTraceElement() throws IOException { ClassReader cls = classSource.get(STACK_TRACE_ELEM_INIT.getClassName()); - boolean supported = cls != null && cls.getMethod(STACK_TRACE_ELEM_INIT.getDescriptor()) != null; + MethodReader stackTraceElemInit = cls != null ? cls.getMethod(STACK_TRACE_ELEM_INIT.getDescriptor()) : null; + boolean supported = stackTraceElemInit != null && stackTraceElemInit.getProgram() != null; writer.append("function $rt_createStackElement(") .append("className,").ws() @@ -235,7 +239,8 @@ public class RuntimeRenderer { private void renderSetStackTrace() throws IOException { ClassReader cls = classSource.get(SET_STACK_TRACE_METHOD.getClassName()); - boolean supported = cls != null && cls.getMethod(SET_STACK_TRACE_METHOD.getDescriptor()) != null; + MethodReader setStackTrace = cls != null ? cls.getMethod(SET_STACK_TRACE_METHOD.getDescriptor()) : null; + boolean supported = setStackTrace != null && setStackTrace.getProgram() != null; writer.append("function $rt_setStack(e,").ws().append("stack)").ws().append("{").indent().softNewLine(); if (supported) { diff --git a/core/src/main/java/org/teavm/dependency/Linker.java b/core/src/main/java/org/teavm/dependency/Linker.java index 9aab7d06a..70a514087 100644 --- a/core/src/main/java/org/teavm/dependency/Linker.java +++ b/core/src/main/java/org/teavm/dependency/Linker.java @@ -46,10 +46,9 @@ public class Linker { for (MethodHolder method : cls.getMethods().toArray(new MethodHolder[0])) { MethodReference methodRef = method.getReference(); MethodDependencyInfo methodDep = dependency.getMethod(methodRef); - if (methodDep == null) { - cls.removeMethod(method); - } else if (!methodDep.isUsed()) { + if (methodDep == null || !methodDep.isUsed()) { method.getModifiers().add(ElementModifier.ABSTRACT); + method.getModifiers().remove(ElementModifier.NATIVE); method.setProgram(null); } else if (method.getProgram() != null) { link(method.getReference(), method.getProgram()); 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 7299aba07..5af6ca51c 100644 --- a/core/src/main/java/org/teavm/model/util/AsyncMethodFinder.java +++ b/core/src/main/java/org/teavm/model/util/AsyncMethodFinder.java @@ -271,7 +271,7 @@ public class AsyncMethodFinder { } } - class AsyncInstructionReader extends AbstractInstructionReader { + static class AsyncInstructionReader extends AbstractInstructionReader { boolean async; @Override diff --git a/core/src/main/java/org/teavm/vm/TeaVM.java b/core/src/main/java/org/teavm/vm/TeaVM.java index c15692533..90caa2fe3 100644 --- a/core/src/main/java/org/teavm/vm/TeaVM.java +++ b/core/src/main/java/org/teavm/vm/TeaVM.java @@ -962,10 +962,9 @@ public class TeaVM implements TeaVMHost, ServiceRepository { for (MethodHolder method : cls.getMethods().toArray(new MethodHolder[0])) { MethodDependencyInfo methodDep = dependencyAnalyzer.getMethod(method.getReference()); - if (methodDep == null) { - cls.removeMethod(method); - } else if (!methodDep.isUsed()) { + if (methodDep == null || !methodDep.isUsed()) { method.getModifiers().add(ElementModifier.ABSTRACT); + method.getModifiers().remove(ElementModifier.NATIVE); method.setProgram(null); } else { MethodReader methodReader = classReader.getMethod(method.getDescriptor()); diff --git a/tools/devserver/src/main/java/org/teavm/devserver/CodeServlet.java b/tools/devserver/src/main/java/org/teavm/devserver/CodeServlet.java index 248fb7855..7872e5631 100644 --- a/tools/devserver/src/main/java/org/teavm/devserver/CodeServlet.java +++ b/tools/devserver/src/main/java/org/teavm/devserver/CodeServlet.java @@ -151,7 +151,7 @@ public class CodeServlet extends HttpServlet { public CodeServlet(String mainClass, String[] classPath) { this.mainClass = mainClass; - this.classPath = classPath.clone(); + this.classPath = classSource != null ? classPath.clone() : new String[0]; httpClient = new HttpClient(); httpClient.setFollowRedirects(false);