diff --git a/src/main/java/me/topchetoeu/jscript/common/Instruction.java b/src/main/java/me/topchetoeu/jscript/common/Instruction.java index d8354be..1bb48d5 100644 --- a/src/main/java/me/topchetoeu/jscript/common/Instruction.java +++ b/src/main/java/me/topchetoeu/jscript/common/Instruction.java @@ -4,6 +4,7 @@ import java.util.HashMap; import java.util.function.IntFunction; import java.util.function.IntSupplier; +import me.topchetoeu.jscript.common.parsing.Location; import me.topchetoeu.jscript.runtime.exceptions.SyntaxException; public class Instruction { @@ -261,6 +262,9 @@ public class Instruction { public static Instruction throwSyntax(String 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() { return new Instruction(Type.DELETE); } diff --git a/src/main/java/me/topchetoeu/jscript/compilation/values/VariableNode.java b/src/main/java/me/topchetoeu/jscript/compilation/values/VariableNode.java index 1ea10ad..4e2e276 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/values/VariableNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/values/VariableNode.java @@ -35,7 +35,7 @@ public class VariableNode extends Node implements AssignableNode { if (i == null) { 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); }); @@ -50,7 +50,7 @@ public class VariableNode extends Node implements AssignableNode { var i = target.scope.get(name, true); 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 _i -> Instruction.loadVar(i.index()); @@ -64,7 +64,7 @@ public class VariableNode extends Node implements AssignableNode { var i = target.scope.get(name, true); 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 if (!define && i.readonly) return _i -> Instruction.throwSyntax(new SyntaxException(loc, "Assignment to constant variable"));