From de7dc645bcd71df809d5979970e5685e2016b86f Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Fri, 6 Feb 2015 01:56:39 +0400 Subject: [PATCH] Add wielding of try/catch statements --- .../teavm/javascript/OptimizingVisitor.java | 36 +++++++++++++++++++ .../java/org/teavm/javascript/Renderer.java | 1 - .../org/teavm/samples/async/AsyncProgram.java | 13 +++++++ 3 files changed, 49 insertions(+), 1 deletion(-) 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 2d57ee859..03eb93654 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/OptimizingVisitor.java +++ b/teavm-core/src/main/java/org/teavm/javascript/OptimizingVisitor.java @@ -321,9 +321,45 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor { return false; } } + wieldTryCatch(statements); return true; } + private void wieldTryCatch(List statements) { + for (int i = 0; i < statements.size() - 1; ++i) { + if (statements.get(i) instanceof TryCatchStatement && statements.get(i + 1) instanceof TryCatchStatement) { + TryCatchStatement first = (TryCatchStatement)statements.get(i); + TryCatchStatement second = (TryCatchStatement)statements.get(i + 1); + if (Objects.equals(first.getExceptionType(), second.getExceptionType()) && + Objects.equals(first.getExceptionVariable(), second.getExceptionVariable()) && + briefStatementComparison(first.getHandler(), second.getHandler())) { + first.getProtectedBody().addAll(second.getProtectedBody()); + statements.remove(i + 1); + wieldTryCatch(first.getProtectedBody()); + --i; + continue; + } + } + } + } + + private boolean briefStatementComparison(List firstSeq, List secondSeq) { + if (firstSeq.isEmpty() && secondSeq.isEmpty()) { + return true; + } + if (firstSeq.size() != 1 || secondSeq.size() != 1) { + return false; + } + Statement first = firstSeq.get(0); + Statement second = secondSeq.get(0); + if (first instanceof BreakStatement && second instanceof BreakStatement) { + BreakStatement firstBreak = (BreakStatement)first; + BreakStatement secondBreak = (BreakStatement)second; + return firstBreak.getTarget() == secondBreak.getTarget(); + } + return false; + } + private void eliminateRedundantBreaks(List statements, IdentifiedStatement exit) { if (statements.isEmpty()) { return; 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 4d0dc05df..490aa00b2 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java +++ b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java @@ -641,7 +641,6 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext writer.append(")").ws().append("{").indent().softNewLine(); AsyncMethodPart part = methodNode.getBody().get(i); part.getStatement().acceptVisitor(Renderer.this); - writer.append("return $return($rt_asyncError($guard));").softNewLine(); writer.outdent().append("},").ws().append("$return);").softNewLine(); } writer.append("return $part_0();").softNewLine(); diff --git a/teavm-samples/teavm-samples-async/src/main/java/org/teavm/samples/async/AsyncProgram.java b/teavm-samples/teavm-samples-async/src/main/java/org/teavm/samples/async/AsyncProgram.java index fca37cc4a..dc1fa0fc3 100644 --- a/teavm-samples/teavm-samples-async/src/main/java/org/teavm/samples/async/AsyncProgram.java +++ b/teavm-samples/teavm-samples-async/src/main/java/org/teavm/samples/async/AsyncProgram.java @@ -55,5 +55,18 @@ public final class AsyncProgram { } } System.out.println("Complete async"); + + System.out.println("Throwing exception"); + try { + throwException(); + } catch (IllegalStateException e) { + System.out.println("Exception caught"); + } + } + + private static void throwException() { + Thread.yield(); + System.out.println("Thread.yield called"); + throw new IllegalStateException(); } }