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 1bf1721..52602d6 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/control/TryNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/control/TryNode.java @@ -53,9 +53,11 @@ public class TryNode extends Node { if (captureName != null) { var catchVar = target.catchBindings.get(this); + target.scope.defineCatch(captureName, catchVar); 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); @@ -65,6 +67,7 @@ public class TryNode extends Node { if (finallyBody != null) { finallyStart = target.size() - start; finallyBody.compile(target, false); + target.add(Instruction.tryEnd()); } LabelContext.getBreak(target.env).pop(label); 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 c5d9f3b..607b543 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/scope/FunctionScope.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/scope/FunctionScope.java @@ -63,6 +63,15 @@ public final class FunctionScope { this.catchesMap.add(var); return var; } + /** + * Creates a catch variable, using a specific variable instance + * Used in the second pass + */ + public Variable defineCatch(String name, Variable var) { + this.locals.add(var); + this.catchesMap.add(var); + return var; + } /** * Removes the last catch variable. * NOTE: the variable is still in the internal list. It just won't be findable by its name