Adds support of JVM class initialization

This commit is contained in:
konsoletyper 2013-12-09 22:04:04 +04:00
parent d948d8d8b0
commit 82ef14fc07

View File

@ -154,8 +154,21 @@ public class Renderer implements ExprVisitor, StatementVisitor {
} }
writer.append("]"); writer.append("]");
writer.append(" };").newLine(); writer.append(" };").newLine();
writer.appendClass(cls.getName()).append(".clinit = function() {").newLine().indent();
writer.appendClass(cls.getName()).append(".clinit = null;").newLine();
for (MethodNode method : cls.getMethods()) { for (MethodNode method : cls.getMethods()) {
render(method); renderBody(method);
}
MethodHolder methodHolder = classSource.getClassHolder(cls.getName()).getMethod(
new MethodDescriptor("<clinit>", ValueType.VOID));
if (methodHolder != null) {
writer.appendClass(cls.getName()).append("_").appendMethod(new MethodReference(
cls.getName(), methodHolder.getDescriptor())).append("();").newLine();
}
writer.outdent().append("}").newLine();
for (MethodNode method : cls.getMethods()) {
renderDeclaration(method);
renderStub(method);
} }
} 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);
@ -210,13 +223,12 @@ public class Renderer implements ExprVisitor, StatementVisitor {
writer.outdent().append("}").newLine(); writer.outdent().append("}").newLine();
} }
public void render(MethodNode method) throws RenderingException { public void renderDeclaration(MethodNode method) throws RenderingException {
try { try {
MethodReference ref = method.getReference(); MethodReference ref = method.getReference();
if (ref.getDescriptor().getName().equals("<init>")) { if (ref.getDescriptor().getName().equals("<init>")) {
renderInitializer(method); renderInitializer(method);
} }
renderWorkingMethod(method);
int startParam = 0; int startParam = 0;
if (method.getModifiers().contains(NodeModifier.STATIC)) { if (method.getModifiers().contains(NodeModifier.STATIC)) {
startParam = 1; startParam = 1;
@ -251,9 +263,34 @@ public class Renderer implements ExprVisitor, StatementVisitor {
} }
} }
private void renderWorkingMethod(MethodNode method) { public void renderStub(MethodNode method) {
MethodReference ref = method.getReference(); MethodReference ref = method.getReference();
writer.append("function ").appendClass(ref.getClassName()).append('_').appendMethod(ref).append('('); writer.appendClass(ref.getClassName()).append('_').appendMethod(ref).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(variableName(i));
}
String owner = ref.getClassName();
writer.append(") { ").appendClass(owner).append(".clinit(); ").append("return ").appendClass(owner)
.append("_").appendMethod(ref).append("(");
for (int i = startParam; i <= ref.parameterCount(); ++i) {
if (i > startParam) {
writer.append(", ");
}
writer.append(variableName(i));
}
writer.append("); };").newLine();
}
public void renderBody(MethodNode method) {
MethodReference ref = method.getReference();
writer.appendClass(ref.getClassName()).append('_').appendMethod(ref).append(" = function(");
int startParam = 0; int startParam = 0;
if (method.getModifiers().contains(NodeModifier.STATIC)) { if (method.getModifiers().contains(NodeModifier.STATIC)) {
startParam = 1; startParam = 1;