From ea3e6adb643ccd06a8ccf7dbd2c891b84222d704 Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Wed, 26 Feb 2014 17:14:08 +0400 Subject: [PATCH] Adds VM test that verifies that exceptions are caught --- .../org/teavm/classlib/java/lang/VMTest.java | 9 +++++++++ .../teavm/javascript/ReadWriteStatsBuilder.java | 4 ++++ .../java/org/teavm/javascript/Renderer.java | 17 +++++++++-------- .../javascript/UnusedVariableEliminator.java | 7 +++++++ .../teavm/javascript/ast/TryCatchStatement.java | 6 +++--- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/VMTest.java b/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/VMTest.java index eef2d2ebc..ef419e99f 100644 --- a/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/VMTest.java +++ b/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/VMTest.java @@ -58,4 +58,13 @@ public class VMTest { long b = 1836311903; assertEquals(2971215073L, a + b); } + + @Test + public void catchesException() { + try { + throw new IllegalArgumentException(); + } catch (IllegalArgumentException e) { + // do nothing + } + } } diff --git a/teavm-core/src/main/java/org/teavm/javascript/ReadWriteStatsBuilder.java b/teavm-core/src/main/java/org/teavm/javascript/ReadWriteStatsBuilder.java index 30eb4c0a5..9be54242a 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/ReadWriteStatsBuilder.java +++ b/teavm-core/src/main/java/org/teavm/javascript/ReadWriteStatsBuilder.java @@ -53,6 +53,10 @@ class ReadWriteStatsBuilder { reads[incoming.getValue().getIndex()]++; } } + for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) { + writes[tryCatch.getExceptionVariable().getIndex()]++; + reads[tryCatch.getExceptionVariable().getIndex()]++; + } } } } 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 38dcf2649..d2eace571 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java +++ b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java @@ -1264,16 +1264,17 @@ public class Renderer implements ExprVisitor, StatementVisitor { for (Statement part : protectedBody) { part.acceptVisitor(this); } - String var = variableName(statement.getExceptionVariable()); - writer.outdent().append("}").ws().append("catch").ws().append("(").append(var).append(")") + writer.outdent().append("}").ws().append("catch").ws().append("($e)") .ws().append("{").indent().softNewLine(); + writer.append("$je").ws().append("=").ws().append("$e.$javaException;").softNewLine(); for (TryCatchStatement catchClause : sequence) { - writer.append("if").ws().append("(").append(var).append(" instanceof ") - .appendClass(catchClause.getExceptionType()).append(")").ws().append("{") - .indent().softNewLine(); + writer.append("if").ws().append("($je && $je instanceof ").appendClass(catchClause.getExceptionType()) + .append(")").ws().append("{").indent().softNewLine(); if (statement.getExceptionVariable() != catchClause.getExceptionVariable()) { - writer.append(variableName(catchClause.getExceptionVariable())).ws().append("=").ws() - .append(var).append(";").softNewLine(); + if (catchClause.getExceptionVariable() != null) { + writer.append(variableName(catchClause.getExceptionVariable())).ws().append("=").ws() + .append("$e;").softNewLine(); + } } for (Statement part : statement.getHandler()) { part.acceptVisitor(this); @@ -1281,7 +1282,7 @@ public class Renderer implements ExprVisitor, StatementVisitor { writer.outdent().append("}").ws().append("else "); } writer.append("{").indent().softNewLine(); - writer.append("throw ").append(var).append(";").softNewLine(); + writer.append("throw $e;").softNewLine(); writer.outdent().append("}").softNewLine(); writer.outdent().append("}").softNewLine(); } catch (IOException e) { 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 61232073e..940e97140 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/UnusedVariableEliminator.java +++ b/teavm-core/src/main/java/org/teavm/javascript/UnusedVariableEliminator.java @@ -224,5 +224,12 @@ class UnusedVariableEliminator implements ExprVisitor, StatementVisitor { for (Statement part : statement.getHandler()) { part.acceptVisitor(this); } + if (statement.getExceptionVariable() != null) { + if (variables[statement.getExceptionVariable()] < 0) { + statement.setExceptionVariable(null); + } else { + statement.setExceptionVariable(renumber(statement.getExceptionVariable())); + } + } } } diff --git a/teavm-core/src/main/java/org/teavm/javascript/ast/TryCatchStatement.java b/teavm-core/src/main/java/org/teavm/javascript/ast/TryCatchStatement.java index 90e13a312..a5d11e66a 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/ast/TryCatchStatement.java +++ b/teavm-core/src/main/java/org/teavm/javascript/ast/TryCatchStatement.java @@ -26,7 +26,7 @@ public class TryCatchStatement extends Statement { private List protectedBody = new ArrayList<>(); private List handler = new ArrayList<>(); private String exceptionType; - private int exceptionVariable; + private Integer exceptionVariable; public List getProtectedBody() { return protectedBody; @@ -44,11 +44,11 @@ public class TryCatchStatement extends Statement { this.exceptionType = exceptionType; } - public int getExceptionVariable() { + public Integer getExceptionVariable() { return exceptionVariable; } - public void setExceptionVariable(int exceptionVariable) { + public void setExceptionVariable(Integer exceptionVariable) { this.exceptionVariable = exceptionVariable; }