Skip to content

Commit 7f4af55

Browse files
Renegade334aduh95
authored andcommitted
lib: add Iterator global to primordials
Signed-off-by: Renegade334 <contact.9a5d6388@renegade334.me.uk> PR-URL: #63698 Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 85c6d46 commit 7f4af55

3 files changed

Lines changed: 81 additions & 31 deletions

File tree

‎lib/eslint.config_partial.mjs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,6 @@ export default [
148148
name: 'Intl',
149149
message: 'Use `const { Intl } = globalThis;` instead of the global.',
150150
},
151-
{
152-
name: 'Iterator',
153-
message: 'Use `const { Iterator } = globalThis;` instead of the global.',
154-
},
155151
{
156152
name: 'MessageChannel',
157153
message: "Use `const { MessageChannel } = require('internal/worker/io');` instead of the global.",
@@ -441,6 +437,7 @@ export default [
441437
{name: 'Int16Array'},
442438
{name: 'Int32Array'},
443439
{name: 'Int8Array'},
440+
{name: 'Iterator'},
444441
{
445442
name: 'isFinite',
446443
into: 'Number',

‎lib/internal/per_context/primordials.js‎

Lines changed: 51 additions & 25 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,56 @@ 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(asyncfunction*(){}).prototype),
269+
},
270+
{
271+
name: 'IteratorHelperPrototype',
272+
original: Reflect.getPrototypeOf(primordials.IteratorPrototypeDrop({__proto__: null},null)),
273+
},
274+
{
275+
name: 'MapIteratorPrototype',
276+
original: Reflect.getPrototypeOf(newprimordials.Map()[Symbol.iterator]()),
277+
},
278+
{
279+
name: 'RegExpStringIteratorPrototype',
280+
original: Reflect.getPrototypeOf(primordials.RegExp.prototype[Symbol.matchAll]()),
281+
},
282+
{
283+
name: 'SetIteratorPrototype',
284+
original: Reflect.getPrototypeOf(newprimordials.Set()[Symbol.iterator]()),
285+
},
286+
{
287+
name: 'StringIteratorPrototype',
288+
original: Reflect.getPrototypeOf(String.prototype[Symbol.iterator]()),
289+
},
290+
{
291+
name: 'WrapForValidIteratorPrototype',
292+
original: Reflect.getPrototypeOf(primordials.IteratorFrom({__proto__: null})),
293+
},
294+
].forEach(({ name, original })=>{
295+
primordials[name]=original;
296+
copyPrototype(original,primordials,name);
297+
});
266298

267299
/* eslint-enable node-core/prefer-primordials */
268300

