Skip to content

Conversation

@anivar
Copy link
Contributor

@anivar anivar commented Sep 12, 2025

Summary

This PR adds the Iterator.from() static method, continuing the ES2025 iterator helpers implementation started in #2078.

What's Added

Iterator.from() converts any iterable or iterator-like object into a proper iterator that inherits from Iterator.prototype. This includes:

  • String primitives get special handling since they're iterable
  • Arrays, Sets, Maps work through their Symbol.iterator
  • Objects with a next() method are treated as iterator-like
  • Objects with null/undefined Symbol.iterator are handled as iterator-like (per spec)
  • Already-proper iterators are returned unchanged

Implementation Approach

The IteratorWrapper class follows the same pattern as NativeArrayIterator - it extends ES6Iterator and uses the standard delegation pattern through isDone() and nextValue(). This keeps things consistent with how Rhino handles iterators elsewhere in the codebase.

The wrapper also handles return() and throw() methods when they exist on the underlying iterator, ensuring proper cleanup and error propagation.

Known Limitation

The global Iterator constructor isn't exposed yet because it conflicts with the legacy Iterator() function that's used for Java interop. We'll need to figure out a migration path for this before the full iterator helpers can be used from JavaScript.

Testing

Basic tests verify that Iterator.prototype exists with the right Symbol properties. The infrastructure is ready for more comprehensive testing once we resolve the global naming conflict.

Part of #1696
Depends on #2078

Refactored the ES2025 Iterator global constructor for better maintainability and spec compliance.

Changes:
- Cleaned up NativeIteratorConstructor with better documentation
- Fixed initialization to work properly with Symbol properties
- Changed from lazy loading to direct initialization (required for Symbol properties)
- Improved error messages using typeErrorById
- Added proper JavaDoc comments

Testing:
- Iterator constructor works as expected
- Symbol.toStringTag and Symbol.iterator properties properly set
- Iterator() and new Iterator() correctly throw TypeError

Related to mozilla#1696
The ES2025 Iterator constructor was overwriting the legacy Iterator()
function used for Java interop, causing test failures.

This change temporarily disables the global Iterator property while
keeping Iterator.prototype properly initialized for future iterator
helpers. The legacy Iterator() function for Java interop remains
functional.

A migration path is needed to have both coexist in the future.
Adds Iterator.from() to convert any iterable or iterator-like object
to an iterator that inherits from Iterator.prototype.

Implementation includes:
- Iterator.from() static method on NativeIteratorConstructor
- IteratorWrapper class to wrap iterators with proper prototype chain
- Support for arrays, strings, sets, maps, and custom iterables
- Proper handling of existing iterators (returns them as-is if already inheriting from Iterator.prototype)
- Tests for Iterator infrastructure

Part of ES2025 Iterator Helpers implementation.
- Handle string primitives specially (they are iterable)
- Correctly handle null/undefined Symbol.iterator (treat as iterator-like)
- Improve error handling in IteratorWrapper return() method
- Better alignment with ES2025 GetIteratorFlattenable algorithm
- Fix primitive type checking to throw appropriate errors
- Extend ES6Iterator instead of ScriptableObject for consistency
- Follow Rhino's iterator implementation patterns (like NativeArrayIterator)
- Use proper prototype ID system for methods
- Implement proper delegation through isDone() and nextValue()
- Better integration with existing Rhino iterator infrastructure
- Maintain serialization compatibility
@anivar
Copy link
Contributor Author

anivar commented Sep 13, 2025

Closing in favor of the more comprehensive implementation in #2078 which includes:

  • Feature flag system for backward compatibility
  • Iterator.from() implementation (this PR's goal)
  • Iterator.prototype.toArray()
  • Proper test infrastructure updates
  • 87.96% test262 pass rate

The implementation in #2078 supersedes this PR.

@anivar anivar closed this Sep 13, 2025
@anivar anivar deleted the es2025-iterator-from branch September 13, 2025 18:40
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