Make $generator->send() return the current value

This makes the API easier to use (and is consistent with Python and JS).
This commit is contained in:
Nikita Popov 2012-05-31 20:03:18 +02:00
parent ee89e228f6
commit 1477be9aa8
2 changed files with 25 additions and 1 deletions

View File

@ -0,0 +1,20 @@
--TEST--
$generator->send() returns the yielded value
--FILE--
<?php
function *reverseEchoGenerator() {
$data = yield;
while (true) {
$data = yield strrev($data);
}
}
$gen = reverseEchoGenerator();
var_dump($gen->send('foo'));
var_dump($gen->send('bar'));
?>
--EXPECT--
string(3) "oof"
string(3) "rab"

View File

@ -336,7 +336,7 @@ ZEND_METHOD(Generator, next)
}
/* }}} */
/* {{{ proto void Generator::send()
/* {{{ proto mixed Generator::send()
* Sends a value to the generator */
ZEND_METHOD(Generator, send)
{
@ -366,6 +366,10 @@ ZEND_METHOD(Generator, send)
generator->send_target->var.ptr_ptr = &value;
zend_generator_resume(object, generator TSRMLS_CC);
if (generator->value) {
RETURN_ZVAL(generator->value, 1, 0);
}
}
/* {{{ proto void Generator::close()