diff --git a/src/main/java/me/topchetoeu/jscript/compilation/scope/FunctionScope.java b/src/main/java/me/topchetoeu/jscript/compilation/scope/FunctionScope.java index efd7cc2..81850c3 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/scope/FunctionScope.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/scope/FunctionScope.java @@ -14,6 +14,7 @@ public class FunctionScope extends Scope { private final HashSet blacklistNames = new HashSet<>(); private final HashMap childToParent = new HashMap<>(); + private final HashMap parentToChild = new HashMap<>(); private final Scope captureParent; @@ -63,11 +64,13 @@ public class FunctionScope extends Scope { var childVar = captures.add(parentVar.clone()); capturesMap.put(childVar.name, childVar); childToParent.put(childVar, parentVar); + parentToChild.put(parentVar, childVar); return childVar; } @Override public Variable get(Variable var, boolean capture) { + if (parentToChild.containsKey(var)) return addCaptured(parentToChild.get(var), capture); if (captures.has(var)) return addCaptured(var, capture); if (locals.has(var)) return addCaptured(var, capture); @@ -78,6 +81,7 @@ public class FunctionScope extends Scope { var childVar = captures.add(parentVar.clone()); childToParent.put(childVar, parentVar); + parentToChild.put(parentVar, childVar); return childVar; } diff --git a/src/main/java/me/topchetoeu/jscript/compilation/scope/Scope.java b/src/main/java/me/topchetoeu/jscript/compilation/scope/Scope.java index 8d1a476..92ffdd4 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/scope/Scope.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/scope/Scope.java @@ -29,7 +29,7 @@ public class Scope { protected void transferCaptured(Variable var) { if (!singleEntry) { - this.capturables.add(var); + if (!this.capturables.has(var)) this.capturables.add(var); } else if (parent != null) { parent.transferCaptured(var);