fix: throw access before decl for const and let in runtime

This commit is contained in:
TopchetoEU 2024-09-05 00:31:03 +03:00
parent 5b4adf5286
commit eac4a3af23
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
2 changed files with 7 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import java.util.HashMap;
import java.util.function.IntFunction; import java.util.function.IntFunction;
import java.util.function.IntSupplier; import java.util.function.IntSupplier;
import me.topchetoeu.jscript.common.parsing.Location;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException; import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public class Instruction { public class Instruction {
@ -261,6 +262,9 @@ public class Instruction {
public static Instruction throwSyntax(String err) { public static Instruction throwSyntax(String err) {
return new Instruction(Type.THROW_SYNTAX, err); return new Instruction(Type.THROW_SYNTAX, err);
} }
public static Instruction throwSyntax(Location loc, String err) {
return new Instruction(Type.THROW_SYNTAX, new SyntaxException(loc, err).getMessage());
}
public static Instruction delete() { public static Instruction delete() {
return new Instruction(Type.DELETE); return new Instruction(Type.DELETE);
} }

View File

@ -35,7 +35,7 @@ public class VariableNode extends Node implements AssignableNode {
if (i == null) { if (i == null) {
target.add(_i -> { target.add(_i -> {
if (target.scope.has(name, false)) throw new SyntaxException(loc(), String.format("Cannot access '%s' before initialization", name)); if (target.scope.has(name, false)) return Instruction.throwSyntax(loc(), String.format("Cannot access '%s' before initialization", name));
return Instruction.globGet(name); return Instruction.globGet(name);
}); });
@ -50,7 +50,7 @@ public class VariableNode extends Node implements AssignableNode {
var i = target.scope.get(name, true); var i = target.scope.get(name, true);
if (i == null) return _i -> { if (i == null) return _i -> {
if (target.scope.has(name, false)) throw new SyntaxException(loc, String.format("Cannot access '%s' before initialization", name)); if (target.scope.has(name, false)) return Instruction.throwSyntax(loc, String.format("Cannot access '%s' before initialization", name));
else return onGlobal.get(); else return onGlobal.get();
}; };
else return _i -> Instruction.loadVar(i.index()); else return _i -> Instruction.loadVar(i.index());
@ -64,7 +64,7 @@ public class VariableNode extends Node implements AssignableNode {
var i = target.scope.get(name, true); var i = target.scope.get(name, true);
if (i == null) return _i -> { if (i == null) return _i -> {
if (target.scope.has(name, false)) throw new SyntaxException(loc, String.format("Cannot access '%s' before initialization", name)); if (target.scope.has(name, false)) return Instruction.throwSyntax(loc, String.format("Cannot access '%s' before initialization", name));
else return Instruction.globSet(name, keep, define); else return Instruction.globSet(name, keep, define);
}; };
else if (!define && i.readonly) return _i -> Instruction.throwSyntax(new SyntaxException(loc, "Assignment to constant variable")); else if (!define && i.readonly) return _i -> Instruction.throwSyntax(new SyntaxException(loc, "Assignment to constant variable"));