mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 16:14:10 -08:00
Change translation of <clinit> invocations
This commit is contained in:
parent
764c9bbb1e
commit
c023ac2053
|
@ -348,26 +348,14 @@ public class Renderer implements RenderingManager {
|
||||||
private void renderMethodBodies(ClassNode cls) throws RenderingException {
|
private void renderMethodBodies(ClassNode cls) throws RenderingException {
|
||||||
debugEmitter.emitClass(cls.getName());
|
debugEmitter.emitClass(cls.getName());
|
||||||
try {
|
try {
|
||||||
List<MethodNode> nonInitMethods = new ArrayList<>();
|
|
||||||
MethodReader clinit = classSource.get(cls.getName()).getMethod(
|
MethodReader clinit = classSource.get(cls.getName()).getMethod(
|
||||||
new MethodDescriptor("<clinit>", ValueType.VOID));
|
new MethodDescriptor("<clinit>", ValueType.VOID));
|
||||||
boolean needsClinit = clinit != null;
|
|
||||||
List<MethodNode> clinitMethods = new ArrayList<>();
|
|
||||||
for (MethodNode method : cls.getMethods()) {
|
|
||||||
if (needsClinit && (method.getModifiers().contains(ElementModifier.STATIC)
|
|
||||||
|| method.getReference().getName().equals("<init>"))) {
|
|
||||||
clinitMethods.add(method);
|
|
||||||
} else {
|
|
||||||
nonInitMethods.add(method);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (needsClinit) {
|
if (clinit != null) {
|
||||||
renderCallClinit(clinit, cls, clinitMethods);
|
renderCallClinit(clinit, cls);
|
||||||
}
|
}
|
||||||
if (!cls.getModifiers().contains(ElementModifier.INTERFACE)) {
|
if (!cls.getModifiers().contains(ElementModifier.INTERFACE)) {
|
||||||
for (MethodNode method : cls.getMethods()) {
|
for (MethodNode method : cls.getMethods()) {
|
||||||
cls.getMethods();
|
|
||||||
if (!method.getModifiers().contains(ElementModifier.STATIC)) {
|
if (!method.getModifiers().contains(ElementModifier.STATIC)) {
|
||||||
if (method.getReference().getName().equals("<init>")) {
|
if (method.getReference().getName().equals("<init>")) {
|
||||||
renderInitializer(method);
|
renderInitializer(method);
|
||||||
|
@ -376,8 +364,8 @@ public class Renderer implements RenderingManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (MethodNode method : nonInitMethods) {
|
for (MethodNode method : cls.getMethods()) {
|
||||||
renderBody(method, false);
|
renderBody(method, clinit != null);
|
||||||
}
|
}
|
||||||
} 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);
|
||||||
|
@ -387,7 +375,7 @@ public class Renderer implements RenderingManager {
|
||||||
debugEmitter.emitClass(null);
|
debugEmitter.emitClass(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void renderCallClinit(MethodReader clinit, ClassNode cls, List<MethodNode> clinitMethods)
|
private void renderCallClinit(MethodReader clinit, ClassNode cls)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
boolean isAsync = asyncMethods.contains(clinit.getReference());
|
boolean isAsync = asyncMethods.contains(clinit.getReference());
|
||||||
|
|
||||||
|
@ -419,9 +407,6 @@ public class Renderer implements RenderingManager {
|
||||||
.softNewLine();
|
.softNewLine();
|
||||||
} else {
|
} else {
|
||||||
renderEraseClinit(cls);
|
renderEraseClinit(cls);
|
||||||
for (MethodNode method : clinitMethods) {
|
|
||||||
renderBody(method, true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isAsync) {
|
if (isAsync) {
|
||||||
|
@ -492,30 +477,12 @@ public class Renderer implements RenderingManager {
|
||||||
}
|
}
|
||||||
writer.append(',').ws();
|
writer.append(',').ws();
|
||||||
|
|
||||||
List<String> stubNames = new ArrayList<>();
|
|
||||||
List<MethodNode> virtualMethods = new ArrayList<>();
|
List<MethodNode> virtualMethods = new ArrayList<>();
|
||||||
for (MethodNode method : cls.getMethods()) {
|
for (MethodNode method : cls.getMethods()) {
|
||||||
if (clinit != null && (method.getModifiers().contains(ElementModifier.STATIC)
|
|
||||||
|| method.getReference().getName().equals("<init>"))) {
|
|
||||||
stubNames.add(naming.getFullNameFor(method.getReference()));
|
|
||||||
}
|
|
||||||
if (!method.getModifiers().contains(ElementModifier.STATIC)) {
|
if (!method.getModifiers().contains(ElementModifier.STATIC)) {
|
||||||
virtualMethods.add(method);
|
virtualMethods.add(method);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (stubNames.size() == 1) {
|
|
||||||
writer.append("'").append(stubNames.get(0)).append("'");
|
|
||||||
} else {
|
|
||||||
writer.append('[');
|
|
||||||
for (int j = 0; j < stubNames.size(); ++j) {
|
|
||||||
if (j > 0) {
|
|
||||||
writer.append(",").ws();
|
|
||||||
}
|
|
||||||
writer.append("'").append(stubNames.get(j)).append("'");
|
|
||||||
}
|
|
||||||
writer.append(']');
|
|
||||||
}
|
|
||||||
writer.append(',').ws();
|
|
||||||
|
|
||||||
renderVirtualDeclarations(virtualMethods);
|
renderVirtualDeclarations(virtualMethods);
|
||||||
}
|
}
|
||||||
|
@ -622,18 +589,19 @@ public class Renderer implements RenderingManager {
|
||||||
writer.append(");").ws().append("}");
|
writer.append(");").ws().append("}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void renderBody(MethodNode method, boolean inner) throws IOException {
|
private void renderBody(MethodNode method, boolean clinitNeeded) throws IOException {
|
||||||
|
boolean isClinit = (method.getModifiers().contains(ElementModifier.STATIC)
|
||||||
|
&& !method.getReference().getName().equals("<clinit>"))
|
||||||
|
|| method.getReference().getName().equals("<init>");
|
||||||
|
|
||||||
StatementRenderer statementRenderer = new StatementRenderer(context, writer);
|
StatementRenderer statementRenderer = new StatementRenderer(context, writer);
|
||||||
statementRenderer.setCurrentMethod(method);
|
statementRenderer.setCurrentMethod(method);
|
||||||
|
|
||||||
MethodReference ref = method.getReference();
|
MethodReference ref = method.getReference();
|
||||||
debugEmitter.emitMethod(ref.getDescriptor());
|
debugEmitter.emitMethod(ref.getDescriptor());
|
||||||
String name = naming.getFullNameFor(ref);
|
String name = naming.getFullNameFor(ref);
|
||||||
if (inner) {
|
|
||||||
writer.append(name).ws().append("=").ws().append("function(");
|
writer.append("function ").append(name).append("(");
|
||||||
} else {
|
|
||||||
writer.append("function ").append(name).append("(");
|
|
||||||
}
|
|
||||||
int startParam = 0;
|
int startParam = 0;
|
||||||
if (method.getModifiers().contains(ElementModifier.STATIC)) {
|
if (method.getModifiers().contains(ElementModifier.STATIC)) {
|
||||||
startParam = 1;
|
startParam = 1;
|
||||||
|
@ -645,11 +613,13 @@ public class Renderer implements RenderingManager {
|
||||||
writer.append(statementRenderer.variableName(i));
|
writer.append(statementRenderer.variableName(i));
|
||||||
}
|
}
|
||||||
writer.append(")").ws().append("{").softNewLine().indent();
|
writer.append(")").ws().append("{").softNewLine().indent();
|
||||||
|
|
||||||
|
if (isClinit & clinitNeeded) {
|
||||||
|
writer.appendClass(method.getReference().getClassName()).append("_$callClinit();").softNewLine();
|
||||||
|
}
|
||||||
method.acceptVisitor(new MethodBodyRenderer(statementRenderer));
|
method.acceptVisitor(new MethodBodyRenderer(statementRenderer));
|
||||||
writer.outdent().append("}");
|
writer.outdent().append("}");
|
||||||
if (inner) {
|
|
||||||
writer.append(';');
|
|
||||||
}
|
|
||||||
writer.newLine();
|
writer.newLine();
|
||||||
debugEmitter.emitMethod(null);
|
debugEmitter.emitMethod(null);
|
||||||
}
|
}
|
||||||
|
|
|
@ -375,7 +375,7 @@ function $rt_putStderr(ch) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function $rt_metadata(data) {
|
function $rt_metadata(data) {
|
||||||
for (var i = 0; i < data.length; i += 8) {
|
for (var i = 0; i < data.length; i += 7) {
|
||||||
var cls = data[i];
|
var cls = data[i];
|
||||||
cls.$meta = {};
|
cls.$meta = {};
|
||||||
var m = cls.$meta;
|
var m = cls.$meta;
|
||||||
|
@ -399,38 +399,8 @@ function $rt_metadata(data) {
|
||||||
var clinit = data[i + 5];
|
var clinit = data[i + 5];
|
||||||
cls.$clinit = clinit !== 0 ? clinit : function() {};
|
cls.$clinit = clinit !== 0 ? clinit : function() {};
|
||||||
|
|
||||||
var names = data[i + 6];
|
var virtualMethods = data[i + 6];
|
||||||
if (!(names instanceof Array)) {
|
for (var j = 0; j < virtualMethods.length; j += 2) {
|
||||||
names = [names];
|
|
||||||
}
|
|
||||||
for (var j = 0; j < names.length; j = (j + 1) | 0) {
|
|
||||||
window[names[j]] = (function(cls, name) {
|
|
||||||
return function() {
|
|
||||||
var ptr = 0;
|
|
||||||
if ($rt_resuming()) {
|
|
||||||
ptr = $rt_nativeThread().pop();
|
|
||||||
}
|
|
||||||
main: while (true) {
|
|
||||||
switch (ptr) {
|
|
||||||
case 0:
|
|
||||||
clinit = cls.$clinit;
|
|
||||||
clinit();
|
|
||||||
ptr = 1;
|
|
||||||
case 1:
|
|
||||||
var result = window[name].apply(window, arguments);
|
|
||||||
if ($rt_suspend()) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$rt_nativeThread().push(ptr);
|
|
||||||
}
|
|
||||||
})(cls, names[j]);
|
|
||||||
}
|
|
||||||
|
|
||||||
var virtualMethods = data[i + 7];
|
|
||||||
for (j = 0; j < virtualMethods.length; j += 2) {
|
|
||||||
var name = virtualMethods[j];
|
var name = virtualMethods[j];
|
||||||
var func = virtualMethods[j + 1];
|
var func = virtualMethods[j + 1];
|
||||||
if (typeof name === 'string') {
|
if (typeof name === 'string') {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user