Skip to content
Browse files

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

This makes the API easier to use (and is consistent with Python and JS).
  • Loading branch information...
1 parent ee89e22 commit 1477be9aa88689f7e547a24258dc4d63637fd5b0 @nikic nikic committed May 31, 2012
Showing with 25 additions and 1 deletion.
  1. +20 −0 Zend/tests/generators/send_returns_current.phpt
  2. +5 −1 Zend/zend_generators.c
View
20 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"
View
6 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()

0 comments on commit 1477be9

Please sign in to comment.
Something went wrong with that request. Please try again.