From 6543e68f8aa4a9f14ba8355db694cda73cd40eec Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 24 Nov 2023 17:18:52 +0100 Subject: [PATCH] JS: fix naming of variables in no-obfuscation mode --- .../rendering/MethodBodyRenderer.java | 6 +++++- .../javascript/rendering/Renderer.java | 11 +++++++++-- .../rendering/StatementRenderer.java | 1 + .../rendering/VariableNameGenerator.java | 19 +++++++++++++++---- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/MethodBodyRenderer.java b/core/src/main/java/org/teavm/backend/javascript/rendering/MethodBodyRenderer.java index 6d9b983c9..facccfab8 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/MethodBodyRenderer.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/MethodBodyRenderer.java @@ -56,6 +56,10 @@ public class MethodBodyRenderer implements MethodNodeVisitor, GeneratorContext { statementRenderer = new StatementRenderer(context, writer); } + public void setCurrentMethod(MethodNode node) { + statementRenderer.setCurrentMethod(node); + } + public boolean isThreadLibraryUsed() { return threadLibraryUsed; } @@ -76,9 +80,9 @@ public class MethodBodyRenderer implements MethodNodeVisitor, GeneratorContext { threadLibraryUsed = false; this.async = async; statementRenderer.setAsync(async); - statementRenderer.setCurrentMethod(node); prepareVariables(node); node.acceptVisitor(this); + statementRenderer.clear(); } private void prepareVariables(MethodNode method) { diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/Renderer.java b/core/src/main/java/org/teavm/backend/javascript/rendering/Renderer.java index aa9a39f73..e265df5ea 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/Renderer.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/Renderer.java @@ -794,8 +794,6 @@ public class Renderer implements RenderingManager { writer.emitMethod(ref.getDescriptor()); writer.appendMethodBody(ref).ws().append("=").ws(); - methodBodyRenderer.renderParameters(ref, method.getModifiers()); - writer.sameLineWs().append("=>").ws().append("{").indent().softNewLine(); if (method.hasModifier(ElementModifier.NATIVE)) { renderNativeBody(method, classSource); } else { @@ -822,6 +820,7 @@ public class Renderer implements RenderingManager { } var async = asyncMethods.contains(reference); + renderMethodPrologue(reference, method.getModifiers()); methodBodyRenderer.renderNative(generator, async, reference); threadLibraryUsed |= methodBodyRenderer.isThreadLibraryUsed(); } @@ -885,10 +884,18 @@ public class Renderer implements RenderingManager { var entry = decompileRegular(decompiler, method); node = entry.method; } + + methodBodyRenderer.setCurrentMethod(node); + renderMethodPrologue(method.getReference(), method.getModifiers()); methodBodyRenderer.render(node, async); threadLibraryUsed |= methodBodyRenderer.isThreadLibraryUsed(); } + private void renderMethodPrologue(MethodReference reference, Set modifier) { + methodBodyRenderer.renderParameters(reference, modifier); + writer.sameLineWs().append("=>").ws().append("{").indent().softNewLine(); + } + private AstCacheEntry decompileRegular(Decompiler decompiler, MethodHolder method) { if (astCache == null) { return decompileRegularCacheMiss(decompiler, method); diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java b/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java index 120ef474e..4b441f573 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java @@ -120,6 +120,7 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { variableNameGenerator.setCurrentMethod(null); locationStack.clear(); lastEmittedLocation = TextLocation.EMPTY; + variableNameGenerator.clear(); } public boolean isAsync() { diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/VariableNameGenerator.java b/core/src/main/java/org/teavm/backend/javascript/rendering/VariableNameGenerator.java index fbd93f489..97bc5e380 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/VariableNameGenerator.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/VariableNameGenerator.java @@ -30,6 +30,21 @@ public class VariableNameGenerator { public VariableNameGenerator(boolean minifying) { this.minifying = minifying; + + } + + public void setCurrentMethod(MethodNode currentMethod) { + this.currentMethod = currentMethod; + } + + public void clear() { + cachedVariableNames.clear(); + usedVariableNames.clear(); + cachedVariableNameLastIndex = 0; + init(); + } + + private void init() { if (!minifying) { usedVariableNames.add("$tmp"); usedVariableNames.add("$ptr"); @@ -37,10 +52,6 @@ public class VariableNameGenerator { } } - public void setCurrentMethod(MethodNode currentMethod) { - this.currentMethod = currentMethod; - } - public String variableName(int index) { if (!minifying) { while (index >= cachedVariableNames.size()) {