fix: try-catch broken (again)

This commit is contained in:
TopchetoEU 2024-12-13 02:29:09 +02:00
parent 66440a9649
commit 6fac295b99
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
2 changed files with 12 additions and 0 deletions

View File

@ -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);

View File

@ -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