Skip to content

lib: add Iterator global to primordials, improve iteration object coverage in frozen intrinsics#63698

Open
Renegade334 wants to merge 2 commits into
nodejs:mainfrom
Renegade334:primordials-iterator-global
Open

lib: add Iterator global to primordials, improve iteration object coverage in frozen intrinsics#63698
Renegade334 wants to merge 2 commits into
nodejs:mainfrom
Renegade334:primordials-iterator-global

Conversation

@Renegade334
Copy link
Copy Markdown
Member

lib: add Iterator global to primordials

iterator_helpers is unflagged in V8 from v24.x.

lib: improve control abstraction coverage in frozen intrinsics

Adds the new internal iteration prototypes (%IteratorHelperPrototype%, %WrapForValidIteratorPrototype%) to frozen intrinsics, and improves/deduplicates coverage of iterator/generator objects.

Note that %AsyncFromSyncIteratorPrototype% is missing from the iterator spec – this is unresolvable AFAICT.

@Renegade334 Renegade334 requested a review from ljharb June 1, 2026 16:31
@Renegade334 Renegade334 added lib / src Issues and PRs related to general changes in the lib or src directory. commit-queue-rebase Add this label to allow the Commit Queue to land a PR in several commits. dont-land-on-v22.x PRs that should not land on the v22.x-staging branch and should not be released in v22.x. labels Jun 1, 2026
@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. typings labels Jun 1, 2026
@Renegade334 Renegade334 force-pushed the primordials-iterator-global branch from 187e6c7 to 51aa657 Compare June 1, 2026 16:33
Signed-off-by: Renegade334 <contact.9a5d6388@renegade334.me.uk>
Signed-off-by: Renegade334 <contact.9a5d6388@renegade334.me.uk>
@Renegade334 Renegade334 force-pushed the primordials-iterator-global branch from 51aa657 to 00e4207 Compare June 1, 2026 17:02
@bakkot
Copy link
Copy Markdown
Contributor

bakkot commented Jun 1, 2026

Note that %AsyncFromSyncIteratorPrototype% is missing from the iterator spec – this is unresolvable AFAICT.

Indeed:

Async-from-Sync Iterator objects are never directly accessible to ECMAScript code.

It's a spec fiction used for defining behavior of other algorithms (for-await over a sync iterator, specifically), not a real thing (although engines might choose to have an internal representation of it for ease of implementation).

@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.35%. Comparing base (39b481b) to head (00e4207).
⚠️ Report is 23 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #63698      +/-   ##
==========================================
- Coverage   91.96%   90.35%   -1.61%     
==========================================
  Files         379      732     +353     
  Lines      166638   236691   +70053     
  Branches    25497    44583   +19086     
==========================================
+ Hits       153242   213867   +60625     
- Misses      13099    14563    +1464     
- Partials      297     8261    +7964     
Files with missing lines Coverage Δ
lib/internal/freeze_intrinsics.js 97.05% <100.00%> (+0.08%) ⬆️
lib/internal/per_context/primordials.js 99.20% <100.00%> (+17.14%) ⬆️

... and 476 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread lib/internal/per_context/primordials.js
@Renegade334 Renegade334 added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels Jun 2, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 2, 2026
@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Comment on lines +261 to +274
[
{
name: 'ArrayIteratorPrototype',
original: Reflect.getPrototypeOf(Array.prototype[Symbol.iterator]()),
},
{
name: 'AsyncIteratorPrototype',
original: Reflect.getPrototypeOf(Reflect.getPrototypeOf(async function*() {}).prototype),
},
{
name: 'StringIteratorPrototype',
original: Reflect.getPrototypeOf(String.prototype[Symbol.iterator]()),
},
].forEach(({ name, original }) => {
Copy link
Copy Markdown
Contributor

@aduh95 aduh95 Jun 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might as well put them all into primordials since we're using them. wdyt about droping the repeating Prototype since this whole section is prototype-only as reminded by the comment you added?

Suggested change
[
{
name: 'ArrayIteratorPrototype',
original: Reflect.getPrototypeOf(Array.prototype[Symbol.iterator]()),
},
{
name: 'AsyncIteratorPrototype',
original: Reflect.getPrototypeOf(Reflect.getPrototypeOf(async function*() {}).prototype),
},
{
name: 'StringIteratorPrototype',
original: Reflect.getPrototypeOf(String.prototype[Symbol.iterator]()),
},
].forEach(({ name, original }) => {
Object.entries({
ArrayIterator: Reflect.getPrototypeOf(Array.prototype[Symbol.iterator]()),
AsyncFunction: Reflect.getPrototypeOf(async function() {}),
AsyncGeneratorFunction: Reflect.getPrototypeOf(async function*() {}),
AsyncIterator: Reflect.getPrototypeOf(Reflect.getPrototypeOf(async function*() {}).prototype),
GeneratorFunction: Reflect.getPrototypeOf(function*() {}),
IteratorHelper: Reflect.getPrototypeOf(primordials.IteratorPrototypeDrop({ __proto__: null }, null)),
MapIterator: Reflect.getPrototypeOf(new primordials.Map()[Symbol.iterator]()),
// eslint-disable-next-line regexp/no-empty-group
RegExpStringIterator: Reflect.getPrototypeOf(/(?:)/[Symbol.matchAll]()),
SetIterator: Reflect.getPrototypeOf(new primordials.Set()[Symbol.iterator]()),
StringIterator: Reflect.getPrototypeOf(String.prototype[Symbol.iterator]()),
WrapForValidIterator: Reflect.getPrototypeOf(primordials.IteratorFrom({ __proto__: null })),
}).forEach(({ 0: name, 1: original }) => {
name += 'Prototype';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we take that suggestion, we should also avoid re-generating the uncurried next function for SafeMapIterator and SafeSetIterator

diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js
index 6598db037e1..ec9e8c8af6d 100644
--- a/lib/internal/per_context/primordials.js
+++ b/lib/internal/per_context/primordials.js
@@ -379,10 +379,9 @@ const copyProps = (src, dest) => {
 /**
  * @type {typeof primordials.makeSafe}
  */
-const makeSafe = (unsafe, safe) => {
+const makeSafe = (unsafe, safe, next) => {
   if (SymbolIterator in unsafe.prototype) {
     const dummy = new unsafe();
-    let next; // We can reuse the same `next` method.
 
     ArrayPrototypeForEach(ReflectOwnKeys(unsafe.prototype), (key) => {
       if (!ReflectGetOwnPropertyDescriptor(safe.prototype, key)) {
@@ -419,6 +418,7 @@ primordials.makeSafe = makeSafe;
 primordials.SafeMap = makeSafe(
   Map,
   class SafeMap extends Map {},
+  primordials.MapIteratorPrototypeNext,
 );
 primordials.SafeWeakMap = makeSafe(
   WeakMap,
@@ -428,6 +428,7 @@ primordials.SafeWeakMap = makeSafe(
 primordials.SafeSet = makeSafe(
   Set,
   class SafeSet extends Set {},
+  primordials.SetIteratorPrototypeNext,
 );
 primordials.SafeWeakSet = makeSafe(
   WeakSet,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue-rebase Add this label to allow the Commit Queue to land a PR in several commits. dont-land-on-v22.x PRs that should not land on the v22.x-staging branch and should not be released in v22.x. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. typings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants