Bläddra i källkod

LibJS: Allow null or undefined as a bound |this| value in strict mode

Jack Karamanian 5 år sedan
förälder
incheckning
b0932b0aec
2 ändrade filer med 13 tillägg och 10 borttagningar
  1. 2 1
      Libraries/LibJS/Runtime/Function.cpp
  2. 11 9
      Libraries/LibJS/Tests/Function.prototype.bind.js

+ 2 - 1
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());

+ 11 - 9
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)