diff --git a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp index bcd06266dd..2b0a01bcc4 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -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(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); diff --git a/Userland/Libraries/LibJS/Tests/eval-basic.js b/Userland/Libraries/LibJS/Tests/eval-basic.js index 955e86dc22..e00c7fcbd9 100644 --- a/Userland/Libraries/LibJS/Tests/eval-basic.js +++ b/Userland/Libraries/LibJS/Tests/eval-basic.js @@ -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)" + ); +});