]> Lady’s Gitweb - Pisces/commitdiff
Cast to function name in createCallableFunction
authorLady <redacted>
Tue, 5 Sep 2023 00:27:48 +0000 (20:27 -0400)
committerLady <redacted>
Tue, 5 Sep 2023 00:39:35 +0000 (20:39 -0400)
This requires moving the definition of `toFunctionName` inside the same
closure as `createCallableFunction` to ensure that it is defined before
`createCallableFunction` is called (e·g when defining
`ordinaryHasInstance`).

function.js

index d5888eb854be9cffff96e0ed4de89f7d3f6607be..efb3871e071040212cde3a0bc9386a90064c294c 100644 (file)
@@ -37,6 +37,12 @@ export const {
    * the provided value.
    */
   createIllegalConstructor,
+
+  /**
+   * Returns a string function name generated from the provided value
+   * and optional prefix.
+   */
+  toFunctionName,
 } = (() => {
   // ☡ Because these functions are used to initialize module constants,
   // they can’t depend on imports from elsewhere.
@@ -50,6 +56,7 @@ export const {
   const {
     create: objectCreate,
     defineProperties: defineOwnProperties,
+    getOwnPropertyDescriptor,
     getPrototypeOf: getPrototype,
     setPrototypeOf: setPrototype,
   } = Object;
@@ -67,6 +74,11 @@ export const {
       };
     },
   };
+  const getSymbolDescription = getOwnPropertyDescriptor(
+    Symbol.prototype,
+    "description",
+  ).get;
+
   return {
     bind: ($, boundThis, boundArgs) =>
       callBind(
@@ -80,7 +92,11 @@ export const {
     createCallableFunction: ($, name = undefined) =>
       defineOwnProperties(callBind(functionCall, $), {
         length: { value: $.length + 1 },
-        name: { value: name ?? $.name ?? "" },
+        name: {
+          value: toFunctionName(
+            name === undefined ? $.name ?? "" : name,
+          ),
+        },
       }),
     createIllegalConstructor: ($, proto = undefined) => {
       const constructor = function () {
@@ -107,6 +123,20 @@ export const {
         prototype: { value: $?.prototype ?? {}, writable: false },
       });
     },
+    toFunctionName: ($, prefix = undefined) => {
+      const name = (() => {
+        if (typeof $ === "symbol") {
+          // The provided value is a symbol; format its description.
+          const description = call(getSymbolDescription, $, []);
+          return description === undefined ? "" : `[${description}]`;
+        } else {
+          // The provided value not a symbol; convert it to a string
+          // property key.
+          return `${$}`;
+        }
+      })();
+      return prefix !== undefined ? `${prefix} ${name}` : name;
+    },
   };
 })();
 
@@ -199,33 +229,3 @@ export const ordinaryHasInstance = createCallableFunction(
   Function.prototype[Symbol.hasInstance],
   "ordinaryHasInstance",
 );
-
-export const {
-  /**
-   * Returns a string function name generated from the provided value
-   * and optional prefix.
-   */
-  toFunctionName,
-} = (() => {
-  const getSymbolDescription = Object.getOwnPropertyDescriptor(
-    Symbol.prototype,
-    "description",
-  ).get;
-
-  return {
-    toFunctionName: ($, prefix = undefined) => {
-      const name = (() => {
-        if (typeof $ === "symbol") {
-          // The provided value is a symbol; format its description.
-          const description = call(getSymbolDescription, $, []);
-          return description === undefined ? "" : `[${description}]`;
-        } else {
-          // The provided value not a symbol; convert it to a string
-          // property key.
-          return `${$}`;
-        }
-      })();
-      return prefix !== undefined ? `${prefix} ${name}` : name;
-    },
-  };
-})();
This page took 0.058792 seconds and 4 git commands to generate.