LibJS: Throw SyntaxError in eval() when parser has error(s)

This commit is contained in:
Linus Groh 2021-03-15 22:08:28 +01:00 committed by Andreas Kling
parent 01a49dda85
commit d6239b691f
2 changed files with 15 additions and 0 deletions

View file

@ -322,6 +322,12 @@ JS_DEFINE_NATIVE_FUNCTION(GlobalObject::eval)
JS::Parser parser { JS::Lexer { code } };
auto program = parser.parse_program();
if (parser.has_errors()) {
auto& error = parser.errors()[0];
vm.throw_exception<SyntaxError>(global_object, error.to_string());
return {};
}
auto& caller_frame = vm.call_stack().at(vm.call_stack().size() - 2);
TemporaryChange scope_change(vm.call_frame().scope, caller_frame->scope);

View file

@ -8,3 +8,12 @@ test("basic eval() functionality", () => {
}
expect(foo(7)).toBe(12);
});
test("syntax error", () => {
expect(() => {
eval("{");
}).toThrowWithMessage(
SyntaxError,
"Unexpected token Eof. Expected CurlyClose (line: 1, column: 2)"
);
});