From 3be32a5851b86cc55d5b91457290854ed6b1676b Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 25 Feb 2019 11:30:30 +0300 Subject: [PATCH] JS: don't generate square brackets in 'if' bodies, when possible. Generate mangled name for $rt_wrapException --- .../rendering/NameFrequencyEstimator.java | 1 + .../javascript/rendering/Renderer.java | 2 +- .../rendering/StatementRenderer.java | 45 ++++++++++++++++--- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/NameFrequencyEstimator.java b/core/src/main/java/org/teavm/backend/javascript/rendering/NameFrequencyEstimator.java index eaada83e3..90595e885 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/NameFrequencyEstimator.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/NameFrequencyEstimator.java @@ -171,6 +171,7 @@ class NameFrequencyEstimator extends RecursiveVisitor implements MethodNodeVisit if (statement.getExceptionType() != null) { consumer.consume(statement.getExceptionType()); } + consumer.consumeFunction("$rt_wrapException"); } @Override diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/Renderer.java b/core/src/main/java/org/teavm/backend/javascript/rendering/Renderer.java index 83b545083..a68a30968 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/Renderer.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/Renderer.java @@ -250,7 +250,7 @@ public class Renderer implements RenderingManager { private void renderRuntimeAliases() throws IOException { String[] names = { "$rt_throw", "$rt_compare", "$rt_nullCheck", "$rt_cls", "$rt_createArray", "$rt_isInstance", "$rt_nativeThread", "$rt_suspending", "$rt_resuming", "$rt_invalidPointer", - "$rt_s", "$rt_eraseClinit", "$rt_imul" }; + "$rt_s", "$rt_eraseClinit", "$rt_imul", "$rt_wrapException" }; boolean first = true; for (String name : names) { if (!first) { diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java b/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java index 8775e93ed..a69c07cea 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java @@ -190,6 +190,7 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { @Override public void visit(ConditionalStatement statement) { try { + boolean needClosingBracket; while (true) { debugEmitter.emitStatementStart(); if (statement.getCondition().getLocation() != null) { @@ -203,27 +204,60 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { popLocation(); } debugEmitter.emitCallSite(); - writer.append(")").ws().append("{").softNewLine().indent(); + writer.append(")"); + if (isSimpleIfContent(statement.getConsequent())) { + needClosingBracket = false; + } else { + writer.ws().append("{"); + needClosingBracket = true; + } + writer.softNewLine().indent(); visitStatements(statement.getConsequent()); + if (!statement.getAlternative().isEmpty()) { - writer.outdent().append("}").ws(); + writer.outdent(); + if (needClosingBracket) { + writer.append("}").ws(); + } if (statement.getAlternative().size() == 1 && statement.getAlternative().get(0) instanceof ConditionalStatement) { statement = (ConditionalStatement) statement.getAlternative().get(0); writer.append("else "); continue; } - writer.append("else").ws().append("{").indent().softNewLine(); + writer.append("else"); + if (isSimpleIfContent(statement.getAlternative())) { + if (minifying) { + writer.append(" "); + } + needClosingBracket = false; + } else { + writer.ws().append("{"); + needClosingBracket = true; + } + writer.indent().softNewLine(); visitStatements(statement.getAlternative()); } break; } - writer.outdent().append("}").softNewLine(); + writer.outdent(); + if (needClosingBracket) { + writer.append("}").softNewLine(); + } } catch (IOException e) { throw new RenderingException("IO error occurred", e); } } + private boolean isSimpleIfContent(List statements) { + if (statements.size() != 1) { + return false; + } + + Statement statement = statements.get(0); + return !(statement instanceof ConditionalStatement); + } + @Override public void visit(SwitchStatement statement) { try { @@ -1408,7 +1442,8 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { visitStatements(protectedBody); writer.outdent().append("}").ws().append("catch").ws().append("($$e)") .ws().append("{").indent().softNewLine(); - writer.append("$$je").ws().append("=").ws().append("$rt_wrapException($$e);").softNewLine(); + writer.append("$$je").ws().append("=").ws().appendFunction("$rt_wrapException").append("($$e);") + .softNewLine(); boolean first = true; boolean defaultHandlerOccurred = false; for (TryCatchStatement catchClause : sequence) {