From 37fe16a99cd9ce31d6dba0b916be67929ae010a3 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 28 Mar 2020 16:40:36 +0100 Subject: [PATCH] LibJS: Add Function.prototype and make "new" Objects delegate to it --- Libraries/LibJS/AST.cpp | 3 +++ Libraries/LibJS/Runtime/Function.cpp | 3 +++ 2 files changed, 6 insertions(+) diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index d5f8da6323..196901851d 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -83,6 +83,9 @@ Value CallExpression::execute(Interpreter& interpreter) const Object* new_object = nullptr; if (is_new_expression()) { new_object = interpreter.heap().allocate(); + auto prototype = function->get("prototype"); + if (prototype.has_value() && prototype.value().is_object()) + new_object->set_prototype(prototype.value().as_object()); call_frame.this_value = new_object; } else { if (m_callee->is_member_expression()) { diff --git a/Libraries/LibJS/Runtime/Function.cpp b/Libraries/LibJS/Runtime/Function.cpp index 5f3d87a25a..c810237e8b 100644 --- a/Libraries/LibJS/Runtime/Function.cpp +++ b/Libraries/LibJS/Runtime/Function.cpp @@ -24,6 +24,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include +#include #include #include @@ -31,6 +33,7 @@ namespace JS { Function::Function() { + put("prototype", heap().allocate()); } Function::~Function()