mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-15 20:33:10 +00:00
LibJS: Throw SyntaxError in eval() when parser has error(s)
This commit is contained in:
parent
01a49dda85
commit
d6239b691f
|
@ -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);
|
||||
|
||||
|
|
|
@ -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)"
|
||||
);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue