mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 16:14:10 -08:00
This commit is contained in:
parent
125ccf48e9
commit
a3f60996e2
|
@ -53,8 +53,7 @@
|
||||||
<module name="teavm-tooling" />
|
<module name="teavm-tooling" />
|
||||||
</profile>
|
</profile>
|
||||||
</annotationProcessing>
|
</annotationProcessing>
|
||||||
<bytecodeTargetLevel>
|
<bytecodeTargetLevel target="1.8">
|
||||||
<module name="impl" target="1.8" />
|
|
||||||
<module name="teavm" target="1.5" />
|
<module name="teavm" target="1.5" />
|
||||||
<module name="teavm-chrome-rdp" target="1.8" />
|
<module name="teavm-chrome-rdp" target="1.8" />
|
||||||
<module name="teavm-classlib" target="1.8" />
|
<module name="teavm-classlib" target="1.8" />
|
||||||
|
|
|
@ -126,6 +126,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
||||||
private final List<String> cachedVariableNames = new ArrayList<>();
|
private final List<String> cachedVariableNames = new ArrayList<>();
|
||||||
private boolean end;
|
private boolean end;
|
||||||
private int currentPart;
|
private int currentPart;
|
||||||
|
private List<PostponedFieldInitializer> postponedFieldInitializers = new ArrayList<>();
|
||||||
|
|
||||||
private static class InjectorHolder {
|
private static class InjectorHolder {
|
||||||
public final Injector injector;
|
public final Injector injector;
|
||||||
|
@ -227,6 +228,17 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void renderStringConstants() throws RenderingException {
|
||||||
|
try {
|
||||||
|
for (PostponedFieldInitializer initializer : postponedFieldInitializers) {
|
||||||
|
writer.appendStaticField(initializer.field).ws().append("=").ws()
|
||||||
|
.append(constantToString(initializer.value)).append(";").softNewLine();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RenderingException("IO error", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void renderRuntime() throws RenderingException {
|
public void renderRuntime() throws RenderingException {
|
||||||
try {
|
try {
|
||||||
renderRuntimeCls();
|
renderRuntimeCls();
|
||||||
|
@ -403,6 +415,11 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
||||||
value = getDefaultValue(field.getType());
|
value = getDefaultValue(field.getType());
|
||||||
}
|
}
|
||||||
FieldReference fieldRef = new FieldReference(cls.getName(), field.getName());
|
FieldReference fieldRef = new FieldReference(cls.getName(), field.getName());
|
||||||
|
if (value instanceof String) {
|
||||||
|
constantToString(value);
|
||||||
|
postponedFieldInitializers.add(new PostponedFieldInitializer(fieldRef, (String) value));
|
||||||
|
value = null;
|
||||||
|
}
|
||||||
writer.append("var ").appendStaticField(fieldRef).ws().append("=").ws()
|
writer.append("var ").appendStaticField(fieldRef).ws().append("=").ws()
|
||||||
.append(constantToString(value)).append(";").softNewLine();
|
.append(constantToString(value)).append(";").softNewLine();
|
||||||
}
|
}
|
||||||
|
@ -2391,4 +2408,14 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
||||||
public <T> T getService(Class<T> type) {
|
public <T> T getService(Class<T> type) {
|
||||||
return services.getService(type);
|
return services.getService(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class PostponedFieldInitializer {
|
||||||
|
FieldReference field;
|
||||||
|
String value;
|
||||||
|
|
||||||
|
public PostponedFieldInitializer(FieldReference field, String value) {
|
||||||
|
this.field = field;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -513,6 +513,7 @@ public class TeaVM implements TeaVMHost, ServiceRepository {
|
||||||
renderer.renderRuntime();
|
renderer.renderRuntime();
|
||||||
renderer.render(clsNodes);
|
renderer.render(clsNodes);
|
||||||
renderer.renderStringPool();
|
renderer.renderStringPool();
|
||||||
|
renderer.renderStringConstants();
|
||||||
for (Map.Entry<String, TeaVMEntryPoint> entry : entryPoints.entrySet()) {
|
for (Map.Entry<String, TeaVMEntryPoint> entry : entryPoints.entrySet()) {
|
||||||
sourceWriter.append("var ").append(entry.getKey()).ws().append("=").ws();
|
sourceWriter.append("var ").append(entry.getKey()).ws().append("=").ws();
|
||||||
MethodReference ref = entry.getValue().reference;
|
MethodReference ref = entry.getValue().reference;
|
||||||
|
|
|
@ -21,10 +21,6 @@ import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.teavm.junit.TeaVMTestRunner;
|
import org.teavm.junit.TeaVMTestRunner;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @author Alexey Andreev
|
|
||||||
*/
|
|
||||||
@RunWith(TeaVMTestRunner.class)
|
@RunWith(TeaVMTestRunner.class)
|
||||||
public class VMTest {
|
public class VMTest {
|
||||||
@Test
|
@Test
|
||||||
|
@ -115,6 +111,24 @@ public class VMTest {
|
||||||
assertNotNull(obj.getValue());
|
assertNotNull(obj.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// See https://github.com/konsoletyper/teavm/issues/196
|
||||||
|
@Test
|
||||||
|
public void stringConstantsInitializedProperly() {
|
||||||
|
assertEquals("FIRST ", ClassWithStaticField.foo(true));
|
||||||
|
assertEquals("SECOND ", ClassWithStaticField.foo(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ClassWithStaticField {
|
||||||
|
public final static String CONST1 = "FIRST";
|
||||||
|
public final static String CONST2 = "SECOND";
|
||||||
|
|
||||||
|
public static String foo(boolean value) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append(value ? CONST1 : CONST2).append(" ");
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static class SuperClass {
|
static class SuperClass {
|
||||||
static final Integer ONE = new Integer(1);
|
static final Integer ONE = new Integer(1);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user