Reduces amount of generated code

This commit is contained in:
konsoletyper 2014-03-01 18:05:04 +04:00
parent f4fd2d884f
commit 4c36ab9db1
3 changed files with 58 additions and 30 deletions

View File

@ -190,49 +190,49 @@ public class Renderer implements ExprVisitor, StatementVisitor {
.append(constantToString(value)).append(";").softNewLine(); .append(constantToString(value)).append(";").softNewLine();
} }
if (!cls.getModifiers().contains(NodeModifier.INTERFACE)) { writer.append("$rt_declClass(").appendClass(cls.getName()).append(",").ws().append("{")
writer.appendClass(cls.getName()).append(".prototype").ws().append("=").ws().append("new ") .indent().softNewLine();
.append(cls.getParentName() != null ? naming.getNameFor(cls.getParentName()) : writer.append("name").ws().append(":").ws().append("\"").append(escapeString(cls.getName()))
"Object").append("();").softNewLine(); .append("\"");
writer.appendClass(cls.getName()).append(".prototype.constructor").ws().append("=").ws()
.appendClass(cls.getName()).append(';').softNewLine();
}
writer.appendClass(cls.getName()).append(".$meta").ws().append("=").ws().append("{").ws();
writer.append("name").ws().append(":").ws().append("\"").append(cls.getName()).append("\",").ws();
if (cls.getModifiers().contains(NodeModifier.ENUM)) { if (cls.getModifiers().contains(NodeModifier.ENUM)) {
writer.append("enum").ws().append(":").ws().append("true,").ws(); writer.append(",").softNewLine().append("enum").ws().append(":").ws().append("true");
} }
writer.append("supertypes").ws().append(":").ws().append("["); if (!cls.getInterfaces().isEmpty()) {
boolean first = true; writer.append(",").softNewLine().append("interfaces").ws().append(":").ws().append("[");
if (cls.getParentName() != null) { for (int i = 0; i < cls.getInterfaces().size(); ++i) {
writer.appendClass(cls.getParentName()); String iface = cls.getInterfaces().get(i);
first = false; if (i > 0) {
} writer.append(",").ws();
for (String iface : cls.getInterfaces()) { }
if (!first) { writer.appendClass(iface);
writer.append(",").ws();
} }
first = false; writer.append("]");
writer.appendClass(iface);
} }
writer.append("]");
if (cls.getParentName() != null) { if (cls.getParentName() != null) {
writer.append(",").ws(); writer.append(",").softNewLine();
writer.append("superclass").ws().append(":").ws().appendClass(cls.getParentName()); writer.append("superclass").ws().append(":").ws().appendClass(cls.getParentName());
} }
writer.ws().append("};").softNewLine();
if (!cls.getModifiers().contains(NodeModifier.INTERFACE)) { if (!cls.getModifiers().contains(NodeModifier.INTERFACE)) {
writer.appendClass(cls.getName()).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("}").newLine(); .appendClass(cls.getName()).append("_$clinit();").ws().append("}");
}
writer.ws().append("});").softNewLine().outdent();
List<MethodNode> nonInitMethods = new ArrayList<>();
if (!cls.getModifiers().contains(NodeModifier.INTERFACE)) {
writer.append("function ").appendClass(cls.getName()).append("_$clinit()").ws() writer.append("function ").appendClass(cls.getName()).append("_$clinit()").ws()
.append("{").softNewLine().indent(); .append("{").softNewLine().indent();
writer.appendClass(cls.getName()).append("_$clinit").ws().append("=").ws() writer.appendClass(cls.getName()).append("_$clinit").ws().append("=").ws()
.append("function(){};").newLine(); .append("function(){};").newLine();
List<String> stubNames = new ArrayList<>(); List<String> stubNames = new ArrayList<>();
for (MethodNode method : cls.getMethods()) { for (MethodNode method : cls.getMethods()) {
renderBody(method); if (!method.getModifiers().contains(NodeModifier.STATIC) &&
stubNames.add(naming.getFullNameFor(method.getReference())); !method.getReference().getName().equals("<init>")) {
nonInitMethods.add(method);
} else {
renderBody(method, true);
stubNames.add(naming.getFullNameFor(method.getReference()));
}
} }
MethodHolder methodHolder = classSource.get(cls.getName()).getMethod( MethodHolder methodHolder = classSource.get(cls.getName()).getMethod(
new MethodDescriptor("<clinit>", ValueType.VOID)); new MethodDescriptor("<clinit>", ValueType.VOID));
@ -259,6 +259,9 @@ public class Renderer implements ExprVisitor, StatementVisitor {
writer.append("]);").newLine(); writer.append("]);").newLine();
} }
} }
for (MethodNode method : nonInitMethods) {
renderBody(method, false);
}
} catch (NamingException e) { } catch (NamingException e) {
throw new RenderingException("Error rendering class " + cls.getName() + ". See a cause for details", e); throw new RenderingException("Error rendering class " + cls.getName() + ". See a cause for details", e);
} catch (IOException e) { } catch (IOException e) {
@ -381,9 +384,13 @@ public class Renderer implements ExprVisitor, StatementVisitor {
} }
} }
public void renderBody(MethodNode method) throws IOException { public void renderBody(MethodNode method, boolean inner) throws IOException {
MethodReference ref = method.getReference(); MethodReference ref = method.getReference();
writer.appendMethodBody(ref).ws().append("=").ws().append("function("); if (inner) {
writer.appendMethodBody(ref).ws().append("=").ws().append("function(");
} else {
writer.append("function ").appendMethodBody(ref).append("(");
}
int startParam = 0; int startParam = 0;
if (method.getModifiers().contains(NodeModifier.STATIC)) { if (method.getModifiers().contains(NodeModifier.STATIC)) {
startParam = 1; startParam = 1;

View File

@ -121,6 +121,11 @@ class ClassRefsRenamer implements InstructionVisitor {
for (Instruction insn : basicBlock.getInstructions()) { for (Instruction insn : basicBlock.getInstructions()) {
insn.acceptVisitor(this); insn.acceptVisitor(this);
} }
for (TryCatchBlock tryCatch : basicBlock.getTryCatchBlocks()) {
if (tryCatch.getExceptionType() != null) {
tryCatch.setExceptionType(classNameMapper.map(tryCatch.getExceptionType()));
}
}
} }
} }

View File

@ -371,6 +371,22 @@ $rt_putStderr = function(ch) {
$rt_stderrBuffer += String.fromCharCode(ch); $rt_stderrBuffer += String.fromCharCode(ch);
} }
} }
function $rt_declClass(cls, data) {
cls.name = data.name;
cls.$meta = {};
cls.$meta.superclass = data.superclass;
cls.$meta.supertypes = data.interfaces ? data.interfaces.slice() : [];
if (data.superclass) {
cls.$meta.supertypes.push(data.superclass);
cls.prototype = new data.superclass();
} else {
cls.prototype = new Object();
}
cls.$meta.name = data.name;
cls.$meta.enum = data.enum;
cls.prototype.constructor = cls;
cls.$clinit = data.clinit;
}
Long = function(lo, hi) { Long = function(lo, hi) {
this.lo = lo | 0; this.lo = lo | 0;