Skip to content

Commit 82f2215

Browse files
committed
lib: add Iterator global to primordials
Signed-off-by: Renegade334 <contact.9a5d6388@renegade334.me.uk>
1 parent 6d02b36 commit 82f2215

3 files changed

Lines changed: 42 additions & 23 deletions

File tree

lib/eslint.config_partial.mjs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,6 @@ export default [
164164
name: 'Intl',
165165
message: 'Use `const { Intl } = globalThis;` instead of the global.',
166166
},
167-
{
168-
name: 'Iterator',
169-
message: 'Use `const { Iterator } = globalThis;` instead of the global.',
170-
},
171167
{
172168
name: 'MessageChannel',
173169
message: "Use `const { MessageChannel } = require('internal/worker/io');` instead of the global.",
@@ -464,6 +460,7 @@ export default [
464460
{ name: 'Int16Array' },
465461
{ name: 'Int32Array' },
466462
{ name: 'Int8Array' },
463+
{ name: 'Iterator' },
467464
{
468465
name: 'isFinite',
469466
into: 'Number',

lib/internal/per_context/primordials.js

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ function copyPrototype(src, dest, prefix) {
202202
'Int16Array',
203203
'Int32Array',
204204
'Int8Array',
205+
'Iterator',
205206
'Map',
206207
'Number',
207208
'Object',
@@ -230,10 +231,10 @@ function copyPrototype(src, dest, prefix) {
230231
});
231232

232233

233-
// Create copies of intrinsic objects that require a valid `this` to call
234-
// static methods.
235-
// Refs: https://www.ecma-international.org/ecma-262/#sec-promise.all
234+
// Create copies of intrinsic objects whose static methods require the
235+
// constructor to be passed as the receiver.
236236
[
237+
// Refs: https://tc39.es/ecma-262/#sec-promise.all
237238
'Promise',
238239
].forEach((name) => {
239240
// eslint-disable-next-line no-restricted-globals
@@ -244,25 +245,36 @@ function copyPrototype(src, dest, prefix) {
244245
});
245246

246247
// Create copies of abstract intrinsic objects that are not directly exposed
247-
// on the global object.
248-
// Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object
248+
// on the global object, and whose static methods require a valid subclass
249+
// constructor to be passed as the receiver.
249250
[
251+
// Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object
250252
{ name: 'TypedArray', original: Reflect.getPrototypeOf(Uint8Array) },
251-
{ name: 'ArrayIterator', original: {
252-
prototype: Reflect.getPrototypeOf(Array.prototype[Symbol.iterator]()),
253-
} },
254-
{ name: 'StringIterator', original: {
255-
prototype: Reflect.getPrototypeOf(String.prototype[Symbol.iterator]()),
256-
} },
257253
].forEach(({ name, original }) => {
258254
primordials[name] = original;
259-
// The static %TypedArray% methods require a valid `this`, but can't be bound,
260-
// as they need a subclass constructor as the receiver:
261255
copyPrototype(original, primordials, name);
262256
copyPrototype(original.prototype, primordials, `${name}Prototype`);
263257
});
264258

265-
primordials.IteratorPrototype = Reflect.getPrototypeOf(primordials.ArrayIteratorPrototype);
259+
// Create copies of abstract intrinsic prototypes that are not directly exposed
260+
// on the global object and which do not have corresponding constructors.
261+
[
262+
{
263+
name: 'ArrayIteratorPrototype',
264+
original: Reflect.getPrototypeOf(Array.prototype[Symbol.iterator]()),
265+
},
266+
{
267+
name: 'AsyncIteratorPrototype',
268+
original: Reflect.getPrototypeOf(Reflect.getPrototypeOf(async function*() {}).prototype),
269+
},
270+
{
271+
name: 'StringIteratorPrototype',
272+
original: Reflect.getPrototypeOf(String.prototype[Symbol.iterator]()),
273+
},
274+
].forEach(({ name, original }) => {
275+
primordials[name] = original;
276+
copyPrototype(original, primordials, name);
277+
});
266278

267279
/* eslint-enable node-core/prefer-primordials */
268280

@@ -453,11 +465,6 @@ primordials.SafePromisePrototypeFinally = (thisPromise, onFinally) =>
453465
.then(a, b),
454466
);
455467

456-
primordials.AsyncIteratorPrototype =
457-
primordials.ReflectGetPrototypeOf(
458-
primordials.ReflectGetPrototypeOf(
459-
async function* () {}).prototype);
460-
461468
const arrayToSafePromiseIterable = (promises, mapFn) =>
462469
new primordials.SafeArrayIterator(
463470
ArrayPrototypeMap(

typings/primordials.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,21 @@ declare namespace primordials {
545545
export const PromisePrototypeFinally: UncurryThis<typeof Promise.prototype.finally>
546546
export const PromiseWithResolvers: typeof Promise.withResolvers
547547
export import Proxy = globalThis.Proxy
548+
export import Iterator = globalThis.Iterator
549+
export const IteratorFrom: typeof Iterator.from
550+
export const IteratorPrototype: typeof Iterator.prototype
551+
export const IteratorPrototypeDrop: UncurryThis<typeof Iterator.prototype.drop>
552+
export const IteratorPrototypeEvery: UncurryThis<typeof Iterator.prototype.every>
553+
export const IteratorPrototypeFilter: UncurryThis<typeof Iterator.prototype.filter>
554+
export const IteratorPrototypeFind: UncurryThis<typeof Iterator.prototype.find>
555+
export const IteratorPrototypeFlatMap: UncurryThis<typeof Iterator.prototype.flatMap>
556+
export const IteratorPrototypeForEach: UncurryThis<typeof Iterator.prototype.forEach>
557+
export const IteratorPrototypeMap: UncurryThis<typeof Iterator.prototype.map>
558+
export const IteratorPrototypeReduce: UncurryThis<typeof Iterator.prototype.reduce>
559+
export const IteratorPrototypeSome: UncurryThis<typeof Iterator.prototype.some>
560+
export const IteratorPrototypeTake: UncurryThis<typeof Iterator.prototype.take>
561+
export const IteratorPrototypeToArray: UncurryThis<typeof Iterator.prototype.toArray>
562+
export const IteratorPrototypeSymbolIterator: UncurryMethod<typeof Iterator.prototype, typeof Symbol.iterator>
548563
import _globalThis = globalThis
549564
export { _globalThis as globalThis }
550565
}

0 commit comments

Comments
 (0)