Produce less code

This commit is contained in:
konsoletyper 2015-02-07 16:57:45 +04:00
parent 268b66e523
commit 942542df6f
4 changed files with 71 additions and 46 deletions

View File

@ -57,15 +57,15 @@ public class DefaultNamingStrategy implements NamingStrategy {
@Override @Override
public String getNameFor(MethodReference method) { public String getNameFor(MethodReference method) {
return getNameFor(method, false); return getNameFor(method, 'S');
} }
@Override @Override
public String getNameForAsync(MethodReference method) throws NamingException { public String getNameForAsync(MethodReference method) throws NamingException {
return getNameFor(method, true); return getNameFor(method, 'A');
} }
private String getNameFor(MethodReference method, boolean async) { private String getNameFor(MethodReference method, char classifier) {
MethodReference origMethod = method; MethodReference origMethod = method;
method = getRealMethod(method); method = getRealMethod(method);
if (method == null) { if (method == null) {
@ -76,7 +76,7 @@ public class DefaultNamingStrategy implements NamingStrategy {
if (methodHolder.hasModifier(ElementModifier.STATIC) || if (methodHolder.hasModifier(ElementModifier.STATIC) ||
method.getDescriptor().getName().equals("<init>") || method.getDescriptor().getName().equals("<init>") ||
methodHolder.getLevel() == AccessLevel.PRIVATE) { methodHolder.getLevel() == AccessLevel.PRIVATE) {
String key = (async ? "A" : "S") + method.toString(); String key = classifier + method.toString();
String alias = privateAliases.get(key); String alias = privateAliases.get(key);
if (alias == null) { if (alias == null) {
alias = aliasProvider.getAlias(method); alias = aliasProvider.getAlias(method);
@ -84,7 +84,7 @@ public class DefaultNamingStrategy implements NamingStrategy {
} }
return alias; return alias;
} else { } else {
String key = (async ? "A" : "S") + method.getDescriptor().toString(); String key = classifier + method.getDescriptor().toString();
String alias = aliases.get(key); String alias = aliases.get(key);
if (alias == null) { if (alias == null) {
alias = aliasProvider.getAlias(method); alias = aliasProvider.getAlias(method);
@ -96,15 +96,24 @@ public class DefaultNamingStrategy implements NamingStrategy {
@Override @Override
public String getFullNameFor(MethodReference method) throws NamingException { public String getFullNameFor(MethodReference method) throws NamingException {
return getFullNameFor(method, 'S');
}
@Override
public String getNameForInit(MethodReference method) throws NamingException {
return getFullNameFor(method, 'I');
}
private String getFullNameFor(MethodReference method, char classifier) throws NamingException {
MethodReference originalMethod = method; MethodReference originalMethod = method;
if (!minifying) { if (!minifying) {
return getNameFor(method.getClassName()) + "_" + getNameFor(method); return getNameFor(method.getClassName()) + "_" + getNameFor(method, classifier);
} }
method = getRealMethod(method); method = getRealMethod(method);
if (method == null) { if (method == null) {
throw new NamingException("Can't provide name for method as it was not found: " + originalMethod); throw new NamingException("Can't provide name for method as it was not found: " + originalMethod);
} }
String key = method.toString(); String key = classifier + method.toString();
String alias = privateAliases.get(key); String alias = privateAliases.get(key);
if (alias == null) { if (alias == null) {
alias = aliasProvider.getAlias(method); alias = aliasProvider.getAlias(method);

View File

@ -29,6 +29,8 @@ public interface NamingStrategy {
String getNameForAsync(MethodReference method) throws NamingException; String getNameForAsync(MethodReference method) throws NamingException;
String getNameForInit(MethodReference method) throws NamingException;
String getFullNameFor(MethodReference method) throws NamingException; String getFullNameFor(MethodReference method) throws NamingException;
String getNameFor(FieldReference field) throws NamingException; String getNameFor(FieldReference field) throws NamingException;

View File

@ -213,8 +213,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.append("for (var i = 0; i < str.length; i = (i + 1) | 0) {").indent().softNewLine(); writer.append("for (var i = 0; i < str.length; i = (i + 1) | 0) {").indent().softNewLine();
writer.append("charsBuffer[i] = str.charCodeAt(i) & 0xFFFF;").softNewLine(); writer.append("charsBuffer[i] = str.charCodeAt(i) & 0xFFFF;").softNewLine();
writer.outdent().append("}").softNewLine(); writer.outdent().append("}").softNewLine();
writer.append("return ").appendClass("java.lang.String").append(".") writer.append("return ").append(naming.getNameForInit(stringCons)).append("(characters);").softNewLine();
.appendMethod(stringCons).append("(characters);").softNewLine();
writer.outdent().append("}").newLine(); writer.outdent().append("}").newLine();
} }
@ -235,11 +234,10 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} }
private void renderRuntimeNullCheck() throws IOException { private void renderRuntimeNullCheck() throws IOException {
String npe = "java.lang.NullPointerException";
writer.append("function $rt_nullCheck(val) {").indent().softNewLine(); writer.append("function $rt_nullCheck(val) {").indent().softNewLine();
writer.append("if (val === null) {").indent().softNewLine(); writer.append("if (val === null) {").indent().softNewLine();
writer.append("$rt_throw(").appendClass(npe).append('.').appendMethod(npe, "<init>", ValueType.VOID) writer.append("$rt_throw(").append(naming.getNameForInit(new MethodReference(NullPointerException.class,
.append("());").softNewLine(); "<init>", void.class))).append("());").softNewLine();
writer.outdent().append("}").softNewLine(); writer.outdent().append("}").softNewLine();
writer.append("return val;").softNewLine(); writer.append("return val;").softNewLine();
writer.outdent().append("}").newLine(); writer.outdent().append("}").newLine();
@ -293,6 +291,23 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
.append(constantToString(value)).append(";").softNewLine(); .append(constantToString(value)).append(";").softNewLine();
} }
List<MethodNode> nonInitMethods = new ArrayList<>();
List<MethodNode> virtualMethods = new ArrayList<>();
MethodHolder clinit = classSource.get(cls.getName()).getMethod(
new MethodDescriptor("<clinit>", ValueType.VOID));
List<String> stubNames = new ArrayList<>();
List<MethodNode> clinitMethods = new ArrayList<>();
for (MethodNode method : cls.getMethods()) {
if (!method.getModifiers().contains(NodeModifier.STATIC) &&
!method.getReference().getName().equals("<init>")) {
nonInitMethods.add(method);
} else {
clinitMethods.add(method);
stubNames.add(naming.getFullNameFor(method.getReference()));
}
}
boolean needsClinit = clinit != null || !clinitMethods.isEmpty();
writer.append("$rt_declClass(").appendClass(cls.getName()).append(",").ws().append("{") writer.append("$rt_declClass(").appendClass(cls.getName()).append(",").ws().append("{")
.indent().softNewLine(); .indent().softNewLine();
writer.append("name").ws().append(":").ws().append("\"").append(escapeString(cls.getName())) writer.append("name").ws().append(":").ws().append("\"").append(escapeString(cls.getName()))
@ -315,41 +330,36 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.append(",").softNewLine(); writer.append(",").softNewLine();
writer.append("superclass").ws().append(":").ws().appendClass(cls.getParentName()); writer.append("superclass").ws().append(":").ws().appendClass(cls.getParentName());
} }
if (!cls.getModifiers().contains(NodeModifier.INTERFACE)) { if (!cls.getModifiers().contains(NodeModifier.INTERFACE) && needsClinit) {
writer.append(",").softNewLine().append("clinit").ws().append(":").ws() writer.append(",").softNewLine().append("clinit").ws().append(":").ws()
.append("function()").ws().append("{").ws() .append("function()").ws().append("{").ws()
.appendClass(cls.getName()).append("_$clinit();").ws().append("}"); .appendClass(cls.getName()).append("_$clinit();").ws().append("}");
} }
writer.ws().append("});").newLine().outdent(); writer.ws().append("});").newLine().outdent();
List<MethodNode> nonInitMethods = new ArrayList<>();
List<MethodNode> virtualMethods = new ArrayList<>();
writer.append("function ").appendClass(cls.getName()).append("_$clinit()").ws() if (needsClinit) {
.append("{").softNewLine().indent(); writer.append("function ").appendClass(cls.getName()).append("_$clinit()").ws()
writer.appendClass(cls.getName()).append("_$clinit").ws().append("=").ws() .append("{").softNewLine().indent();
.append("function(){};").newLine(); writer.appendClass(cls.getName()).append("_$clinit").ws().append("=").ws()
List<String> stubNames = new ArrayList<>(); .append("function(){};").newLine();
for (MethodNode method : cls.getMethods()) { for (MethodNode method : clinitMethods) {
if (!method.getModifiers().contains(NodeModifier.STATIC) &&
!method.getReference().getName().equals("<init>")) {
nonInitMethods.add(method);
} else {
renderBody(method, true); renderBody(method, true);
stubNames.add(naming.getFullNameFor(method.getReference()));
} }
if (clinit != null) {
writer.appendMethodBody(new MethodReference(cls.getName(), clinit.getDescriptor()))
.append("();").softNewLine();
}
writer.outdent().append("}").newLine();
} }
MethodHolder methodHolder = classSource.get(cls.getName()).getMethod(
new MethodDescriptor("<clinit>", ValueType.VOID));
if (methodHolder != null) {
writer.appendMethodBody(new MethodReference(cls.getName(), methodHolder.getDescriptor()))
.append("();").softNewLine();
}
writer.outdent().append("}").newLine();
if (!cls.getModifiers().contains(NodeModifier.INTERFACE)) { if (!cls.getModifiers().contains(NodeModifier.INTERFACE)) {
for (MethodNode method : cls.getMethods()) { for (MethodNode method : cls.getMethods()) {
cls.getMethods(); cls.getMethods();
if (!method.getModifiers().contains(NodeModifier.STATIC)) { if (!method.getModifiers().contains(NodeModifier.STATIC)) {
virtualMethods.add(method); if (method.getReference().getName().equals("<init>")) {
renderInitializer(method);
} else {
virtualMethods.add(method);
}
} else if (method.isOriginalNamePreserved()) { } else if (method.isOriginalNamePreserved()) {
renderStaticDeclaration(method); renderStaticDeclaration(method);
} }
@ -407,7 +417,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
private void renderInitializer(MethodNode method) throws IOException { private void renderInitializer(MethodNode method) throws IOException {
MethodReference ref = method.getReference(); MethodReference ref = method.getReference();
debugEmitter.emitMethod(ref.getDescriptor()); debugEmitter.emitMethod(ref.getDescriptor());
writer.appendClass(ref.getClassName()).append(".").appendMethod(ref).ws().append("=").ws().append("function("); writer.append("function ").append(naming.getNameForInit(ref)).append("(");
for (int i = 1; i <= ref.parameterCount(); ++i) { for (int i = 1; i <= ref.parameterCount(); ++i) {
if (i > 1) { if (i > 1) {
writer.append(",").ws(); writer.append(",").ws();
@ -417,11 +427,9 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.append(")").ws().append("{").newLine().indent(); writer.append(")").ws().append("{").newLine().indent();
writer.append("var result").ws().append("=").ws().append("new ").appendClass( writer.append("var result").ws().append("=").ws().append("new ").appendClass(
ref.getClassName()).append("();").softNewLine(); ref.getClassName()).append("();").softNewLine();
writer.append("result.").appendMethod(ref).append("("); writer.append(naming.getFullNameFor(ref)).append("(result");
for (int i = 1; i <= ref.parameterCount(); ++i) { for (int i = 1; i <= ref.parameterCount(); ++i) {
if (i > 1) { writer.append(",").ws();
writer.append(",").ws();
}
writer.append(variableName(i)); writer.append(variableName(i));
} }
writer.append(");").softNewLine(); writer.append(");").softNewLine();
@ -454,11 +462,18 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.append("\"").append(methodName).append("\""); writer.append("\"").append(methodName).append("\"");
} }
writer.append(",").ws().append("function("); writer.append(",").ws().append("function(");
List<String> args = new ArrayList<>();
for (int i = 1; i <= ref.parameterCount(); ++i) { for (int i = 1; i <= ref.parameterCount(); ++i) {
if (i > 1) { args.add(variableName(i));
}
if (method.isAsync()) {
args.add("$return");
}
for (int i = 0; i < args.size(); ++i) {
if (i > 0) {
writer.append(",").ws(); writer.append(",").ws();
} }
writer.append(variableName(i)); writer.append(args.get(i));
} }
writer.append(")").ws().append("{").ws(); writer.append(")").ws().append("{").ws();
if (ref.getDescriptor().getResultType() != ValueType.VOID) { if (ref.getDescriptor().getResultType() != ValueType.VOID) {
@ -466,8 +481,8 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} }
writer.appendMethodBody(ref).append("("); writer.appendMethodBody(ref).append("(");
writer.append("this"); writer.append("this");
for (int i = 1; i <= ref.parameterCount(); ++i) { for (int i = 0; i < args.size(); ++i) {
writer.append(",").ws().append(variableName(i)); writer.append(",").ws().append(args.get(i));
} }
writer.append(");").ws().append("}"); writer.append(");").ws().append("}");
debugEmitter.emitMethod(null); debugEmitter.emitMethod(null);
@ -1438,7 +1453,6 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
if (expr.getType() == InvocationType.DYNAMIC) { if (expr.getType() == InvocationType.DYNAMIC) {
expr.getArguments().get(0).acceptVisitor(this); expr.getArguments().get(0).acceptVisitor(this);
} }
String className = naming.getNameFor(expr.getMethod().getClassName());
String name = expr.getAsyncTarget() == null ? naming.getNameFor(expr.getMethod()) : String name = expr.getAsyncTarget() == null ? naming.getNameFor(expr.getMethod()) :
naming.getNameForAsync(expr.getMethod()); naming.getNameForAsync(expr.getMethod());
String fullName = naming.getFullNameFor(expr.getMethod()); String fullName = naming.getFullNameFor(expr.getMethod());
@ -1484,7 +1498,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
virtual = true; virtual = true;
break; break;
case CONSTRUCTOR: case CONSTRUCTOR:
writer.append(className).append(".").append(name).append("("); writer.append(naming.getNameForInit(expr.getMethod())).append("(");
prevCallSite = debugEmitter.emitCallSite(); prevCallSite = debugEmitter.emitCallSite();
for (int i = 0; i < expr.getArguments().size(); ++i) { for (int i = 0; i < expr.getArguments().size(); ++i) {
if (i > 0) { if (i > 0) {

View File

@ -377,7 +377,7 @@ function $rt_declClass(cls, data) {
cls.$meta.binaryName = "L" + data.name + ";"; cls.$meta.binaryName = "L" + data.name + ";";
cls.$meta.enum = data.enum; cls.$meta.enum = data.enum;
cls.prototype.constructor = cls; cls.prototype.constructor = cls;
cls.$clinit = data.clinit; cls.$clinit = data.clinit ? data.clinit : function() {};
} }
function $rt_virtualMethods(cls) { function $rt_virtualMethods(cls) {
for (var i = 1; i < arguments.length; i += 2) { for (var i = 1; i < arguments.length; i += 2) {