diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/AstWriter.java b/core/src/main/java/org/teavm/backend/javascript/rendering/AstWriter.java index d5e2503cd..1f9ad1841 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/AstWriter.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/AstWriter.java @@ -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 backup) { + protected void onEnterScope(Scope scope) { + } + + private void leaveScope(Map 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) { diff --git a/core/src/main/java/org/teavm/backend/javascript/templating/TemplatingAstWriter.java b/core/src/main/java/org/teavm/backend/javascript/templating/TemplatingAstWriter.java index eaeded4f2..f8abf10ae 100644 --- a/core/src/main/java/org/teavm/backend/javascript/templating/TemplatingAstWriter.java +++ b/core/src/main/java/org/teavm/backend/javascript/templating/TemplatingAstWriter.java @@ -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 fragments = new HashMap<>(); private ClassInitializerInfo classInitializerInfo; + private Set topLevelScopes = new HashSet<>(); + private boolean inFunction; public TemplatingAstWriter(SourceWriter writer, Map 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); + } + } } diff --git a/core/src/main/resources/org/teavm/backend/javascript/long.js b/core/src/main/resources/org/teavm/backend/javascript/long.js index 4c52148d5..086342042 100644 --- a/core/src/main/resources/org/teavm/backend/javascript/long.js +++ b/core/src/main/resources/org/teavm/backend/javascript/long.js @@ -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"); }