+ const { get: wmGet, set: wmSet } = WeakMap.prototype;
+ const wsConstructor = WeakSet;
+ const { add: wsAdd, has: wsHas } = WeakSet.prototype;
+ const proxyConstructorValuesMap = new WeakMap();
+ const registerConstructedProxy = (constructor, proxy) => {
+ const values = (() => {
+ const existing = reflectApply(wmGet, proxyConstructorValuesMap, [
+ constructor,
+ ]);
+ if (existing) {
+ return existing;
+ } else {
+ const result = new wsConstructor();
+ reflectApply(wmSet, proxyConstructorValuesMap, [
+ constructor,
+ result,
+ ]);
+ return result;
+ }
+ })();
+ reflectApply(wsAdd, values, [proxy]);
+ return proxy;
+ };
+ const applyBaseFunction = ($, base, lengthDelta = 0) => {
+ if (base === UNDEFINED) {
+ // No base function was provided to apply.
+ return $;
+ } else {
+ // A base function was provided; apply it.
+ const { length, name, prototype } = base;
+ if (getPrototype($) === functionPrototype) {
+ setPrototype($, getPrototype(base));
+ } else {
+ /* do nothing */
+ }
+ return applyProperties($, {
+ length: +length + lengthDelta,
+ name,
+ prototype,
+ });
+ }
+ };
+ const applyProperties = ($, override) => {
+ if (override === UNDEFINED) {
+ // No properties were provided to apply.
+ return $;
+ } else {
+ // Properties were provided; apply them.
+ const { length, name, prototype } = override;
+ 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.
+ /* do nothing */
+ } else {
+ // The provided function is a constructor and a prototype value
+ // was provided.
+ //
+ // Change the prototype property of the provided function to
+ // match.
+ defineOwnProperty(
+ $,
+ "prototype",
+ defineOwnDataProperty(
+ objectCreate(null),
+ "value",
+ prototype,
+ ),
+ );
+ }
+ return defineOwnProperties($, {
+ length: defineOwnDataProperty(
+ objectCreate(null),
+ "value",
+ toLength(length === UNDEFINED ? $.length : length),
+ ),
+ name: defineOwnDataProperty(
+ objectCreate(null),
+ "value",
+ toFunctionName(name === UNDEFINED ? $.name ?? "" : name),
+ ),
+ });
+ }
+ };
+