mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2025-01-23 23:04:50 -08:00
JS: reduce size of declaration of classes without fields
This commit is contained in:
parent
c1891a1908
commit
146776ff55
|
@ -129,6 +129,17 @@ class NameFrequencyEstimator extends RecursiveVisitor implements MethodNodeVisit
|
||||||
for (String iface : cls.getClassHolder().getInterfaces()) {
|
for (String iface : cls.getClassHolder().getInterfaces()) {
|
||||||
consumer.consume(iface);
|
consumer.consume(iface);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean hasFields = false;
|
||||||
|
for (FieldHolder field : cls.getClassHolder().getFields()) {
|
||||||
|
if (!field.hasModifier(ElementModifier.STATIC)) {
|
||||||
|
hasFields = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!hasFields) {
|
||||||
|
consumer.consumeFunction("$rt_classWithoutFields");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -252,7 +252,8 @@ public class Renderer implements RenderingManager {
|
||||||
"$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_wrapException", "$rt_checkBounds",
|
"$rt_s", "$rt_eraseClinit", "$rt_imul", "$rt_wrapException", "$rt_checkBounds",
|
||||||
"$rt_checkUpperBound", "$rt_checkLowerBound", "$rt_wrapFunction0", "$rt_wrapFunction1",
|
"$rt_checkUpperBound", "$rt_checkLowerBound", "$rt_wrapFunction0", "$rt_wrapFunction1",
|
||||||
"$rt_wrapFunction2", "$rt_wrapFunction3", "$rt_wrapFunction4" };
|
"$rt_wrapFunction2", "$rt_wrapFunction3", "$rt_wrapFunction4",
|
||||||
|
"$rt_classWithoutFields" };
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
for (String name : names) {
|
for (String name : names) {
|
||||||
if (!first) {
|
if (!first) {
|
||||||
|
@ -303,10 +304,6 @@ public class Renderer implements RenderingManager {
|
||||||
ScopedName jsName = naming.getNameFor(cls.getName());
|
ScopedName jsName = naming.getNameFor(cls.getName());
|
||||||
debugEmitter.addClass(jsName.value, cls.getName(), cls.getParentName());
|
debugEmitter.addClass(jsName.value, cls.getName(), cls.getParentName());
|
||||||
try {
|
try {
|
||||||
renderFunctionDeclaration(jsName);
|
|
||||||
writer.append("()").ws().append("{")
|
|
||||||
.indent().softNewLine();
|
|
||||||
boolean thisAliased = false;
|
|
||||||
List<FieldHolder> nonStaticFields = new ArrayList<>();
|
List<FieldHolder> nonStaticFields = new ArrayList<>();
|
||||||
List<FieldHolder> staticFields = new ArrayList<>();
|
List<FieldHolder> staticFields = new ArrayList<>();
|
||||||
for (FieldHolder field : cls.getClassHolder().getFields()) {
|
for (FieldHolder field : cls.getClassHolder().getFields()) {
|
||||||
|
@ -316,38 +313,13 @@ public class Renderer implements RenderingManager {
|
||||||
nonStaticFields.add(field);
|
nonStaticFields.add(field);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (nonStaticFields.size() > 1) {
|
|
||||||
thisAliased = true;
|
|
||||||
writer.append("var a").ws().append("=").ws().append("this;").ws();
|
|
||||||
}
|
|
||||||
if (!cls.getClassHolder().getModifiers().contains(ElementModifier.INTERFACE)
|
|
||||||
&& cls.getParentName() != null) {
|
|
||||||
writer.appendClass(cls.getParentName()).append(".call(").append(thisAliased ? "a" : "this")
|
|
||||||
.append(");").softNewLine();
|
|
||||||
}
|
|
||||||
for (FieldHolder field : nonStaticFields) {
|
|
||||||
Object value = field.getInitialValue();
|
|
||||||
if (value == null) {
|
|
||||||
value = getDefaultValue(field.getType());
|
|
||||||
}
|
|
||||||
FieldReference fieldRef = new FieldReference(cls.getName(), field.getName());
|
|
||||||
writer.append(thisAliased ? "a" : "this").append(".").appendField(fieldRef).ws()
|
|
||||||
.append("=").ws();
|
|
||||||
context.constantToString(writer, value);
|
|
||||||
writer.append(";").softNewLine();
|
|
||||||
debugEmitter.addField(field.getName(), naming.getNameFor(fieldRef));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cls.getName().equals("java.lang.Object")) {
|
if (nonStaticFields.isEmpty() && !cls.getClassHolder().getName().equals("java.lang.Object")) {
|
||||||
writer.append("this.$id$").ws().append('=').ws().append("0;").softNewLine();
|
renderShortClassFunctionDeclaration(cls, jsName);
|
||||||
|
} else {
|
||||||
|
renderFullClassFunctionDeclaration(cls, jsName, nonStaticFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.outdent().append("}");
|
|
||||||
if (jsName.scoped) {
|
|
||||||
writer.append(";");
|
|
||||||
}
|
|
||||||
writer.newLine();
|
|
||||||
|
|
||||||
for (FieldHolder field : staticFields) {
|
for (FieldHolder field : staticFields) {
|
||||||
Object value = field.getInitialValue();
|
Object value = field.getInitialValue();
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
|
@ -375,6 +347,59 @@ public class Renderer implements RenderingManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void renderFullClassFunctionDeclaration(PreparedClass cls, ScopedName jsName,
|
||||||
|
List<FieldHolder> nonStaticFields) throws IOException {
|
||||||
|
boolean thisAliased = false;
|
||||||
|
renderFunctionDeclaration(jsName);
|
||||||
|
writer.append("()").ws().append("{").indent().softNewLine();
|
||||||
|
if (nonStaticFields.size() > 1) {
|
||||||
|
thisAliased = true;
|
||||||
|
writer.append("var a").ws().append("=").ws().append("this;").ws();
|
||||||
|
}
|
||||||
|
if (!cls.getClassHolder().getModifiers().contains(ElementModifier.INTERFACE)
|
||||||
|
&& cls.getParentName() != null) {
|
||||||
|
writer.appendClass(cls.getParentName()).append(".call(").append(thisAliased ? "a" : "this")
|
||||||
|
.append(");").softNewLine();
|
||||||
|
}
|
||||||
|
for (FieldHolder field : nonStaticFields) {
|
||||||
|
Object value = field.getInitialValue();
|
||||||
|
if (value == null) {
|
||||||
|
value = getDefaultValue(field.getType());
|
||||||
|
}
|
||||||
|
FieldReference fieldRef = new FieldReference(cls.getName(), field.getName());
|
||||||
|
writer.append(thisAliased ? "a" : "this").append(".").appendField(fieldRef).ws()
|
||||||
|
.append("=").ws();
|
||||||
|
context.constantToString(writer, value);
|
||||||
|
writer.append(";").softNewLine();
|
||||||
|
debugEmitter.addField(field.getName(), naming.getNameFor(fieldRef));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cls.getName().equals("java.lang.Object")) {
|
||||||
|
writer.append("this.$id$").ws().append('=').ws().append("0;").softNewLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.outdent().append("}");
|
||||||
|
if (jsName.scoped) {
|
||||||
|
writer.append(";");
|
||||||
|
}
|
||||||
|
writer.newLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void renderShortClassFunctionDeclaration(PreparedClass cls, ScopedName jsName) throws IOException {
|
||||||
|
if (jsName.scoped) {
|
||||||
|
writer.append(naming.getScopeName()).append(".");
|
||||||
|
} else {
|
||||||
|
writer.append("var ");
|
||||||
|
}
|
||||||
|
writer.append(jsName.value).ws().append("=").ws().appendFunction("$rt_classWithoutFields").append("(");
|
||||||
|
if (cls.getClassHolder().hasModifier(ElementModifier.INTERFACE)) {
|
||||||
|
writer.append("0");
|
||||||
|
} else if (!cls.getParentName().equals("java.lang.Object")) {
|
||||||
|
writer.appendClass(cls.getParentName());
|
||||||
|
}
|
||||||
|
writer.append(");").newLine();
|
||||||
|
}
|
||||||
|
|
||||||
private void renderMethodBodies(PreparedClass cls) throws RenderingException {
|
private void renderMethodBodies(PreparedClass cls) throws RenderingException {
|
||||||
debugEmitter.emitClass(cls.getName());
|
debugEmitter.emitClass(cls.getName());
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -699,4 +699,15 @@ function $rt_checkLowerBound(index) {
|
||||||
$rt_throwAIOOBE();
|
$rt_throwAIOOBE();
|
||||||
}
|
}
|
||||||
return index;
|
return index;
|
||||||
|
}
|
||||||
|
function $rt_classWithoutFields(superclass) {
|
||||||
|
if (superclass === 0) {
|
||||||
|
return function() {};
|
||||||
|
}
|
||||||
|
if (superclass === void 0) {
|
||||||
|
superclass = $rt_objcls();
|
||||||
|
}
|
||||||
|
return function() {
|
||||||
|
superclass.call(this);
|
||||||
|
};
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user