Fix issue

This commit is contained in:
Alexey Andreev 2015-03-11 19:49:31 +04:00
parent 7e23498c5b
commit b19c545318
19 changed files with 54 additions and 114 deletions

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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) {

View File

@ -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) {
}

View File

@ -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) {
}

View File

@ -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();

View File

@ -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) {

View File

@ -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);

View File

@ -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) {
}

View File

@ -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) {
}

View File

@ -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 {

View File

@ -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);
}

View File

@ -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) {
}

View File

@ -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);

View File

@ -27,6 +27,7 @@ public class AssignmentStatement extends Statement {
private Expr rightValue;
private NodeLocation location;
private Set<String> 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);

View File

@ -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) {
}

View File

@ -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);
}
}

View File

@ -49,6 +49,4 @@ public interface StatementVisitor {
void visit(MonitorEnterStatement statement);
void visit(MonitorExitStatement statement);
void visit(SaveStatement statement);
}

View File

@ -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("<init>", 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();