Commit: 1477be9aa88689f7e547a24258dc4d63637fd5b0 Author: Nikita Popov <ni...@php.net> Thu, 31 May 2012 20:03:18 +0200 Parents: ee89e228f6f684555dd219d8a46d173cfed3230a Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=1477be9aa88689f7e547a24258dc4d63637fd5b0 Log: Make $generator->send() return the current value This makes the API easier to use (and is consistent with Python and JS). Changed paths: A Zend/tests/generators/send_returns_current.phpt M Zend/zend_generators.c Diff: diff --git a/Zend/tests/generators/send_returns_current.phpt b/Zend/tests/generators/send_returns_current.phpt new file mode 100644 index 0000000..d3a4afd --- /dev/null +++ b/Zend/tests/generators/send_returns_current.phpt @@ -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" diff --git a/Zend/zend_generators.c b/Zend/zend_generators.c index 4754d75..5b58e21 100644 --- a/Zend/zend_generators.c +++ b/Zend/zend_generators.c @@ -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() -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php