mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
Generate readable names for method parameters
This commit is contained in:
parent
77863ad6fd
commit
1380e7dbf4
|
@ -65,6 +65,8 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
|||
private boolean wasGrouped;
|
||||
private Deque<OperatorPrecedence> precedenceStack = new ArrayDeque<>();
|
||||
private Map<String, String> blockIdMap = new HashMap<>();
|
||||
private List<Set<String>> debugNames = new ArrayList<>();
|
||||
private List<String> cachedVariableNames = new ArrayList<>();
|
||||
|
||||
private static class OperatorPrecedence {
|
||||
Priority priority;
|
||||
|
@ -574,6 +576,9 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
|||
}
|
||||
|
||||
public void renderBody(MethodNode method, boolean inner) throws IOException {
|
||||
debugNames.clear();
|
||||
cachedVariableNames.clear();
|
||||
debugNames.addAll(method.getParameterDebugNames());
|
||||
blockIdMap.clear();
|
||||
MethodReference ref = method.getReference();
|
||||
debugEmitter.emitMethod(ref.getDescriptor());
|
||||
|
@ -627,6 +632,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
|||
debugEmitter.emitVariable(method.getParameterDebugNames().get(i).toArray(new String[0]),
|
||||
variableName(i));
|
||||
}
|
||||
|
||||
int variableCount = 0;
|
||||
for (int var : method.getVariables()) {
|
||||
variableCount = Math.max(variableCount, var + 1);
|
||||
|
@ -1128,15 +1134,39 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
|||
}
|
||||
|
||||
public String variableName(int index) {
|
||||
while (index >= cachedVariableNames.size()) {
|
||||
cachedVariableNames.add(null);
|
||||
}
|
||||
String name = cachedVariableNames.get(index);
|
||||
if (name == null) {
|
||||
name = generateVariableName(index);
|
||||
cachedVariableNames.set(index, name);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
private String generateVariableName(int index) {
|
||||
if (index == 0) {
|
||||
return minifying ? "$t" : "$this";
|
||||
}
|
||||
--index;
|
||||
if (index < variableNames.length()) {
|
||||
return Character.toString(variableNames.charAt(index));
|
||||
|
||||
Set<String> names = index < debugNames.size() ? debugNames.get(index) : null;
|
||||
if (minifying || names == null || names.isEmpty()) {
|
||||
--index;
|
||||
if (index < variableNames.length()) {
|
||||
return Character.toString(variableNames.charAt(index));
|
||||
} else {
|
||||
return Character.toString(variableNames.charAt(index % variableNames.length())) +
|
||||
index / variableNames.length();
|
||||
}
|
||||
} else {
|
||||
return Character.toString(variableNames.charAt(index % variableNames.length())) +
|
||||
index / variableNames.length();
|
||||
List<String> nameList = new ArrayList<>(names);
|
||||
Collections.sort(nameList);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String name : nameList) {
|
||||
sb.append('_').append(name);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -41,6 +41,7 @@ public class AsyncMethodNode extends MethodNode {
|
|||
return variables;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Set<String>> getParameterDebugNames() {
|
||||
return parameterDebugNames;
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
package org.teavm.javascript.ast;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import org.teavm.model.MethodReference;
|
||||
|
||||
|
@ -43,4 +44,6 @@ public abstract class MethodNode {
|
|||
public abstract void acceptVisitor(MethodNodeVisitor visitor);
|
||||
|
||||
public abstract boolean isAsync();
|
||||
|
||||
public abstract List<Set<String>> getParameterDebugNames();
|
||||
}
|
||||
|
|
|
@ -15,6 +15,9 @@
|
|||
*/
|
||||
package org.teavm.javascript.ast;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import org.teavm.javascript.spi.Generator;
|
||||
import org.teavm.model.MethodReference;
|
||||
|
||||
|
@ -51,4 +54,9 @@ public class NativeMethodNode extends MethodNode {
|
|||
public void acceptVisitor(MethodNodeVisitor visitor) {
|
||||
visitor.visit(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Set<String>> getParameterDebugNames() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,6 +45,7 @@ public class RegularMethodNode extends MethodNode {
|
|||
return variables;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Set<String>> getParameterDebugNames() {
|
||||
return parameterDebugNames;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user