Skip to content

Commit

Permalink
Fix implementation of Iterator interface
Browse files Browse the repository at this point in the history
It looks like you have to implement the Iterator interface *before*
assigning get_iterator. Otherwise the structure for user iterators isn't
correctly zeroed out.

Additionaly I'm setting class_entry->iterator_funcs.funcs now. Not sure if
this is strictly necessary, but better safe than sorry ;)
  • Loading branch information
nikic committed Jul 26, 2012
1 parent 99f93dd commit 268740d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
37 changes: 37 additions & 0 deletions Zend/tests/generators/generator_in_multipleiterator.phpt
@@ -0,0 +1,37 @@
--TEST--
Generators work properly in MultipleIterator
--FILE--
<?php

function gen1() {
yield 'a';
yield 'aa';
}

function gen2() {
yield 'b';
yield 'bb';
}

$it = new MultipleIterator;
$it->attachIterator(gen1());
$it->attachIterator(gen2());

foreach ($it as $values) {
var_dump($values);
}

?>
--EXPECT--
array(2) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
}
array(2) {
[0]=>
string(2) "aa"
[1]=>
string(2) "bb"
}
4 changes: 3 additions & 1 deletion Zend/zend_generators.c
Expand Up @@ -759,9 +759,11 @@ void zend_register_generator_ce(TSRMLS_D) /* {{{ */
zend_ce_generator = zend_register_internal_class(&ce TSRMLS_CC);
zend_ce_generator->ce_flags |= ZEND_ACC_FINAL_CLASS;
zend_ce_generator->create_object = zend_generator_create;
zend_ce_generator->get_iterator = zend_generator_get_iterator;

/* get_iterator has to be assigned *after* implementing the inferface */
zend_class_implements(zend_ce_generator TSRMLS_CC, 1, zend_ce_iterator);
zend_ce_generator->get_iterator = zend_generator_get_iterator;
zend_ce_generator->iterator_funcs.funcs = &zend_generator_iterator_functions;

memcpy(&zend_generator_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
zend_generator_handlers.get_constructor = zend_generator_get_constructor;
Expand Down

0 comments on commit 268740d

Please sign in to comment.