]> Lady’s Gitweb - Pisces/commitdiff
Make create⸺Function setup more flexible
authorLady <redacted>
Wed, 22 Nov 2023 02:27:25 +0000 (21:27 -0500)
committerLady <redacted>
Thu, 23 Nov 2023 02:04:36 +0000 (21:04 -0500)
- 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.

function.js

index d3416192089169bb369caa7a2e9a20191accfc81..d58d2905cfbc5df6862645ce39764375b4bef9bc 100644 (file)
@@ -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.
This page took 0.035385 seconds and 4 git commands to generate.