From 7b8765c311ab4a2f42c923e009216478c6c3e2d1 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 13 May 2020 01:09:49 +0100 Subject: [PATCH] LibJS: Make the Function() constructor throw a SyntaxError, not return --- Libraries/LibJS/Runtime/FunctionConstructor.cpp | 3 ++- Libraries/LibJS/Tests/Function.js | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Libraries/LibJS/Runtime/FunctionConstructor.cpp b/Libraries/LibJS/Runtime/FunctionConstructor.cpp index dc94f1a322..bb8db2d28c 100644 --- a/Libraries/LibJS/Runtime/FunctionConstructor.cpp +++ b/Libraries/LibJS/Runtime/FunctionConstructor.cpp @@ -71,7 +71,8 @@ Value FunctionConstructor::construct(Interpreter& interpreter) auto function_expression = parser.parse_function_node(); if (parser.has_errors()) { // FIXME: The parser should expose parsing error strings rather than just fprintf()'ing them - return Error::create(interpreter.global_object(), "SyntaxError", ""); + interpreter.throw_exception(""); + return {}; } return function_expression->execute(interpreter); } diff --git a/Libraries/LibJS/Tests/Function.js b/Libraries/LibJS/Tests/Function.js index 946ff73270..146976df90 100644 --- a/Libraries/LibJS/Tests/Function.js +++ b/Libraries/LibJS/Tests/Function.js @@ -26,6 +26,12 @@ try { assert(new Function().name === "anonymous"); assert(new Function().toString() === "function anonymous() {\n ???\n}"); + assertThrowsError(() => { + new Function("["); + }, { + error: SyntaxError + }); + console.log("PASS"); } catch (e) { console.log("FAIL: " + e.message);