From 3ffb0a4e87366b128e6d7ad749bd7083f7016975 Mon Sep 17 00:00:00 2001 From: Jack Karamanian Date: Sat, 30 May 2020 00:16:42 -0500 Subject: [PATCH] LibJS: Throw a TypeError when an arrow function is used as a constructor --- Libraries/LibJS/Runtime/ScriptFunction.cpp | 2 ++ Libraries/LibJS/Tests/arrow-functions.js | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/Libraries/LibJS/Runtime/ScriptFunction.cpp b/Libraries/LibJS/Runtime/ScriptFunction.cpp index 5828bcdde3..fddee25d2a 100644 --- a/Libraries/LibJS/Runtime/ScriptFunction.cpp +++ b/Libraries/LibJS/Runtime/ScriptFunction.cpp @@ -125,6 +125,8 @@ Value ScriptFunction::call(Interpreter& interpreter) Value ScriptFunction::construct(Interpreter& interpreter) { + if (m_is_arrow_function) + return interpreter.throw_exception(String::format("%s is not a constructor", m_name.characters())); return call(interpreter); } diff --git a/Libraries/LibJS/Tests/arrow-functions.js b/Libraries/LibJS/Tests/arrow-functions.js index e224415ded..add2c0a6fc 100644 --- a/Libraries/LibJS/Tests/arrow-functions.js +++ b/Libraries/LibJS/Tests/arrow-functions.js @@ -81,6 +81,13 @@ try { assert(Baz.prototype === undefined); + assertThrowsError(() => { + new Baz(); + }, { + error: TypeError, + message: "Baz is not a constructor" + }); + (() => { "use strict"; assert(isStrictMode());