mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2025-01-03 05:44:10 -08:00
JS: fix removal of unused functions in handwritten JS
This commit is contained in:
parent
f6741b49d6
commit
1955973c3b
|
@ -268,7 +268,7 @@ public class AstVisitor {
|
||||||
public void visit(Scope node) {
|
public void visit(Scope node) {
|
||||||
var scope = enterScope(node);
|
var scope = enterScope(node);
|
||||||
visitChildren(node);
|
visitChildren(node);
|
||||||
leaveScope(scope);
|
leaveScope(node, scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visit(LabeledStatement node) {
|
public void visit(LabeledStatement node) {
|
||||||
|
@ -294,7 +294,7 @@ public class AstVisitor {
|
||||||
var scope = enterScope(node);
|
var scope = enterScope(node);
|
||||||
visitProperty(node, DoLoop::getBody, DoLoop::setBody, EMPTY_DEFAULT);
|
visitProperty(node, DoLoop::getBody, DoLoop::setBody, EMPTY_DEFAULT);
|
||||||
visitProperty(node, DoLoop::getCondition, DoLoop::setCondition, NULL_DEFAULT);
|
visitProperty(node, DoLoop::getCondition, DoLoop::setCondition, NULL_DEFAULT);
|
||||||
leaveScope(scope);
|
leaveScope(node, scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visit(ForInLoop node) {
|
public void visit(ForInLoop node) {
|
||||||
|
@ -302,7 +302,7 @@ public class AstVisitor {
|
||||||
visitProperty(node, ForInLoop::getIterator, ForInLoop::setIterator, NULL_DEFAULT);
|
visitProperty(node, ForInLoop::getIterator, ForInLoop::setIterator, NULL_DEFAULT);
|
||||||
visitProperty(node, ForInLoop::getIteratedObject, ForInLoop::setIteratedObject, NULL_DEFAULT);
|
visitProperty(node, ForInLoop::getIteratedObject, ForInLoop::setIteratedObject, NULL_DEFAULT);
|
||||||
visitProperty(node, ForInLoop::getBody, ForInLoop::setBody, EMPTY_DEFAULT);
|
visitProperty(node, ForInLoop::getBody, ForInLoop::setBody, EMPTY_DEFAULT);
|
||||||
leaveScope(scope);
|
leaveScope(node, scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visit(ForLoop node) {
|
public void visit(ForLoop node) {
|
||||||
|
@ -311,14 +311,14 @@ public class AstVisitor {
|
||||||
visitProperty(node, ForLoop::getCondition, ForLoop::setCondition, EMPTY_EXPR_DEFAULT);
|
visitProperty(node, ForLoop::getCondition, ForLoop::setCondition, EMPTY_EXPR_DEFAULT);
|
||||||
visitProperty(node, ForLoop::getIncrement, ForLoop::setIncrement, EMPTY_EXPR_DEFAULT);
|
visitProperty(node, ForLoop::getIncrement, ForLoop::setIncrement, EMPTY_EXPR_DEFAULT);
|
||||||
visitProperty(node, ForLoop::getBody, ForLoop::setBody, EMPTY_DEFAULT);
|
visitProperty(node, ForLoop::getBody, ForLoop::setBody, EMPTY_DEFAULT);
|
||||||
leaveScope(scope);
|
leaveScope(node, scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visit(WhileLoop node) {
|
public void visit(WhileLoop node) {
|
||||||
var scope = enterScope(node);
|
var scope = enterScope(node);
|
||||||
visitProperty(node, WhileLoop::getCondition, WhileLoop::setCondition, NULL_DEFAULT);
|
visitProperty(node, WhileLoop::getCondition, WhileLoop::setCondition, NULL_DEFAULT);
|
||||||
visitProperty(node, WhileLoop::getBody, WhileLoop::setBody, EMPTY_DEFAULT);
|
visitProperty(node, WhileLoop::getBody, WhileLoop::setBody, EMPTY_DEFAULT);
|
||||||
leaveScope(scope);
|
leaveScope(node, scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visit(IfStatement node) {
|
public void visit(IfStatement node) {
|
||||||
|
@ -397,7 +397,7 @@ public class AstVisitor {
|
||||||
}
|
}
|
||||||
visitProperty(node, ArrayComprehension::getFilter, ArrayComprehension::setFilter);
|
visitProperty(node, ArrayComprehension::getFilter, ArrayComprehension::setFilter);
|
||||||
visitProperty(node, ArrayComprehension::getResult, ArrayComprehension::setResult);
|
visitProperty(node, ArrayComprehension::getResult, ArrayComprehension::setResult);
|
||||||
leaveScope(scope);
|
leaveScope(node, scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visit(GeneratorExpression node) {
|
public void visit(GeneratorExpression node) {
|
||||||
|
@ -409,7 +409,7 @@ public class AstVisitor {
|
||||||
}
|
}
|
||||||
visitProperty(node, GeneratorExpression::getFilter, GeneratorExpression::setFilter);
|
visitProperty(node, GeneratorExpression::getFilter, GeneratorExpression::setFilter);
|
||||||
visitProperty(node, GeneratorExpression::getResult, GeneratorExpression::setResult);
|
visitProperty(node, GeneratorExpression::getResult, GeneratorExpression::setResult);
|
||||||
leaveScope(scope);
|
leaveScope(node, scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visit(NumberLiteral node) {
|
public void visit(NumberLiteral node) {
|
||||||
|
@ -452,14 +452,14 @@ public class AstVisitor {
|
||||||
visitProperty(node, FunctionNode::getFunctionName, FunctionNode::setFunctionName);
|
visitProperty(node, FunctionNode::getFunctionName, FunctionNode::setFunctionName);
|
||||||
visitMany(node.getParams());
|
visitMany(node.getParams());
|
||||||
visitChildren(node.getBody());
|
visitChildren(node.getBody());
|
||||||
leaveScope(scope);
|
leaveScope(node, scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visit(LetNode node) {
|
public void visit(LetNode node) {
|
||||||
var scope = enterScope(node);
|
var scope = enterScope(node);
|
||||||
visitProperty(node, LetNode::getVariables, LetNode::setVariables);
|
visitProperty(node, LetNode::getVariables, LetNode::setVariables);
|
||||||
visitProperty(node, LetNode::getBody, LetNode::setBody);
|
visitProperty(node, LetNode::getBody, LetNode::setBody);
|
||||||
leaveScope(scope);
|
leaveScope(node, scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visit(ParenthesizedExpression node) {
|
public void visit(ParenthesizedExpression node) {
|
||||||
|
@ -492,6 +492,7 @@ public class AstVisitor {
|
||||||
|
|
||||||
|
|
||||||
private Map<String, Scope> enterScope(Scope scope) {
|
private Map<String, Scope> enterScope(Scope scope) {
|
||||||
|
onEnterScope(scope);
|
||||||
if (scope.getSymbolTable() == null) {
|
if (scope.getSymbolTable() == null) {
|
||||||
return Collections.emptyMap();
|
return Collections.emptyMap();
|
||||||
}
|
}
|
||||||
|
@ -503,7 +504,11 @@ public class AstVisitor {
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void leaveScope(Map<String, Scope> backup) {
|
protected void onEnterScope(Scope scope) {
|
||||||
|
}
|
||||||
|
|
||||||
|
private void leaveScope(Scope scope, Map<String, Scope> backup) {
|
||||||
|
onLeaveScope(scope);
|
||||||
for (var entry : backup.entrySet()) {
|
for (var entry : backup.entrySet()) {
|
||||||
if (entry.getValue() == null) {
|
if (entry.getValue() == null) {
|
||||||
currentScopes.remove(entry.getKey());
|
currentScopes.remove(entry.getKey());
|
||||||
|
@ -513,6 +518,9 @@ public class AstVisitor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void onLeaveScope(Scope scope) {
|
||||||
|
}
|
||||||
|
|
||||||
protected Scope scopeOfId(String id) {
|
protected Scope scopeOfId(String id) {
|
||||||
return currentScopes.get(id);
|
return currentScopes.get(id);
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ import org.teavm.backend.javascript.ast.AstVisitor;
|
||||||
|
|
||||||
public class RemovablePartsFinder extends AstVisitor {
|
public class RemovablePartsFinder extends AstVisitor {
|
||||||
private Map<String, List<AstNode>> removableDeclarations = new HashMap<>();
|
private Map<String, List<AstNode>> removableDeclarations = new HashMap<>();
|
||||||
private Map<String, Scope> removableDeclarationScopes = new HashMap<>();
|
private Set<Scope> removableDeclarationScopes = new HashSet<>();
|
||||||
private Map<String, Set<String>> dependencies = new HashMap<>();
|
private Map<String, Set<String>> dependencies = new HashMap<>();
|
||||||
private String insideDeclaration;
|
private String insideDeclaration;
|
||||||
private boolean topLevel = true;
|
private boolean topLevel = true;
|
||||||
|
@ -44,7 +44,6 @@ public class RemovablePartsFinder extends AstVisitor {
|
||||||
if (topLevel) {
|
if (topLevel) {
|
||||||
if (node.getName() != null && !node.getName().isEmpty()) {
|
if (node.getName() != null && !node.getName().isEmpty()) {
|
||||||
removableDeclarations.computeIfAbsent(node.getName(), k -> new ArrayList<>()).add(node);
|
removableDeclarations.computeIfAbsent(node.getName(), k -> new ArrayList<>()).add(node);
|
||||||
removableDeclarationScopes.put(node.getName(), scopeOfId(node.getName()));
|
|
||||||
}
|
}
|
||||||
topLevel = false;
|
topLevel = false;
|
||||||
insideDeclaration = node.getName();
|
insideDeclaration = node.getName();
|
||||||
|
@ -64,7 +63,6 @@ public class RemovablePartsFinder extends AstVisitor {
|
||||||
if (name != null) {
|
if (name != null) {
|
||||||
removableDeclarations.computeIfAbsent(name.getIdentifier(), k -> new ArrayList<>())
|
removableDeclarations.computeIfAbsent(name.getIdentifier(), k -> new ArrayList<>())
|
||||||
.add(initializer);
|
.add(initializer);
|
||||||
removableDeclarationScopes.put(name.getIdentifier(), scopeOfId(name.getIdentifier()));
|
|
||||||
if (initializer.getInitializer() != null) {
|
if (initializer.getInitializer() != null) {
|
||||||
topLevel = false;
|
topLevel = false;
|
||||||
insideDeclaration = name.getIdentifier();
|
insideDeclaration = name.getIdentifier();
|
||||||
|
@ -86,7 +84,6 @@ public class RemovablePartsFinder extends AstVisitor {
|
||||||
var name = extractName(assign.getLeft());
|
var name = extractName(assign.getLeft());
|
||||||
removableDeclarations.computeIfAbsent(name.getIdentifier(), k -> new ArrayList<>())
|
removableDeclarations.computeIfAbsent(name.getIdentifier(), k -> new ArrayList<>())
|
||||||
.add(node.getExpression());
|
.add(node.getExpression());
|
||||||
removableDeclarationScopes.put(name.getIdentifier(), scopeOfId(name.getIdentifier()));
|
|
||||||
if (name != null) {
|
if (name != null) {
|
||||||
topLevel = false;
|
topLevel = false;
|
||||||
insideDeclaration = name.getIdentifier();
|
insideDeclaration = name.getIdentifier();
|
||||||
|
@ -106,11 +103,13 @@ public class RemovablePartsFinder extends AstVisitor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(Name node) {
|
public void visit(Name node) {
|
||||||
if (scopeOfId(node.getIdentifier()) == removableDeclarationScopes.get(node.getIdentifier())
|
if (insideDeclaration != null) {
|
||||||
&& insideDeclaration != null) {
|
var actualScope = scopeOfId(node.getIdentifier());
|
||||||
|
if (actualScope == null || removableDeclarationScopes.contains(actualScope)) {
|
||||||
dependencies.computeIfAbsent(insideDeclaration, k -> new HashSet<>()).add(node.getIdentifier());
|
dependencies.computeIfAbsent(insideDeclaration, k -> new HashSet<>()).add(node.getIdentifier());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Name extractName(AstNode node) {
|
private Name extractName(AstNode node) {
|
||||||
if (node instanceof Name) {
|
if (node instanceof Name) {
|
||||||
|
@ -141,4 +140,18 @@ public class RemovablePartsFinder extends AstVisitor {
|
||||||
}
|
}
|
||||||
return nodes;
|
return nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onEnterScope(Scope scope) {
|
||||||
|
if (topLevel) {
|
||||||
|
removableDeclarationScopes.add(scope);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onLeaveScope(Scope scope) {
|
||||||
|
if (topLevel) {
|
||||||
|
removableDeclarationScopes.remove(scope);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user