diff --git a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java index 0e5d7618a..e9f5544a4 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java +++ b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java @@ -65,6 +65,8 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext private boolean wasGrouped; private Deque precedenceStack = new ArrayDeque<>(); private Map blockIdMap = new HashMap<>(); + private List> debugNames = new ArrayList<>(); + private List 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 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 nameList = new ArrayList<>(names); + Collections.sort(nameList); + StringBuilder sb = new StringBuilder(); + for (String name : nameList) { + sb.append('_').append(name); + } + return sb.toString(); } } diff --git a/teavm-core/src/main/java/org/teavm/javascript/ast/AsyncMethodNode.java b/teavm-core/src/main/java/org/teavm/javascript/ast/AsyncMethodNode.java index bdeb269e3..d15b2a2e6 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/ast/AsyncMethodNode.java +++ b/teavm-core/src/main/java/org/teavm/javascript/ast/AsyncMethodNode.java @@ -41,6 +41,7 @@ public class AsyncMethodNode extends MethodNode { return variables; } + @Override public List> getParameterDebugNames() { return parameterDebugNames; } diff --git a/teavm-core/src/main/java/org/teavm/javascript/ast/MethodNode.java b/teavm-core/src/main/java/org/teavm/javascript/ast/MethodNode.java index 53bb6356f..7186fdbe4 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/ast/MethodNode.java +++ b/teavm-core/src/main/java/org/teavm/javascript/ast/MethodNode.java @@ -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> getParameterDebugNames(); } diff --git a/teavm-core/src/main/java/org/teavm/javascript/ast/NativeMethodNode.java b/teavm-core/src/main/java/org/teavm/javascript/ast/NativeMethodNode.java index 4b7d4b83d..f973e7b2c 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/ast/NativeMethodNode.java +++ b/teavm-core/src/main/java/org/teavm/javascript/ast/NativeMethodNode.java @@ -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> getParameterDebugNames() { + return Collections.emptyList(); + } } diff --git a/teavm-core/src/main/java/org/teavm/javascript/ast/RegularMethodNode.java b/teavm-core/src/main/java/org/teavm/javascript/ast/RegularMethodNode.java index f532b4892..2bc132ac4 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/ast/RegularMethodNode.java +++ b/teavm-core/src/main/java/org/teavm/javascript/ast/RegularMethodNode.java @@ -45,6 +45,7 @@ public class RegularMethodNode extends MethodNode { return variables; } + @Override public List> getParameterDebugNames() { return parameterDebugNames; }