primordials.mjs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* eslint-disable no-restricted-globals, no-restricted-syntax */
  2. /* global SharedArrayBuffer */
  3. import { CANNOT_CONVERT_UNDEFINED_OR_NULL_TO_OBJECT } from "./messages.mjs";
  4. /** @type {<T extends (...args: any) => any>(target: T) => (thisArg: ThisType<T>, ...args: any[]) => any} */
  5. function uncurryThis(target) {
  6. return (thisArg, ...args) => {
  7. return ReflectApply(target, thisArg, args);
  8. };
  9. }
  10. /** @type {(target: any, key: string | symbol) => (thisArg: any, ...args: any[]) => any} */
  11. function uncurryThisGetter(target, key) {
  12. return uncurryThis(
  13. ReflectGetOwnPropertyDescriptor(
  14. target,
  15. key
  16. ).get
  17. );
  18. }
  19. // Reflect
  20. export const {
  21. apply: ReflectApply,
  22. construct: ReflectConstruct,
  23. defineProperty: ReflectDefineProperty,
  24. get: ReflectGet,
  25. getOwnPropertyDescriptor: ReflectGetOwnPropertyDescriptor,
  26. getPrototypeOf: ReflectGetPrototypeOf,
  27. has: ReflectHas,
  28. ownKeys: ReflectOwnKeys,
  29. set: ReflectSet,
  30. setPrototypeOf: ReflectSetPrototypeOf,
  31. } = Reflect;
  32. // Proxy
  33. export const NativeProxy = Proxy;
  34. // Number
  35. export const {
  36. MAX_SAFE_INTEGER,
  37. isFinite: NumberIsFinite,
  38. isNaN: NumberIsNaN,
  39. } = Number;
  40. // Symbol
  41. export const {
  42. iterator: SymbolIterator,
  43. species: SymbolSpecies,
  44. toStringTag: SymbolToStringTag,
  45. for: SymbolFor,
  46. } = Symbol;
  47. // Object
  48. export const NativeObject = Object;
  49. export const {
  50. create: ObjectCreate,
  51. defineProperty: ObjectDefineProperty,
  52. freeze: ObjectFreeze,
  53. is: ObjectIs,
  54. } = NativeObject;
  55. const ObjectPrototype = NativeObject.prototype;
  56. /** @type {(object: object, key: PropertyKey) => Function | undefined} */
  57. export const ObjectPrototype__lookupGetter__ = /** @type {any} */ (ObjectPrototype).__lookupGetter__
  58. ? uncurryThis(/** @type {any} */ (ObjectPrototype).__lookupGetter__)
  59. : (object, key) => {
  60. if (object == null) {
  61. throw NativeTypeError(
  62. CANNOT_CONVERT_UNDEFINED_OR_NULL_TO_OBJECT
  63. );
  64. }
  65. let target = NativeObject(object);
  66. do {
  67. const descriptor = ReflectGetOwnPropertyDescriptor(target, key);
  68. if (descriptor !== undefined) {
  69. if (ObjectHasOwn(descriptor, "get")) {
  70. return descriptor.get;
  71. }
  72. return;
  73. }
  74. } while ((target = ReflectGetPrototypeOf(target)) !== null);
  75. };
  76. /** @type {(object: object, key: PropertyKey) => boolean} */
  77. export const ObjectHasOwn = /** @type {any} */ (NativeObject).hasOwn ||
  78. uncurryThis(ObjectPrototype.hasOwnProperty);
  79. // Array
  80. const NativeArray = Array;
  81. export const ArrayIsArray = NativeArray.isArray;
  82. const ArrayPrototype = NativeArray.prototype;
  83. /** @type {(array: ArrayLike<unknown>, separator?: string) => string} */
  84. export const ArrayPrototypeJoin = uncurryThis(ArrayPrototype.join);
  85. /** @type {<T>(array: T[], ...items: T[]) => number} */
  86. export const ArrayPrototypePush = uncurryThis(ArrayPrototype.push);
  87. /** @type {(array: ArrayLike<unknown>, ...opts: any[]) => string} */
  88. export const ArrayPrototypeToLocaleString = uncurryThis(
  89. ArrayPrototype.toLocaleString
  90. );
  91. export const NativeArrayPrototypeSymbolIterator = ArrayPrototype[SymbolIterator];
  92. /** @type {<T>(array: T[]) => IterableIterator<T>} */
  93. export const ArrayPrototypeSymbolIterator = uncurryThis(NativeArrayPrototypeSymbolIterator);
  94. // Math
  95. export const MathTrunc = Math.trunc;
  96. // ArrayBuffer
  97. export const NativeArrayBuffer = ArrayBuffer;
  98. export const ArrayBufferIsView = NativeArrayBuffer.isView;
  99. const ArrayBufferPrototype = NativeArrayBuffer.prototype;
  100. /** @type {(buffer: ArrayBuffer, begin?: number, end?: number) => number} */
  101. export const ArrayBufferPrototypeSlice = uncurryThis(ArrayBufferPrototype.slice);
  102. /** @type {(buffer: ArrayBuffer) => ArrayBuffer} */
  103. export const ArrayBufferPrototypeGetByteLength = uncurryThisGetter(ArrayBufferPrototype, "byteLength");
  104. // SharedArrayBuffer
  105. export const NativeSharedArrayBuffer = typeof SharedArrayBuffer !== "undefined" ? SharedArrayBuffer : null;
  106. /** @type {(buffer: SharedArrayBuffer) => SharedArrayBuffer} */
  107. export const SharedArrayBufferPrototypeGetByteLength = NativeSharedArrayBuffer
  108. && uncurryThisGetter(NativeSharedArrayBuffer.prototype, "byteLength");
  109. // TypedArray
  110. /** @typedef {Uint8Array|Uint8ClampedArray|Uint16Array|Uint32Array|Int8Array|Int16Array|Int32Array|Float32Array|Float64Array|BigUint64Array|BigInt64Array} TypedArray */
  111. /** @type {any} */
  112. export const TypedArray = ReflectGetPrototypeOf(Uint8Array);
  113. const TypedArrayFrom = TypedArray.from;
  114. export const TypedArrayPrototype = TypedArray.prototype;
  115. export const NativeTypedArrayPrototypeSymbolIterator = TypedArrayPrototype[SymbolIterator];
  116. /** @type {(typedArray: TypedArray) => IterableIterator<number>} */
  117. export const TypedArrayPrototypeKeys = uncurryThis(TypedArrayPrototype.keys);
  118. /** @type {(typedArray: TypedArray) => IterableIterator<number>} */
  119. export const TypedArrayPrototypeValues = uncurryThis(
  120. TypedArrayPrototype.values
  121. );
  122. /** @type {(typedArray: TypedArray) => IterableIterator<[number, number]>} */
  123. export const TypedArrayPrototypeEntries = uncurryThis(
  124. TypedArrayPrototype.entries
  125. );
  126. /** @type {(typedArray: TypedArray, array: ArrayLike<number>, offset?: number) => void} */
  127. export const TypedArrayPrototypeSet = uncurryThis(TypedArrayPrototype.set);
  128. /** @type {<T extends TypedArray>(typedArray: T) => T} */
  129. export const TypedArrayPrototypeReverse = uncurryThis(
  130. TypedArrayPrototype.reverse
  131. );
  132. /** @type {<T extends TypedArray>(typedArray: T, value: number, start?: number, end?: number) => T} */
  133. export const TypedArrayPrototypeFill = uncurryThis(TypedArrayPrototype.fill);
  134. /** @type {<T extends TypedArray>(typedArray: T, target: number, start: number, end?: number) => T} */
  135. export const TypedArrayPrototypeCopyWithin = uncurryThis(
  136. TypedArrayPrototype.copyWithin
  137. );
  138. /** @type {<T extends TypedArray>(typedArray: T, compareFn?: (a: number, b: number) => number) => T} */
  139. export const TypedArrayPrototypeSort = uncurryThis(TypedArrayPrototype.sort);
  140. /** @type {<T extends TypedArray>(typedArray: T, start?: number, end?: number) => T} */
  141. export const TypedArrayPrototypeSlice = uncurryThis(TypedArrayPrototype.slice);
  142. /** @type {<T extends TypedArray>(typedArray: T, start?: number, end?: number) => T} */
  143. export const TypedArrayPrototypeSubarray = uncurryThis(
  144. TypedArrayPrototype.subarray
  145. );
  146. /** @type {((typedArray: TypedArray) => ArrayBuffer)} */
  147. export const TypedArrayPrototypeGetBuffer = uncurryThisGetter(
  148. TypedArrayPrototype,
  149. "buffer"
  150. );
  151. /** @type {((typedArray: TypedArray) => number)} */
  152. export const TypedArrayPrototypeGetByteOffset = uncurryThisGetter(
  153. TypedArrayPrototype,
  154. "byteOffset"
  155. );
  156. /** @type {((typedArray: TypedArray) => number)} */
  157. export const TypedArrayPrototypeGetLength = uncurryThisGetter(
  158. TypedArrayPrototype,
  159. "length"
  160. );
  161. /** @type {(target: unknown) => string} */
  162. export const TypedArrayPrototypeGetSymbolToStringTag = uncurryThisGetter(
  163. TypedArrayPrototype,
  164. SymbolToStringTag
  165. );
  166. // Uint16Array
  167. export const NativeUint16Array = Uint16Array;
  168. /** @type {Uint16ArrayConstructor["from"]} */
  169. export const Uint16ArrayFrom = (...args) => {
  170. return ReflectApply(TypedArrayFrom, NativeUint16Array, args);
  171. };
  172. // Uint32Array
  173. export const NativeUint32Array = Uint32Array;
  174. // Float32Array
  175. export const NativeFloat32Array = Float32Array;
  176. // ArrayIterator
  177. /** @type {any} */
  178. export const ArrayIteratorPrototype = ReflectGetPrototypeOf([][SymbolIterator]());
  179. /** @type {<T>(arrayIterator: IterableIterator<T>) => IteratorResult<T>} */
  180. export const ArrayIteratorPrototypeNext = uncurryThis(ArrayIteratorPrototype.next);
  181. // Generator
  182. /** @type {<T = unknown, TReturn = any, TNext = unknown>(generator: Generator<T, TReturn, TNext>, value?: TNext) => T} */
  183. export const GeneratorPrototypeNext = uncurryThis((function* () {})().next);
  184. // Iterator
  185. export const IteratorPrototype = ReflectGetPrototypeOf(ArrayIteratorPrototype);
  186. // DataView
  187. const DataViewPrototype = DataView.prototype;
  188. /** @type {(dataView: DataView, byteOffset: number, littleEndian?: boolean) => number} */
  189. export const DataViewPrototypeGetUint16 = uncurryThis(
  190. DataViewPrototype.getUint16
  191. );
  192. /** @type {(dataView: DataView, byteOffset: number, value: number, littleEndian?: boolean) => void} */
  193. export const DataViewPrototypeSetUint16 = uncurryThis(
  194. DataViewPrototype.setUint16
  195. );
  196. // Error
  197. export const NativeTypeError = TypeError;
  198. export const NativeRangeError = RangeError;
  199. // WeakSet
  200. /**
  201. * Do not construct with arguments to avoid calling the "add" method
  202. *
  203. * @type {{new <T extends {}>(): WeakSet<T>}}
  204. */
  205. export const NativeWeakSet = WeakSet;
  206. const WeakSetPrototype = NativeWeakSet.prototype;
  207. /** @type {<T extends {}>(set: WeakSet<T>, value: T) => Set<T>} */
  208. export const WeakSetPrototypeAdd = uncurryThis(WeakSetPrototype.add);
  209. /** @type {<T extends {}>(set: WeakSet<T>, value: T) => boolean} */
  210. export const WeakSetPrototypeHas = uncurryThis(WeakSetPrototype.has);
  211. // WeakMap
  212. /**
  213. * Do not construct with arguments to avoid calling the "set" method
  214. *
  215. * @type {{new <K extends {}, V>(): WeakMap<K, V>}}
  216. */
  217. export const NativeWeakMap = WeakMap;
  218. const WeakMapPrototype = NativeWeakMap.prototype;
  219. /** @type {<K extends {}, V>(weakMap: WeakMap<K, V>, key: K) => V} */
  220. export const WeakMapPrototypeGet = uncurryThis(WeakMapPrototype.get);
  221. /** @type {<K extends {}, V>(weakMap: WeakMap<K, V>, key: K) => boolean} */
  222. export const WeakMapPrototypeHas = uncurryThis(WeakMapPrototype.has);
  223. /** @type {<K extends {}, V>(weakMap: WeakMap<K, V>, key: K, value: V) => WeakMap} */
  224. export const WeakMapPrototypeSet = uncurryThis(WeakMapPrototype.set);