From 45ccd9f8d9dae34120d3001f1885f2b521b7bba9 Mon Sep 17 00:00:00 2001 From: Jack Karamanian Date: Sat, 30 May 2020 00:13:42 -0500 Subject: [PATCH] LibJS: Set the bound |this| value to the |this| value of the current scope for arrow functions --- Libraries/LibJS/Runtime/ScriptFunction.cpp | 2 +- Libraries/LibJS/Tests/Function.prototype.bind.js | 5 ++--- Libraries/LibJS/Tests/arrow-functions.js | 13 +++++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Libraries/LibJS/Runtime/ScriptFunction.cpp b/Libraries/LibJS/Runtime/ScriptFunction.cpp index b5b857f620..5d0857823f 100644 --- a/Libraries/LibJS/Runtime/ScriptFunction.cpp +++ b/Libraries/LibJS/Runtime/ScriptFunction.cpp @@ -53,7 +53,7 @@ ScriptFunction* ScriptFunction::create(GlobalObject& global_object, const FlyStr } ScriptFunction::ScriptFunction(const FlyString& name, const Statement& body, Vector parameters, i32 m_function_length, LexicalEnvironment* parent_environment, Object& prototype, bool is_arrow_function) - : Function(prototype) + : Function(prototype, is_arrow_function ? interpreter().this_value() : Value(), {}) , m_name(name) , m_body(body) , m_parameters(move(parameters)) diff --git a/Libraries/LibJS/Tests/Function.prototype.bind.js b/Libraries/LibJS/Tests/Function.prototype.bind.js index 77d5a76faf..c6df364c15 100644 --- a/Libraries/LibJS/Tests/Function.prototype.bind.js +++ b/Libraries/LibJS/Tests/Function.prototype.bind.js @@ -102,9 +102,8 @@ try { // assert(strictIdentity.bind(undefined)() === undefined); // })(); - // FIXME: Uncomment me when arrow functions have the correct |this| value. - // // Arrow functions can not have their |this| value set. - // assert((() => this).bind("foo")() === globalThis) + // Arrow functions can not have their |this| value set. + assert((() => this).bind("foo")() === globalThis) console.log("PASS"); } catch (e) { diff --git a/Libraries/LibJS/Tests/arrow-functions.js b/Libraries/LibJS/Tests/arrow-functions.js index ba2cb5cb0b..d34a7836f2 100644 --- a/Libraries/LibJS/Tests/arrow-functions.js +++ b/Libraries/LibJS/Tests/arrow-functions.js @@ -64,6 +64,19 @@ try { assert(foo === undefined); assert(bar === undefined); + function FooBar() { + this.x = { + y: () => this, + z: function () { + return (() => this)(); + } + }; + } + + var foobar = new FooBar(); + assert(foobar.x.y() === foobar); + assert(foobar.x.z() === foobar.x); + (() => { "use strict"; assert(isStrictMode());