diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ClassNativeGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ClassNativeGenerator.java index a6ac2a177..cdd12a13a 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ClassNativeGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ClassNativeGenerator.java @@ -27,17 +27,17 @@ public class ClassNativeGenerator implements Generator { private void generateIsInstance(GeneratorContext context, SourceWriter writer) { writer.append("return $rt_isInstance(").append(context.getParameterName(1)).append(", ") - .append(context.getParameterName(0)).append(".$data);").newLine(); + .append(context.getParameterName(0)).append(".$data);").softNewLine(); } private void generateIsAssignableFrom(GeneratorContext context, SourceWriter writer) { writer.append("return $rt_isAssignable(").append(context.getParameterName(1)).append(".$data, ") - .append(context.getParameterName(0)).append(".$data;").newLine(); + .append(context.getParameterName(0)).append(".$data;").softNewLine(); } private void generateGetComponentType(GeneratorContext context, SourceWriter writer) { String thisArg = context.getParameterName(0); - writer.append("var item = " + thisArg + ".$data.$meta.item;").newLine(); - writer.append("return item != null ? $rt_cls(item) : null;").newLine(); + writer.append("var item = " + thisArg + ".$data.$meta.item;").softNewLine(); + writer.append("return item != null ? $rt_cls(item) : null;").softNewLine(); } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ObjectNativeGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ObjectNativeGenerator.java index 44e9b7d16..5d5ce32bb 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ObjectNativeGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ObjectNativeGenerator.java @@ -52,12 +52,12 @@ public class ObjectNativeGenerator implements Generator, DependencyPlugin { } private void generateInit(GeneratorContext context, SourceWriter writer) { - writer.append(context.getParameterName(0)).append(".$id = $rt_nextId();").newLine(); + writer.append(context.getParameterName(0)).append(".$id = $rt_nextId();").softNewLine(); } private void generateGetClass(GeneratorContext context, SourceWriter writer) { String thisArg = context.getParameterName(0); - writer.append("return $rt_cls(").append(thisArg).append(".$class);").newLine(); + writer.append("return $rt_cls(").append(thisArg).append(".$class);").softNewLine(); } private void achieveGetClass(DependencyChecker checker) { @@ -68,16 +68,16 @@ public class ObjectNativeGenerator implements Generator, DependencyPlugin { } private void generateHashCode(GeneratorContext context, SourceWriter writer) { - writer.append("return ").append(context.getParameterName(0)).append(".$id;").newLine(); + writer.append("return ").append(context.getParameterName(0)).append(".$id;").softNewLine(); } private void generateClone(GeneratorContext context, SourceWriter writer) { - writer.append("var copy = new ").append(context.getParameterName(0)).append(".$class();").newLine(); - writer.append("for (var field in obj) {").newLine().indent(); - writer.append("if (!obj.hasOwnProperty(field)) {").newLine().indent(); - writer.append("continue;").newLine().outdent().append("}").newLine(); - writer.append("copy[field] = obj[field];").newLine().outdent().append("}").newLine(); - writer.append("return copy;").newLine(); + writer.append("var copy = new ").append(context.getParameterName(0)).append(".$class();").softNewLine(); + writer.append("for (var field in obj) {").softNewLine().indent(); + writer.append("if (!obj.hasOwnProperty(field)) {").softNewLine().indent(); + writer.append("continue;").softNewLine().outdent().append("}").softNewLine(); + writer.append("copy[field] = obj[field];").softNewLine().outdent().append("}").softNewLine(); + writer.append("return copy;").softNewLine(); } private void achieveClone(DependencyChecker checker, MethodReference method) { @@ -86,7 +86,7 @@ public class ObjectNativeGenerator implements Generator, DependencyPlugin { } private void generateWrap(GeneratorContext context, SourceWriter writer) { - writer.append("return ").append(context.getParameterName(1)).append(";").newLine(); + writer.append("return ").append(context.getParameterName(1)).append(";").softNewLine(); } private void achieveWrap(DependencyChecker checker, MethodReference method) { diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/StringNativeGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/StringNativeGenerator.java index 80c8a4c75..c997fb5e2 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/StringNativeGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/StringNativeGenerator.java @@ -20,6 +20,6 @@ public class StringNativeGenerator implements Generator { } private void generateWrap(GeneratorContext context, SourceWriter writer) { - writer.append("return ").append(context.getParameterName(1)).newLine(); + writer.append("return ").append(context.getParameterName(1)).softNewLine(); } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java index b480807e8..fadbea995 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java @@ -38,9 +38,9 @@ public class SystemNativeGenerator implements Generator, DependencyPlugin { String dest = context.getParameterName(3); String destPos = context.getParameterName(4); String length = context.getParameterName(5); - writer.append("for (var i = 0; i < " + length + "; i = (i + 1) | 0) {").indent().newLine(); - writer.append(dest + ".data[" + srcPos + "++] = " + src + ".data[" + destPos + "++];").newLine(); - writer.outdent().append("}").newLine(); + writer.append("for (var i = 0; i < " + length + "; i = (i + 1) | 0) {").indent().softNewLine(); + writer.append(dest + ".data[" + srcPos + "++] = " + src + ".data[" + destPos + "++];").softNewLine(); + writer.outdent().append("}").softNewLine(); } private void achieveArrayCopy(DependencyChecker checker, MethodReference method) { diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java index 4b283ec0b..50f863705 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java @@ -35,12 +35,12 @@ public class ArrayNativeGenerator implements Generator, DependencyPlugin { private void generateGetLength(GeneratorContext context, SourceWriter writer) { String array = context.getParameterName(1); writer.append("if (" + array + " === null || " + array + " .$class.$meta.item === undefined) {") - .newLine().indent(); + .softNewLine().indent(); String clsName = "java.lang.IllegalArgumentException"; MethodReference cons = new MethodReference(clsName, new MethodDescriptor("", ValueType.VOID)); - writer.append("$rt_throw(").appendClass(clsName).append(".").appendMethod(cons).append("());").newLine(); - writer.outdent().append("}").newLine(); - writer.append("return " + array + ".data.length;").newLine(); + writer.append("$rt_throw(").appendClass(clsName).append(".").appendMethod(cons).append("());").softNewLine(); + writer.outdent().append("}").softNewLine(); + writer.append("return " + array + ".data.length;").softNewLine(); } private void achieveGetLength(final DependencyChecker checker, MethodReference methodRef) { diff --git a/teavm-core/src/main/java/org/teavm/codegen/MinifyingAliasProvider.java b/teavm-core/src/main/java/org/teavm/codegen/MinifyingAliasProvider.java index a460278f7..6e612b713 100644 --- a/teavm-core/src/main/java/org/teavm/codegen/MinifyingAliasProvider.java +++ b/teavm-core/src/main/java/org/teavm/codegen/MinifyingAliasProvider.java @@ -8,8 +8,8 @@ import org.teavm.model.MethodReference; * @author Alexey Andreev */ public class MinifyingAliasProvider implements AliasProvider { - private String startLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - private String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + private static String startLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + private static String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; private int lastSuffix; @Override @@ -28,7 +28,7 @@ public class MinifyingAliasProvider implements AliasProvider { } private String getNewAlias() { - int index = lastSuffix; + int index = lastSuffix++; StringBuilder sb = new StringBuilder(); sb.append(startLetters.charAt(index % startLetters.length())); index /= startLetters.length(); 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 7707a9acc..31ddf1da5 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java +++ b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java @@ -122,7 +122,7 @@ public class Renderer implements ExprVisitor, StatementVisitor { .append("=").ws().append(constantToString(value)).append(";").softNewLine(); } writer.append("this.$class").ws().append("=").ws().appendClass(cls.getName()).append(";").softNewLine(); - writer.outdent().append("}").softNewLine(); + writer.outdent().append("}").newLine(); for (FieldNode field : cls.getFields()) { if (!field.getModifiers().contains(NodeModifier.STATIC)) { @@ -243,25 +243,25 @@ public class Renderer implements ExprVisitor, StatementVisitor { if (startParam == 0) { writer.append("prototype."); } - writer.appendMethod(ref).append(" = function("); + writer.appendMethod(ref).ws().append("=").ws().append("function("); for (int i = 1; i <= ref.parameterCount(); ++i) { if (i > 1) { writer.append(", "); } writer.append(variableName(i)); } - writer.append(") {").newLine().indent(); - writer.append("return ").appendClass(ref.getClassName()).append('_').appendMethod(ref).append("("); + writer.append(")").ws().append("{").softNewLine().indent(); + writer.append("return ").appendMethodBody(ref).append("("); if (startParam == 0) { writer.append("this"); } for (int i = 1; i <= ref.parameterCount(); ++i) { if (i > 1 || startParam == 0) { - writer.append(", "); + writer.append(",").ws(); } writer.append(variableName(i)); } - writer.append(");").newLine(); + writer.append(");").softNewLine(); writer.outdent().append("}").newLine(); } catch (NamingException e) { throw new RenderingException("Error rendering method " + method.getReference() + ". " + @@ -271,32 +271,32 @@ public class Renderer implements ExprVisitor, StatementVisitor { public void renderStub(MethodNode method) { MethodReference ref = method.getReference(); - writer.appendClass(ref.getClassName()).append('_').appendMethod(ref).append(" = function("); + writer.appendMethodBody(ref).ws().append("=").ws().append("function("); int startParam = 0; if (method.getModifiers().contains(NodeModifier.STATIC)) { startParam = 1; } for (int i = startParam; i <= ref.parameterCount(); ++i) { if (i > startParam) { - writer.append(", "); + writer.append(",").ws(); } writer.append(variableName(i)); } String owner = ref.getClassName(); - writer.append(") { ").appendClass(owner).append(".clinit(); ").append("return ").appendClass(owner) - .append("_").appendMethod(ref).append("("); + writer.append(")").ws().append("{").ws().appendClass(owner).append(".clinit();") + .ws().append("return ").appendMethodBody(ref).append("("); for (int i = startParam; i <= ref.parameterCount(); ++i) { if (i > startParam) { writer.append(", "); } writer.append(variableName(i)); } - writer.append("); };").newLine(); + writer.append(");").ws().append("};").newLine(); } public void renderBody(MethodNode method) { MethodReference ref = method.getReference(); - writer.appendClass(ref.getClassName()).append('_').appendMethod(ref).append(" = function("); + writer.appendMethodBody(ref).ws().append("=").ws().append("function("); int startParam = 0; if (method.getModifiers().contains(NodeModifier.STATIC)) { startParam = 1; @@ -307,7 +307,7 @@ public class Renderer implements ExprVisitor, StatementVisitor { } writer.append(variableName(i)); } - writer.append(") {").newLine().indent(); + writer.append(")").ws().append("{").softNewLine().indent(); method.acceptVisitor(new MethodBodyRenderer()); writer.outdent().append("}").newLine(); } @@ -333,7 +333,7 @@ public class Renderer implements ExprVisitor, StatementVisitor { first = false; writer.append(variableName(i)); } - writer.append(";").newLine(); + writer.append(";").softNewLine(); } method.getBody().acceptVisitor(Renderer.this); } @@ -348,10 +348,10 @@ public class Renderer implements ExprVisitor, StatementVisitor { public void visit(AssignmentStatement statement) { if (statement.getLeftValue() != null) { statement.getLeftValue().acceptVisitor(this); - writer.append(" = "); + writer.ws().append("=").ws(); } statement.getRightValue().acceptVisitor(this); - writer.append(";").newLine(); + writer.append(";").softNewLine(); } @Override @@ -363,15 +363,15 @@ public class Renderer implements ExprVisitor, StatementVisitor { @Override public void visit(ConditionalStatement statement) { - writer.append("if ("); + writer.append("if").ws().append("("); statement.getCondition().acceptVisitor(this); - writer.append(") {").newLine().indent(); + writer.append(")").ws().append("{").softNewLine().indent(); statement.getConsequent().acceptVisitor(this); if (statement.getAlternative() != null) { - writer.outdent().append("} else {").indent().newLine(); + writer.outdent().append("}").ws().append("else").ws().append("{").indent().softNewLine(); statement.getAlternative().acceptVisitor(this); } - writer.outdent().append("}").newLine(); + writer.outdent().append("}").softNewLine(); } @Override @@ -379,50 +379,50 @@ public class Renderer implements ExprVisitor, StatementVisitor { if (statement.getId() != null) { writer.append(statement.getId()).append(": "); } - writer.append("switch ("); + writer.append("switch").ws().append("("); statement.getValue().acceptVisitor(this); - writer.append(") {").newLine().indent(); + writer.append(")").ws().append("{").softNewLine().indent(); for (SwitchClause clause : statement.getClauses()) { for (int condition : clause.getConditions()) { - writer.append("case ").append(condition).append(":").newLine(); + writer.append("case ").append(condition).append(":").softNewLine(); } writer.indent(); clause.getStatement().acceptVisitor(this); writer.outdent(); } if (statement.getDefaultClause() != null) { - writer.append("default:").newLine().indent(); + writer.append("default:").softNewLine().indent(); statement.getDefaultClause().acceptVisitor(this); writer.outdent(); } - writer.outdent().append("}").newLine(); + writer.outdent().append("}").softNewLine(); } @Override public void visit(WhileStatement statement) { if (statement.getId() != null) { - writer.append(statement.getId()).append(": "); + writer.append(statement.getId()).append(":").ws(); } - writer.append("while ("); + writer.append("while").ws().append("("); if (statement.getCondition() != null) { statement.getCondition().acceptVisitor(this); } else { writer.append("true"); } - writer.append(") {").newLine().indent(); + writer.append(")").ws().append("{").softNewLine().indent(); for (Statement part : statement.getBody()) { part.acceptVisitor(this); } - writer.outdent().append("}").newLine(); + writer.outdent().append("}").softNewLine(); } @Override public void visit(BlockStatement statement) { - writer.append(statement.getId()).append(": {").newLine().indent(); + writer.append(statement.getId()).append(":").ws().append("{").softNewLine().indent(); for (Statement part : statement.getBody()) { part.acceptVisitor(this); } - writer.outdent().append("}").newLine(); + writer.outdent().append("}").softNewLine(); } @Override @@ -432,12 +432,12 @@ public class Renderer implements ExprVisitor, StatementVisitor { @Override public void visit(BreakStatement statement) { - writer.append("break ").append(statement.getTarget().getId()).append(";").newLine(); + writer.append("break ").append(statement.getTarget().getId()).append(";").softNewLine(); } @Override public void visit(ContinueStatement statement) { - writer.append("continue ").append(statement.getTarget().getId()).append(";").newLine(); + writer.append("continue ").append(statement.getTarget().getId()).append(";").softNewLine(); } @Override @@ -447,14 +447,14 @@ public class Renderer implements ExprVisitor, StatementVisitor { writer.append(' '); statement.getResult().acceptVisitor(this); } - writer.append(";").newLine(); + writer.append(";").softNewLine(); } @Override public void visit(ThrowStatement statement) { writer.append("$rt_throw("); statement.getException().acceptVisitor(this); - writer.append(");").newLine(); + writer.append(");").softNewLine(); } @Override @@ -464,16 +464,16 @@ public class Renderer implements ExprVisitor, StatementVisitor { if (statement.getAmount() == 1) { writer.append("++"); } else { - writer.append(" += ").append(statement.getAmount()); + writer.ws().append("+=").ws().append(statement.getAmount()); } } else { if (statement.getAmount() == -1) { writer.append("--"); } else { - writer.append(" -= ").append(statement.getAmount()); + writer.ws().append("-=").ws().append(statement.getAmount()); } } - writer.append(";").newLine(); + writer.append(";").softNewLine(); } public String variableName(int index) { @@ -492,7 +492,7 @@ public class Renderer implements ExprVisitor, StatementVisitor { private void visitBinary(BinaryExpr expr, String op) { writer.append('('); expr.getFirstOperand().acceptVisitor(this); - writer.append(' ').append(op).append(' '); + writer.ws().append(op).ws(); expr.getSecondOperand().acceptVisitor(this); writer.append(')'); } @@ -501,7 +501,7 @@ public class Renderer implements ExprVisitor, StatementVisitor { writer.append(function); writer.append('('); expr.getFirstOperand().acceptVisitor(this); - writer.append(", "); + writer.append(",").ws(); expr.getSecondOperand().acceptVisitor(this); writer.append(')'); } @@ -673,9 +673,9 @@ public class Renderer implements ExprVisitor, StatementVisitor { public void visit(ConditionalExpr expr) { writer.append('('); expr.getCondition().acceptVisitor(this); - writer.append(" ? "); + writer.ws().append("?").ws(); expr.getConsequent().acceptVisitor(this); - writer.append(" : "); + writer.ws().append(":").ws(); expr.getAlternative().acceptVisitor(this); writer.append(')'); } @@ -819,22 +819,23 @@ public class Renderer implements ExprVisitor, StatementVisitor { public void visit(InvocationExpr expr) { String className = naming.getNameFor(expr.getMethod().getClassName()); String name = naming.getNameFor(expr.getMethod()); + String fullName = naming.getFullNameFor(expr.getMethod()); switch (expr.getType()) { case STATIC: - writer.append(className).append("_").append(name).append("("); + writer.append(fullName).append("("); for (int i = 0; i < expr.getArguments().size(); ++i) { if (i > 0) { - writer.append(", "); + writer.append(",").ws(); } expr.getArguments().get(i).acceptVisitor(this); } writer.append(')'); break; case SPECIAL: - writer.append(className).append("_").append(name).append("("); + writer.append(fullName).append("("); expr.getArguments().get(0).acceptVisitor(this); for (int i = 1; i < expr.getArguments().size(); ++i) { - writer.append(", "); + writer.append(",").ws(); expr.getArguments().get(i).acceptVisitor(this); } writer.append(")"); @@ -844,7 +845,7 @@ public class Renderer implements ExprVisitor, StatementVisitor { writer.append(".").append(name).append("("); for (int i = 1; i < expr.getArguments().size(); ++i) { if (i > 1) { - writer.append(", "); + writer.append(",").ws(); } expr.getArguments().get(i).acceptVisitor(this); } @@ -854,7 +855,7 @@ public class Renderer implements ExprVisitor, StatementVisitor { writer.append(className).append(".").append(name).append("("); for (int i = 0; i < expr.getArguments().size(); ++i) { if (i > 0) { - writer.append(", "); + writer.append(",").ws(); } expr.getArguments().get(i).acceptVisitor(this); } @@ -932,11 +933,12 @@ public class Renderer implements ExprVisitor, StatementVisitor { @Override public void visit(NewMultiArrayExpr expr) { - writer.append("$rt_createMultiArray(").append(typeToClsString(naming, expr.getType())).append(", ["); + writer.append("$rt_createMultiArray(").append(typeToClsString(naming, expr.getType())).append(",") + .ws().append("["); boolean first = true; for (Expr dimension : expr.getDimensions()) { if (!first) { - writer.append(", "); + writer.append(",").ws(); } first = false; dimension.acceptVisitor(this); @@ -958,7 +960,7 @@ public class Renderer implements ExprVisitor, StatementVisitor { } writer.append("$rt_isInstance("); expr.getExpr().acceptVisitor(this); - writer.append(", ").append(typeToClsString(naming, expr.getType())).append(")"); + writer.append(",").ws().append(typeToClsString(naming, expr.getType())).append(")"); } @Override