summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-03-15 22:08:28 +0100
committerAndreas Kling <kling@serenityos.org>2021-03-15 22:43:27 +0100
commitd6239b691ffc07f424c237f866859a8172186a44 (patch)
treefe8b54e4609b2bc9a7ce48c6d3b624c031ed3caa /Userland
parent01a49dda85c6f6e14f65cf9bab03f896d74755ed (diff)
downloadserenity-d6239b691ffc07f424c237f866859a8172186a44.zip
LibJS: Throw SyntaxError in eval() when parser has error(s)
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/GlobalObject.cpp6
-rw-r--r--Userland/Libraries/LibJS/Tests/eval-basic.js9
2 files changed, 15 insertions, 0 deletions
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<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);
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)"
+ );
+});