diff options
author | Jack Karamanian <karamanian.jack@gmail.com> | 2020-06-02 21:26:35 -0500 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-06-03 08:19:03 +0200 |
commit | b0932b0aecb488e4fa546bce8596b8fb1c68d31a (patch) | |
tree | 2348980eaa06fabf3fcb10fc7084fd11876ef494 | |
parent | 870bcaeef64ef568dcc7590d6f944b88b14475e9 (diff) | |
download | serenity-b0932b0aecb488e4fa546bce8596b8fb1c68d31a.zip |
LibJS: Allow null or undefined as a bound |this| value in strict mode
-rw-r--r-- | Libraries/LibJS/Runtime/Function.cpp | 3 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/Function.prototype.bind.js | 20 |
2 files changed, 13 insertions, 10 deletions
diff --git a/Libraries/LibJS/Runtime/Function.cpp b/Libraries/LibJS/Runtime/Function.cpp index e09e197758..c7c1df8c17 100644 --- a/Libraries/LibJS/Runtime/Function.cpp +++ b/Libraries/LibJS/Runtime/Function.cpp @@ -57,7 +57,8 @@ BoundFunction* Function::bind(Value bound_this_value, Vector<Value> arguments) switch (bound_this_value.type()) { case Value::Type::Undefined: case Value::Type::Null: - // FIXME: Null or undefined should be passed through in strict mode. + if (interpreter().in_strict_mode()) + return bound_this_value; return &interpreter().global_object(); default: return bound_this_value.to_object(interpreter()); diff --git a/Libraries/LibJS/Tests/Function.prototype.bind.js b/Libraries/LibJS/Tests/Function.prototype.bind.js index 34df409e21..7f2801fb14 100644 --- a/Libraries/LibJS/Tests/Function.prototype.bind.js +++ b/Libraries/LibJS/Tests/Function.prototype.bind.js @@ -103,15 +103,17 @@ try { assert(Make5() === 5); assert(new Make5().valueOf() === 5); - // FIXME: Uncomment me when strict mode is implemented - // function strictIdentity() { - // return this; - // } - - // assert(strictIdentity.bind()() === undefined); - // assert(strictIdentity.bind(null)() === null); - // assert(strictIdentity.bind(undefined)() === undefined); - // })(); + // Null or undefined should be passed through as a |this| value in strict mode. + (() => { + "use strict"; + function strictIdentity() { + return this; + } + + assert(strictIdentity.bind()() === undefined); + assert(strictIdentity.bind(null)() === null); + assert(strictIdentity.bind(undefined)() === undefined); + })(); // Arrow functions can not have their |this| value set. assert((() => this).bind("foo")() === globalThis) |