From: Lady Date: Tue, 5 Sep 2023 00:27:48 +0000 (-0400) Subject: Cast to function name in createCallableFunction X-Git-Url: https://git.ladys.computer/Pisces/commitdiff_plain/393f4d07436fec754c9863fddf98b8503afdb2c9?ds=sidebyside;hp=b6bd2e5effcc14b40cdd43c6fba9f01c23504ba5 Cast to function name in createCallableFunction 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`). --- diff --git a/function.js b/function.js index d5888eb..efb3871 100644 --- a/function.js +++ b/function.js @@ -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; - }, - }; -})();