feat(iterator): add MapIterator and support for custom iterators in coroutine opcodes#83
Conversation
siyul-park
left a comment
There was a problem hiding this comment.
PR Review — feat(iterator): add MapIterator and support for custom iterators in coroutine opcodes
Decision
Approve — This PR is focused, correct, well-tested, and ready for production.
Merge Readiness Summary
This PR successfully adds a minimal but powerful iterator abstraction to the VM, extending the coroutine opcode family to work with custom iterator types. The code is architecturally sound, properly handles reference counting for the GC, includes comprehensive test coverage, and documents the new interface clearly. All merge-readiness criteria are satisfied.
Blocking Findings
None.
Important Findings
None. The implementation is solid.
Suggestions
Minor: CI Completion Before Merge
The PR was merged while CI was still in_progress (check started 11:06:07Z, merged 11:06:30Z). While the code quality is excellent, consider waiting for full CI completion before future merges to catch any environment-specific issues early.
Readiness Checklist
- Scope control: Pass — Single focused concern: custom iterator support for coroutine opcodes
- Correctness: Pass — Type dispatch, reference counting, and iterator state management are correct
- Test coverage: Pass — Tests cover multiple map types, custom iterators, and opcode interactions
- Architecture consistency: Pass — Follows existing type-dispatch patterns; Iterator interface is minimal
- Risk / compatibility: Pass — New interface/opcode are additive; no breaking changes
- Maintainability: Pass — Clear code, follows existing conventions, good documentation
- CI / validation: Pass — Tests run, CI in progress at merge time (complete validation recommended)
Detailed Analysis
Scope & Design
The PR introduces two key components:
Iteratorinterface (types/value.go): A minimal 4-method contract (Next(),Current(),Done(), inheritedValue)MapIteratorimplementation (types/map.go): Wraps Go's reflect-based map iterator, properly implementsTraceablefor GC integration
The design is clean: custom producers (host-supplied iterators) can now flow through the same coroutine opcode family as *Coroutine, without requiring snapshotting.
Implementation Correctness
Reference Counting: The PR carefully manages lifetimes:
MAP_ITERopcode retains the current key withretainIteratorCurrentafter advancingresumeIteratorreleases the old current value before advancing, retains the new oneCORO_VALUEcallsboxIteratorCurrent, which retains the returned valueMapIterator.Refs()reports both the source map and any ref-typed current key
Type Safety: All opcode handlers use type switches to distinguish *Coroutine from types.Iterator, with panic on type mismatch—no silent failures.
Initialization: NewMapIterator starts at done=true, but MAP_ITER immediately calls Next() to advance to the first element. This is intentional and correct.
Test Coverage
Tests verify:
- ✓ Different map types (I32, I64, String keys)
- ✓ Iterator correctly yields first key on
CORO_VALUE - ✓
RESUMEadvances to next key - ✓
CORO_DONEcorrectly reports exhaustion - ✓ Custom iterator implementation (testIterator) works end-to-end
Documentation
Documentation updates are clear:
- host-integration.md: Explains custom producer types marshaling to
Iterator - instruction-set.md: Clarifies that
RESUME/CORO_DONE/CORO_VALUEnow accept iterators - value-representation.md: Documents
Iteratorinterface semantics andMapIteratorbehavior
Final Recommendation
Merge — This PR is production-ready. The iterator abstraction is well-designed, the implementation is correct, tests are comprehensive, and documentation is clear. No changes required.
Generated by Claude Code
Changes Made
Related Issues
Additional Information