]> Lady’s Gitweb - Pisces/commitdiff
Ensure each function in function.js has proper name
authorLady <redacted>
Sat, 22 Jul 2023 07:42:24 +0000 (00:42 -0700)
committerLady <redacted>
Mon, 4 Sep 2023 19:40:24 +0000 (15:40 -0400)
Also provides a wrapper for `constructor` for parity even though this
isn’t strictly required, and simplifies the implementation of
`isConstructor` to make use of `completesNormally`.

function.js
function.test.js

index ca7cad52503f98db03d6d5e5e91ad152723588d3..d1d337a18d1c19d1602b9415e20f8fc2fb4c83d7 100644 (file)
@@ -39,7 +39,7 @@ export const {
   ]);
   const {
     create: objectCreate,
-    defineProperty: defineOwnProperty,
+    defineProperties: defineOwnProperties,
     getPrototypeOf: getPrototype,
   } = Object;
   const { [ITERATOR]: arrayIterator } = Array.prototype;
@@ -66,12 +66,11 @@ export const {
           { args: { value: boundArgs } },
         ),
       ),
-    makeCallable: ($) =>
-      defineOwnProperty(
-        callBind(functionCall, $),
-        "length",
-        { value: $.length + 1 },
-      ),
+    makeCallable: ($, name = undefined) =>
+      defineOwnProperties(callBind(functionCall, $), {
+        length: { value: $.length + 1 },
+        name: { value: name ?? $.name ?? "" },
+      }),
   };
 })();
 
@@ -80,20 +79,40 @@ export const {
    * Calls the provided function with the provided this value and
    * arguments list.
    *
-   * ☡ This is an alias for `Reflect.apply`—the arguments must be
-   * passed as an arraylike.
+   * ☡ This is effectively an alias for `Reflect.apply`—the arguments
+   * must be passed as an arraylike.
    */
-  apply: call,
+  call,
 
   /**
    * Constructs the provided function with the provided arguments list
    * and new target.
    *
-   * ☡ This is an alias for `Reflect.construct`—the arguments must be
-   * passed as an arraylike.
+   * ☡ This is effectively an alias for `Reflect.construct`—the
+   * arguments must be passed as an arraylike.
    */
   construct,
-} = Reflect;
+
+  /** Returns whether the provided value is a constructor. */
+  isConstructor,
+} = (() => {
+  const { apply, construct } = Reflect;
+  return {
+    call: (target, thisArgument, argumentsList) =>
+      apply(target, thisArgument, argumentsList),
+    construct: (target, argumentsList, ...args) =>
+      args.length > 0
+        ? construct(target, argumentsList, args[0])
+        : construct(target, argumentsList),
+    isConstructor: ($) =>
+      completesNormally(() =>
+        // Try constructing a new object with the provided value as its
+        // `new.target`. This will throw if the provided value is not a
+        // constructor.
+        construct(function () {}, [], $)
+      ),
+  };
+})();
 
 /**
  * Returns whether calling the provided function with no `this` value
@@ -136,29 +155,11 @@ export const identity = function ($) {
 /** Returns whether the provided value is callable. */
 export const isCallable = ($) => typeof $ === "function";
 
-/** Returns whether the provided value is a constructor. */
-export const isConstructor = ($) => {
-  // The provided value is an object.
-  try {
-    // Try constructing a new object with the provided value as its
-    // `new.target`. This will throw if the provided value is not a
-    // constructor.
-    construct(
-      function () {},
-      [],
-      $,
-    );
-    return true;
-  } catch {
-    // The provided value was not a constructor.
-    return false;
-  }
-};
-
 /**
  * Returns whether the provided object inherits from the prototype of
  * the provided function.
  */
 export const ordinaryHasInstance = makeCallable(
   Function.prototype[Symbol.hasInstance],
+  "ordinaryHasInstance",
 );
index a9a5f3f3b07b51b8f5778ce5575591ff7a48e440..e9286624b895a35c4a439c119f56760f558a6f00 100644 (file)
@@ -69,6 +69,12 @@ describe("bind", () => {
       ["etaoin", undefined, "shrdlu", undefined, "cmfwyp"],
     );
   });
+
+  describe(".name", () => {
+    it("[[Get]] returns the correct name", () => {
+      assertStrictEquals(bind.name, "bind");
+    });
+  });
 });
 
 describe("call", () => {
@@ -115,6 +121,12 @@ describe("call", () => {
       ["etaoin", undefined, "shrdlu", undefined, "cmfwyp"],
     );
   });
+
+  describe(".name", () => {
+    it("[[Get]] returns the correct name", () => {
+      assertStrictEquals(call.name, "call");
+    });
+  });
 });
 
 describe("completesNormally", () => {
@@ -142,6 +154,12 @@ describe("completesNormally", () => {
       completesNormally();
     });
   });
+
+  describe(".name", () => {
+    it("[[Get]] returns the correct name", () => {
+      assertStrictEquals(completesNormally.name, "completesNormally");
+    });
+  });
 });
 
 describe("construct", () => {
@@ -200,6 +218,12 @@ describe("construct", () => {
       ["etaoin", undefined, "shrdlu", undefined, "cmfwyp"],
     );
   });
+
+  describe(".name", () => {
+    it("[[Get]] returns the correct name", () => {
+      assertStrictEquals(construct.name, "construct");
+    });
+  });
 });
 
 describe("identity", () => {
@@ -217,6 +241,12 @@ describe("identity", () => {
     const value = {};
     assertStrictEquals(new class extends identity {}(value), value);
   });
+
+  describe(".name", () => {
+    it("[[Get]] returns the correct name", () => {
+      assertStrictEquals(identity.name, "identity");
+    });
+  });
 });
 
 describe("isCallable", () => {
@@ -273,6 +303,12 @@ describe("isCallable", () => {
   it("[[Call]] returns false for objects", () => {
     assertStrictEquals(isCallable({}), false);
   });
+
+  describe(".name", () => {
+    it("[[Get]] returns the correct name", () => {
+      assertStrictEquals(isCallable.name, "isCallable");
+    });
+  });
 });
 
 describe("isConstructor", () => {
@@ -329,6 +365,12 @@ describe("isConstructor", () => {
   it("[[Call]] returns false for objects", () => {
     assertStrictEquals(isConstructor({}), false);
   });
+
+  describe(".name", () => {
+    it("[[Get]] returns the correct name", () => {
+      assertStrictEquals(isConstructor.name, "isConstructor");
+    });
+  });
 });
 
 describe("makeCallable", () => {
@@ -353,6 +395,12 @@ describe("makeCallable", () => {
       ["etaoin", "shrdlu", "cmfwyp"],
     );
   });
+
+  describe(".name", () => {
+    it("[[Get]] returns the correct name", () => {
+      assertStrictEquals(makeCallable.name, "makeCallable");
+    });
+  });
 });
 
 describe("ordinaryHasInstance", () => {
@@ -370,4 +418,13 @@ describe("ordinaryHasInstance", () => {
       true,
     );
   });
+
+  describe(".name", () => {
+    it("[[Get]] returns the correct name", () => {
+      assertStrictEquals(
+        ordinaryHasInstance.name,
+        "ordinaryHasInstance",
+      );
+    });
+  });
 });
This page took 0.026642 seconds and 4 git commands to generate.