JS: fix naming of variables in no-obfuscation mode

This commit is contained in:
Alexey Andreev 2023-11-24 17:18:52 +01:00
parent a1355bb2f7
commit 6543e68f8a
4 changed files with 30 additions and 7 deletions

View File

@ -56,6 +56,10 @@ public class MethodBodyRenderer implements MethodNodeVisitor, GeneratorContext {
statementRenderer = new StatementRenderer(context, writer); statementRenderer = new StatementRenderer(context, writer);
} }
public void setCurrentMethod(MethodNode node) {
statementRenderer.setCurrentMethod(node);
}
public boolean isThreadLibraryUsed() { public boolean isThreadLibraryUsed() {
return threadLibraryUsed; return threadLibraryUsed;
} }
@ -76,9 +80,9 @@ public class MethodBodyRenderer implements MethodNodeVisitor, GeneratorContext {
threadLibraryUsed = false; threadLibraryUsed = false;
this.async = async; this.async = async;
statementRenderer.setAsync(async); statementRenderer.setAsync(async);
statementRenderer.setCurrentMethod(node);
prepareVariables(node); prepareVariables(node);
node.acceptVisitor(this); node.acceptVisitor(this);
statementRenderer.clear();
} }
private void prepareVariables(MethodNode method) { private void prepareVariables(MethodNode method) {

View File

@ -794,8 +794,6 @@ public class Renderer implements RenderingManager {
writer.emitMethod(ref.getDescriptor()); writer.emitMethod(ref.getDescriptor());
writer.appendMethodBody(ref).ws().append("=").ws(); writer.appendMethodBody(ref).ws().append("=").ws();
methodBodyRenderer.renderParameters(ref, method.getModifiers());
writer.sameLineWs().append("=>").ws().append("{").indent().softNewLine();
if (method.hasModifier(ElementModifier.NATIVE)) { if (method.hasModifier(ElementModifier.NATIVE)) {
renderNativeBody(method, classSource); renderNativeBody(method, classSource);
} else { } else {
@ -822,6 +820,7 @@ public class Renderer implements RenderingManager {
} }
var async = asyncMethods.contains(reference); var async = asyncMethods.contains(reference);
renderMethodPrologue(reference, method.getModifiers());
methodBodyRenderer.renderNative(generator, async, reference); methodBodyRenderer.renderNative(generator, async, reference);
threadLibraryUsed |= methodBodyRenderer.isThreadLibraryUsed(); threadLibraryUsed |= methodBodyRenderer.isThreadLibraryUsed();
} }
@ -885,10 +884,18 @@ public class Renderer implements RenderingManager {
var entry = decompileRegular(decompiler, method); var entry = decompileRegular(decompiler, method);
node = entry.method; node = entry.method;
} }
methodBodyRenderer.setCurrentMethod(node);
renderMethodPrologue(method.getReference(), method.getModifiers());
methodBodyRenderer.render(node, async); methodBodyRenderer.render(node, async);
threadLibraryUsed |= methodBodyRenderer.isThreadLibraryUsed(); threadLibraryUsed |= methodBodyRenderer.isThreadLibraryUsed();
} }
private void renderMethodPrologue(MethodReference reference, Set<ElementModifier> modifier) {
methodBodyRenderer.renderParameters(reference, modifier);
writer.sameLineWs().append("=>").ws().append("{").indent().softNewLine();
}
private AstCacheEntry decompileRegular(Decompiler decompiler, MethodHolder method) { private AstCacheEntry decompileRegular(Decompiler decompiler, MethodHolder method) {
if (astCache == null) { if (astCache == null) {
return decompileRegularCacheMiss(decompiler, method); return decompileRegularCacheMiss(decompiler, method);

View File

@ -120,6 +120,7 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
variableNameGenerator.setCurrentMethod(null); variableNameGenerator.setCurrentMethod(null);
locationStack.clear(); locationStack.clear();
lastEmittedLocation = TextLocation.EMPTY; lastEmittedLocation = TextLocation.EMPTY;
variableNameGenerator.clear();
} }
public boolean isAsync() { public boolean isAsync() {

View File

@ -30,6 +30,21 @@ public class VariableNameGenerator {
public VariableNameGenerator(boolean minifying) { public VariableNameGenerator(boolean minifying) {
this.minifying = 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) { if (!minifying) {
usedVariableNames.add("$tmp"); usedVariableNames.add("$tmp");
usedVariableNames.add("$ptr"); usedVariableNames.add("$ptr");
@ -37,10 +52,6 @@ public class VariableNameGenerator {
} }
} }
public void setCurrentMethod(MethodNode currentMethod) {
this.currentMethod = currentMethod;
}
public String variableName(int index) { public String variableName(int index) {
if (!minifying) { if (!minifying) {
while (index >= cachedVariableNames.size()) { while (index >= cachedVariableNames.size()) {