Skip to content

Implement ES2025 Iterator helpers (supersedes #2078) - #2541

Open
average-web-dev wants to merge 3 commits into
mozilla:masterfrom
average-web-dev:es2025-iterator-helpers
Open

average-web-dev wants to merge 3 commits into
mozilla:masterfrom
average-web-dev:es2025-iterator-helpers

Conversation

@average-web-dev

Copy link
Copy Markdown

This adds the ES2025 Iterator global and the iterator helper methods, plus
Iterator.concat from the iterator sequencing proposal. It is a fresh
implementation rather than a rebase of #2078, which had conflicts and caused
regressions in unrelated areas of test262.properties.

What's included

  • Iterator constructor: abstract (throws a TypeError when called or
    constructed directly), with Iterator.prototype non-writable and
    non-configurable.
  • Iterator.from, including %WrapForValidIteratorPrototype% for iterators
    that don't inherit from Iterator.prototype.
  • Iterator.concat.
  • Lazy helpers: map, filter, take, drop, flatMap, implemented as a
    small state machine behind %IteratorHelperPrototype%. They follow the
    generator state semantics: a reentrant next() throws, and return() behaves
    correctly in the suspended-start and suspended-yield states.
  • Eager helpers: reduce, toArray, forEach, some, every, find.
  • Iterator.prototype.constructor and [Symbol.toStringTag] as accessor
    properties, using SetterThatIgnoresPrototypeProperties.
  • Spec-ordered argument validation: the underlying iterator is closed when
    validation fails, next is read only once (GetIteratorDirect), and
    IteratorClose keeps its throw and normal completion semantics.

Shared %Iterator.prototype%

All built-in iterators (Array, String, Map, Set, RegExp String, Java
iterators) and generators now inherit from a common %Iterator.prototype%.
Before this change, each iterator prototype defined its own
[Symbol.iterator] and inherited directly from Object.prototype.

Language version instead of a feature flag

Following the discussion in #2078, the new Iterator is installed only for
Context.VERSION_ECMASCRIPT, with no feature flag. Older language versions
keep the legacy JS 1.7 Iterator and see no behavior change. For those
versions only a hidden %Iterator.prototype% is created, so that built-in
iterators still share a common prototype.

test262

built-ins/Iterator: 465/510 → 243/510 failing. No new failures in any
other directory.

The remaining failures are outside the scope of this PR:

Reason Tests
Iterator.zip / zipKeyed (not yet in the spec) 73
Test uses class … extends Iterator (class syntax is unsupported) 144
Primitive this gets boxed, since Callable.call takes a Scriptable receiver 15
Symbol.dispose (explicit resource management) 6
Multiple spreads in a single call crash codegen (f(...a, ...b)), an existing bug 1
Cross-realm ($262) / subclassable.js 2

Tests

New JUnit 5 class tests/.../es2025/IteratorHelpersTest.java. It covers the
prototype chain, the abstract constructor, each helper, laziness, closing of
the underlying iterator on early exit and on validation errors, reentrancy,
from, concat, the accessor properties, and unchanged legacy behavior below
VERSION_ECMASCRIPT.

SerializationCoverageTest is updated for the new serializable root
(IteratorRecord).

A note on how this was developed

I developed this PR with the help of an AI coding agent (Claude Code), and I
reviewed, tested and take responsibility for all of the changes. If the project
has concerns about or rules for AI-assisted contributions, please let me know.
I'm happy to rework parts of it, split it up, or change my approach.

I'm dyslexic, so I usually let AI help me write or proofread text like this
description. If anything reads unclear, please ask and I'll clarify.

🤖 Generated with Claude Code

average-web-dev and others added 3 commits October 29, 2026 23:03
Add the ES2025 global Iterator constructor with Iterator.from and the
iterator helper methods (map, filter, take, drop, flatMap, reduce,
toArray, forEach, some, every, find), as well as Iterator.concat from
the iterator sequencing proposal.

The new Iterator is only installed for VERSION_ECMASCRIPT; older
language versions keep the legacy JS 1.7 Iterator. All built-in
iterators now share a common %Iterator.prototype%.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant