lib: add Iterator global to primordials

Signed-off-by: Renegade334 <contact.9a5d6388@renegade334.me.uk>
PR-URL: https://github.com/nodejs/node/pull/63698
Reviewed-By: Jordan Harband <ljharb@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Renegade334 authored and Antoine du Hamel committed 2026-07-30 13:51:35 +02:00
1 parent 85c6d46606
commit 7f4af5568f
3 files changed
+81 -31

No files matched your search

+1 -4
View File
@@ -148,10 +148,6 @@ export default [
name: 'Intl',
message: 'Use `const { Intl } = globalThis;` instead of the global.',
},
{
name: 'Iterator',
message: 'Use `const { Iterator } = globalThis;` instead of the global.',
},
{
name: 'MessageChannel',
message: "Use `const { MessageChannel } = require('internal/worker/io');` instead of the global.",
@@ -441,6 +437,7 @@ export default [
{ name: 'Int16Array' },
{ name: 'Int32Array' },
{ name: 'Int8Array' },
{ name: 'Iterator' },
{
name: 'isFinite',
into: 'Number',
+51 -25
View File
@@ -202,6 +202,7 @@ function copyPrototype(src, dest, prefix) {
'Int16Array',
'Int32Array',
'Int8Array',
'Iterator',
'Map',
'Number',
'Object',
@@ -230,10 +231,10 @@ function copyPrototype(src, dest, prefix) {
});
// Create copies of intrinsic objects that require a valid `this` to call
// static methods.
// Refs: https://www.ecma-international.org/ecma-262/#sec-promise.all
// Create copies of intrinsic objects whose static methods require the
// constructor to be passed as the receiver.
[
// Refs: https://tc39.es/ecma-262/#sec-promise.all
'Promise',
].forEach((name) => {
// eslint-disable-next-line no-restricted-globals
@@ -244,25 +245,56 @@ function copyPrototype(src, dest, prefix) {
});
// Create copies of abstract intrinsic objects that are not directly exposed
// on the global object.
// Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object
// on the global object, and whose static methods require a valid subclass
// constructor to be passed as the receiver.
[
// Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object
{ name: 'TypedArray', original: Reflect.getPrototypeOf(Uint8Array) },
{ name: 'ArrayIterator', original: {
prototype: Reflect.getPrototypeOf(Array.prototype[Symbol.iterator]()),
} },
{ name: 'StringIterator', original: {
prototype: Reflect.getPrototypeOf(String.prototype[Symbol.iterator]()),
} },
].forEach(({ name, original }) => {
primordials[name] = original;
// The static %TypedArray% methods require a valid `this`, but can't be bound,
// as they need a subclass constructor as the receiver:
copyPrototype(original, primordials, name);
copyPrototype(original.prototype, primordials, `${name}Prototype`);
});
primordials.IteratorPrototype = Reflect.getPrototypeOf(primordials.ArrayIteratorPrototype);
// Create copies of abstract intrinsic prototypes that are not directly exposed
// on the global object and which do not have corresponding constructors.
[
{
name: 'ArrayIteratorPrototype',
original: Reflect.getPrototypeOf(Array.prototype[Symbol.iterator]()),
},
{
name: 'AsyncIteratorPrototype',
original: Reflect.getPrototypeOf(Reflect.getPrototypeOf(async function*() {}).prototype),
},
{
name: 'IteratorHelperPrototype',
original: Reflect.getPrototypeOf(primordials.IteratorPrototypeDrop({ __proto__: null }, null)),
},
{
name: 'MapIteratorPrototype',
original: Reflect.getPrototypeOf(new primordials.Map()[Symbol.iterator]()),
},
{
name: 'RegExpStringIteratorPrototype',
original: Reflect.getPrototypeOf(primordials.RegExp.prototype[Symbol.matchAll]()),
},
{
name: 'SetIteratorPrototype',
original: Reflect.getPrototypeOf(new primordials.Set()[Symbol.iterator]()),
},
{
name: 'StringIteratorPrototype',
original: Reflect.getPrototypeOf(String.prototype[Symbol.iterator]()),
},
{
name: 'WrapForValidIteratorPrototype',
original: Reflect.getPrototypeOf(primordials.IteratorFrom({ __proto__: null })),
},
].forEach(({ name, original }) => {
primordials[name] = original;
copyPrototype(original, primordials, name);
});
/* eslint-enable node-core/prefer-primordials */
@@ -366,21 +398,18 @@ const copyProps = (src, dest) => {
/**
* @type {typeof primordials.makeSafe}
*/
const makeSafe = (unsafe, safe) => {
if (SymbolIterator in unsafe.prototype) {
const makeSafe = (unsafe, safe, next) => {
if (next) {
const dummy = new unsafe();
let next; // We can reuse the same `next` method.
ArrayPrototypeForEach(ReflectOwnKeys(unsafe.prototype), (key) => {
if (!ReflectGetOwnPropertyDescriptor(safe.prototype, key)) {
const desc = ReflectGetOwnPropertyDescriptor(unsafe.prototype, key);
if (
typeof desc.value === 'function' &&
desc.value.length === 0 &&
SymbolIterator in (FunctionPrototypeCall(desc.value, dummy) ?? {})
FunctionPrototypeCall(desc.value, dummy)?.next === next
) {
const createIterator = uncurryThis(desc.value);
next ??= uncurryThis(createIterator(dummy).next);
const SafeIterator = createSafeIterator(createIterator, next);
desc.value = function() {
return new SafeIterator(this);
@@ -406,6 +435,7 @@ primordials.makeSafe = makeSafe;
primordials.SafeMap = makeSafe(
Map,
class SafeMap extends Map {},
primordials.MapIteratorPrototypeNext,
);
primordials.SafeWeakMap = makeSafe(
WeakMap,
@@ -415,6 +445,7 @@ primordials.SafeWeakMap = makeSafe(
primordials.SafeSet = makeSafe(
Set,
class SafeSet extends Set {},
primordials.SetIteratorPrototypeNext,
);
primordials.SafeWeakSet = makeSafe(
WeakSet,
@@ -453,11 +484,6 @@ primordials.SafePromisePrototypeFinally = (thisPromise, onFinally) =>
.then(a, b),
);
primordials.AsyncIteratorPrototype =
primordials.ReflectGetPrototypeOf(
primordials.ReflectGetPrototypeOf(
async function* () {}).prototype);
const arrayToSafePromiseIterable = (promises, mapFn) =>
new primordials.SafeArrayIterator(
ArrayPrototypeMap(
+29 -2
View File
@@ -40,7 +40,7 @@ type TypedArrayContentType<T extends TypedArrayConstructor> = InstanceType<T>[nu
*/
declare namespace primordials {
export function uncurryThis<T extends (...args: unknown[]) => unknown>(fn: T): UncurryThis<T>;
export function makeSafe<T extends NewableFunction>(unsafe: NewableFunction, safe: T): T;
export function makeSafe<T extends NewableFunction>(unsafe: NewableFunction, safe: T, next?: Function): T;
export import decodeURI = globalThis.decodeURI;
export import decodeURIComponent = globalThis.decodeURIComponent;
@@ -168,7 +168,6 @@ declare namespace primordials {
export const ArrayBufferPrototypeSlice: UncurryThis<typeof ArrayBuffer.prototype.slice>
export const ArrayBufferPrototypeTransfer: UncurryThis<typeof ArrayBuffer.prototype.transfer>
export const ArrayBufferPrototypeGetByteLength: UncurryGetter<typeof ArrayBuffer.prototype , "byteLength">;
export const AsyncIteratorPrototype: AsyncIterable<any>;
export import BigInt = globalThis.BigInt;
export const BigIntPrototype: typeof BigInt.prototype
export const BigIntAsUintN: typeof BigInt.asUintN
@@ -545,6 +544,34 @@ declare namespace primordials {
export const PromisePrototypeFinally: UncurryThis<typeof Promise.prototype.finally>
export const PromiseWithResolvers: typeof Promise.withResolvers
export import Proxy = globalThis.Proxy
export import Iterator = globalThis.Iterator
export const IteratorFrom: typeof Iterator.from
export const IteratorPrototype: typeof Iterator.prototype
export const IteratorPrototypeDrop: UncurryThis<typeof Iterator.prototype.drop>
export const IteratorPrototypeEvery: UncurryThis<typeof Iterator.prototype.every>
export const IteratorPrototypeFilter: UncurryThis<typeof Iterator.prototype.filter>
export const IteratorPrototypeFind: UncurryThis<typeof Iterator.prototype.find>
export const IteratorPrototypeFlatMap: UncurryThis<typeof Iterator.prototype.flatMap>
export const IteratorPrototypeForEach: UncurryThis<typeof Iterator.prototype.forEach>
export const IteratorPrototypeMap: UncurryThis<typeof Iterator.prototype.map>
export const IteratorPrototypeReduce: UncurryThis<typeof Iterator.prototype.reduce>
export const IteratorPrototypeSome: UncurryThis<typeof Iterator.prototype.some>
export const IteratorPrototypeTake: UncurryThis<typeof Iterator.prototype.take>
export const IteratorPrototypeToArray: UncurryThis<typeof Iterator.prototype.toArray>
export const IteratorPrototypeSymbolIterator: UncurryMethod<typeof Iterator.prototype, typeof Symbol.iterator>
export const ArrayIteratorPrototype: ReturnType<typeof Array.prototype[typeof Symbol.iterator]>
export const ArrayIteratorPrototypeNext: UncurryThis<typeof ArrayIteratorPrototype.next>
export const AsyncIteratorPrototype: AsyncIterable<any>
export const IteratorHelperPrototype: ReturnType<typeof Iterator.prototype.drop>
export const MapIteratorPrototype: ReturnType<typeof Map.prototype[typeof Symbol.iterator]>
export const MapIteratorPrototypeNext: UncurryThis<typeof MapIteratorPrototype.next>
export const RegExpStringIteratorPrototype: ReturnType<typeof RegExp.prototype[typeof Symbol.matchAll]>
export const RegExpStringIteratorPrototypeNext: UncurryThis<typeof RegExpStringIteratorPrototype.next>
export const SetIteratorPrototype: ReturnType<typeof Set.prototype[typeof Symbol.iterator]>
export const SetIteratorPrototypeNext: UncurryThis<typeof SetIteratorPrototype.next>
export const StringIteratorPrototype: ReturnType<typeof String.prototype[typeof Symbol.iterator]>
export const StringIteratorPrototypeNext: UncurryThis<typeof StringIteratorPrototype.next>
export const WrapForValidIteratorPrototype: ReturnType<typeof Iterator.from>
import _globalThis = globalThis
export { _globalThis as globalThis }
}