123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763 |
- #include "zend.h"
- #include "zend_API.h"
- #include "zend_interfaces.h"
- #include "zend_exceptions.h"
- #include "zend_generators.h"
- ZEND_API zend_class_entry *zend_ce_generator;
- static zend_object_handlers zend_generator_handlers;
- static zend_object_value zend_generator_create(zend_class_entry *class_type TSRMLS_DC);
- static void zend_generator_cleanup_unfinished_execution(zend_generator *generator TSRMLS_DC)
- {
- zend_execute_data *execute_data = generator->execute_data;
- zend_op_array *op_array = execute_data->op_array;
- if (generator->send_target) {
- Z_DELREF_PP(generator->send_target);
- generator->send_target = NULL;
- }
-
- {
-
- zend_uint op_num = execute_data->opline - op_array->opcodes - 1;
- 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:
- {
- temp_variable *var = EX_TMP_VAR(execute_data, brk_opline->op1.var);
- zval_ptr_dtor(&var->var.ptr);
- }
- break;
- case ZEND_FREE:
- {
- temp_variable *var = EX_TMP_VAR(execute_data, brk_opline->op1.var);
- zval_dtor(&var->tmp_var);
- }
- break;
- }
- }
- }
- }
-
- {
- void **ptr = generator->stack->top - 1;
- void **end = zend_vm_stack_frame_base(execute_data);
- for (; ptr >= end; --ptr) {
- zval_ptr_dtor((zval **) ptr);
- }
- }
-
- while (execute_data->call >= execute_data->call_slots) {
- if (execute_data->call->object) {
- zval_ptr_dtor(&execute_data->call->object);
- }
- execute_data->call--;
- }
- }
- ZEND_API void zend_generator_close(zend_generator *generator, zend_bool finished_execution TSRMLS_DC)
- {
- if (generator->value) {
- zval_ptr_dtor(&generator->value);
- generator->value = NULL;
- }
- if (generator->key) {
- zval_ptr_dtor(&generator->key);
- generator->key = NULL;
- }
- if (generator->execute_data) {
- zend_execute_data *execute_data = generator->execute_data;
- zend_op_array *op_array = execute_data->op_array;
- if (!execute_data->symbol_table) {
- zend_free_compiled_variables(execute_data TSRMLS_CC);
- } else {
- zend_clean_and_cache_symbol_table(execute_data->symbol_table TSRMLS_CC);
- }
- if (execute_data->current_this) {
- zval_ptr_dtor(&execute_data->current_this);
- }
-
- if (CG(unclean_shutdown)) {
- generator->execute_data = NULL;
- return;
- }
-
- {
- 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);
- }
- }
- }
-
- if (!finished_execution) {
- zend_generator_cleanup_unfinished_execution(generator TSRMLS_CC);
- }
-
- if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
- destroy_op_array(op_array TSRMLS_CC);
- efree(op_array);
- }
- efree(generator->stack);
- generator->execute_data = NULL;
- }
- }
- 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;
- }
-
- op_num = ex->opline - ex->op_array->opcodes - 1;
-
- 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 (finally_op_num) {
- ex->opline = &ex->op_array->opcodes[finally_op_num];
- ex->fast_ret = NULL;
- ex->delayed_exception = EG(exception);
- EG(exception) = NULL;
- generator->flags |= ZEND_GENERATOR_FORCED_CLOSE;
- zend_generator_resume(generator TSRMLS_CC);
- }
- }
- static void zend_generator_free_storage(zend_generator *generator TSRMLS_DC)
- {
- zend_generator_close(generator, 0 TSRMLS_CC);
- zend_object_std_dtor(&generator->std TSRMLS_CC);
- 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));
-
- generator->largest_used_integer_key = -1;
- zend_object_std_init(&generator->std, class_type TSRMLS_CC);
- object.handle = zend_objects_store_put(generator,
- (zend_objects_store_dtor_t) zend_generator_dtor_storage,
- (zend_objects_free_object_storage_t) zend_generator_free_storage,
- NULL TSRMLS_CC
- );
- object.handlers = &zend_generator_handlers;
- return object;
- }
- static void copy_closure_static_var(zval **var TSRMLS_DC, int num_args, va_list args, zend_hash_key *key)
- {
- HashTable *target = va_arg(args, HashTable *);
- SEPARATE_ZVAL_TO_MAKE_IS_REF(var);
- Z_ADDREF_PP(var);
- zend_hash_quick_update(target, key->arKey, key->nKeyLength, key->h, var, sizeof(zval *), NULL);
- }
- ZEND_API zval *zend_generator_create_zval(zend_op_array *op_array TSRMLS_DC)
- {
- zval *return_value;
- zend_generator *generator;
- zend_execute_data *current_execute_data;
- zend_op **opline_ptr;
- HashTable *current_symbol_table;
- zend_execute_data *execute_data;
- zend_vm_stack current_stack = EG(argument_stack);
-
- 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;
- (*op_array->refcount)++;
- op_array->run_time_cache = NULL;
- if (op_array->static_variables) {
- ALLOC_HASHTABLE(op_array_copy->static_variables);
- zend_hash_init(
- op_array_copy->static_variables,
- zend_hash_num_elements(op_array->static_variables),
- NULL, ZVAL_PTR_DTOR, 0
- );
- zend_hash_apply_with_arguments(
- op_array->static_variables TSRMLS_CC,
- (apply_func_args_t) copy_closure_static_var,
- 1, op_array_copy->static_variables
- );
- }
- op_array = op_array_copy;
- }
-
-
- current_execute_data = EG(current_execute_data);
- opline_ptr = EG(opline_ptr);
- current_symbol_table = EG(active_symbol_table);
- EG(active_symbol_table) = NULL;
- execute_data = zend_create_execute_data_from_op_array(op_array, 0 TSRMLS_CC);
- EG(active_symbol_table) = current_symbol_table;
- 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));
- }
-
- 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);
-
- generator = (zend_generator *) zend_object_store_get_object(return_value TSRMLS_CC);
- generator->execute_data = execute_data;
- generator->stack = EG(argument_stack);
- EG(argument_stack) = current_stack;
- return return_value;
- }
- 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;
- }
- ZEND_API void zend_generator_resume(zend_generator *generator TSRMLS_DC)
- {
-
- if (!generator->execute_data) {
- return;
- }
- if (generator->flags & ZEND_GENERATOR_CURRENTLY_RUNNING) {
- zend_error(E_ERROR, "Cannot resume an already running generator");
- }
-
- generator->flags &= ~ZEND_GENERATOR_AT_FIRST_YIELD;
- {
-
- zval **original_return_value_ptr_ptr = EG(return_value_ptr_ptr);
- zend_execute_data *original_execute_data = EG(current_execute_data);
- 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);
- zend_vm_stack original_stack = EG(argument_stack);
-
- EG(return_value_ptr_ptr) = (zval **) generator;
-
- EG(current_execute_data) = generator->execute_data;
- 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;
- EG(argument_stack) = generator->stack;
-
- generator->execute_data->prev_execute_data->prev_execute_data = original_execute_data;
-
- generator->flags |= ZEND_GENERATOR_CURRENTLY_RUNNING;
- zend_execute_ex(generator->execute_data TSRMLS_CC);
- generator->flags &= ~ZEND_GENERATOR_CURRENTLY_RUNNING;
-
- EG(return_value_ptr_ptr) = original_return_value_ptr_ptr;
- EG(current_execute_data) = original_execute_data;
- 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;
- EG(argument_stack) = original_stack;
-
- if (UNEXPECTED(EG(exception) != NULL)) {
- zend_throw_exception_internal(NULL TSRMLS_CC);
- }
- }
- }
- static void zend_generator_ensure_initialized(zend_generator *generator TSRMLS_DC)
- {
- if (generator->execute_data && !generator->value) {
- zend_generator_resume(generator TSRMLS_CC);
- 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);
- }
- }
- ZEND_METHOD(Generator, rewind)
- {
- zend_generator *generator;
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
- zend_generator_rewind(generator TSRMLS_CC);
- }
- ZEND_METHOD(Generator, valid)
- {
- zend_generator *generator;
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
- zend_generator_ensure_initialized(generator TSRMLS_CC);
- RETURN_BOOL(generator->value != NULL);
- }
- ZEND_METHOD(Generator, current)
- {
- zend_generator *generator;
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
- zend_generator_ensure_initialized(generator TSRMLS_CC);
- if (generator->value) {
- RETURN_ZVAL_FAST(generator->value);
- }
- }
- ZEND_METHOD(Generator, key)
- {
- zend_generator *generator;
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
- zend_generator_ensure_initialized(generator TSRMLS_CC);
- if (generator->key) {
- RETURN_ZVAL_FAST(generator->key);
- }
- }
- ZEND_METHOD(Generator, next)
- {
- zend_generator *generator;
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
- zend_generator_ensure_initialized(generator TSRMLS_CC);
- zend_generator_resume(generator TSRMLS_CC);
- }
- ZEND_METHOD(Generator, send)
- {
- zval *value;
- zend_generator *generator;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE) {
- return;
- }
- generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
- zend_generator_ensure_initialized(generator TSRMLS_CC);
-
- if (!generator->execute_data) {
- return;
- }
-
- if (generator->send_target) {
- Z_DELREF_PP(generator->send_target);
- Z_ADDREF_P(value);
- *generator->send_target = value;
- }
- zend_generator_resume(generator TSRMLS_CC);
- if (generator->value) {
- RETURN_ZVAL_FAST(generator->value);
- }
- }
- 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);
- zend_generator_ensure_initialized(generator TSRMLS_CC);
- if (generator->execute_data) {
-
- 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) {
- RETURN_ZVAL_FAST(generator->value);
- }
- } else {
-
- zend_throw_exception_object(exception_copy TSRMLS_CC);
- }
- }
- ZEND_METHOD(Generator, __wakeup)
- {
-
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- zend_throw_exception(NULL, "Unserialization of 'Generator' is not allowed", 0 TSRMLS_CC);
- }
- static void zend_generator_iterator_dtor(zend_object_iterator *iterator TSRMLS_DC)
- {
- zend_generator_iterator *iter = (zend_generator_iterator *) iterator;
- zend_objects_store_del_ref_by_handle(iter->handle TSRMLS_CC);
- }
- static int zend_generator_iterator_valid(zend_object_iterator *iterator TSRMLS_DC)
- {
- zend_generator *generator = (zend_generator *) iterator->data;
- zend_generator_ensure_initialized(generator TSRMLS_CC);
- return generator->value != NULL ? SUCCESS : FAILURE;
- }
- static void zend_generator_iterator_get_data(zend_object_iterator *iterator, zval ***data TSRMLS_DC)
- {
- zend_generator *generator = (zend_generator *) iterator->data;
- zend_generator_ensure_initialized(generator TSRMLS_CC);
- if (generator->value) {
- *data = &generator->value;
- } else {
- *data = NULL;
- }
- }
- static void zend_generator_iterator_get_key(zend_object_iterator *iterator, zval *key TSRMLS_DC)
- {
- zend_generator *generator = (zend_generator *) iterator->data;
- zend_generator_ensure_initialized(generator TSRMLS_CC);
- if (generator->key) {
- ZVAL_ZVAL(key, generator->key, 1, 0);
- } else {
- ZVAL_NULL(key);
- }
- }
- static void zend_generator_iterator_move_forward(zend_object_iterator *iterator TSRMLS_DC)
- {
- zend_generator *generator = (zend_generator *) iterator->data;
- zend_generator_ensure_initialized(generator TSRMLS_CC);
- zend_generator_resume(generator TSRMLS_CC);
- }
- 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);
- }
- 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,
- zend_generator_iterator_rewind
- };
- zend_object_iterator *zend_generator_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC)
- {
- zend_generator_iterator *iterator;
- zend_generator *generator;
- generator = (zend_generator *) zend_object_store_get_object(object TSRMLS_CC);
- if (!generator->execute_data) {
- zend_throw_exception(NULL, "Cannot traverse an already closed generator", 0 TSRMLS_CC);
- return NULL;
- }
- if (by_ref && !(generator->execute_data->op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
- 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;
- }
- iterator = &generator->iterator;
- iterator->intern.funcs = &zend_generator_iterator_functions;
- iterator->intern.data = (void *) generator;
-
- iterator->handle = Z_OBJ_HANDLE_P(object);
- zend_objects_store_add_ref_by_handle(iterator->handle TSRMLS_CC);
- return (zend_object_iterator *) iterator;
- }
- ZEND_BEGIN_ARG_INFO(arginfo_generator_void, 0)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(arginfo_generator_send, 0, 0, 1)
- ZEND_ARG_INFO(0, value)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(arginfo_generator_throw, 0, 0, 1)
- ZEND_ARG_INFO(0, exception)
- ZEND_END_ARG_INFO()
- static const zend_function_entry generator_functions[] = {
- 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)
- ZEND_ME(Generator, throw, arginfo_generator_throw, ZEND_ACC_PUBLIC)
- ZEND_ME(Generator, __wakeup, arginfo_generator_void, ZEND_ACC_PUBLIC)
- 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;
- zend_ce_generator->create_object = zend_generator_create;
- zend_ce_generator->serialize = zend_class_serialize_deny;
- zend_ce_generator->unserialize = zend_class_unserialize_deny;
-
- zend_class_implements(zend_ce_generator TSRMLS_CC, 1, zend_ce_iterator);
- zend_ce_generator->get_iterator = zend_generator_get_iterator;
- zend_ce_generator->iterator_funcs.funcs = &zend_generator_iterator_functions;
- memcpy(&zend_generator_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
- zend_generator_handlers.get_constructor = zend_generator_get_constructor;
- zend_generator_handlers.clone_obj = NULL;
- }
|