Copy exception variables when splitting async programs. Fix https://github.com/konsoletyper/teavm/issues/230

This commit is contained in:
Alexey Andreev 2017-01-28 22:39:44 +03:00
parent 58563a3e2d
commit f6103ec00b
2 changed files with 25 additions and 0 deletions

View File

@ -95,6 +95,10 @@ public class AsyncProgramSplitter {
// If we met asynchronous invocation...
// Copy portion of current block from last occurrence (or from start) to i'th instruction.
if (sourceBlock.getExceptionVariable() != null) {
targetBlock.setExceptionVariable(targetBlock.getProgram().variableAt(
sourceBlock.getExceptionVariable().getIndex()));
}
targetBlock.addAll(ProgramUtils.copyInstructions(last, insn, targetBlock.getProgram()));
targetBlock.getTryCatchBlocks().addAll(ProgramUtils.copyTryCatches(sourceBlock,
targetBlock.getProgram()));
@ -140,6 +144,11 @@ public class AsyncProgramSplitter {
step.targetPart = part;
part.originalBlocks[targetBlock.getIndex()] = step.source;
}
if (sourceBlock.getExceptionVariable() != null) {
targetBlock.setExceptionVariable(targetBlock.getProgram().variableAt(
sourceBlock.getExceptionVariable().getIndex()));
}
targetBlock.addAll(ProgramUtils.copyInstructions(last, null, targetBlock.getProgram()));
targetBlock.getTryCatchBlocks().addAll(ProgramUtils.copyTryCatches(sourceBlock, targetBlock.getProgram()));
targetBlock.setExceptionVariable(sourceBlock.getExceptionVariable());

View File

@ -165,12 +165,28 @@ public class VMTest {
}
}
@Test
@SkipJVM
public void asyncExceptionHandler() {
try {
throw new RuntimeException("OK");
} catch (RuntimeException e) {
assertEquals("OK", suspendAndReturn(e).getMessage());
}
}
@Async
private static native void throwExceptionAsync();
private static void throwExceptionAsync(AsyncCallback<Void> callback) {
callback.error(new RuntimeException("OK"));
}
@Async
private static native <T> T suspendAndReturn(T value);
private static <T> void suspendAndReturn(T value, AsyncCallback<T> callback) {
callback.complete(value);
}
@JSBody(script = "return [1, 2]")
private static native int[] createArray();