zend_execute_data *sw_zend_create_execute_data_from_op_array(zend_op_array *op_array, zend_bool nested TSRMLS_DC) { zend_execute_data *execute_data; size_t execute_data_size = ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data)); size_t CVs_size = ZEND_MM_ALIGNED_SIZE(sizeof(zval **) * op_array->last_var * (EG(active_symbol_table) ? 1 : 2)); size_t Ts_size = ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable)) * op_array->T; size_t call_slots_size = ZEND_MM_ALIGNED_SIZE(sizeof(call_slot)) * op_array->nested_calls; size_t stack_size = ZEND_MM_ALIGNED_SIZE(sizeof(zval*)) * op_array->used_stack; size_t total_size = execute_data_size + Ts_size + CVs_size + call_slots_size + stack_size; execute_data = zend_vm_stack_alloc(total_size TSRMLS_CC); execute_data = (zend_execute_data*)((char*)execute_data + Ts_size); execute_data->prev_execute_data = EG(current_execute_data); memset(EX_CV_NUM(execute_data, 0), 0, sizeof(zval **) * op_array->last_var); execute_data->call_slots = (call_slot*)((char *)execute_data + execute_data_size + CVs_size); execute_data->op_array = op_array; EG(argument_stack)->top = zend_vm_stack_frame_base(execute_data); execute_data->object = NULL; execute_data->current_this = NULL; execute_data->old_error_reporting = NULL; execute_data->symbol_table = EG(active_symbol_table); execute_data->call = NULL; EG(current_execute_data) = execute_data; execute_data->nested = nested; if (!op_array->run_time_cache && op_array->last_cache_slot) { op_array->run_time_cache = ecalloc(op_array->last_cache_slot, sizeof(void*)); } if (op_array->this_var != -1 && EG(This)) { Z_ADDREF_P(EG(This)); /* For $this pointer */ if (!EG(active_symbol_table)) { SW_EX_CV(op_array->this_var) = (zval **) SW_EX_CV_NUM(execute_data, op_array->last_var + op_array->this_var); *SW_EX_CV(op_array->this_var) = EG(This); } else { if (zend_hash_add(EG(active_symbol_table), "this", sizeof("this"), &EG(This), sizeof(zval *), (void **) EX_CV_NUM(execute_data, op_array->this_var))==FAILURE) { Z_DELREF_P(EG(This)); } } } execute_data->opline = UNEXPECTED((op_array->fn_flags & ZEND_ACC_INTERACTIVE) != 0) && EG(start_op) ? EG(start_op) : op_array->opcodes; EG(opline_ptr) = &(execute_data->opline); execute_data->function_state.function = (zend_function *) op_array; execute_data->function_state.arguments = NULL; return execute_data; }
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; } }