From b19c5453182309de306a1e15f00018e5356e0e88 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Wed, 11 Mar 2015 19:49:31 +0400 Subject: [PATCH] Fix issue --- .../org/teavm/classlib/java/lang/TThread.java | 2 - .../src/main/java/org/teavm/cache/AstIO.java | 13 +------ .../cache/DiskRegularMethodNodeCache.java | 4 -- .../javascript/BreakToContinueReplacer.java | 4 -- .../javascript/CertainBlockCountVisitor.java | 4 -- .../java/org/teavm/javascript/Decompiler.java | 8 +--- .../javascript/NameFrequencyEstimator.java | 4 -- .../teavm/javascript/OptimizingVisitor.java | 5 --- .../javascript/RedundantLabelEliminator.java | 4 -- .../javascript/ReferenceCountingVisitor.java | 4 -- .../java/org/teavm/javascript/Renderer.java | 38 ++++++++++++------- .../teavm/javascript/StatementGenerator.java | 14 +++++-- .../org/teavm/javascript/TryCatchFinder.java | 4 -- .../javascript/UnusedVariableEliminator.java | 4 -- .../javascript/ast/AssignmentStatement.java | 9 +++++ .../teavm/javascript/ast/RenamingVisitor.java | 4 -- .../teavm/javascript/ast/SaveStatement.java | 27 ------------- .../javascript/ast/StatementVisitor.java | 2 - .../platform/plugin/PlatformGenerator.java | 14 ++++--- 19 files changed, 54 insertions(+), 114 deletions(-) delete mode 100644 teavm-core/src/main/java/org/teavm/javascript/ast/SaveStatement.java 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 52e53ba0d..e45e65fa8 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 @@ -15,7 +15,6 @@ */ package org.teavm.classlib.java.lang; -import org.teavm.dom.browser.TimerHandler; import org.teavm.dom.browser.Window; import org.teavm.javascript.spi.Async; import org.teavm.jso.JS; @@ -167,5 +166,4 @@ public class TThread extends TObject implements TRunnable { public final int getPriority(){ return this.priority; } - } diff --git a/teavm-core/src/main/java/org/teavm/cache/AstIO.java b/teavm-core/src/main/java/org/teavm/cache/AstIO.java index 78264c5ba..ddb706233 100644 --- a/teavm-core/src/main/java/org/teavm/cache/AstIO.java +++ b/teavm-core/src/main/java/org/teavm/cache/AstIO.java @@ -314,19 +314,10 @@ public class AstIO { } } - @Override - public void visit(SaveStatement statement) { - try { - output.writeByte(18); - } catch (IOException e) { - throw new IOExceptionWrapper(e); - } - } - @Override public void visit(MonitorEnterStatement statement) { try { - output.writeByte(19); + output.writeByte(18); writeExpr(statement.getObjectRef()); } catch (IOException e) { throw new IOExceptionWrapper(e); @@ -336,7 +327,7 @@ public class AstIO { @Override public void visit(MonitorExitStatement statement) { try { - output.writeByte(20); + output.writeByte(19); writeExpr(statement.getObjectRef()); } catch (IOException e) { throw new IOExceptionWrapper(e); diff --git a/teavm-core/src/main/java/org/teavm/cache/DiskRegularMethodNodeCache.java b/teavm-core/src/main/java/org/teavm/cache/DiskRegularMethodNodeCache.java index a08e16bfd..bb6d058e0 100644 --- a/teavm-core/src/main/java/org/teavm/cache/DiskRegularMethodNodeCache.java +++ b/teavm-core/src/main/java/org/teavm/cache/DiskRegularMethodNodeCache.java @@ -262,10 +262,6 @@ public class DiskRegularMethodNodeCache implements RegularMethodNodeCache { public void visit(GotoPartStatement statement) { } - @Override - public void visit(SaveStatement statement) { - } - @Override public void visit(MonitorEnterStatement statement) { diff --git a/teavm-core/src/main/java/org/teavm/javascript/BreakToContinueReplacer.java b/teavm-core/src/main/java/org/teavm/javascript/BreakToContinueReplacer.java index 6e17963fb..f80c3f981 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/BreakToContinueReplacer.java +++ b/teavm-core/src/main/java/org/teavm/javascript/BreakToContinueReplacer.java @@ -117,10 +117,6 @@ class BreakToContinueReplacer implements StatementVisitor { public void visit(GotoPartStatement statement) { } - @Override - public void visit(SaveStatement statement) { - } - @Override public void visit(MonitorEnterStatement statement) { } diff --git a/teavm-core/src/main/java/org/teavm/javascript/CertainBlockCountVisitor.java b/teavm-core/src/main/java/org/teavm/javascript/CertainBlockCountVisitor.java index 7892d98b4..11702da38 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/CertainBlockCountVisitor.java +++ b/teavm-core/src/main/java/org/teavm/javascript/CertainBlockCountVisitor.java @@ -112,10 +112,6 @@ class CertainBlockCountVisitor implements StatementVisitor { public void visit(GotoPartStatement statement) { } - @Override - public void visit(SaveStatement statement) { - } - @Override public void visit(MonitorEnterStatement statement) { } 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 0e133d9d0..6b4a24a19 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java +++ b/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java @@ -22,8 +22,6 @@ import org.teavm.javascript.spi.GeneratedBy; import org.teavm.javascript.spi.Generator; import org.teavm.javascript.spi.InjectedBy; import org.teavm.model.*; -import org.teavm.model.instructions.InvokeInstruction; -import org.teavm.model.instructions.MonitorEnterInstruction; import org.teavm.model.util.AsyncProgramSplitter; import org.teavm.model.util.ProgramUtils; @@ -264,7 +262,7 @@ public class Decompiler { generator.indexer = indexer; parentNode = codeTree.getRoot(); currentNode = parentNode.getFirstChild(); - boolean saved = !async; + generator.async = async; for (int i = 0; i < this.graph.size(); ++i) { Block block = stack.peek(); while (block.end == i) { @@ -311,10 +309,6 @@ public class Decompiler { generator.setCurrentLocation(nodeLocation); } insn.acceptVisitor(generator); - if (!saved && (insn instanceof InvokeInstruction || insn instanceof MonitorEnterInstruction)) { - generator.statements.add(new SaveStatement()); - saved = true; - } } if (targetBlocks[node] >= 0) { GotoPartStatement stmt = new GotoPartStatement(); diff --git a/teavm-core/src/main/java/org/teavm/javascript/NameFrequencyEstimator.java b/teavm-core/src/main/java/org/teavm/javascript/NameFrequencyEstimator.java index 20773208e..fdfade618 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/NameFrequencyEstimator.java +++ b/teavm-core/src/main/java/org/teavm/javascript/NameFrequencyEstimator.java @@ -191,10 +191,6 @@ public class NameFrequencyEstimator implements StatementVisitor, ExprVisitor, Me public void visit(GotoPartStatement statement) { } - @Override - public void visit(SaveStatement statement) { - } - @Override public void visit(MonitorEnterStatement statement) { if (async) { diff --git a/teavm-core/src/main/java/org/teavm/javascript/OptimizingVisitor.java b/teavm-core/src/main/java/org/teavm/javascript/OptimizingVisitor.java index eb9369618..f155919c4 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/OptimizingVisitor.java +++ b/teavm-core/src/main/java/org/teavm/javascript/OptimizingVisitor.java @@ -619,11 +619,6 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor { resultStmt = statement; } - @Override - public void visit(SaveStatement statement) { - resultStmt = statement; - } - @Override public void visit(MonitorEnterStatement statement) { statement.getObjectRef().acceptVisitor(this); diff --git a/teavm-core/src/main/java/org/teavm/javascript/RedundantLabelEliminator.java b/teavm-core/src/main/java/org/teavm/javascript/RedundantLabelEliminator.java index 25c8bd389..c0d240ebd 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/RedundantLabelEliminator.java +++ b/teavm-core/src/main/java/org/teavm/javascript/RedundantLabelEliminator.java @@ -122,10 +122,6 @@ class RedundantLabelEliminator implements StatementVisitor { public void visit(GotoPartStatement statement) { } - @Override - public void visit(SaveStatement statement) { - } - @Override public void visit(MonitorEnterStatement statement) { } diff --git a/teavm-core/src/main/java/org/teavm/javascript/ReferenceCountingVisitor.java b/teavm-core/src/main/java/org/teavm/javascript/ReferenceCountingVisitor.java index d9714e86b..031ca7d2a 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/ReferenceCountingVisitor.java +++ b/teavm-core/src/main/java/org/teavm/javascript/ReferenceCountingVisitor.java @@ -116,10 +116,6 @@ class ReferenceCountingVisitor implements StatementVisitor { public void visit(GotoPartStatement statement) { } - @Override - public void visit(SaveStatement statement) { - } - @Override public void visit(MonitorEnterStatement statement) { } 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 402649453..b2d1d8d48 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java +++ b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java @@ -686,6 +686,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext variableNames.add("$je"); } variableNames.add("$ptr"); + variableNames.add("$tmp"); if (!variableNames.isEmpty()) { writer.append("var "); for (int i = 0; i < variableNames.size(); ++i) { @@ -817,9 +818,13 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext } prevCallSite = debugEmitter.emitCallSite(); if (statement.getLeftValue() != null) { - priority = Priority.COMMA; - associativity = Associativity.NONE; - statement.getLeftValue().acceptVisitor(this); + if (statement.isAsync()) { + writer.append("$tmp"); + } else { + priority = Priority.COMMA; + associativity = Associativity.NONE; + statement.getLeftValue().acceptVisitor(this); + } writer.ws().append("=").ws(); } priority = Priority.COMMA; @@ -827,6 +832,15 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext statement.getRightValue().acceptVisitor(this); debugEmitter.emitCallSite(); writer.append(";").softNewLine(); + if (statement.isAsync()) { + emitSuspendChecker(); + if (statement.getLeftValue() != null) { + priority = Priority.COMMA; + associativity = Associativity.NONE; + statement.getLeftValue().acceptVisitor(this); + writer.ws().append("=").ws().append("$tmp;").softNewLine(); + } + } if (statement.getLocation() != null) { popLocation(); } @@ -1974,17 +1988,6 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext } } - @Override - public void visit(SaveStatement statement) { - try { - writer.append("if").ws().append("($rt_suspending())").ws().append("{").indent().softNewLine(); - writer.append("return $save();").softNewLine(); - writer.outdent().append("}").softNewLine(); - } catch (IOException ex){ - throw new RenderingException("IO error occured", ex); - } - } - @Override public void visit(MonitorEnterStatement statement) { try { @@ -1994,6 +1997,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext writer.appendMethodBody(monitorEnterRef).append("("); statement.getObjectRef().acceptVisitor(this); writer.append(");").softNewLine(); + emitSuspendChecker(); } else { MethodReference monitorEnterRef = new MethodReference( Object.class, "monitorEnterSync", Object.class, void.class); @@ -2006,6 +2010,12 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext } } + private void emitSuspendChecker() throws IOException { + writer.append("if").ws().append("($rt_suspending())").ws().append("{").indent().softNewLine(); + writer.append("return $save();").softNewLine(); + writer.outdent().append("}").softNewLine(); + } + @Override public void visit(MonitorExitStatement statement) { try { diff --git a/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java b/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java index 4b9665225..1e098a853 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java +++ b/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java @@ -37,6 +37,7 @@ class StatementGenerator implements InstructionVisitor { Program program; ClassHolderSource classSource; private NodeLocation currentLocation; + boolean async; public void setCurrentLocation(NodeLocation currentLocation) { this.currentLocation = currentLocation; @@ -549,13 +550,17 @@ class StatementGenerator implements InstructionVisitor { } else { invocationExpr = Expr.invokeStatic(insn.getMethod(), exprArgs); } + AssignmentStatement stmt; if (insn.getReceiver() != null) { - assign(invocationExpr, insn.getReceiver()); + stmt = Statement.assign(Expr.var(insn.getReceiver().getIndex()), invocationExpr); + stmt.getDebugNames().addAll(insn.getReceiver().getDebugNames()); } else { - AssignmentStatement stmt = Statement.assign(null, invocationExpr); - stmt.setLocation(currentLocation); - statements.add(stmt); + stmt = Statement.assign(null, invocationExpr); } + stmt.setLocation(currentLocation); + stmt.setAsync(async); + async = false; + statements.add(stmt); } @Override @@ -656,6 +661,7 @@ class StatementGenerator implements InstructionVisitor { MonitorEnterStatement stmt = new MonitorEnterStatement(); stmt.setLocation(currentLocation); stmt.setObjectRef(Expr.var(insn.getObjectRef().getIndex())); + async = false; statements.add(stmt); } diff --git a/teavm-core/src/main/java/org/teavm/javascript/TryCatchFinder.java b/teavm-core/src/main/java/org/teavm/javascript/TryCatchFinder.java index 3f65ad2b8..6bc07c4f0 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/TryCatchFinder.java +++ b/teavm-core/src/main/java/org/teavm/javascript/TryCatchFinder.java @@ -115,10 +115,6 @@ class TryCatchFinder implements StatementVisitor { public void visit(GotoPartStatement statement) { } - @Override - public void visit(SaveStatement statement) { - } - @Override public void visit(MonitorEnterStatement statement) { } diff --git a/teavm-core/src/main/java/org/teavm/javascript/UnusedVariableEliminator.java b/teavm-core/src/main/java/org/teavm/javascript/UnusedVariableEliminator.java index b828f31b5..82c00b82a 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/UnusedVariableEliminator.java +++ b/teavm-core/src/main/java/org/teavm/javascript/UnusedVariableEliminator.java @@ -229,10 +229,6 @@ class UnusedVariableEliminator implements ExprVisitor, StatementVisitor { public void visit(GotoPartStatement statement) { } - @Override - public void visit(SaveStatement statement) { - } - @Override public void visit(MonitorEnterStatement statement) { statement.getObjectRef().acceptVisitor(this); diff --git a/teavm-core/src/main/java/org/teavm/javascript/ast/AssignmentStatement.java b/teavm-core/src/main/java/org/teavm/javascript/ast/AssignmentStatement.java index 8388d4a6c..adbf79fa6 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/ast/AssignmentStatement.java +++ b/teavm-core/src/main/java/org/teavm/javascript/ast/AssignmentStatement.java @@ -27,6 +27,7 @@ public class AssignmentStatement extends Statement { private Expr rightValue; private NodeLocation location; private Set debugNames = new HashSet<>(); + private boolean async; public Expr getLeftValue() { return leftValue; @@ -56,6 +57,14 @@ public class AssignmentStatement extends Statement { return debugNames; } + public boolean isAsync() { + return async; + } + + public void setAsync(boolean async) { + this.async = async; + } + @Override public void acceptVisitor(StatementVisitor visitor) { visitor.visit(this); diff --git a/teavm-core/src/main/java/org/teavm/javascript/ast/RenamingVisitor.java b/teavm-core/src/main/java/org/teavm/javascript/ast/RenamingVisitor.java index db5e26719..e2b14d069 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/ast/RenamingVisitor.java +++ b/teavm-core/src/main/java/org/teavm/javascript/ast/RenamingVisitor.java @@ -196,10 +196,6 @@ public class RenamingVisitor implements StatementVisitor, ExprVisitor { public void visit(GotoPartStatement statement) { } - @Override - public void visit(SaveStatement statement) { - } - @Override public void visit(MonitorEnterStatement statement) { } diff --git a/teavm-core/src/main/java/org/teavm/javascript/ast/SaveStatement.java b/teavm-core/src/main/java/org/teavm/javascript/ast/SaveStatement.java deleted file mode 100644 index 8e8c955b1..000000000 --- a/teavm-core/src/main/java/org/teavm/javascript/ast/SaveStatement.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2015 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.javascript.ast; - -/** - * - * @author Alexey Andreev - */ -public class SaveStatement extends Statement { - @Override - public void acceptVisitor(StatementVisitor visitor) { - visitor.visit(this); - } -} diff --git a/teavm-core/src/main/java/org/teavm/javascript/ast/StatementVisitor.java b/teavm-core/src/main/java/org/teavm/javascript/ast/StatementVisitor.java index 7c4f6cf9b..7194cdb4e 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/ast/StatementVisitor.java +++ b/teavm-core/src/main/java/org/teavm/javascript/ast/StatementVisitor.java @@ -49,6 +49,4 @@ public interface StatementVisitor { void visit(MonitorEnterStatement statement); void visit(MonitorExitStatement statement); - - void visit(SaveStatement statement); } 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 92c0435d5..cef535e2b 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 @@ -69,7 +69,7 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { switch (methodRef.getName()) { case "newInstanceImpl": - generateNewInstance(writer); + generateNewInstance(context, writer); break; case "prepareNewInstance": generatePrepareNewInstance(context, writer); @@ -100,7 +100,7 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin MethodReader method = cls.getMethod(new MethodDescriptor("", void.class)); if (method != null) { writer.appendClass(clsName).append("[c]").ws().append("=").ws() - .appendMethodBody(method.getReference()).append(")").append(";").softNewLine(); + .appendMethodBody(method.getReference()).append(";").softNewLine(); } } writer.appendMethodBody(Platform.class, "newInstance", PlatformClass.class, Object.class).ws().append('=').ws() @@ -108,17 +108,19 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin .append(";").softNewLine(); } - private void generateNewInstance(SourceWriter writer) throws IOException { + private void generateNewInstance(GeneratorContext context, SourceWriter writer) throws IOException { writer.append("if").ws().append("($rt_resuming())").ws().append("{").indent().softNewLine(); writer.append("return $rt_nativeThread().pop();").softNewLine(); writer.outdent().append("}").softNewLine(); - writer.append("if").ws().append("(!cls.hasOwnProperty('$$constructor$$'))").ws().append("{") - .indent().softNewLine(); + String cls = context.getParameterName(1); + writer.append("if").ws().append("(!").append(cls).append(".hasOwnProperty('$$constructor$$'))") + .ws().append("{").indent().softNewLine(); writer.append("return null;").softNewLine(); writer.outdent().append("}").softNewLine(); - writer.append("var $r").ws().append('=').ws().append("cls.$$constructor$$();"); + writer.append("var $r").ws().append('=').ws().append("new ").append(cls).append("();").softNewLine(); + writer.append(cls).append(".$$constructor$$($r);").softNewLine(); writer.append("if").ws().append("($rt_suspending())").ws().append("{").indent().softNewLine(); writer.append("return $rt_nativeThread().push($r);").softNewLine(); writer.outdent().append("}").softNewLine();