From: Lady Date: Wed, 22 Nov 2023 02:27:25 +0000 (-0500) Subject: Make create⸺Function setup more flexible X-Git-Url: https://git.ladys.computer/Pisces/commitdiff_plain/7b948a4eb357614555c4cd031ea2a13efdffb7a5?ds=inline Make create⸺Function setup more flexible - Don’t assign the `.prototype` if it doesn’t already exist. (Note: It is not possible for a constructor to lack a `.prototype` without proxying.) - Don’t change the prototype of a function which has already had its prototype changed. --- diff --git a/function.js b/function.js index d341619..d58d290 100644 --- a/function.js +++ b/function.js @@ -53,12 +53,11 @@ export const { */ createIllegalConstructor, } = (() => { - // ☡ Because these functions are used to initialize module constants, - // they can’t depend on imports from elsewhere. + const { prototype: functionPrototype } = Function; const { bind: functionBind, call: functionCall, - } = Function.prototype; + } = functionPrototype; const callBind = Reflect.apply(functionBind, functionCall, [ functionBind, ]); @@ -66,6 +65,7 @@ export const { create: objectCreate, defineProperty: defineOwnProperty, defineProperties: defineOwnProperties, + hasOwn: hasOwnProperty, getPrototypeOf: getPrototype, setPrototypeOf: setPrototype, } = Object; @@ -93,7 +93,11 @@ export const { } else { // A base function was provided; apply it. const { length, name, prototype } = base; - setPrototype($, getPrototype(base)); + if (getPrototype($) === functionPrototype) { + setPrototype($, getPrototype(base)); + } else { + /* do nothing */ + } return applyProperties($, { length: +length + lengthDelta, name, @@ -108,9 +112,12 @@ export const { } else { // Properties were provided; apply them. const { length, name, prototype } = override; - if (!isConstructor($) || prototype === undefined) { - // The provided function is not a constructor or no prototype - // value was provided. + if ( + !isConstructor($) || !hasOwnProperty($, "prototype") || + prototype === undefined + ) { + // The provided function is not a constructor, has no + // `.prototype`, or no prototype value was provided. // // Do not modify the prototype property of the provided // function.