static void phalcon_execute_internal(zend_execute_data *execute_data_ptr, zend_fcall_info *fci, int return_value_used TSRMLS_DC) { if (fci) { ((zend_internal_function *) execute_data_ptr->function_state.function)->handler(fci->param_count, *fci->retval_ptr_ptr, fci->retval_ptr_ptr, fci->object_ptr, return_value_used TSRMLS_CC); } else { zval **return_value_ptr = EX_TMP_VAR(execute_data_ptr, execute_data_ptr->opline->result.var)->var.ptr_ptr; ((zend_internal_function *) execute_data_ptr->function_state.function)->handler(execute_data_ptr->opline->extended_value, *return_value_ptr, return_value_ptr, execute_data_ptr->object, return_value_used TSRMLS_CC); } }
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); } 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); } /* A fatal error / die occured during the generator execution. Trying to clean * up the stack may not be safe in this case. */ if (CG(unclean_shutdown)) { return; } /* 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) { /* -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; 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; } } } } /* Clear any backed up stack arguments */ if (generator->stack != EG(argument_stack)) { 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); } } while (execute_data->call >= execute_data->call_slots) { if (execute_data->call->object) { zval_ptr_dtor(&execute_data->call->object); } execute_data->call--; } /* 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); } } } /* Free a clone of closure */ if (op_array->fn_flags & ZEND_ACC_CLOSURE) { destroy_op_array(op_array TSRMLS_CC); efree(op_array); } efree(generator->stack); if (generator->stack == EG(argument_stack)) { /* abnormal exit for running generator */ EG(argument_stack) = NULL; } generator->execute_data = NULL; } }
ZEND_API void zend_generator_close(zend_generator *generator, zend_bool finished_execution TSRMLS_DC) /* {{{ */ { if (generator->execute_data) { zend_execute_data *execute_data = generator->execute_data; zend_op_array *op_array = execute_data->op_array; if (!finished_execution) { if (op_array->has_finally_block) { /* -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; zend_uint finally_op_num = 0; /* Find next finally block */ int i; for (i = 0; i < op_array->last_try_catch; i++) { zend_try_catch_element *try_catch = &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. Furthermore we abort this close call * because the generator will already be closed somewhere in * the resume. */ if (finally_op_num) { execute_data->opline = &op_array->opcodes[finally_op_num]; execute_data->fast_ret = NULL; generator->flags |= ZEND_GENERATOR_FORCED_CLOSE; zend_generator_resume(generator TSRMLS_CC); return; } } } if (!execute_data->symbol_table) { zend_free_compiled_variables(execute_data); } 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 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) { /* -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; 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; } } } } /* Clear any backed up stack arguments */ if (generator->stack != EG(argument_stack)) { void **stack_frame = zend_vm_stack_frame_base(execute_data); while (generator->stack->top != stack_frame) { zval_ptr_dtor((zval**)stack_frame); stack_frame++; } } while (execute_data->call >= execute_data->call_slots) { if (execute_data->call->object) { zval_ptr_dtor(&execute_data->call->object); } execute_data->call--; } /* 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); } } } /* Free a clone of closure */ if (op_array->fn_flags & ZEND_ACC_CLOSURE) { destroy_op_array(op_array TSRMLS_CC); efree(op_array); } efree(generator->stack); if (generator->stack == EG(argument_stack)) { /* abnormal exit for running generator */ EG(argument_stack) = NULL; } generator->execute_data = NULL; } if (generator->value) { zval_ptr_dtor(&generator->value); generator->value = NULL; } if (generator->key) { zval_ptr_dtor(&generator->key); generator->key = NULL; } }