Generate readable names for method parameters

This commit is contained in:
Alexey Andreev 2015-03-13 19:01:13 +04:00
parent 77863ad6fd
commit 1380e7dbf4
5 changed files with 48 additions and 5 deletions

View File

@ -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,9 +1134,24 @@ 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";
}
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));
@ -1138,6 +1159,15 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
return Character.toString(variableNames.charAt(index % variableNames.length())) +
index / variableNames.length();
}
} else {
List<String> nameList = new ArrayList<>(names);
Collections.sort(nameList);
StringBuilder sb = new StringBuilder();
for (String name : nameList) {
sb.append('_').append(name);
}
return sb.toString();
}
}
private String pointerName() {

View File

@ -41,6 +41,7 @@ public class AsyncMethodNode extends MethodNode {
return variables;
}
@Override
public List<Set<String>> getParameterDebugNames() {
return parameterDebugNames;
}

View File

@ -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();
}

View File

@ -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();
}
}

View File

@ -45,6 +45,7 @@ public class RegularMethodNode extends MethodNode {
return variables;
}
@Override
public List<Set<String>> getParameterDebugNames() {
return parameterDebugNames;
}