fix: catch variable causing issues

This commit is contained in:
TopchetoEU 2024-12-11 11:53:19 +02:00
parent bed4014bef
commit 45f52ff123
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
2 changed files with 12 additions and 3 deletions

View File

@ -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<Void> 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<TryNode, Variable> catchBindings = new HashMap<>();
public int temp() {
instructions.add(null);

View File

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