From 1dc3609ff22e6a7be4d86d2ba983f81dfd8c1fd4 Mon Sep 17 00:00:00 2001 From: Kenta Moriuchi Date: Sun, 15 Jan 2023 13:26:05 +0900 Subject: [PATCH] fix(core): Add `Generator` and `AsyncGenerator` to promordials (#17241) --- core/00_primordials.js | 37 +++---- core/internal.d.ts | 214 ++++++++++++++++++++++++++++------------- ext/web/06_streams.js | 5 +- ext/web/09_file.js | 16 ++- 4 files changed, 179 insertions(+), 93 deletions(-) diff --git a/core/00_primordials.js b/core/00_primordials.js index 33d7299d53..d394d12940 100644 --- a/core/00_primordials.js +++ b/core/00_primordials.js @@ -248,24 +248,6 @@ copyPrototype(original.prototype, primordials, `${name}Prototype`); }); - const { - ArrayPrototypeForEach, - ArrayPrototypeMap, - FunctionPrototypeCall, - Map, - ObjectDefineProperty, - ObjectFreeze, - ObjectPrototypeIsPrototypeOf, - ObjectSetPrototypeOf, - Promise, - PromisePrototype, - PromisePrototypeThen, - Set, - SymbolIterator, - WeakMap, - WeakSet, - } = primordials; - // Create copies of abstract intrinsic objects that are not directly exposed // on the global object. // Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object @@ -295,6 +277,11 @@ prototype: Reflect.getPrototypeOf(String.prototype[Symbol.iterator]()), }, }, + { name: "Generator", original: Reflect.getPrototypeOf(function* () {}) }, + { + name: "AsyncGenerator", + original: Reflect.getPrototypeOf(async function* () {}), + }, ].forEach(({ name, original }) => { primordials[name] = original; // The static %TypedArray% methods require a valid `this`, but can't be bound, @@ -303,6 +290,20 @@ copyPrototype(original.prototype, primordials, `${name}Prototype`); }); + const { + ArrayPrototypeForEach, + ArrayPrototypeMap, + FunctionPrototypeCall, + ObjectDefineProperty, + ObjectFreeze, + ObjectPrototypeIsPrototypeOf, + ObjectSetPrototypeOf, + Promise, + PromisePrototype, + PromisePrototypeThen, + SymbolIterator, + } = primordials; + // Because these functions are used by `makeSafe`, which is exposed // on the `primordials` object, it's important to use const references // to the primordials that they use: diff --git a/core/internal.d.ts b/core/internal.d.ts index 1871c50fc1..efe6ed8a8e 100644 --- a/core/internal.d.ts +++ b/core/internal.d.ts @@ -58,16 +58,48 @@ declare namespace __bootstrap { export function uncurryThis unknown>( fn: T, ): (self: ThisType, ...args: Parameters) => ReturnType; + export function applyBind unknown>( + fn: T, + ): (self: ThisType, args: Parameters) => ReturnType; + + // safe objects export function makeSafe( unsafe: NewableFunction, safe: T, ): T; + export const SafeMap: typeof globalThis.Map; + export const SafeWeakMap: typeof globalThis.WeakMap; + export const SafeSet: typeof globalThis.Set; + export const SafeWeakSet: typeof globalThis.WeakSet; + export const SafeFinalizationRegistry: + typeof globalThis.FinalizationRegistry; + export const SafeWeakRef: typeof globalThis.WeakRef; + export const SafePromiseAll: typeof Promise.all; + export const SafePromisePrototypeFinally: UncurryThis< + Promise.prototype.finally + >; + // safe iterators + export const SafeArrayIterator: new (array: T[]) => IterableIterator; + export const SafeSetIterator: new (set: Set) => IterableIterator; + export const SafeMapIterator: new ( + map: Map, + ) => IterableIterator<[K, V]>; + export const SafeStringIterator: new ( + str: string, + ) => IterableIterator; + + // intrinsic objects + export const indirectEval: typeof globalThis.eval; export const isNaN: typeof globalThis.isNaN; export const decodeURI: typeof globalThis.decodeURI; export const decodeURIComponent: typeof globalThis.decodeURIComponent; export const encodeURI: typeof globalThis.encodeURI; export const encodeURIComponent: typeof globalThis.encodeURIComponent; + export const queueMicrotask: typeof globalThis.queueMicrotask; + export const setQueueMicrotask: ( + queueMicrotask: typeof globalThis.queueMicrotask, + ) => void; export const JSONParse: typeof JSON.parse; export const JSONStringify: typeof JSON.stringify; export const MathAbs: typeof Math.abs; @@ -88,6 +120,7 @@ declare namespace __bootstrap { export const MathFloor: typeof Math.floor; export const MathFround: typeof Math.fround; export const MathHypot: typeof Math.hypot; + export const MathHypotApply: StaticApply; export const MathImul: typeof Math.imul; export const MathLog: typeof Math.log; export const MathLog1p: typeof Math.log1p; @@ -96,6 +129,7 @@ declare namespace __bootstrap { export const MathMax: typeof Math.max; export const MathMaxApply: StaticApply; export const MathMin: typeof Math.min; + export const MathMinApply: StaticApply; export const MathPow: typeof Math.pow; export const MathRandom: typeof Math.random; export const MathRound: typeof Math.round; @@ -114,6 +148,10 @@ declare namespace __bootstrap { export const MathPI: typeof Math.PI; export const MathSQRT1_2: typeof Math.SQRT1_2; export const MathSQRT2: typeof Math.SQRT2; + export const Proxy: typeof globalThis.Proxy; + export const ProxyLength: typeof Proxy.length; + export const ProxyName: typeof Proxy.name; + export const ProxyRevocable: typeof Proxy.revocable; export const ReflectDefineProperty: typeof Reflect.defineProperty; export const ReflectDeleteProperty: typeof Reflect.deleteProperty; export const ReflectApply: typeof Reflect.apply; @@ -139,6 +177,8 @@ declare namespace __bootstrap { export const ArrayIsArray: typeof Array.isArray; export const ArrayFrom: typeof Array.from; export const ArrayOf: typeof Array.of; + export const ArrayOfApply: StaticApply; + export const ArrayPrototypeAt: UncurryThis; export const ArrayPrototypeConcat: UncurryThis< typeof Array.prototype.concat >; @@ -475,6 +515,17 @@ declare namespace __bootstrap { export const EvalErrorLength: typeof EvalError.length; export const EvalErrorName: typeof EvalError.name; export const EvalErrorPrototype: typeof EvalError.prototype; + export const FinalizationRegistry: typeof globalThis.FinalizationRegistry; + export const FinalizationRegistryLength: typeof FinalizationRegistry.length; + export const FinalizationRegistryName: typeof FinalizationRegistry.name; + export const FinalizationRegistryPrototype: + typeof FinalizationRegistry.prototype; + export const FinalizationRegistryPrototypeRegister: UncurryThis< + typeof FinalizationRegistry.prototype.registar + >; + export const FinalizationRegistryPrototypeUnregister: UncurryThis< + typeof FinalizationRegistry.prototype.unregister + >; export const Float32Array: typeof globalThis.Float32Array; export const Float32ArrayLength: typeof Float32Array.length; export const Float32ArrayName: typeof Float32Array.name; @@ -627,7 +678,6 @@ declare namespace __bootstrap { export const ObjectPrototypeToLocaleString: UncurryThis< typeof Object.prototype.toLocaleString >; - export const queueMicrotask: typeof globalThis.queueMicrotask; export const RangeError: typeof globalThis.RangeError; export const RangeErrorLength: typeof RangeError.length; export const RangeErrorName: typeof RangeError.name; @@ -687,6 +737,9 @@ declare namespace __bootstrap { export const StringPrototypeConcat: UncurryThis< typeof String.prototype.concat >; + export const StringPrototypeConcatApply: UncurryThisStaticApply< + typeof String.prototype.concat + >; export const StringPrototypeEndsWith: UncurryThis< typeof String.prototype.endsWith >; @@ -829,6 +882,99 @@ declare namespace __bootstrap { export const TypeErrorLength: typeof TypeError.length; export const TypeErrorName: typeof TypeError.name; export const TypeErrorPrototype: typeof TypeError.prototype; + export const URIError: typeof globalThis.URIError; + export const URIErrorLength: typeof URIError.length; + export const URIErrorName: typeof URIError.name; + export const URIErrorPrototype: typeof URIError.prototype; + export const Uint16Array: typeof globalThis.Uint16Array; + export const Uint16ArrayLength: typeof Uint16Array.length; + export const Uint16ArrayName: typeof Uint16Array.name; + export const Uint16ArrayPrototype: typeof Uint16Array.prototype; + export const Uint16ArrayBYTES_PER_ELEMENT: + typeof Uint16Array.BYTES_PER_ELEMENT; + export const Uint32Array: typeof globalThis.Uint32Array; + export const Uint32ArrayLength: typeof Uint32Array.length; + export const Uint32ArrayName: typeof Uint32Array.name; + export const Uint32ArrayPrototype: typeof Uint32Array.prototype; + export const Uint32ArrayBYTES_PER_ELEMENT: + typeof Uint32Array.BYTES_PER_ELEMENT; + export const Uint8Array: typeof globalThis.Uint8Array; + export const Uint8ArrayLength: typeof Uint8Array.length; + export const Uint8ArrayName: typeof Uint8Array.name; + export const Uint8ArrayPrototype: typeof Uint8Array.prototype; + export const Uint8ArrayBYTES_PER_ELEMENT: + typeof Uint8Array.BYTES_PER_ELEMENT; + export const Uint8ClampedArray: typeof globalThis.Uint8ClampedArray; + export const Uint8ClampedArrayLength: typeof Uint8ClampedArray.length; + export const Uint8ClampedArrayName: typeof Uint8ClampedArray.name; + export const Uint8ClampedArrayPrototype: typeof Uint8ClampedArray.prototype; + export const Uint8ClampedArrayBYTES_PER_ELEMENT: + typeof Uint8ClampedArray.BYTES_PER_ELEMENT; + export const WeakMap: typeof globalThis.WeakMap; + export const WeakMapLength: typeof WeakMap.length; + export const WeakMapName: typeof WeakMap.name; + export const WeakMapPrototype: typeof WeakMap.prototype; + export const WeakMapPrototypeDelete: UncurryThis< + typeof WeakMap.prototype.delete + >; + export const WeakMapPrototypeGet: UncurryThis; + export const WeakMapPrototypeSet: UncurryThis; + export const WeakMapPrototypeHas: UncurryThis; + export const WeakRef: typeof globalThis.WeakRef; + export const WeakRefLength: typeof WeakRef.length; + export const WeakRefName: typeof WeakRef.name; + export const WeakRefPrototype: typeof WeakRef.prototype; + export const WeakRefPrototypeDeref: UncurryThis< + typeof WeakRef.prototype.deref + >; + export const WeakSet: typeof globalThis.WeakSet; + export const WeakSetLength: typeof WeakSet.length; + export const WeakSetName: typeof WeakSet.name; + export const WeakSetPrototype: typeof WeakSet.prototype; + export const WeakSetPrototypeDelete: UncurryThis< + typeof WeakSet.prototype.delete + >; + export const WeakSetPrototypeHas: UncurryThis; + export const WeakSetPrototypeAdd: UncurryThis; + export const Promise: typeof globalThis.Promise; + export const PromiseLength: typeof Promise.length; + export const PromiseName: typeof Promise.name; + export const PromisePrototype: typeof Promise.prototype; + export const PromiseAll: typeof Promise.all; + export const PromiseRace: typeof Promise.race; + export const PromiseResolve: typeof Promise.resolve; + export const PromiseReject: typeof Promise.reject; + export const PromiseAllSettled: typeof Promise.allSettled; + export const PromiseAny: typeof Promise.any; + export const PromisePrototypeThen: UncurryThis< + typeof Promise.prototype.then + >; + export const PromisePrototypeCatch: UncurryThis< + typeof Promise.prototype.catch + >; + export const PromisePrototypeFinally: UncurryThis< + typeof Promise.prototype.finally + >; + + // abstract intrinsic objects + export const ArrayIteratorPrototypeNext: ( + iterator: IterableIterator, + ) => IteratorResult; + export const SetIteratorPrototypeNext: ( + iterator: IterableIterator, + ) => IteratorResult; + export const MapIteratorPrototypeNext: ( + iterator: IterableIterator, + ) => IteratorResult; + export const StringIteratorPrototypeNext: ( + iterator: IterableIterator, + ) => IteratorResult; + export const GeneratorPrototypeNext: ( + generator: Generator, + ) => IteratorResult; + export const AsyncGeneratorPrototypeNext: ( + asyncGenerator: AsyncGenerator, + ) => Promise>; export const TypedArrayFrom: ( constructor: Uint8ArrayConstructor, arrayLike: ArrayLike, @@ -899,71 +1045,5 @@ declare namespace __bootstrap { export const TypedArrayPrototypeValueOf: UncurryThis< typeof Uint8Array.prototype.valueOf >; - export const URIError: typeof globalThis.URIError; - export const URIErrorLength: typeof URIError.length; - export const URIErrorName: typeof URIError.name; - export const URIErrorPrototype: typeof URIError.prototype; - export const Uint16Array: typeof globalThis.Uint16Array; - export const Uint16ArrayLength: typeof Uint16Array.length; - export const Uint16ArrayName: typeof Uint16Array.name; - export const Uint16ArrayPrototype: typeof Uint16Array.prototype; - export const Uint16ArrayBYTES_PER_ELEMENT: - typeof Uint16Array.BYTES_PER_ELEMENT; - export const Uint32Array: typeof globalThis.Uint32Array; - export const Uint32ArrayLength: typeof Uint32Array.length; - export const Uint32ArrayName: typeof Uint32Array.name; - export const Uint32ArrayPrototype: typeof Uint32Array.prototype; - export const Uint32ArrayBYTES_PER_ELEMENT: - typeof Uint32Array.BYTES_PER_ELEMENT; - export const Uint8Array: typeof globalThis.Uint8Array; - export const Uint8ArrayLength: typeof Uint8Array.length; - export const Uint8ArrayName: typeof Uint8Array.name; - export const Uint8ArrayPrototype: typeof Uint8Array.prototype; - export const Uint8ArrayBYTES_PER_ELEMENT: - typeof Uint8Array.BYTES_PER_ELEMENT; - export const Uint8ClampedArray: typeof globalThis.Uint8ClampedArray; - export const Uint8ClampedArrayLength: typeof Uint8ClampedArray.length; - export const Uint8ClampedArrayName: typeof Uint8ClampedArray.name; - export const Uint8ClampedArrayPrototype: typeof Uint8ClampedArray.prototype; - export const Uint8ClampedArrayBYTES_PER_ELEMENT: - typeof Uint8ClampedArray.BYTES_PER_ELEMENT; - export const WeakMap: typeof globalThis.WeakMap; - export const WeakMapLength: typeof WeakMap.length; - export const WeakMapName: typeof WeakMap.name; - export const WeakMapPrototype: typeof WeakMap.prototype; - export const WeakMapPrototypeDelete: UncurryThis< - typeof WeakMap.prototype.delete - >; - export const WeakMapPrototypeGet: UncurryThis; - export const WeakMapPrototypeSet: UncurryThis; - export const WeakMapPrototypeHas: UncurryThis; - export const WeakSet: typeof globalThis.WeakSet; - export const WeakSetLength: typeof WeakSet.length; - export const WeakSetName: typeof WeakSet.name; - export const WeakSetPrototype: typeof WeakSet.prototype; - export const WeakSetPrototypeDelete: UncurryThis< - typeof WeakSet.prototype.delete - >; - export const WeakSetPrototypeHas: UncurryThis; - export const WeakSetPrototypeAdd: UncurryThis; - export const Promise: typeof globalThis.Promise; - export const PromiseLength: typeof Promise.length; - export const PromiseName: typeof Promise.name; - export const PromisePrototype: typeof Promise.prototype; - export const PromiseAll: typeof Promise.all; - export const PromiseRace: typeof Promise.race; - export const PromiseResolve: typeof Promise.resolve; - export const PromiseReject: typeof Promise.reject; - export const PromiseAllSettled: typeof Promise.allSettled; - export const PromiseAny: typeof Promise.any; - export const PromisePrototypeThen: UncurryThis< - typeof Promise.prototype.then - >; - export const PromisePrototypeCatch: UncurryThis< - typeof Promise.prototype.catch - >; - export const PromisePrototypeFinally: UncurryThis< - typeof Promise.prototype.finally - >; } } diff --git a/ext/web/06_streams.js b/ext/web/06_streams.js index 22baa1234a..5b1c0141dd 100644 --- a/ext/web/06_streams.js +++ b/ext/web/06_streams.js @@ -19,6 +19,7 @@ ArrayPrototypeMap, ArrayPrototypePush, ArrayPrototypeShift, + AsyncGeneratorPrototype, BigInt64ArrayPrototype, BigUint64ArrayPrototype, DataView, @@ -4439,9 +4440,7 @@ } /** @type {AsyncIterator} */ - const asyncIteratorPrototype = ObjectGetPrototypeOf( - ObjectGetPrototypeOf(async function* () {}).prototype, - ); + const asyncIteratorPrototype = ObjectGetPrototypeOf(AsyncGeneratorPrototype); const _iteratorNext = Symbol("[[iteratorNext]]"); const _iteratorFinished = Symbol("[[iteratorFinished]]"); diff --git a/ext/web/09_file.js b/ext/web/09_file.js index f789a24d2a..0fc1e7e964 100644 --- a/ext/web/09_file.js +++ b/ext/web/09_file.js @@ -20,6 +20,7 @@ ArrayBufferPrototypeSlice, ArrayBufferIsView, ArrayPrototypePush, + AsyncGeneratorPrototypeNext, Date, DatePrototypeGetTime, MathMax, @@ -330,7 +331,9 @@ /** @param {ReadableByteStreamController} controller */ async pull(controller) { while (true) { - const { value, done } = await partIterator.next(); + const { value, done } = await AsyncGeneratorPrototypeNext( + partIterator, + ); if (done) return controller.close(); if (value.byteLength > 0) { return controller.enqueue(value); @@ -354,11 +357,14 @@ const bytes = new Uint8Array(size); const partIterator = toIterator(this[_parts]); let offset = 0; - // deno-lint-ignore prefer-primordials - for await (const chunk of partIterator) { - const byteLength = chunk.byteLength; + while (true) { + const { value, done } = await AsyncGeneratorPrototypeNext( + partIterator, + ); + if (done) break; + const byteLength = value.byteLength; if (byteLength > 0) { - TypedArrayPrototypeSet(bytes, chunk, offset); + TypedArrayPrototypeSet(bytes, value, offset); offset += byteLength; } }