diff --git a/src/main/java/me/topchetoeu/jscript/compilation/CompileResult.java b/src/main/java/me/topchetoeu/jscript/compilation/CompileResult.java index 367ed38..e2040aa 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/CompileResult.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/CompileResult.java @@ -15,7 +15,9 @@ import me.topchetoeu.jscript.common.environment.Key; import me.topchetoeu.jscript.common.mapping.FunctionMap; import me.topchetoeu.jscript.common.mapping.FunctionMap.FunctionMapBuilder; import me.topchetoeu.jscript.common.parsing.Location; +import me.topchetoeu.jscript.compilation.control.TryNode; import me.topchetoeu.jscript.compilation.scope.FunctionScope; +import me.topchetoeu.jscript.compilation.scope.Variable; public final class CompileResult { public static final Key DEBUG_LOG = new Key<>(); @@ -28,6 +30,7 @@ public final class CompileResult { public final Environment env; public int length; public final FunctionScope scope; + public final Map catchBindings = new HashMap<>(); public int temp() { instructions.add(null); diff --git a/src/main/java/me/topchetoeu/jscript/compilation/control/TryNode.java b/src/main/java/me/topchetoeu/jscript/compilation/control/TryNode.java index 2cc2419..1bf1721 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/control/TryNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/control/TryNode.java @@ -27,7 +27,14 @@ public class TryNode extends Node { } @Override public void compileFunctions(CompileResult target) { tryBody.compileFunctions(target); - if (catchBody != null) catchBody.compileFunctions(target); + if (catchBody != null) { + if (captureName != null) { + var index = target.scope.defineCatch(captureName); + target.catchBindings.put(this, index); + } + catchBody.compileFunctions(target); + if (captureName != null) target.scope.undefineCatch(); + } if (finallyBody != null) finallyBody.compileFunctions(target); } @Override public void compile(CompileResult target, boolean pollute, BreakpointType bpt) { @@ -45,11 +52,10 @@ public class TryNode extends Node { catchStart = target.size() - start; if (captureName != null) { - var catchVar = target.scope.defineCatch(captureName); + var catchVar = target.catchBindings.get(this); target.add(Instruction.loadError()).setLocation(catchBody.loc()); target.add(catchVar.index().toSet(false)).setLocation(catchBody.loc()); catchBody.compile(target, false); - target.scope.undefineCatch(); } else catchBody.compile(target, false);