diff options
author | Jack Karamanian <karamanian.jack@gmail.com> | 2020-05-30 00:13:42 -0500 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-30 10:33:24 +0200 |
commit | 45ccd9f8d9dae34120d3001f1885f2b521b7bba9 (patch) | |
tree | 6a9d3085bf064d8533c3b3665ab4c188bbfd6075 /Libraries | |
parent | c12125fa817bdbf423cb01e171addf9765eca417 (diff) | |
download | serenity-45ccd9f8d9dae34120d3001f1885f2b521b7bba9.zip |
LibJS: Set the bound |this| value to the |this| value of the current
scope for arrow functions
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibJS/Runtime/ScriptFunction.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/Function.prototype.bind.js | 5 | ||||
-rw-r--r-- | 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<FunctionNode::Parameter> 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()); |