Working on async exception catching

This commit is contained in:
Alexey Andreev 2015-02-02 18:58:44 +04:00
parent 4496b1ab30
commit b36c10760c
7 changed files with 140 additions and 20 deletions

View File

@ -0,0 +1,25 @@
/*
* 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;
/**
*
* @author Alexey Andreev
*/
public enum AsyncInvocationType {
COMPLETE,
ERROR
}

View File

@ -203,9 +203,8 @@ public class Decompiler {
AsyncProgramSplitter splitter = new AsyncProgramSplitter(asyncMethods);
splitter.split(method.getProgram());
for (int i = 0; i < splitter.size(); ++i) {
AsyncMethodPart part = new AsyncMethodPart();
AsyncMethodPart part = getRegularMethodStatement(splitter.getProgram(i), splitter.getBlockSuccessors(i));
part.setInputVariable(splitter.getInput(i));
part.setStatement(getRegularMethodStatement(splitter.getProgram(i), splitter.getBlockSuccessors(i)));
node.getBody().add(part);
}
Program program = method.getProgram();
@ -226,7 +225,9 @@ public class Decompiler {
public RegularMethodNode decompileRegularCacheMiss(MethodHolder method) {
RegularMethodNode methodNode = new RegularMethodNode(method.getReference());
Program program = method.getProgram();
methodNode.setBody(getRegularMethodStatement(program, new int[program.basicBlockCount()]));
int[] targetBlocks = new int[program.basicBlockCount()];
Arrays.fill(targetBlocks, -1);
methodNode.setBody(getRegularMethodStatement(program, targetBlocks).getStatement());
for (int i = 0; i < program.variableCount(); ++i) {
methodNode.getVariables().add(program.variableAt(i).getRegister());
}
@ -241,7 +242,8 @@ public class Decompiler {
return methodNode;
}
private Statement getRegularMethodStatement(Program program, int[] targetBlocks) {
private AsyncMethodPart getRegularMethodStatement(Program program, int[] targetBlocks) {
AsyncMethodPart result = new AsyncMethodPart();
lastBlockId = 1;
graph = ProgramUtils.buildControlFlowGraph(program);
indexer = new GraphIndexer(graph);
@ -297,6 +299,7 @@ public class Decompiler {
InstructionLocation lastLocation = null;
NodeLocation nodeLocation = null;
List<Instruction> instructions = generator.currentBlock.getInstructions();
boolean asyncInvocation = false;
for (int j = 0; j < instructions.size(); ++j) {
Instruction insn = generator.currentBlock.getInstructions().get(j);
if (insn.getLocation() != null && lastLocation != insn.getLocation()) {
@ -308,26 +311,48 @@ public class Decompiler {
}
if (targetBlocks[node] >= 0 && j == instructions.size() - 1) {
generator.asyncTarget = targetBlocks[node];
asyncInvocation = true;
}
insn.acceptVisitor(generator);
}
boolean hasAsyncCatch = false;
for (TryCatchBlock tryCatch : generator.currentBlock.getTryCatchBlocks()) {
TryCatchStatement tryCatchStmt = new TryCatchStatement();
tryCatchStmt.setExceptionType(tryCatch.getExceptionType());
tryCatchStmt.setExceptionVariable(tryCatch.getExceptionVariable().getIndex());
tryCatchStmt.getProtectedBody().addAll(generator.statements);
generator.statements.clear();
generator.statements.add(tryCatchStmt);
Statement handlerStmt = generator.generateJumpStatement(tryCatch.getHandler());
if (handlerStmt != null) {
tryCatchStmt.getHandler().add(handlerStmt);
if (asyncInvocation) {
TryCatchStatement tryCatchStmt = new TryCatchStatement();
tryCatchStmt.setExceptionType(tryCatch.getExceptionType());
tryCatchStmt.setExceptionVariable(tryCatch.getExceptionVariable().getIndex());
tryCatchStmt.getProtectedBody().addAll(generator.statements);
generator.statements.clear();
generator.statements.add(tryCatchStmt);
Statement handlerStmt = generator.generateJumpStatement(tryCatch.getHandler());
if (handlerStmt != null) {
tryCatchStmt.getHandler().add(handlerStmt);
}
} else {
AsyncMethodCatch asyncCatch = new AsyncMethodCatch();
asyncCatch.setExceptionType(tryCatch.getExceptionType());
asyncCatch.setExceptionVariable(tryCatch.getExceptionVariable().getIndex());
Statement handlerStmt = generator.generateJumpStatement(tryCatch.getHandler());
if (handlerStmt != null) {
asyncCatch.getHandler().add(handlerStmt);
}
result.getCatches().add(asyncCatch);
hasAsyncCatch = true;
}
}
if (hasAsyncCatch) {
TryCatchStatement guardTryCatch = new TryCatchStatement();
guardTryCatch.setAsync(true);
guardTryCatch.getProtectedBody().addAll(generator.statements);
generator.statements.clear();
generator.statements.add(guardTryCatch);
}
block.body.addAll(generator.statements);
}
}
SequentialStatement result = new SequentialStatement();
result.getSequence().addAll(rootStmt.getBody());
SequentialStatement resultBody = new SequentialStatement();
resultBody.getSequence().addAll(rootStmt.getBody());
result.setStatement(resultBody);
return result;
}

View File

@ -939,11 +939,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
if (statement.getLocation() != null) {
pushLocation(statement.getLocation());
}
if (!async) {
writer.append("$rt_throw(");
} else {
writer.append("return $throw(");
}
writer.append("$rt_throw(");
prevCallSite = debugEmitter.emitCallSite();
statement.getException().acceptVisitor(this);
writer.append(");").softNewLine();

View File

@ -0,0 +1,49 @@
/*
* 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;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Alexey Andreev
*/
public class AsyncMethodCatch {
private List<Statement> handler = new ArrayList<>();
private String exceptionType;
private Integer exceptionVariable;
public List<Statement> getHandler() {
return handler;
}
public String getExceptionType() {
return exceptionType;
}
public void setExceptionType(String exceptionType) {
this.exceptionType = exceptionType;
}
public Integer getExceptionVariable() {
return exceptionVariable;
}
public void setExceptionVariable(Integer exceptionVariable) {
this.exceptionVariable = exceptionVariable;
}
}

View File

@ -15,6 +15,9 @@
*/
package org.teavm.javascript.ast;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Alexey Andreev
@ -22,6 +25,7 @@ package org.teavm.javascript.ast;
public class AsyncMethodPart {
private Statement statement;
private Integer inputVariable;
private List<AsyncMethodCatch> catches = new ArrayList<>();
public Statement getStatement() {
return statement;
@ -38,4 +42,8 @@ public class AsyncMethodPart {
public void setInputVariable(Integer inputVariable) {
this.inputVariable = inputVariable;
}
public List<AsyncMethodCatch> getCatches() {
return catches;
}
}

View File

@ -27,6 +27,7 @@ public class TryCatchStatement extends Statement {
private List<Statement> handler = new ArrayList<>();
private String exceptionType;
private Integer exceptionVariable;
private boolean async;
public List<Statement> getProtectedBody() {
return protectedBody;
@ -52,6 +53,14 @@ public class TryCatchStatement extends Statement {
this.exceptionVariable = exceptionVariable;
}
public boolean isAsync() {
return async;
}
public void setAsync(boolean async) {
this.async = async;
}
@Override
public void acceptVisitor(StatementVisitor visitor) {
visitor.visit(this);

View File

@ -69,6 +69,14 @@ public class AsyncProgramSplitter {
targetBlock.getInstructions().addAll(ProgramUtils.copyInstructions(sourceBlock,
last, i + 1, targetBlock.getProgram()));
ProgramUtils.copyTryCatches(sourceBlock, targetBlock.getProgram());
for (TryCatchBlock tryCatch : targetBlock.getTryCatchBlocks()) {
if (tryCatch.getHandler() != null) {
Step next = new Step();
next.source = tryCatch.getHandler().getIndex();
next.targetPart = step.targetPart;
queue.add(next);
}
}
last = i + 1;
// If this instruction already separates program, end with current block and refer to the