From 949bffdc933a5e992c7ae09fa7d2031abd343276 Mon Sep 17 00:00:00 2001 From: Jack Karamanian Date: Mon, 8 Jun 2020 13:24:15 -0500 Subject: [PATCH] LibJS: Define the "constructor" property on ScriptFunction's prototype and set it to the current function --- Libraries/LibJS/Runtime/ScriptFunction.cpp | 7 +++++-- Libraries/LibJS/Tests/constructor-basic.js | 22 ++++++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/Libraries/LibJS/Runtime/ScriptFunction.cpp b/Libraries/LibJS/Runtime/ScriptFunction.cpp index 650d7937da..11fa9c5da0 100644 --- a/Libraries/LibJS/Runtime/ScriptFunction.cpp +++ b/Libraries/LibJS/Runtime/ScriptFunction.cpp @@ -66,8 +66,11 @@ ScriptFunction::ScriptFunction(GlobalObject& global_object, const FlyString& nam void ScriptFunction::initialize(Interpreter& interpreter, GlobalObject& global_object) { Function::initialize(interpreter, global_object); - if (!m_is_arrow_function) - define_property("prototype", Object::create_empty(interpreter, global_object), 0); + if (!is_arrow_function) { + Object* prototype = Object::create_empty(interpreter(), interpreter().global_object()); + prototype->define_property("constructor", this, Attribute::Writable | Attribute::Configurable); + define_property("prototype", prototype, 0); + } define_native_property("length", length_getter, nullptr, Attribute::Configurable); define_native_property("name", name_getter, nullptr, Attribute::Configurable); } diff --git a/Libraries/LibJS/Tests/constructor-basic.js b/Libraries/LibJS/Tests/constructor-basic.js index 9e2e1233ea..08eb97739d 100644 --- a/Libraries/LibJS/Tests/constructor-basic.js +++ b/Libraries/LibJS/Tests/constructor-basic.js @@ -1,7 +1,17 @@ -function Foo() { - this.x = 123; -} +load("test-common.js"); -var foo = new Foo(); -if (foo.x === 123) - console.log("PASS"); +try { + function Foo() { + this.x = 123; + } + + assert(Foo.prototype.constructor === Foo); + + var foo = new Foo(); + assert(foo.constructor === Foo); + assert(foo.x === 123); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +}