1 // ♓🌟 Piscēs ∷ function.js
2 // ====================================================================
4 // Copyright © 2022‐2023 Lady [@ Lady’s Computer].
6 // This Source Code Form is subject to the terms of the Mozilla Public
7 // License, v. 2.0. If a copy of the MPL was not distributed with this
8 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
10 import { ITERATOR
} from "./value.js";
14 * Creates a bound function from the provided function using the
15 * provided this value and arguments list.
17 * ☡ As with `call` and `construct`, the arguments must be passed as
23 * Returns a new function which calls the provided function with its
24 * first argument as the `this` value and the remaining arguments
27 * ※ This is effectively an alias for `Function::call.bind`.
31 // ☡ Because these functions are used to initialize module constants,
32 // they can’t depend on imports from elsewhere.
36 } = Function
.prototype;
37 const callBind
= Reflect
.apply(functionBind
, functionCall
, [
42 defineProperty
: defineOwnProperty
,
43 getPrototypeOf
: getPrototype
,
45 const { [ITERATOR
]: arrayIterator
} = Array
.prototype;
47 next
: arrayIteratorNext
,
48 } = getPrototype([][ITERATOR
]());
49 const argumentIterablePrototype
= {
54 call(arrayIterator
, this.args
, []),
60 bind
: ($, boundThis
, boundArgs
) =>
65 argumentIterablePrototype
,
66 { args
: { value
: boundArgs
} },
71 callBind(functionCall
, $),
73 { value
: $.length
+ 1 },
80 * Calls the provided function with the provided this value and
83 * ☡ This is an alias for `Reflect.apply`—the arguments must be
84 * passed as an arraylike.
89 * Constructs the provided function with the provided arguments list
92 * ☡ This is an alias for `Reflect.construct`—the arguments must be
93 * passed as an arraylike.
99 * Returns the provided value.
101 * ※ This function can be called as a constructor. When used in an
102 * `extends` clause and called via `super`, it will set the value of
103 * `this` to the provided value, enabling it to be extended with
104 * private class features.
106 export const identity = function ($) {
110 /** Returns whether the provided value is callable. */
111 export const isCallable
= ($) => typeof $ === "function";
113 /** Returns whether the provided value is a constructor. */
114 export const isConstructor
= ($) => {
115 // The provided value is an object.
117 // Try constructing a new object with the provided value as its
118 // `new.target`. This will throw if the provided value is not a
127 // The provided value was not a constructor.
133 * Returns whether the provided object inherits from the prototype of
134 * the provided function.
136 export const ordinaryHasInstance
= makeCallable(
137 Function
.prototype[Symbol
.hasInstance
],