Implement ES2025 Iterator.from() static method#2082
Closed
anivar wants to merge 5 commits intomozilla:masterfrom
Closed
Implement ES2025 Iterator.from() static method#2082anivar wants to merge 5 commits intomozilla:masterfrom
anivar wants to merge 5 commits intomozilla:masterfrom
Conversation
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
Contributor
Author
|
Closing in favor of the more comprehensive implementation in #2078 which includes:
The implementation in #2078 supersedes this PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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