Skip to content

Conversation

@anivar
Copy link
Contributor

@anivar anivar commented Sep 12, 2025

Iterator Infrastructure Improvements

This PR adds robust iterator handling infrastructure to Rhino with three new utility classes that solve Context safety and architectural issues.

What's Added

IteratorOperations.java - Context-safe iterator utilities

  • collectToArray() / collectToList() - Convert iterators to collections
  • forEach() - Process iterator values with proper cleanup
  • isIterable() / getIterator() - Iterator protocol helpers
  • Key benefit: All methods take Context as parameter instead of capturing it (thread-safe)

ES6IteratorAdapter.java - Iterator.from() wrapper

  • Wraps any iterator to inherit from Iterator.prototype
  • Delegates all operations (next, return, properties) to wrapped iterator
  • Enables proper prototype chain for Iterator.from() results

Enhanced IteratorLikeIterable.java - ECMAScript → Java bridge

  • Handles ECMAScript iterator-like objects per spec
  • Now uses IteratorOperations internally for Context safety
  • Provides Java Iterable interface for Promise.all, Array.from, etc.
  • Added Context-safe overloads: iterator(cx, scope), close(cx, scope)

Why This Matters

Context Safety: Rhino's multi-threaded environment requires careful Context handling. These utilities avoid the common bug of capturing Context in instance variables or closures.

Architecture: Clean separation between ECMAScript iterator protocol and Java's Iterable interface, enabling robust iterator support across Rhino's codebase.

Developer Experience: Consistent, reusable patterns for iterator handling that other contributors can safely use.

@anivar anivar marked this pull request as draft September 12, 2025 18:46
@anivar anivar force-pushed the es2025-iterator-constructor branch 2 times, most recently from a5b0893 to 75d541a Compare September 12, 2025 18:59
@anivar anivar marked this pull request as ready for review September 13, 2025 12:56
@gbrail
Copy link
Collaborator

gbrail commented Sep 16, 2025

Before we get farther on this -- can some of the other users of the project take a look at this feature and let me know how we should handle it? It's the first time we've added a feature flag for something as basic as iterator behavior AFAIK.

When we went through this before, we created VERSION_ECMASCRIPT and said that, in the future, we'd track the spec.

To me, that means that this feature should not have a feature flag, but should instead be invoked if the language version is VERSION_ECMASCRIPT.

Anyone else have an opinion on this?

@anivar anivar force-pushed the es2025-iterator-constructor branch 3 times, most recently from e22bf98 to 15cf2fb Compare October 12, 2025 06:22
@anivar anivar force-pushed the es2025-iterator-constructor branch 3 times, most recently from 37f0c29 to ab2b35d Compare October 12, 2025 16:43
anivar added a commit to anivar/rhino that referenced this pull request Oct 13, 2025
- Remove iterator/iterable support (will be added after Iterator PR merges)
- Replace LambdaFunction with BaseFunction for Context safety
- Remove AsyncIteratorProcessor (not needed without iterator support)
- Keep AsyncArrayLikeProcessor for array-like processing
- Avoids dependency on IteratorLikeIterable which has Context capture issues

This simplified version ensures Array.fromAsync can be merged independently
without waiting for the Iterator constructor PR (mozilla#2078) to be fixed.
@anivar anivar marked this pull request as draft October 13, 2025 12:09
This PR implements the ES2025 Iterator constructor and Iterator.from() static method.

Changes:
- Add NativeES2025Iterator implementing the ES2025 Iterator constructor
- Add ES6IteratorAdapter to wrap iterators with Iterator.prototype
- Add IteratorOperations with Context-safe utility methods
- Fix IteratorLikeIterable Context capture bug (remove Context from instance fields)
- Register NativeES2025Iterator in ScriptRuntime
- Update test262.properties with regenerated test expectations

The Iterator constructor throws TypeError when called directly (per spec).
Iterator.from() wraps iterables to inherit from Iterator.prototype.

Fixed critical thread safety issue where IteratorLikeIterable was storing
Context in instance fields, which breaks in multi-threaded environments.
@anivar anivar force-pushed the es2025-iterator-constructor branch from e682b6a to 671bb1b Compare October 13, 2025 14:55
@rbri
Copy link
Collaborator

rbri commented Oct 13, 2025

can some of the other users of the project take a look at this feature and let me know how we should handle it?

@gbrail before have a serious look at this i like to have it in a good shape. During the last weeks is spend far too much time discussing the pr's having this garbarge test262.properties file.

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.

4 participants