Drop Generator::close() method

This commit is contained in:
Nikita Popov 2012-08-20 12:53:18 +02:00
parent 7195a5b376
commit 05f10480c5
9 changed files with 6 additions and 94 deletions

View file

@ -20,7 +20,7 @@ $g2->next();
var_dump($g1->current());
var_dump($g2->current());
$g1->close();
unset($g1);
$g2->next();
var_dump($g2->current());

View file

@ -10,7 +10,7 @@ function gen() {
$g1 = gen();
$g1->rewind();
$g2 = clone $g1;
$g1->close();
unset($g1);
$g2->send(10);
?>

View file

@ -19,7 +19,7 @@ function gen() {
$g1 = gen();
$g1->rewind();
$g2 = clone $g1;
$g1->close();
unset($g1);
$g2->next();
?>

View file

@ -16,7 +16,7 @@ class Test {
$g1 = (new Test)->gen();
$g1->rewind(); // goto yield
$g2 = clone $g1;
$g1->close();
unset($g1);
$g2->next();
?>

View file

@ -1,22 +0,0 @@
--TEST--
Calling close() during the exectution of the generator
--FILE--
<?php
function gen() {
/* Pass the generator object itself in */
$gen = yield;
/* Close generator while it is currently running */
$gen->close();
echo "Still running";
}
$gen = gen();
$gen->send($gen);
?>
--EXPECTF--
Warning: A generator cannot be closed while it is running in %s on line %d
Still running

View file

@ -1,32 +0,0 @@
--TEST--
Generator can be closed by calling ->close()
--FILE--
<?php
function allNumbers() {
for ($i = 0; true; ++$i) {
yield $i;
}
}
$numbers = allNumbers();
foreach ($numbers as $n) {
var_dump($n);
if ($n == 9) {
$numbers->close();
}
}
?>
--EXPECT--
int(0)
int(1)
int(2)
int(3)
int(4)
int(5)
int(6)
int(7)
int(8)
int(9)

View file

@ -20,13 +20,13 @@ $gen->send('foo');
// test resource cleanup
$gen = gen();
$gen->rewind();
$gen->close();
unset($gen);
// test cloning
$g1 = gen();
$g1->rewind();
$g2 = clone $g1;
$g1->close();
unset($g1);
$g2->send('bar');
?>

View file

@ -301,8 +301,6 @@ static zend_object_value zend_generator_create(zend_class_entry *class_type TSRM
/* The key will be incremented on first use, so it'll start at 0 */
generator->largest_used_integer_key = -1;
generator->is_currently_running = 0;
zend_object_std_init(&generator->std, class_type TSRMLS_CC);
object.handle = zend_objects_store_put(generator, NULL,
@ -391,8 +389,6 @@ static void zend_generator_resume(zend_generator *generator TSRMLS_DC) /* {{{ */
zend_class_entry *original_scope = EG(scope);
zend_class_entry *original_called_scope = EG(called_scope);
zend_bool original_is_currently_running = generator->is_currently_running;
/* Remember the current stack position so we can back up pushed args */
generator->original_stack_top = zend_vm_stack_top(TSRMLS_C);
@ -417,8 +413,6 @@ static void zend_generator_resume(zend_generator *generator TSRMLS_DC) /* {{{ */
EG(scope) = generator->execute_data->current_scope;
EG(called_scope) = generator->execute_data->current_called_scope;
generator->is_currently_running = 1;
/* We want the backtrace to look as if the generator function was
* called from whatever method we are current running (e.g. next()).
* The first prev_execute_data contains an additional stack frame,
@ -440,8 +434,6 @@ static void zend_generator_resume(zend_generator *generator TSRMLS_DC) /* {{{ */
EG(scope) = original_scope;
EG(called_scope) = original_called_scope;
generator->is_currently_running = original_is_currently_running;
/* The stack top before and after the execution differ, i.e. there are
* arguments pushed to the stack. */
if (generator->original_stack_top != zend_vm_stack_top(TSRMLS_C)) {
@ -598,27 +590,6 @@ ZEND_METHOD(Generator, send)
}
}
/* {{{ proto void Generator::close()
* Closes the generator */
ZEND_METHOD(Generator, close)
{
zend_generator *generator;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
if (generator->is_currently_running) {
zend_error(E_WARNING, "A generator cannot be closed while it is running");
return;
}
zend_generator_close(generator, 0 TSRMLS_CC);
}
/* }}} */
/* get_iterator implementation */
typedef struct _zend_generator_iterator {
@ -747,7 +718,6 @@ static const zend_function_entry generator_functions[] = {
ZEND_ME(Generator, key, arginfo_generator_void, ZEND_ACC_PUBLIC)
ZEND_ME(Generator, next, arginfo_generator_void, ZEND_ACC_PUBLIC)
ZEND_ME(Generator, send, arginfo_generator_send, ZEND_ACC_PUBLIC)
ZEND_ME(Generator, close, arginfo_generator_void, ZEND_ACC_PUBLIC)
ZEND_FE_END
};

View file

@ -46,10 +46,6 @@ typedef struct _zend_generator {
temp_variable *send_target;
/* Largest used integer key for auto-incrementing keys */
long largest_used_integer_key;
/* We need to know whether the generator is currently executed to avoid it
* being closed while still running */
zend_bool is_currently_running;
} zend_generator;
extern ZEND_API zend_class_entry *zend_ce_generator;