mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:27:35 +00:00
LibJS: Throw SyntaxError in eval() when parser has error(s)
This commit is contained in:
parent
01a49dda85
commit
d6239b691f
2 changed files with 15 additions and 0 deletions
|
@ -322,6 +322,12 @@ JS_DEFINE_NATIVE_FUNCTION(GlobalObject::eval)
|
||||||
JS::Parser parser { JS::Lexer { code } };
|
JS::Parser parser { JS::Lexer { code } };
|
||||||
auto program = parser.parse_program();
|
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);
|
auto& caller_frame = vm.call_stack().at(vm.call_stack().size() - 2);
|
||||||
TemporaryChange scope_change(vm.call_frame().scope, caller_frame->scope);
|
TemporaryChange scope_change(vm.call_frame().scope, caller_frame->scope);
|
||||||
|
|
||||||
|
|
|
@ -8,3 +8,12 @@ test("basic eval() functionality", () => {
|
||||||
}
|
}
|
||||||
expect(foo(7)).toBe(12);
|
expect(foo(7)).toBe(12);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("syntax error", () => {
|
||||||
|
expect(() => {
|
||||||
|
eval("{");
|
||||||
|
}).toThrowWithMessage(
|
||||||
|
SyntaxError,
|
||||||
|
"Unexpected token Eof. Expected CurlyClose (line: 1, column: 2)"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue