Skip to content

Stdlib: iterator_to_array() — materialize Traversable/Generator (ext/spl parity) #3100

Description

@PurHur

Category

stdlib

Status (refreshed 2026-06-02 — backlog-architect)

Phase 1 landed: iterator_to_array() is registered in ext/standard/Module.php. VM handles arrays and Generators via VM::iteratorToArray(); JIT handles compile-time Generator args via JitIteratorToArray.

Remaining gap (phase 2 → #4244): Iterator / IteratorAggregate objects still throw LogicException — see lib/VM.php:705-707.

Piece Status
Builtin registered
VM: array input
VM: Generator input
VM: Iterator/IteratorAggregate ❌ → #4244
JIT: runtime $preserve_keys ❌ (compile-time bool only)
JIT: Iterator objects

Problem (original)

Zend iterator_to_array(Traversable $iterator, bool $preserve_keys = false) materializes any traversable into an array. Frameworks use it for generator→array conversion and SPL iterators.

php-src reference

Repro — Generator (passes today)

<?php
$g = (function () { yield 1; yield 2; })();
var_export(iterator_to_array($g));
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php -r "
\$g = (function () { yield 1; yield 2; })();
var_export(iterator_to_array(\$g));
"'
Engine Result
Zend PHP 8.3+ array ( 0 => 1, 1 => 2, )
This compiler VM ✅ same (after phase 1)

Repro — Iterator object (still fails → #4244)

<?php
class OneShot implements Iterator {
    private bool $done = false;
    public function rewind(): void { $this->done = false; }
    public function valid(): bool { return !$this->done; }
    public function current(): int { return 42; }
    public function key(): int { return 0; }
    public function next(): void { $this->done = true; }
}
var_export(iterator_to_array(new OneShot()));

Actual: LogicException: iterator_to_array() argument must be an array or Generator

Scope (close this issue when)

Done when (this issue)

Related

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions