JS: fix some runtime function names not being mangled

This commit is contained in:
Alexey Andreev 2023-11-22 18:49:37 +01:00
parent be53236d23
commit 57c22ab2c8
3 changed files with 51 additions and 13 deletions

View File

@ -328,7 +328,7 @@ public class AstWriter {
writer.softNewLine();
}
writer.outdent().append('}');
leaveScope(scope);
leaveScope(scope, node);
}
private void print(LabeledStatement node) {
@ -376,7 +376,7 @@ public class AstWriter {
writer.append("while").ws().append('(');
print(node.getCondition());
writer.append(");");
leaveScope(scope);
leaveScope(scope, node);
}
private void print(ForInLoop node) {
@ -391,7 +391,7 @@ public class AstWriter {
print(node.getIteratedObject());
writer.append(')').ws();
print(node.getBody());
leaveScope(scope);
leaveScope(scope, node);
}
private void print(ForLoop node) {
@ -404,7 +404,7 @@ public class AstWriter {
print(node.getIncrement());
writer.append(')').ws();
print(node.getBody());
leaveScope(scope);
leaveScope(scope, node);
}
private void print(WhileLoop node) {
@ -413,7 +413,7 @@ public class AstWriter {
print(node.getCondition());
writer.append(')').ws();
print(node.getBody());
leaveScope(scope);
leaveScope(scope, node);
}
private void print(IfStatement node) {
@ -634,7 +634,7 @@ public class AstWriter {
}
print(node.getResult());
writer.append(']');
leaveScope(scope);
leaveScope(scope, node);
}
private void print(GeneratorExpression node) {
@ -654,7 +654,7 @@ public class AstWriter {
}
print(node.getResult());
writer.append(')');
leaveScope(scope);
leaveScope(scope, node);
}
private void print(NumberLiteral node) {
@ -722,7 +722,7 @@ public class AstWriter {
print(node.getRight());
}
private void print(FunctionNode node) {
protected void print(FunctionNode node) {
var scope = enterScope(node);
var isArrow = node.getFunctionType() == FunctionNode.ARROW_FUNCTION;
if (!isArrow) {
@ -759,7 +759,7 @@ public class AstWriter {
print(node.getBody());
}
leaveScope(scope);
leaveScope(scope, node);
}
private void print(LetNode node) {
@ -768,7 +768,7 @@ public class AstWriter {
printList(node.getVariables().getVariables());
writer.append(')');
print(node.getBody());
leaveScope(scope);
leaveScope(scope, node);
}
private void print(ParenthesizedExpression node, int precedence) {
@ -975,10 +975,14 @@ public class AstWriter {
map.put(name, currentScopes.get(name));
currentScopes.put(name, scope);
}
onEnterScope(scope);
return map;
}
private void leaveScope(Map<String, Scope> backup) {
protected void onEnterScope(Scope scope) {
}
private void leaveScope(Map<String, Scope> backup, Scope scope) {
for (var entry : backup.entrySet()) {
if (entry.getValue() == null) {
currentScopes.remove(entry.getKey());
@ -986,6 +990,10 @@ public class AstWriter {
currentScopes.put(entry.getKey(), entry.getValue());
}
}
onLeaveScope(scope);
}
protected void onLeaveScope(Scope scope) {
}
protected Scope scopeOfId(String id) {

View File

@ -16,7 +16,9 @@
package org.teavm.backend.javascript.templating;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.mozilla.javascript.ast.ElementGet;
import org.mozilla.javascript.ast.FunctionCall;
import org.mozilla.javascript.ast.FunctionNode;
@ -37,6 +39,8 @@ public class TemplatingAstWriter extends AstWriter {
private Scope scope;
private Map<String, SourceFragment> fragments = new HashMap<>();
private ClassInitializerInfo classInitializerInfo;
private Set<Scope> topLevelScopes = new HashSet<>();
private boolean inFunction;
public TemplatingAstWriter(SourceWriter writer, Map<String, SourceFragment> names, Scope scope,
ClassInitializerInfo classInitializerInfo) {
@ -237,11 +241,36 @@ public class TemplatingAstWriter extends AstWriter {
return;
}
}
if (definingScope == null) {
if (definingScope == null || topLevelScopes.contains(definingScope)) {
writer.appendFunction(node.getIdentifier());
return;
}
}
super.print(node, precedence);
}
@Override
protected void print(FunctionNode node) {
if (inFunction) {
super.print(node);
} else {
inFunction = true;
super.print(node);
inFunction = false;
}
}
@Override
protected void onEnterScope(Scope scope) {
if (names == null && !inFunction) {
topLevelScopes.add(scope);
}
}
@Override
protected void onLeaveScope(Scope scope) {
if (names == null && !inFunction) {
topLevelScopes.remove(scope);
}
}
}

View File

@ -33,6 +33,7 @@ let Long_fromNumber;
let Long_toNumber;
let Long_hi;
let Long_lo;
let Long_divRem;
if (typeof teavm_globals.BigInt !== "function") {
Long.prototype.toString = function() {
let result = [];
@ -320,7 +321,7 @@ if (typeof teavm_globals.BigInt !== 'function') {
return Long_udivRem(a, b)[1];
}
let Long_divRem = (a, b) => {
Long_divRem = (a, b) => {
if (b.lo === 0 && b.hi === 0) {
throw new teavm_globals.Error("Division by zero");
}