2012-05-20 06:03:27 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
2013-01-01 16:37:09 +08:00
|
|
|
| Copyright (c) 1998-2013 Zend Technologies Ltd. (http://www.zend.com) |
|
2012-05-20 06:03:27 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 2.00 of the Zend license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available through the world-wide-web at the following url: |
|
|
|
|
| http://www.zend.com/license/2_00.txt. |
|
|
|
|
| If you did not receive a copy of the Zend license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@zend.com so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Nikita Popov <nikic@php.net> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include "zend.h"
|
|
|
|
#include "zend_API.h"
|
2012-05-23 22:07:15 +08:00
|
|
|
#include "zend_interfaces.h"
|
2012-07-22 23:46:46 +08:00
|
|
|
#include "zend_exceptions.h"
|
2012-05-20 06:03:27 +08:00
|
|
|
#include "zend_generators.h"
|
|
|
|
|
|
|
|
ZEND_API zend_class_entry *zend_ce_generator;
|
2012-05-20 20:19:16 +08:00
|
|
|
static zend_object_handlers zend_generator_handlers;
|
|
|
|
|
2013-02-02 01:33:26 +08:00
|
|
|
static zend_object_value zend_generator_create(zend_class_entry *class_type TSRMLS_DC);
|
|
|
|
|
2012-12-11 21:25:32 +08:00
|
|
|
ZEND_API void zend_generator_close(zend_generator *generator, zend_bool finished_execution TSRMLS_DC) /* {{{ */
|
2012-05-20 20:19:16 +08:00
|
|
|
{
|
2012-12-22 00:28:20 +08:00
|
|
|
if (generator->value) {
|
|
|
|
zval_ptr_dtor(&generator->value);
|
|
|
|
generator->value = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (generator->key) {
|
|
|
|
zval_ptr_dtor(&generator->key);
|
|
|
|
generator->key = NULL;
|
|
|
|
}
|
|
|
|
|
2012-05-23 20:20:25 +08:00
|
|
|
if (generator->execute_data) {
|
|
|
|
zend_execute_data *execute_data = generator->execute_data;
|
2012-08-25 23:40:08 +08:00
|
|
|
zend_op_array *op_array = execute_data->op_array;
|
2012-12-12 21:47:55 +08:00
|
|
|
|
2012-05-23 20:20:25 +08:00
|
|
|
if (!execute_data->symbol_table) {
|
2012-12-04 14:14:39 +08:00
|
|
|
zend_free_compiled_variables(execute_data);
|
2012-05-23 20:20:25 +08:00
|
|
|
} else {
|
2012-06-21 03:31:23 +08:00
|
|
|
zend_clean_and_cache_symbol_table(execute_data->symbol_table TSRMLS_CC);
|
2012-05-23 20:20:25 +08:00
|
|
|
}
|
|
|
|
|
2012-05-27 09:50:31 +08:00
|
|
|
if (execute_data->current_this) {
|
|
|
|
zval_ptr_dtor(&execute_data->current_this);
|
|
|
|
}
|
|
|
|
|
2013-07-08 05:40:16 +08:00
|
|
|
/* A fatal error / die occurred during the generator execution. Trying to clean
|
2013-06-30 03:51:54 +08:00
|
|
|
* up the stack may not be safe in this case. */
|
|
|
|
if (CG(unclean_shutdown)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-27 23:14:20 +08:00
|
|
|
/* If the generator is closed before it can finish execution (reach
|
|
|
|
* a return statement) we have to free loop variables manually, as
|
|
|
|
* we don't know whether the SWITCH_FREE / FREE opcodes have run */
|
|
|
|
if (!finished_execution) {
|
2012-08-25 23:40:08 +08:00
|
|
|
/* -1 required because we want the last run opcode, not the
|
|
|
|
* next to-be-run one. */
|
|
|
|
zend_uint op_num = execute_data->opline - op_array->opcodes - 1;
|
2012-05-27 23:14:20 +08:00
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < op_array->last_brk_cont; ++i) {
|
|
|
|
zend_brk_cont_element *brk_cont = op_array->brk_cont_array + i;
|
|
|
|
|
|
|
|
if (brk_cont->start < 0) {
|
|
|
|
continue;
|
|
|
|
} else if (brk_cont->start > op_num) {
|
|
|
|
break;
|
|
|
|
} else if (brk_cont->brk > op_num) {
|
|
|
|
zend_op *brk_opline = op_array->opcodes + brk_cont->brk;
|
|
|
|
|
|
|
|
switch (brk_opline->opcode) {
|
|
|
|
case ZEND_SWITCH_FREE:
|
|
|
|
{
|
2012-12-04 14:14:39 +08:00
|
|
|
temp_variable *var = EX_TMP_VAR(execute_data, brk_opline->op1.var);
|
2012-05-27 23:14:20 +08:00
|
|
|
zval_ptr_dtor(&var->var.ptr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZEND_FREE:
|
|
|
|
{
|
2012-12-04 14:14:39 +08:00
|
|
|
temp_variable *var = EX_TMP_VAR(execute_data, brk_opline->op1.var);
|
2012-05-27 23:14:20 +08:00
|
|
|
zval_dtor(&var->tmp_var);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-31 06:00:30 +08:00
|
|
|
/* Clear any backed up stack arguments */
|
2012-11-30 17:39:23 +08:00
|
|
|
if (generator->stack != EG(argument_stack)) {
|
2013-02-02 02:48:05 +08:00
|
|
|
void **ptr = generator->stack->top - 1;
|
|
|
|
void **end = zend_vm_stack_frame_base(execute_data);
|
|
|
|
|
|
|
|
/* If the top stack element is the argument count, skip it */
|
|
|
|
if (execute_data->function_state.arguments) {
|
|
|
|
ptr--;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; ptr >= end; --ptr) {
|
|
|
|
zval_ptr_dtor((zval**) ptr);
|
2012-05-31 06:00:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-30 17:39:23 +08:00
|
|
|
while (execute_data->call >= execute_data->call_slots) {
|
|
|
|
if (execute_data->call->object) {
|
|
|
|
zval_ptr_dtor(&execute_data->call->object);
|
2012-09-23 01:12:21 +08:00
|
|
|
}
|
2012-11-30 17:39:23 +08:00
|
|
|
execute_data->call--;
|
|
|
|
}
|
2012-09-23 01:12:21 +08:00
|
|
|
|
2012-06-09 06:40:47 +08:00
|
|
|
/* We have added an additional stack frame in prev_execute_data, so we
|
|
|
|
* have to free it. It also contains the arguments passed to the
|
|
|
|
* generator (for func_get_args) so those have to be freed too. */
|
|
|
|
{
|
|
|
|
zend_execute_data *prev_execute_data = execute_data->prev_execute_data;
|
|
|
|
void **arguments = prev_execute_data->function_state.arguments;
|
|
|
|
|
|
|
|
if (arguments) {
|
|
|
|
int arguments_count = (int) (zend_uintptr_t) *arguments;
|
|
|
|
zval **arguments_start = (zval **) (arguments - arguments_count);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < arguments_count; ++i) {
|
|
|
|
zval_ptr_dtor(arguments_start + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-05 13:50:55 +08:00
|
|
|
/* Free a clone of closure */
|
|
|
|
if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
|
|
|
|
destroy_op_array(op_array TSRMLS_CC);
|
|
|
|
efree(op_array);
|
|
|
|
}
|
|
|
|
|
2012-11-30 17:39:23 +08:00
|
|
|
efree(generator->stack);
|
|
|
|
if (generator->stack == EG(argument_stack)) {
|
|
|
|
/* abnormal exit for running generator */
|
|
|
|
EG(argument_stack) = NULL;
|
|
|
|
}
|
2012-05-27 05:59:22 +08:00
|
|
|
generator->execute_data = NULL;
|
2012-05-23 20:20:25 +08:00
|
|
|
}
|
2012-05-27 05:59:22 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2013-01-31 06:52:02 +08:00
|
|
|
static void zend_generator_dtor_storage(zend_generator *generator, zend_object_handle handle TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
zend_execute_data *ex = generator->execute_data;
|
|
|
|
zend_uint op_num, finally_op_num;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!ex || !ex->op_array->has_finally_block) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -1 required because we want the last run opcode, not the
|
|
|
|
* next to-be-run one. */
|
|
|
|
op_num = ex->opline - ex->op_array->opcodes - 1;
|
|
|
|
|
|
|
|
/* Find next finally block */
|
|
|
|
finally_op_num = 0;
|
|
|
|
for (i = 0; i < ex->op_array->last_try_catch; i++) {
|
|
|
|
zend_try_catch_element *try_catch = &ex->op_array->try_catch_array[i];
|
|
|
|
|
|
|
|
if (op_num < try_catch->try_op) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (op_num < try_catch->finally_op) {
|
|
|
|
finally_op_num = try_catch->finally_op;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If a finally block was found we jump directly to it and
|
|
|
|
* resume the generator. */
|
|
|
|
if (finally_op_num) {
|
|
|
|
ex->opline = &ex->op_array->opcodes[finally_op_num];
|
|
|
|
ex->fast_ret = NULL;
|
|
|
|
generator->flags |= ZEND_GENERATOR_FORCED_CLOSE;
|
|
|
|
zend_generator_resume(generator TSRMLS_CC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2012-05-27 05:59:22 +08:00
|
|
|
static void zend_generator_free_storage(zend_generator *generator TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
2012-05-27 23:14:20 +08:00
|
|
|
zend_generator_close(generator, 0 TSRMLS_CC);
|
2012-05-27 05:59:22 +08:00
|
|
|
|
|
|
|
zend_object_std_dtor(&generator->std TSRMLS_CC);
|
2012-05-20 20:19:16 +08:00
|
|
|
efree(generator);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
static zend_object_value zend_generator_create(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
zend_generator *generator;
|
|
|
|
zend_object_value object;
|
|
|
|
|
|
|
|
generator = emalloc(sizeof(zend_generator));
|
|
|
|
memset(generator, 0, sizeof(zend_generator));
|
|
|
|
|
2012-05-30 11:05:49 +08:00
|
|
|
/* The key will be incremented on first use, so it'll start at 0 */
|
|
|
|
generator->largest_used_integer_key = -1;
|
|
|
|
|
2012-05-20 20:19:16 +08:00
|
|
|
zend_object_std_init(&generator->std, class_type TSRMLS_CC);
|
|
|
|
|
2013-01-31 06:52:02 +08:00
|
|
|
object.handle = zend_objects_store_put(generator,
|
|
|
|
(zend_objects_store_dtor_t) zend_generator_dtor_storage,
|
2012-05-20 20:19:16 +08:00
|
|
|
(zend_objects_free_object_storage_t) zend_generator_free_storage,
|
2013-02-02 01:33:26 +08:00
|
|
|
NULL TSRMLS_CC
|
2012-05-20 20:19:16 +08:00
|
|
|
);
|
|
|
|
object.handlers = &zend_generator_handlers;
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2012-07-20 06:49:50 +08:00
|
|
|
/* Requires globals EG(scope), EG(current_scope), EG(This),
|
|
|
|
* EG(active_symbol_table) and EG(current_execute_data). */
|
2012-12-11 21:25:32 +08:00
|
|
|
ZEND_API zval *zend_generator_create_zval(zend_op_array *op_array TSRMLS_DC) /* {{{ */
|
2012-07-20 06:49:50 +08:00
|
|
|
{
|
|
|
|
zval *return_value;
|
|
|
|
zend_generator *generator;
|
2012-09-05 18:53:29 +08:00
|
|
|
zend_execute_data *current_execute_data;
|
|
|
|
zend_op **opline_ptr;
|
2012-11-30 17:39:23 +08:00
|
|
|
HashTable *current_symbol_table;
|
2012-09-05 18:53:29 +08:00
|
|
|
zend_execute_data *execute_data;
|
2012-11-30 17:39:23 +08:00
|
|
|
zend_vm_stack current_stack = EG(argument_stack);
|
2012-07-20 06:49:50 +08:00
|
|
|
|
2012-09-05 13:50:55 +08:00
|
|
|
/* Create a clone of closure, because it may be destroyed */
|
|
|
|
if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
|
|
|
|
zend_op_array *op_array_copy = (zend_op_array*)emalloc(sizeof(zend_op_array));
|
|
|
|
*op_array_copy = *op_array;
|
2012-09-17 04:01:07 +08:00
|
|
|
function_add_ref((zend_function *) op_array_copy);
|
2012-09-05 13:50:55 +08:00
|
|
|
op_array = op_array_copy;
|
|
|
|
}
|
|
|
|
|
2012-07-20 06:49:50 +08:00
|
|
|
/* Create new execution context. We have to back up and restore
|
2012-11-30 17:39:23 +08:00
|
|
|
* EG(current_execute_data), EG(opline_ptr) and EG(active_symbol_table)
|
|
|
|
* here because the function modifies or uses them */
|
2012-09-05 18:53:29 +08:00
|
|
|
current_execute_data = EG(current_execute_data);
|
|
|
|
opline_ptr = EG(opline_ptr);
|
2012-11-30 17:39:23 +08:00
|
|
|
current_symbol_table = EG(active_symbol_table);
|
|
|
|
EG(active_symbol_table) = NULL;
|
2012-09-05 18:53:29 +08:00
|
|
|
execute_data = zend_create_execute_data_from_op_array(op_array, 0 TSRMLS_CC);
|
2012-11-30 17:39:23 +08:00
|
|
|
EG(active_symbol_table) = current_symbol_table;
|
2012-07-20 06:49:50 +08:00
|
|
|
EG(current_execute_data) = current_execute_data;
|
|
|
|
EG(opline_ptr) = opline_ptr;
|
|
|
|
|
|
|
|
ALLOC_INIT_ZVAL(return_value);
|
|
|
|
object_init_ex(return_value, zend_ce_generator);
|
|
|
|
|
|
|
|
if (EG(This)) {
|
|
|
|
Z_ADDREF_P(EG(This));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Back up executor globals. */
|
|
|
|
execute_data->current_scope = EG(scope);
|
|
|
|
execute_data->current_called_scope = EG(called_scope);
|
|
|
|
execute_data->symbol_table = EG(active_symbol_table);
|
|
|
|
execute_data->current_this = EG(This);
|
|
|
|
|
|
|
|
/* Save execution context in generator object. */
|
|
|
|
generator = (zend_generator *) zend_object_store_get_object(return_value TSRMLS_CC);
|
|
|
|
generator->execute_data = execute_data;
|
2012-11-30 17:39:23 +08:00
|
|
|
generator->stack = EG(argument_stack);
|
|
|
|
EG(argument_stack) = current_stack;
|
2012-07-20 06:49:50 +08:00
|
|
|
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2012-05-20 20:19:16 +08:00
|
|
|
static zend_function *zend_generator_get_constructor(zval *object TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
zend_error(E_RECOVERABLE_ERROR, "The \"Generator\" class is reserved for internal use and cannot be manually instantiated");
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* }}} */
|
2012-05-20 06:03:27 +08:00
|
|
|
|
2012-12-11 21:25:32 +08:00
|
|
|
ZEND_API void zend_generator_resume(zend_generator *generator TSRMLS_DC) /* {{{ */
|
2012-05-26 23:53:13 +08:00
|
|
|
{
|
2012-05-27 05:59:22 +08:00
|
|
|
/* The generator is already closed, thus can't resume */
|
|
|
|
if (!generator->execute_data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-25 23:40:08 +08:00
|
|
|
if (generator->flags & ZEND_GENERATOR_CURRENTLY_RUNNING) {
|
|
|
|
zend_error(E_ERROR, "Cannot resume an already running generator");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Drop the AT_FIRST_YIELD flag */
|
|
|
|
generator->flags &= ~ZEND_GENERATOR_AT_FIRST_YIELD;
|
|
|
|
|
2012-05-27 09:50:31 +08:00
|
|
|
{
|
|
|
|
/* Backup executor globals */
|
|
|
|
zval **original_return_value_ptr_ptr = EG(return_value_ptr_ptr);
|
2012-05-28 06:24:58 +08:00
|
|
|
zend_execute_data *original_execute_data = EG(current_execute_data);
|
2012-05-27 09:50:31 +08:00
|
|
|
zend_op **original_opline_ptr = EG(opline_ptr);
|
|
|
|
zend_op_array *original_active_op_array = EG(active_op_array);
|
|
|
|
HashTable *original_active_symbol_table = EG(active_symbol_table);
|
|
|
|
zval *original_This = EG(This);
|
|
|
|
zend_class_entry *original_scope = EG(scope);
|
|
|
|
zend_class_entry *original_called_scope = EG(called_scope);
|
2012-11-30 17:39:23 +08:00
|
|
|
zend_vm_stack original_stack = EG(argument_stack);
|
2012-09-23 01:12:21 +08:00
|
|
|
|
2012-05-27 09:50:31 +08:00
|
|
|
/* We (mis)use the return_value_ptr_ptr to provide the generator object
|
|
|
|
* to the executor, so YIELD will be able to set the yielded value */
|
2012-06-23 20:43:52 +08:00
|
|
|
EG(return_value_ptr_ptr) = (zval **) generator;
|
2012-05-27 09:50:31 +08:00
|
|
|
|
|
|
|
/* Set executor globals */
|
2012-05-28 06:24:58 +08:00
|
|
|
EG(current_execute_data) = generator->execute_data;
|
2012-05-27 09:50:31 +08:00
|
|
|
EG(opline_ptr) = &generator->execute_data->opline;
|
|
|
|
EG(active_op_array) = generator->execute_data->op_array;
|
|
|
|
EG(active_symbol_table) = generator->execute_data->symbol_table;
|
|
|
|
EG(This) = generator->execute_data->current_this;
|
|
|
|
EG(scope) = generator->execute_data->current_scope;
|
|
|
|
EG(called_scope) = generator->execute_data->current_called_scope;
|
2012-11-30 17:39:23 +08:00
|
|
|
EG(argument_stack) = generator->stack;
|
2012-05-27 09:50:31 +08:00
|
|
|
|
2012-06-09 06:40:47 +08:00
|
|
|
/* 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,
|
|
|
|
* which makes the generator function show up in the backtrace and
|
|
|
|
* makes the arguments available to func_get_args(). So we have to
|
|
|
|
* set the prev_execute_data of that prev_execute_data :) */
|
|
|
|
generator->execute_data->prev_execute_data->prev_execute_data = original_execute_data;
|
2012-06-03 08:00:11 +08:00
|
|
|
|
2012-05-27 09:50:31 +08:00
|
|
|
/* Resume execution */
|
2012-08-25 23:40:08 +08:00
|
|
|
generator->flags |= ZEND_GENERATOR_CURRENTLY_RUNNING;
|
2012-11-30 17:39:23 +08:00
|
|
|
zend_execute_ex(generator->execute_data TSRMLS_CC);
|
2012-08-25 23:40:08 +08:00
|
|
|
generator->flags &= ~ZEND_GENERATOR_CURRENTLY_RUNNING;
|
2012-05-27 09:50:31 +08:00
|
|
|
|
|
|
|
/* Restore executor globals */
|
|
|
|
EG(return_value_ptr_ptr) = original_return_value_ptr_ptr;
|
2012-05-28 06:24:58 +08:00
|
|
|
EG(current_execute_data) = original_execute_data;
|
2012-05-27 09:50:31 +08:00
|
|
|
EG(opline_ptr) = original_opline_ptr;
|
|
|
|
EG(active_op_array) = original_active_op_array;
|
|
|
|
EG(active_symbol_table) = original_active_symbol_table;
|
|
|
|
EG(This) = original_This;
|
|
|
|
EG(scope) = original_scope;
|
|
|
|
EG(called_scope) = original_called_scope;
|
2012-11-30 17:39:23 +08:00
|
|
|
EG(argument_stack) = original_stack;
|
2012-09-23 01:12:21 +08:00
|
|
|
|
2012-07-22 23:46:46 +08:00
|
|
|
/* If an exception was thrown in the generator we have to internally
|
|
|
|
* rethrow it in the parent scope. */
|
|
|
|
if (UNEXPECTED(EG(exception) != NULL)) {
|
|
|
|
zend_throw_exception_internal(NULL TSRMLS_CC);
|
|
|
|
}
|
2012-05-27 09:50:31 +08:00
|
|
|
}
|
2012-05-26 23:53:13 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
static void zend_generator_ensure_initialized(zend_generator *generator TSRMLS_DC) /* {{{ */
|
2012-05-26 23:53:13 +08:00
|
|
|
{
|
2012-08-30 02:31:34 +08:00
|
|
|
if (generator->execute_data && !generator->value) {
|
2012-06-23 20:43:52 +08:00
|
|
|
zend_generator_resume(generator TSRMLS_CC);
|
2012-08-25 23:40:08 +08:00
|
|
|
generator->flags |= ZEND_GENERATOR_AT_FIRST_YIELD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
static void zend_generator_rewind(zend_generator *generator TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
zend_generator_ensure_initialized(generator TSRMLS_CC);
|
|
|
|
|
|
|
|
if (!(generator->flags & ZEND_GENERATOR_AT_FIRST_YIELD)) {
|
|
|
|
zend_throw_exception(NULL, "Cannot rewind a generator that was already run", 0 TSRMLS_CC);
|
2012-05-26 23:53:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2012-05-23 22:07:15 +08:00
|
|
|
/* {{{ proto void Generator::rewind()
|
|
|
|
* Rewind the generator */
|
|
|
|
ZEND_METHOD(Generator, rewind)
|
|
|
|
{
|
2012-05-26 23:53:13 +08:00
|
|
|
zend_generator *generator;
|
|
|
|
|
2012-05-23 22:07:15 +08:00
|
|
|
if (zend_parse_parameters_none() == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
2012-05-26 23:53:13 +08:00
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
|
2012-05-26 23:53:13 +08:00
|
|
|
|
2012-08-25 23:40:08 +08:00
|
|
|
zend_generator_rewind(generator TSRMLS_CC);
|
2012-05-23 22:07:15 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto bool Generator::valid()
|
|
|
|
* Check whether the generator is valid */
|
|
|
|
ZEND_METHOD(Generator, valid)
|
|
|
|
{
|
2012-05-26 23:53:13 +08:00
|
|
|
zend_generator *generator;
|
|
|
|
|
2012-05-23 22:07:15 +08:00
|
|
|
if (zend_parse_parameters_none() == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
2012-05-26 23:53:13 +08:00
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
|
2012-05-26 23:53:13 +08:00
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
zend_generator_ensure_initialized(generator TSRMLS_CC);
|
2012-05-27 05:07:05 +08:00
|
|
|
|
|
|
|
RETURN_BOOL(generator->value != NULL);
|
2012-05-23 22:07:15 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto mixed Generator::current()
|
|
|
|
* Get the current value */
|
|
|
|
ZEND_METHOD(Generator, current)
|
|
|
|
{
|
2012-05-26 23:53:13 +08:00
|
|
|
zend_generator *generator;
|
|
|
|
|
2012-05-23 22:07:15 +08:00
|
|
|
if (zend_parse_parameters_none() == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
2012-05-26 23:53:13 +08:00
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
|
2012-05-26 23:53:13 +08:00
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
zend_generator_ensure_initialized(generator TSRMLS_CC);
|
2012-05-27 05:07:05 +08:00
|
|
|
|
|
|
|
if (generator->value) {
|
2013-08-27 01:14:34 +08:00
|
|
|
RETURN_ZVAL_FAST(generator->value);
|
2012-05-27 05:07:05 +08:00
|
|
|
}
|
2012-05-23 22:07:15 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto mixed Generator::key()
|
|
|
|
* Get the current key */
|
|
|
|
ZEND_METHOD(Generator, key)
|
|
|
|
{
|
2012-05-26 23:53:13 +08:00
|
|
|
zend_generator *generator;
|
|
|
|
|
2012-05-23 22:07:15 +08:00
|
|
|
if (zend_parse_parameters_none() == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
2012-05-26 23:53:13 +08:00
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
|
2012-05-26 23:53:13 +08:00
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
zend_generator_ensure_initialized(generator TSRMLS_CC);
|
2012-05-30 08:44:06 +08:00
|
|
|
|
|
|
|
if (generator->key) {
|
2013-08-27 01:14:34 +08:00
|
|
|
RETURN_ZVAL_FAST(generator->key);
|
2012-05-30 08:44:06 +08:00
|
|
|
}
|
2012-05-23 22:07:15 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto void Generator::next()
|
|
|
|
* Advances the generator */
|
|
|
|
ZEND_METHOD(Generator, next)
|
|
|
|
{
|
2012-05-26 23:53:13 +08:00
|
|
|
zend_generator *generator;
|
|
|
|
|
2012-05-23 22:07:15 +08:00
|
|
|
if (zend_parse_parameters_none() == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
2012-05-26 23:53:13 +08:00
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
|
2012-05-26 23:53:13 +08:00
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
zend_generator_ensure_initialized(generator TSRMLS_CC);
|
2012-05-27 05:07:05 +08:00
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
zend_generator_resume(generator TSRMLS_CC);
|
2012-05-23 22:07:15 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2012-12-18 05:02:32 +08:00
|
|
|
/* {{{ proto mixed Generator::send(mixed $value)
|
2012-05-29 23:34:33 +08:00
|
|
|
* Sends a value to the generator */
|
|
|
|
ZEND_METHOD(Generator, send)
|
|
|
|
{
|
2012-06-23 20:43:52 +08:00
|
|
|
zval *value;
|
2012-05-29 23:34:33 +08:00
|
|
|
zend_generator *generator;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
|
2012-05-29 23:34:33 +08:00
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
zend_generator_ensure_initialized(generator TSRMLS_CC);
|
2012-05-29 23:34:33 +08:00
|
|
|
|
2012-05-30 00:01:08 +08:00
|
|
|
/* The generator is already closed, thus can't send anything */
|
|
|
|
if (!generator->execute_data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-19 04:36:48 +08:00
|
|
|
/* Put sent value into the TMP_VAR slot */
|
|
|
|
MAKE_COPY_ZVAL(&value, &generator->send_target->tmp_var);
|
2012-05-29 23:34:33 +08:00
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
zend_generator_resume(generator TSRMLS_CC);
|
2012-06-01 02:03:18 +08:00
|
|
|
|
|
|
|
if (generator->value) {
|
2013-08-27 01:14:34 +08:00
|
|
|
RETURN_ZVAL_FAST(generator->value);
|
2012-06-01 02:03:18 +08:00
|
|
|
}
|
2012-05-29 23:34:33 +08:00
|
|
|
}
|
2012-08-30 02:49:14 +08:00
|
|
|
/* }}} */
|
2012-05-29 23:34:33 +08:00
|
|
|
|
2012-12-18 05:02:32 +08:00
|
|
|
/* {{{ proto mixed Generator::throw(Exception $exception)
|
|
|
|
* Throws an exception into the generator */
|
|
|
|
ZEND_METHOD(Generator, throw)
|
|
|
|
{
|
|
|
|
zval *exception, *exception_copy;
|
|
|
|
zend_generator *generator;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &exception) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ALLOC_ZVAL(exception_copy);
|
|
|
|
MAKE_COPY_ZVAL(&exception, exception_copy);
|
|
|
|
|
|
|
|
generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
|
|
|
|
|
|
|
|
if (generator->execute_data) {
|
|
|
|
/* Throw the exception in the context of the generator */
|
|
|
|
zend_execute_data *current_execute_data = EG(current_execute_data);
|
|
|
|
EG(current_execute_data) = generator->execute_data;
|
|
|
|
|
|
|
|
zend_throw_exception_object(exception_copy TSRMLS_CC);
|
|
|
|
|
|
|
|
EG(current_execute_data) = current_execute_data;
|
|
|
|
|
|
|
|
zend_generator_resume(generator TSRMLS_CC);
|
|
|
|
|
|
|
|
if (generator->value) {
|
2013-08-27 01:14:34 +08:00
|
|
|
RETURN_ZVAL_FAST(generator->value);
|
2012-12-18 05:02:32 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* If the generator is already closed throw the exception in the
|
|
|
|
* current context */
|
|
|
|
zend_throw_exception_object(exception_copy TSRMLS_CC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2012-08-30 02:49:14 +08:00
|
|
|
/* {{{ proto void Generator::__wakeup()
|
2012-08-20 22:01:16 +08:00
|
|
|
* Throws an Exception as generators can't be serialized */
|
|
|
|
ZEND_METHOD(Generator, __wakeup)
|
|
|
|
{
|
|
|
|
/* Just specifying the zend_class_unserialize_deny handler is not enough,
|
|
|
|
* because it is only invoked for C unserialization. For O the error has
|
|
|
|
* to be thrown in __wakeup. */
|
|
|
|
|
|
|
|
if (zend_parse_parameters_none() == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
zend_throw_exception(NULL, "Unserialization of 'Generator' is not allowed", 0 TSRMLS_CC);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2012-06-23 07:28:16 +08:00
|
|
|
/* get_iterator implementation */
|
|
|
|
|
|
|
|
static void zend_generator_iterator_dtor(zend_object_iterator *iterator TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
2012-06-23 20:43:52 +08:00
|
|
|
zval *object = ((zend_generator_iterator *) iterator)->object;
|
|
|
|
|
|
|
|
zval_ptr_dtor(&object);
|
2012-06-23 07:28:16 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
static int zend_generator_iterator_valid(zend_object_iterator *iterator TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
2012-06-23 20:43:52 +08:00
|
|
|
zend_generator *generator = (zend_generator *) iterator->data;
|
2012-06-23 07:28:16 +08:00
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
zend_generator_ensure_initialized(generator TSRMLS_CC);
|
2012-06-23 07:28:16 +08:00
|
|
|
|
|
|
|
return generator->value != NULL ? SUCCESS : FAILURE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
static void zend_generator_iterator_get_data(zend_object_iterator *iterator, zval ***data TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
2012-06-23 20:43:52 +08:00
|
|
|
zend_generator *generator = (zend_generator *) iterator->data;
|
2012-06-23 07:28:16 +08:00
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
zend_generator_ensure_initialized(generator TSRMLS_CC);
|
2012-06-23 07:28:16 +08:00
|
|
|
|
|
|
|
if (generator->value) {
|
|
|
|
*data = &generator->value;
|
|
|
|
} else {
|
|
|
|
*data = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2013-02-17 02:13:36 +08:00
|
|
|
static void zend_generator_iterator_get_key(zend_object_iterator *iterator, zval *key TSRMLS_DC) /* {{{ */
|
2012-06-23 07:28:16 +08:00
|
|
|
{
|
2012-06-23 20:43:52 +08:00
|
|
|
zend_generator *generator = (zend_generator *) iterator->data;
|
2012-06-23 07:28:16 +08:00
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
zend_generator_ensure_initialized(generator TSRMLS_CC);
|
2012-06-23 07:28:16 +08:00
|
|
|
|
2013-02-17 02:13:36 +08:00
|
|
|
if (generator->key) {
|
|
|
|
ZVAL_ZVAL(key, generator->key, 1, 0);
|
|
|
|
} else {
|
|
|
|
ZVAL_NULL(key);
|
2012-06-23 07:28:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
static void zend_generator_iterator_move_forward(zend_object_iterator *iterator TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
2012-06-23 20:43:52 +08:00
|
|
|
zend_generator *generator = (zend_generator *) iterator->data;
|
2012-06-23 07:28:16 +08:00
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
zend_generator_ensure_initialized(generator TSRMLS_CC);
|
2012-06-23 07:28:16 +08:00
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
zend_generator_resume(generator TSRMLS_CC);
|
2012-06-23 07:28:16 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2012-08-25 23:40:08 +08:00
|
|
|
static void zend_generator_iterator_rewind(zend_object_iterator *iterator TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
zend_generator *generator = (zend_generator *) iterator->data;
|
|
|
|
|
|
|
|
zend_generator_rewind(generator TSRMLS_CC);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2012-06-23 07:28:16 +08:00
|
|
|
static zend_object_iterator_funcs zend_generator_iterator_functions = {
|
|
|
|
zend_generator_iterator_dtor,
|
|
|
|
zend_generator_iterator_valid,
|
|
|
|
zend_generator_iterator_get_data,
|
|
|
|
zend_generator_iterator_get_key,
|
|
|
|
zend_generator_iterator_move_forward,
|
2012-08-25 23:40:08 +08:00
|
|
|
zend_generator_iterator_rewind
|
2012-06-23 07:28:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
zend_object_iterator *zend_generator_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
zend_generator_iterator *iterator;
|
2012-06-23 20:43:52 +08:00
|
|
|
zend_generator *generator;
|
2012-06-23 07:28:16 +08:00
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
generator = (zend_generator *) zend_object_store_get_object(object TSRMLS_CC);
|
|
|
|
|
2012-08-30 02:46:56 +08:00
|
|
|
if (!generator->execute_data) {
|
|
|
|
zend_throw_exception(NULL, "Cannot traverse an already closed generator", 0 TSRMLS_CC);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-17 19:24:27 +08:00
|
|
|
if (by_ref && !(generator->execute_data->op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
|
2012-08-30 02:46:56 +08:00
|
|
|
zend_throw_exception(NULL, "You can only iterate a generator by-reference if it declared that it yields by-reference", 0 TSRMLS_CC);
|
|
|
|
return NULL;
|
2012-07-17 19:24:27 +08:00
|
|
|
}
|
|
|
|
|
2012-11-30 17:39:23 +08:00
|
|
|
iterator = &generator->iterator;
|
2012-06-23 07:28:16 +08:00
|
|
|
iterator->intern.funcs = &zend_generator_iterator_functions;
|
2012-06-23 20:43:52 +08:00
|
|
|
iterator->intern.data = (void *) generator;
|
2012-06-23 07:28:16 +08:00
|
|
|
|
2012-06-23 20:43:52 +08:00
|
|
|
/* We have to keep a reference to the generator object zval around,
|
|
|
|
* otherwise the generator may be destroyed during iteration. */
|
2012-06-23 07:28:16 +08:00
|
|
|
Z_ADDREF_P(object);
|
2012-06-23 20:43:52 +08:00
|
|
|
iterator->object = object;
|
2012-06-23 07:28:16 +08:00
|
|
|
|
|
|
|
return (zend_object_iterator *) iterator;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2012-05-23 22:07:15 +08:00
|
|
|
ZEND_BEGIN_ARG_INFO(arginfo_generator_void, 0)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2012-05-29 23:34:33 +08:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_generator_send, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, value)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2012-12-18 05:02:32 +08:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_generator_throw, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, exception)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2012-05-20 06:03:27 +08:00
|
|
|
static const zend_function_entry generator_functions[] = {
|
2012-08-20 22:01:16 +08:00
|
|
|
ZEND_ME(Generator, rewind, arginfo_generator_void, ZEND_ACC_PUBLIC)
|
|
|
|
ZEND_ME(Generator, valid, arginfo_generator_void, ZEND_ACC_PUBLIC)
|
|
|
|
ZEND_ME(Generator, current, arginfo_generator_void, ZEND_ACC_PUBLIC)
|
|
|
|
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)
|
2012-12-18 05:02:32 +08:00
|
|
|
ZEND_ME(Generator, throw, arginfo_generator_throw, ZEND_ACC_PUBLIC)
|
2012-08-20 22:01:16 +08:00
|
|
|
ZEND_ME(Generator, __wakeup, arginfo_generator_void, ZEND_ACC_PUBLIC)
|
2012-05-20 06:03:27 +08:00
|
|
|
ZEND_FE_END
|
|
|
|
};
|
|
|
|
|
|
|
|
void zend_register_generator_ce(TSRMLS_D) /* {{{ */
|
|
|
|
{
|
|
|
|
zend_class_entry ce;
|
|
|
|
|
|
|
|
INIT_CLASS_ENTRY(ce, "Generator", generator_functions);
|
|
|
|
zend_ce_generator = zend_register_internal_class(&ce TSRMLS_CC);
|
|
|
|
zend_ce_generator->ce_flags |= ZEND_ACC_FINAL_CLASS;
|
2012-05-20 20:19:16 +08:00
|
|
|
zend_ce_generator->create_object = zend_generator_create;
|
2012-08-20 22:01:16 +08:00
|
|
|
zend_ce_generator->serialize = zend_class_serialize_deny;
|
|
|
|
zend_ce_generator->unserialize = zend_class_unserialize_deny;
|
2012-05-20 20:19:16 +08:00
|
|
|
|
2012-07-26 23:07:24 +08:00
|
|
|
/* get_iterator has to be assigned *after* implementing the inferface */
|
2012-05-23 22:07:15 +08:00
|
|
|
zend_class_implements(zend_ce_generator TSRMLS_CC, 1, zend_ce_iterator);
|
2012-07-26 23:07:24 +08:00
|
|
|
zend_ce_generator->get_iterator = zend_generator_get_iterator;
|
|
|
|
zend_ce_generator->iterator_funcs.funcs = &zend_generator_iterator_functions;
|
2012-05-23 22:07:15 +08:00
|
|
|
|
2012-05-20 20:19:16 +08:00
|
|
|
memcpy(&zend_generator_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
|
|
|
|
zend_generator_handlers.get_constructor = zend_generator_get_constructor;
|
2013-03-26 00:40:58 +08:00
|
|
|
zend_generator_handlers.clone_obj = NULL;
|
2012-05-20 06:03:27 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* End:
|
|
|
|
*/
|