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 boolean wasGrouped;
private Deque<OperatorPrecedence> precedenceStack = new ArrayDeque<>(); private Deque<OperatorPrecedence> precedenceStack = new ArrayDeque<>();
private Map<String, String> blockIdMap = new HashMap<>(); private Map<String, String> blockIdMap = new HashMap<>();
private List<Set<String>> debugNames = new ArrayList<>();
private List<String> cachedVariableNames = new ArrayList<>();
private static class OperatorPrecedence { private static class OperatorPrecedence {
Priority priority; Priority priority;
@ -574,6 +576,9 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} }
public void renderBody(MethodNode method, boolean inner) throws IOException { public void renderBody(MethodNode method, boolean inner) throws IOException {
debugNames.clear();
cachedVariableNames.clear();
debugNames.addAll(method.getParameterDebugNames());
blockIdMap.clear(); blockIdMap.clear();
MethodReference ref = method.getReference(); MethodReference ref = method.getReference();
debugEmitter.emitMethod(ref.getDescriptor()); 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]), debugEmitter.emitVariable(method.getParameterDebugNames().get(i).toArray(new String[0]),
variableName(i)); variableName(i));
} }
int variableCount = 0; int variableCount = 0;
for (int var : method.getVariables()) { for (int var : method.getVariables()) {
variableCount = Math.max(variableCount, var + 1); variableCount = Math.max(variableCount, var + 1);
@ -1128,15 +1134,39 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} }
public String variableName(int index) { 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) { if (index == 0) {
return minifying ? "$t" : "$this"; return minifying ? "$t" : "$this";
} }
--index;
if (index < variableNames.length()) { Set<String> names = index < debugNames.size() ? debugNames.get(index) : null;
return Character.toString(variableNames.charAt(index)); 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 { } else {
return Character.toString(variableNames.charAt(index % variableNames.length())) + List<String> nameList = new ArrayList<>(names);
index / variableNames.length(); Collections.sort(nameList);
StringBuilder sb = new StringBuilder();
for (String name : nameList) {
sb.append('_').append(name);
}
return sb.toString();
} }
} }

View File

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

View File

@ -16,6 +16,7 @@
package org.teavm.javascript.ast; package org.teavm.javascript.ast;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List;
import java.util.Set; import java.util.Set;
import org.teavm.model.MethodReference; import org.teavm.model.MethodReference;
@ -43,4 +44,6 @@ public abstract class MethodNode {
public abstract void acceptVisitor(MethodNodeVisitor visitor); public abstract void acceptVisitor(MethodNodeVisitor visitor);
public abstract boolean isAsync(); public abstract boolean isAsync();
public abstract List<Set<String>> getParameterDebugNames();
} }

View File

@ -15,6 +15,9 @@
*/ */
package org.teavm.javascript.ast; 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.javascript.spi.Generator;
import org.teavm.model.MethodReference; import org.teavm.model.MethodReference;
@ -51,4 +54,9 @@ public class NativeMethodNode extends MethodNode {
public void acceptVisitor(MethodNodeVisitor visitor) { public void acceptVisitor(MethodNodeVisitor visitor) {
visitor.visit(this); 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; return variables;
} }
@Override
public List<Set<String>> getParameterDebugNames() { public List<Set<String>> getParameterDebugNames() {
return parameterDebugNames; return parameterDebugNames;
} }