JS: don't generate square brackets in 'if' bodies, when possible.

Generate mangled name for $rt_wrapException
This commit is contained in:
Alexey Andreev 2019-02-25 11:30:30 +03:00
parent a8f1940df3
commit 3be32a5851
3 changed files with 42 additions and 6 deletions

View File

@ -171,6 +171,7 @@ class NameFrequencyEstimator extends RecursiveVisitor implements MethodNodeVisit
if (statement.getExceptionType() != null) { if (statement.getExceptionType() != null) {
consumer.consume(statement.getExceptionType()); consumer.consume(statement.getExceptionType());
} }
consumer.consumeFunction("$rt_wrapException");
} }
@Override @Override

View File

@ -250,7 +250,7 @@ public class Renderer implements RenderingManager {
private void renderRuntimeAliases() throws IOException { private void renderRuntimeAliases() throws IOException {
String[] names = { "$rt_throw", "$rt_compare", "$rt_nullCheck", "$rt_cls", "$rt_createArray", String[] names = { "$rt_throw", "$rt_compare", "$rt_nullCheck", "$rt_cls", "$rt_createArray",
"$rt_isInstance", "$rt_nativeThread", "$rt_suspending", "$rt_resuming", "$rt_invalidPointer", "$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; boolean first = true;
for (String name : names) { for (String name : names) {
if (!first) { if (!first) {

View File

@ -190,6 +190,7 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
@Override @Override
public void visit(ConditionalStatement statement) { public void visit(ConditionalStatement statement) {
try { try {
boolean needClosingBracket;
while (true) { while (true) {
debugEmitter.emitStatementStart(); debugEmitter.emitStatementStart();
if (statement.getCondition().getLocation() != null) { if (statement.getCondition().getLocation() != null) {
@ -203,27 +204,60 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
popLocation(); popLocation();
} }
debugEmitter.emitCallSite(); 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()); visitStatements(statement.getConsequent());
if (!statement.getAlternative().isEmpty()) { if (!statement.getAlternative().isEmpty()) {
writer.outdent().append("}").ws(); writer.outdent();
if (needClosingBracket) {
writer.append("}").ws();
}
if (statement.getAlternative().size() == 1 if (statement.getAlternative().size() == 1
&& statement.getAlternative().get(0) instanceof ConditionalStatement) { && statement.getAlternative().get(0) instanceof ConditionalStatement) {
statement = (ConditionalStatement) statement.getAlternative().get(0); statement = (ConditionalStatement) statement.getAlternative().get(0);
writer.append("else "); writer.append("else ");
continue; 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()); visitStatements(statement.getAlternative());
} }
break; break;
} }
writer.outdent().append("}").softNewLine(); writer.outdent();
if (needClosingBracket) {
writer.append("}").softNewLine();
}
} catch (IOException e) { } catch (IOException e) {
throw new RenderingException("IO error occurred", e); throw new RenderingException("IO error occurred", e);
} }
} }
private boolean isSimpleIfContent(List<Statement> statements) {
if (statements.size() != 1) {
return false;
}
Statement statement = statements.get(0);
return !(statement instanceof ConditionalStatement);
}
@Override @Override
public void visit(SwitchStatement statement) { public void visit(SwitchStatement statement) {
try { try {
@ -1408,7 +1442,8 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
visitStatements(protectedBody); visitStatements(protectedBody);
writer.outdent().append("}").ws().append("catch").ws().append("($$e)") writer.outdent().append("}").ws().append("catch").ws().append("($$e)")
.ws().append("{").indent().softNewLine(); .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 first = true;
boolean defaultHandlerOccurred = false; boolean defaultHandlerOccurred = false;
for (TryCatchStatement catchClause : sequence) { for (TryCatchStatement catchClause : sequence) {