]> Lady’s Gitweb - Pisces/blobdiff - function.js
Build function.js on top of object.js infra
[Pisces] / function.js
index d02fe6e85eb6a8ec2a7137dd221ecb227d7640c1..71e4d098d41dfbad04e954dfd42c06d552af1a38 100644 (file)
@@ -7,7 +7,23 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
 
-import { ITERATOR, toFunctionName, toLength } from "./value.js";
+import {
+  ITERATOR,
+  toFunctionName,
+  toLength,
+  type,
+  UNDEFINED,
+} from "./value.js";
+import {
+  defineOwnDataProperty,
+  defineOwnProperties,
+  defineOwnProperty,
+  getOwnPropertyDescriptor,
+  getPrototype,
+  objectCreate,
+  setPropertyValues,
+  setPrototype,
+} from "./object.js";
 
 export const {
   /**
@@ -52,24 +68,49 @@ export const {
    * be used to override `length`, `name`, and `prototype`.
    */
   createIllegalConstructor,
+
+  /**
+   * Returns a constructor which produces a new constructor which wraps
+   * the provided constructor, but returns a proxy of the result using
+   * the provided handler.
+   *
+   * The resulting constructor inherits from, and has the same basic
+   * shape as, `Proxy`.
+   *
+   * If a base constructor is not provided, `Object` will be used.
+   *
+   * If a third argument is provided, it is used as the target for the
+   * provided constructor when it is constructed. This can be used to
+   * prevent leakage of the provided constructor to superclasses
+   * through `new.target`.
+   *
+   * The `length` of the provided function will be preserved in the new
+   * one. A fourth argument may be used to override `length` and
+   * `name`.
+   *
+   * ※ `.prototype` will be present, but undefined, on the resulting
+   * constructor. This differs from the behaviour of `Proxy`, for which
+   * `.prototype` is not present at all. It is not presently possible
+   * to create a constructor with no `.prototype` property in
+   * Ecmascript code.
+   */
+  createProxyConstructor,
 } = (() => {
   const { prototype: functionPrototype } = Function;
   const {
     bind: functionBind,
     call: functionCall,
   } = functionPrototype;
-  const { apply: reflectApply } = Reflect;
+  const objectConstructor = Object;
+  const proxyConstructor = Proxy;
   const {
-    create: objectCreate,
-    defineProperty: defineOwnProperty,
-    defineProperties: defineOwnProperties,
-    hasOwn: hasOwnProperty,
-    getPrototypeOf: getPrototype,
-    setPrototypeOf: setPrototype,
-  } = Object;
+    apply: reflectApply,
+    construct: reflectConstruct,
+  } = Reflect;
   const callBind = reflectApply(functionBind, functionCall, [
     functionBind,
   ]);
+  const { revocable } = Proxy;
   const { [ITERATOR]: arrayIterator } = Array.prototype;
   const {
     next: arrayIteratorNext,
@@ -88,7 +129,7 @@ export const {
     },
   };
   const applyBaseFunction = ($, base, lengthDelta = 0) => {
-    if (base === undefined) {
+    if (base === UNDEFINED) {
       // No base function was provided to apply.
       return $;
     } else {
@@ -107,15 +148,18 @@ export const {
     }
   };
   const applyProperties = ($, override) => {
-    if (override === undefined) {
+    if (override === UNDEFINED) {
       // No properties were provided to apply.
       return $;
     } else {
       // Properties were provided; apply them.
       const { length, name, prototype } = override;
-      if (!hasOwnProperty($, "prototype") || prototype === undefined) {
-        // The provided function has no `.prototype` or no prototype
-        // value was provided.
+      if (
+        prototype === UNDEFINED ||
+        !getOwnPropertyDescriptor($, "prototype")?.writable
+      ) {
+        // The provided function has no `.prototype`, its prototype is
+        // not writable, or no prototype value was provided.
         //
         // Do not modify the prototype property of the provided
         // function.
@@ -126,17 +170,27 @@ export const {
         //
         // Change the prototype property of the provided function to
         // match.
-        defineOwnProperty($, "prototype", { value: prototype });
+        defineOwnProperty(
+          $,
+          "prototype",
+          defineOwnDataProperty(
+            objectCreate(null),
+            "value",
+            prototype,
+          ),
+        );
       }
       return defineOwnProperties($, {
-        length: {
-          value: toLength(length === undefined ? $.length : length),
-        },
-        name: {
-          value: toFunctionName(
-            name === undefined ? $.name ?? "" : name,
-          ),
-        },
+        length: defineOwnDataProperty(
+          objectCreate(null),
+          "value",
+          toLength(length === UNDEFINED ? $.length : length),
+        ),
+        name: defineOwnDataProperty(
+          objectCreate(null),
+          "value",
+          toFunctionName(name === UNDEFINED ? $.name ?? "" : name),
+        ),
       });
     }
   };
@@ -146,26 +200,28 @@ export const {
       callBind(
         $,
         boundThis,
-        ...objectCreate(
-          argumentIterablePrototype,
-          { args: { value: boundArgs } },
+        ...defineOwnDataProperty(
+          objectCreate(argumentIterablePrototype),
+          "args",
+          boundArgs,
         ),
       ),
-    createArrowFunction: ($, propertyOverride = undefined) =>
+    createArrowFunction: ($, propertyOverride = UNDEFINED) =>
       applyProperties(
         applyBaseFunction(
-          (...$s) => reflectApply($, undefined, $s),
+          (...$s) => reflectApply($, UNDEFINED, $s),
           $,
         ),
         propertyOverride,
       ),
-    createCallableFunction: ($, propertyOverride = undefined) =>
+    createCallableFunction: ($, propertyOverride = UNDEFINED) =>
       applyProperties(
         applyBaseFunction(
           (...$s) => {
-            const iterator = objectCreate(
-              argumentIterablePrototype,
-              { args: { value: $s } },
+            const iterator = defineOwnDataProperty(
+              objectCreate(argumentIterablePrototype),
+              "args",
+              $s,
             )[ITERATOR]();
             const { value: thisValue } = iterator.next();
             return reflectApply($, thisValue, [...iterator]);
@@ -175,7 +231,7 @@ export const {
         ),
         propertyOverride,
       ),
-    createIllegalConstructor: ($, propertyOverride = undefined) =>
+    createIllegalConstructor: ($, propertyOverride = UNDEFINED) =>
       defineOwnProperty(
         applyProperties(
           applyBaseFunction(
@@ -189,6 +245,109 @@ export const {
         "prototype",
         { writable: false },
       ),
+    createProxyConstructor: (
+      handler,
+      $,
+      newTarget = UNDEFINED,
+      propertyOverride = UNDEFINED,
+    ) => {
+      const constructor = $ === UNDEFINED ? objectConstructor : $;
+      const target = newTarget === UNDEFINED ? constructor : newTarget;
+      const len = toLength(constructor.length);
+      if (!(type(handler) === "object")) {
+        // The provided handler is not an object; this is an error.
+        throw new TypeError(
+          `Piscēs: Proxy handler must be an object, but got: ${handler}.`,
+        );
+      } else if (!isConstructor(constructor)) {
+        // The provided constructor is not a constructor; this is an
+        // error.
+        throw new TypeError(
+          "Piscēs: Cannot create proxy constructor from nonconstructible value.",
+        );
+      } else if (!isConstructor(target)) {
+        // The provided new target is not a constructor; this is an
+        // error.
+        throw new TypeError(
+          "Piscēs: New target must be a constructor.",
+        );
+      } else {
+        // The arguments are acceptable.
+        return applyProperties(
+          defineOwnProperties(
+            setPrototype(
+              function C(...$s) {
+                if (new.target === UNDEFINED) {
+                  // The constructor was not called with new; this is
+                  // an error.
+                  throw new TypeError(
+                    `Piscēs: ${
+                      C.name ?? "Proxy"
+                    } must be called with new.`,
+                  );
+                } else {
+                  // The constructor was called with new; return the
+                  // appropriate proxy.
+                  const O = reflectConstruct(
+                    constructor,
+                    $s,
+                    target,
+                  );
+                  return new proxyConstructor(O, handler);
+                }
+              },
+              proxyConstructor,
+            ),
+            {
+              length: defineOwnDataProperty(
+                objectCreate(null),
+                "value",
+                len,
+              ),
+              name: defineOwnDataProperty(
+                objectCreate(null),
+                "value",
+                `${toFunctionName(constructor.name ?? "")}Proxy`,
+              ),
+              prototype: setPropertyValues(objectCreate(null), {
+                configurable: false,
+                enumerable: false,
+                value: UNDEFINED,
+                writable: false,
+              }),
+              revocable: setPropertyValues(objectCreate(null), {
+                configurable: true,
+                enumerable: false,
+                value: defineOwnProperties(
+                  (...$s) => {
+                    const O = reflectConstruct(
+                      constructor,
+                      $s,
+                      target,
+                    );
+                    return revocable(O, handler);
+                  },
+                  {
+                    length: defineOwnDataProperty(
+                      objectCreate(null),
+                      "value",
+                      len,
+                    ),
+                    name: defineOwnDataProperty(
+                      objectCreate(null),
+                      "value",
+                      "revocable",
+                    ),
+                  },
+                ),
+                writable: true,
+              }),
+            },
+          ),
+          propertyOverride,
+        );
+      }
+    },
   };
 })();
 
This page took 0.263201 seconds and 4 git commands to generate.