@@ -366,21 +398,18 @@ const copyProps = (src, dest) => {
366398
/**
367399
* @type {typeof primordials.makeSafe}
368400
*/
369-
constmakeSafe=(unsafe,safe)=>{
370-
if(SymbolIteratorinunsafe.prototype){
401+
constmakeSafe=(unsafe,safe,next)=>{
402+
if(next){
371403
constdummy=newunsafe();
372-
letnext;// We can reuse the same `next` method.
373-
374404
ArrayPrototypeForEach(ReflectOwnKeys(unsafe.prototype),(key)=>{
375405
if(!ReflectGetOwnPropertyDescriptor(safe.prototype,key)){
376406
constdesc=ReflectGetOwnPropertyDescriptor(unsafe.prototype,key);
377407
if(
378408
typeofdesc.value==='function'&&
379409
desc.value.length===0&&
380-
SymbolIteratorin(FunctionPrototypeCall(desc.value,dummy)??{})
410+
FunctionPrototypeCall(desc.value,dummy)?.next===next
381411
){
382412
constcreateIterator=uncurryThis(desc.value);
383-
next??=uncurryThis(createIterator(dummy).next);
384413
constSafeIterator=createSafeIterator(createIterator,next);
385414
desc.value=function(){
386415
returnnewSafeIterator(this);
@@ -406,6 +435,7 @@ primordials.makeSafe = makeSafe;
406435
primordials.SafeMap=makeSafe(
407436
Map,
408437
classSafeMapextendsMap{},
438+
primordials.MapIteratorPrototypeNext,
409439
);
410440
primordials.SafeWeakMap=makeSafe(
411441
WeakMap,
@@ -415,6 +445,7 @@ primordials.SafeWeakMap = makeSafe(
415445
primordials.SafeSet=makeSafe(
416446
Set,
417447
classSafeSetextendsSet{},
448+
primordials.SetIteratorPrototypeNext,
418449
);
419450
primordials.SafeWeakSet=makeSafe(
420451
WeakSet,
@@ -453,11 +484,6 @@ primordials.SafePromisePrototypeFinally = (thisPromise, onFinally) =>
453484
.then(a,b),
454485
);
455486

456-
primordials.AsyncIteratorPrototype=
457-
primordials.ReflectGetPrototypeOf(
458-
primordials.ReflectGetPrototypeOf(
459-
asyncfunction*(){}).prototype);
460-
461487
constarrayToSafePromiseIterable=(promises,mapFn)=>
462488
newprimordials.SafeArrayIterator(
463489
ArrayPrototypeMap(

‎typings/primordials.d.ts‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type TypedArrayContentType<T extends TypedArrayConstructor> = InstanceType<T>[nu
4040
*/
4141
declarenamespaceprimordials{
4242
exportfunctionuncurryThis<Textends(...args: unknown[])=>unknown>(fn: T): UncurryThis<T>;
43-
exportfunctionmakeSafe<TextendsNewableFunction>(unsafe: NewableFunction,safe: T): T;
43+
exportfunctionmakeSafe<TextendsNewableFunction>(unsafe: NewableFunction,safe: T,next?: Function): T;
4444

4545
exportimportdecodeURI=globalThis.decodeURI;
4646
exportimportdecodeURIComponent=globalThis.decodeURIComponent;
@@ -168,7 +168,6 @@ declare namespace primordials {
168168
exportconstArrayBufferPrototypeSlice: UncurryThis<typeofArrayBuffer.prototype.slice>
169169
exportconstArrayBufferPrototypeTransfer: UncurryThis<typeofArrayBuffer.prototype.transfer>
170170
exportconstArrayBufferPrototypeGetByteLength: UncurryGetter<typeofArrayBuffer.prototype,"byteLength">;
171-
exportconstAsyncIteratorPrototype: AsyncIterable<any>;
172171
exportimportBigInt=globalThis.BigInt;
173172
exportconstBigIntPrototype: typeofBigInt.prototype
174173
exportconstBigIntAsUintN: typeofBigInt.asUintN
@@ -545,6 +544,34 @@ declare namespace primordials {
545544
exportconstPromisePrototypeFinally: UncurryThis<typeofPromise.prototype.finally>
546545
exportconstPromiseWithResolvers: typeofPromise.withResolvers
547546
exportimportProxy=globalThis.Proxy
547+
exportimportIterator=globalThis.Iterator
548+
exportconstIteratorFrom: typeofIterator.from
549+
exportconstIteratorPrototype: typeofIterator.prototype
550+
exportconstIteratorPrototypeDrop: UncurryThis<typeofIterator.prototype.drop>
551+
exportconstIteratorPrototypeEvery: UncurryThis<typeofIterator.prototype.every>
552+
exportconstIteratorPrototypeFilter: UncurryThis<typeofIterator.prototype.filter>
553+
exportconstIteratorPrototypeFind: UncurryThis<typeofIterator.prototype.find>
554+
exportconstIteratorPrototypeFlatMap: UncurryThis<typeofIterator.prototype.flatMap>
555+
exportconstIteratorPrototypeForEach: UncurryThis<typeofIterator.prototype.forEach>
556+
exportconstIteratorPrototypeMap: UncurryThis<typeofIterator.prototype.map>
557+
exportconstIteratorPrototypeReduce: UncurryThis<typeofIterator.prototype.reduce>
558+
exportconstIteratorPrototypeSome: UncurryThis<typeofIterator.prototype.some>
559+
exportconstIteratorPrototypeTake: UncurryThis<typeofIterator.prototype.take>
560+
exportconstIteratorPrototypeToArray: UncurryThis<typeofIterator.prototype.toArray>
561+
exportconstIteratorPrototypeSymbolIterator: UncurryMethod<typeofIterator.prototype,typeofSymbol.iterator>
562+
exportconstArrayIteratorPrototype: ReturnType<typeofArray.prototype[typeofSymbol.iterator]>
563+
exportconstArrayIteratorPrototypeNext: UncurryThis<typeofArrayIteratorPrototype.next>
564+
exportconstAsyncIteratorPrototype: AsyncIterable<any>
565+
exportconstIteratorHelperPrototype: ReturnType<typeofIterator.prototype.drop>
566+
exportconstMapIteratorPrototype: ReturnType<typeofMap.prototype[typeofSymbol.iterator]>
567+
exportconstMapIteratorPrototypeNext: UncurryThis<typeofMapIteratorPrototype.next>
568+
exportconstRegExpStringIteratorPrototype: ReturnType<typeofRegExp.prototype[typeofSymbol.matchAll]>
569+
exportconstRegExpStringIteratorPrototypeNext: UncurryThis<typeofRegExpStringIteratorPrototype.next>
570+
exportconstSetIteratorPrototype: ReturnType<typeofSet.prototype[typeofSymbol.iterator]>
571+
exportconstSetIteratorPrototypeNext: UncurryThis<typeofSetIteratorPrototype.next>
572+
exportconstStringIteratorPrototype: ReturnType<typeofString.prototype[typeofSymbol.iterator]>
573+
exportconstStringIteratorPrototypeNext: UncurryThis<typeofStringIteratorPrototype.next>
574+
exportconstWrapForValidIteratorPrototype: ReturnType<typeofIterator.from>
548575
import_globalThis=globalThis
549576
export{_globalThisasglobalThis}
550577
}

0 commit comments

Comments
 (0)