com php-src: Merge branch 'master' into Closure_apply: Ze nd/zend_closures.c Zend/zend_closures.h Zend/zend _compile.h Zend/zend_vm_def.h
Zend/zend_vm_ex ecute.h
| From: | Tjerk Anne Meesters | Date: | Sun, 17 Aug 2014 16:20:02 +0000 |
| Subject: | com php-src: Merge branch 'master' into Closure_apply: Ze nd/zend_closures.c Zend/zend_closures.h Zend/zend _compile.h Zend/zend_vm_def.h Zend/zend_vm_ex ecute.h |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-81154@lists.php.net to get a copy of this message | ||
Commit: e35c8ae13eb89984bdb889f2dea15fbf2df2d78b
Author: Andrea Faulds <ajf@ajf.me> Sun, 17 Aug 2014 17:20:02 +0100
Parents: 668d0683cdad18eae818165b6bb12f7176ce21de d790eceb01009489358c3781c94ebea3b3400f67
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=e35c8ae13eb89984bdb889f2dea15fbf2df2d78b
Log:
Merge branch 'master' into Closure_apply
Conflicts:
Zend/zend_closures.c
Zend/zend_closures.h
Zend/zend_execute_API.c
Zend/zend_vm_def.h
Changed paths:
MM Zend/zend_closures.c
MM Zend/zend_closures.h
MM Zend/zend_compile.h
MM Zend/zend_vm_def.h
MM Zend/zend_vm_execute.h
diff --cc Zend/zend_closures.c index a5735f4,c10c1a4..d9d1179 --- a/Zend/zend_closures.c +++ b/Zend/zend_closures.c @@@ -70,71 -66,15 +66,71 @@@ ZEND_METHOD(Closure, __invoke) /* {{{ * } /* }}} */ -/* {{{ proto Closure Closure::bind(Closure $old, object $to [, mixed $scope = "static" ] ) +/* {{{ proto mixed Closure::call(object $to [, mixed $parameter] [, mixed $...] ) + Call closure, binding to a given object */ +ZEND_METHOD(Closure, call) /* {{{ */ +{ - zval *zclosure, *newthis, *closure_result_ptr = NULL; ++ zval *zclosure, *newthis, closure_result; + zend_closure *closure; + zend_fcall_info fci; + zend_fcall_info_cache fci_cache; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oo*", &zclosure, zend_ce_closure, &newthis, &fci.params, &fci.param_count) == FAILURE) { + RETURN_NULL(); + } + - closure = (zend_closure *)zend_object_store_get_object(zclosure TSRMLS_CC); ++ closure = (zend_closure *)Z_OBJ_P(zclosure); + + if (closure->func.common.fn_flags & ZEND_ACC_STATIC) { + zend_error(E_WARNING, "Cannot bind an instance to a static closure"); + RETURN_NULL(); + } + + if (closure->func.type == ZEND_INTERNAL_FUNCTION) { + /* verify that we aren't binding internal function to a wrong object */ + if((closure->func.common.fn_flags & ZEND_ACC_STATIC) == 0 && + !instanceof_function(Z_OBJCE_P(newthis), closure->func.common.scope TSRMLS_CC)) { + zend_error(E_WARNING, "Cannot bind function %s::%s to object of class %s", closure->func.common.scope->name, closure->func.common.function_name, Z_OBJCE_P(newthis)->name); + RETURN_NULL(); + } + } + + fci.size = sizeof(fci); + fci.function_table = CG(function_table); - fci.object_ptr = newthis; - fci.function_name = zclosure; - fci.retval_ptr_ptr = &closure_result_ptr; ++ fci.object = Z_OBJ_P(newthis); ++ fci.function_name = *zclosure; ++ fci.retval = &closure_result; + fci.no_separation = 1; + fci.symbol_table = NULL; + + fci_cache.initialized = 1; + fci_cache.function_handler = &closure->func; - fci_cache.object_ptr = newthis; ++ fci_cache.object = Z_OBJ_P(newthis); + fci_cache.calling_scope = fci_cache.called_scope = Z_OBJCE_P(newthis); + + if (zend_call_function(&fci, &fci_cache TSRMLS_CC) == FAILURE) { + RETVAL_FALSE; - } else if (closure_result_ptr) { ++ } else { + zval_ptr_dtor(&return_value); - *return_value_ptr = closure_result_ptr; ++ *return_value = closure_result; + } + + if (fci.param_count) { + efree(fci.params); + } +} +/* }}} */ + +/* {{{ proto Closure Closure::bind(Closure $old, object $to [, mixed $scope = "static" ] [, bool $unbound_scoped = false ] ) Create a closure from another one and bind to another object and scope */ ZEND_METHOD(Closure, bind) { zval *newthis, *zclosure, *scope_arg = NULL; zend_closure *closure; - zend_class_entry *ce, **ce_p; + zend_class_entry *ce; + zend_bool unbound_scoped = false; - if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oo!|z", &zclosure, zend_ce_closure, &newthis, &scope_arg) == FAILURE) { + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oo!|zb", &zclosure, zend_ce_closure, &newthis, &scope_arg, &unbound_scoped) == FAILURE) { RETURN_NULL(); } @@@ -549,26 -456,17 +525,25 @@@ ZEND_API void zend_create_closure_ex(zv } } - closure->this_ptr = NULL; + ZVAL_UNDEF(&closure->this_ptr); /* Invariants: - * If the closure is unscoped, it has no bound object. - * The the closure is scoped, it's either static or it's bound */ + * If the closure is unscoped, it is unbound. + * If the closure is scoped, it may be static, bound or unbound */ closure->func.common.scope = scope; if (scope) { closure->func.common.fn_flags |= ZEND_ACC_PUBLIC; - if (this_ptr && (closure->func.common.fn_flags & ZEND_ACC_STATIC) == 0) { - closure->this_ptr = this_ptr; - Z_ADDREF_P(this_ptr); - /* We assume that a static scoped closure is desired if given NULL to bind to - If an unbound scoped closure is desired, the parameter must be set to 1*/ - } else if (!unbound_scoped) { + if (this_ptr && Z_TYPE_P(this_ptr) == IS_OBJECT && (closure->func.common.fn_flags & ZEND_ACC_STATIC) == 0) { + ZVAL_COPY(&closure->this_ptr, this_ptr); - } else { ++ /* We assume that a static scoped closure is desired if given NULL to bind to ++ If an unbound scoped closure is desired, the parameter must be set to 1*/ ++ } else if (!unbound_scoped) { closure->func.common.fn_flags |= ZEND_ACC_STATIC; + /* Unbound but scoped was explicitly specified and we wish to avoid E_ERROR when calling without object + We don't do this if it has implict allowed static (i.e. is a method, should E_STRICT) + Nor do we do this if it's an internal function (which would blow up if $this was NULL) + (In that case, we're actually creating a closure which can't be called without apply) */ + } else if ((func->common.fn_flags & ZEND_ACC_ALLOW_STATIC) == 0 && func->type == ZEND_USER_FUNCTION) { + closure->func.common.fn_flags |= ZEND_ACC_ALLOW_STATIC_EXPLICIT; } } } diff --cc Zend/zend_closures.h index d916d5e,4f8ae6d..0f9ab5c --- a/Zend/zend_closures.h +++ b/Zend/zend_closures.h @@@ -29,8 -29,7 +29,8 @@@ void zend_register_closure_ce(TSRMLS_D) extern ZEND_API zend_class_entry *zend_ce_closure; ZEND_API void zend_create_closure(zval *res, zend_function *op_array, zend_class_entry *scope, zval *this_ptr TSRMLS_DC); +ZEND_API void zend_create_closure_ex(zval *res, zend_function *op_array, zend_class_entry *scope, zval *this_ptr, zend_bool unbound_scoped TSRMLS_DC); - ZEND_API zend_function *zend_get_closure_invoke_method(zval *obj TSRMLS_DC); + ZEND_API zend_function *zend_get_closure_invoke_method(zend_object *obj TSRMLS_DC); ZEND_API const zend_function *zend_get_closure_method_def(zval *obj TSRMLS_DC); ZEND_API zval* zend_get_closure_this_ptr(zval *obj TSRMLS_DC); diff --cc Zend/zend_vm_def.h index 5a93cdd,7749197..9e91663 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@@ -2804,39 -2582,234 +2582,235 @@@ ZEND_VM_HANDLER(69, ZEND_INIT_NS_FCALL_ ZEND_VM_NEXT_OPCODE(); } - ZEND_VM_HANDLER(61, ZEND_DO_FCALL_BY_NAME, ANY, ANY) + ZEND_VM_HANDLER(61, ZEND_INIT_FCALL, ANY, CONST) { - EX(function_state).function = EX(call)->fbc; - ZEND_VM_DISPATCH_TO_HELPER(zend_do_fcall_common_helper); + USE_OPLINE + zend_free_op free_op2; + zval *fname = GET_OP2_ZVAL_PTR(BP_VAR_R); + zval *func; + zend_function *fbc; + + if (CACHED_PTR(Z_CACHE_SLOT_P(fname))) { + fbc = CACHED_PTR(Z_CACHE_SLOT_P(fname)); + } else if (UNEXPECTED((func = zend_hash_find(EG(function_table), Z_STR_P(fname))) == NULL)) { + SAVE_OPLINE(); + zend_error_noreturn(E_ERROR, "Call to undefined function %s()", Z_STRVAL_P(fname)); + } else { + fbc = Z_FUNC_P(func); + CACHE_PTR(Z_CACHE_SLOT_P(fname), fbc); + } + + EX(call) = zend_vm_stack_push_call_frame( + fbc, opline->extended_value, 0, NULL, NULL, EX(call) TSRMLS_CC); + + FREE_OP2(); + + ZEND_VM_NEXT_OPCODE(); } - ZEND_VM_HANDLER(60, ZEND_DO_FCALL, CONST, ANY) + ZEND_VM_HANDLER(60, ZEND_DO_FCALL, ANY, ANY) { USE_OPLINE - zend_free_op free_op1; - zval *fname = GET_OP1_ZVAL_PTR(BP_VAR_R); - call_slot *call = EX(call_slots) + opline->op2.num; + zend_execute_data *call = EX(call); + zend_function *fbc = call->func; - if (CACHED_PTR(opline->op1.literal->cache_slot)) { - EX(function_state).function = CACHED_PTR(opline->op1.literal->cache_slot); - } else if (UNEXPECTED(zend_hash_quick_find(EG(function_table), Z_STRVAL_P(fname), Z_STRLEN_P(fname)+1, Z_HASH_P(fname), (void **) &EX(function_state).function)==FAILURE)) { - SAVE_OPLINE(); - zend_error_noreturn(E_ERROR, "Call to undefined function %s()", fname->value.str.val); - } else { - CACHE_PTR(opline->op1.literal->cache_slot, EX(function_state).function); + SAVE_OPLINE(); + EX(call) = call->prev_nested_call; + if (UNEXPECTED((fbc->common.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_DEPRECATED)) != 0)) { + if (UNEXPECTED((fbc->common.fn_flags & ZEND_ACC_ABSTRACT) != 0)) { + zend_error_noreturn(E_ERROR, "Cannot call abstract method %s::%s()", fbc->common.scope->name->val, fbc->common.function_name->val); + } + if (UNEXPECTED((fbc->common.fn_flags & ZEND_ACC_DEPRECATED) != 0)) { + zend_error(E_DEPRECATED, "Function %s%s%s() is deprecated", + fbc->common.scope ? fbc->common.scope->name->val : "", + fbc->common.scope ? "::" : "", + fbc->common.function_name->val); + if (UNEXPECTED(EG(exception) != NULL)) { + HANDLE_EXCEPTION(); + } + } + } + if (fbc->common.scope && + !(fbc->common.fn_flags & ZEND_ACC_STATIC) && + !call->object) { + + if (fbc->common.fn_flags & ZEND_ACC_ALLOW_STATIC) { + /* FIXME: output identifiers properly */ + zend_error(E_STRICT, "Non-static method %s::%s() should not be called statically", fbc->common.scope->name->val, fbc->common.function_name->val); + if (UNEXPECTED(EG(exception) != NULL)) { + HANDLE_EXCEPTION(); + } - } else { ++ } else if (!(fbc->common.fn_flags & ZEND_ACC_ALLOW_STATIC_EXPLICIT)){ + /* FIXME: output identifiers properly */ + /* An internal function assumes $this is present and won't check that. So PHP would crash by allowing the call. */ + zend_error_noreturn(E_ERROR, "Non-static method %s::%s() cannot be called statically", fbc->common.scope->name->val, fbc->common.function_name->val); + } } ++} - call->fbc = EX(function_state).function; - call->object = NULL; - call->called_scope = NULL; - call->num_additional_args = 0; - call->is_ctor_call = 0; - EX(call) = call; + LOAD_OPLINE(); - FREE_OP1(); + if (UNEXPECTED(fbc->type == ZEND_INTERNAL_FUNCTION)) { + int should_change_scope = 0; + zval *ret; + + if (fbc->common.scope) { + should_change_scope = 1; + Z_OBJ(EG(This)) = call->object; + /* TODO: we don't set scope if we call an object method ??? */ + /* See: ext/pdo_sqlite/tests/pdo_fetch_func_001.phpt */ + #if 1 + EG(scope) = (call->object) ? NULL : fbc->common.scope; + #else + EG(scope) = fbc->common.scope; + #endif + } else { + call->called_scope = EX(called_scope); + } + + call->prev_execute_data = execute_data; + EG(current_execute_data) = call; - ZEND_VM_DISPATCH_TO_HELPER(zend_do_fcall_common_helper); + if (fbc->common.fn_flags & ZEND_ACC_HAS_TYPE_HINTS) { + zend_uint i; + zval *p = ZEND_CALL_ARG(call, 1); + + for (i = 0; i < call->num_args; ++i) { + zend_verify_arg_type(fbc, i + 1, p, 0 TSRMLS_CC); + p++; + } + if (UNEXPECTED(EG(exception) != NULL)) { + EG(current_execute_data) = call->prev_execute_data; + zend_vm_stack_free_args(call TSRMLS_CC); + zend_vm_stack_free_call_frame(call TSRMLS_CC); + if (RETURN_VALUE_USED(opline)) { + ZVAL_UNDEF(EX_VAR(opline->result.var)); + } + if (UNEXPECTED(should_change_scope)) { + ZEND_VM_C_GOTO(fcall_end_change_scope); + } else { + ZEND_VM_C_GOTO(fcall_end); + } + } + } + + ret = EX_VAR(opline->result.var); + ZVAL_NULL(ret); + Z_VAR_FLAGS_P(ret) = (fbc->common.fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0 ? IS_VAR_RET_REF : 0; + + if (!zend_execute_internal) { + /* saves one function call if zend_execute_internal is not used */ + fbc->internal_function.handler(call->num_args, ret TSRMLS_CC); + } else { + zend_execute_internal(call, ret TSRMLS_CC); + } + EG(current_execute_data) = call->prev_execute_data; + zend_vm_stack_free_args(call TSRMLS_CC); + zend_vm_stack_free_call_frame(call TSRMLS_CC); + + if (!RETURN_VALUE_USED(opline)) { + zval_ptr_dtor(ret); + } + + if (UNEXPECTED(should_change_scope)) { + ZEND_VM_C_GOTO(fcall_end_change_scope); + } else { + ZEND_VM_C_GOTO(fcall_end); + } + } else if (EXPECTED(fbc->type == ZEND_USER_FUNCTION)) { + zval *return_value = NULL; + + Z_OBJ(EG(This)) = call->object; + EG(scope) = fbc->common.scope; + call->symbol_table = NULL; + if (RETURN_VALUE_USED(opline)) { + return_value = EX_VAR(opline->result.var); + + ZVAL_NULL(return_value); + Z_VAR_FLAGS_P(return_value) = (fbc->common.fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0 ? IS_VAR_RET_REF : 0; + } + + if (UNEXPECTED((fbc->common.fn_flags & ZEND_ACC_GENERATOR) != 0)) { + if (RETURN_VALUE_USED(opline)) { + zend_generator_create_zval(call, &fbc->op_array, EX_VAR(opline->result.var) TSRMLS_CC); + } else { + zend_vm_stack_free_args(call TSRMLS_CC); + } + + zend_vm_stack_free_call_frame(call TSRMLS_CC); + } else { + call->prev_execute_data = execute_data; + i_init_func_execute_data(call, &fbc->op_array, return_value, EXPECTED(zend_execute_ex == execute_ex) ? VM_FRAME_NESTED_FUNCTION : VM_FRAME_TOP_FUNCTION TSRMLS_CC); + + if (EXPECTED(zend_execute_ex == execute_ex)) { + ZEND_VM_ENTER(); + } else { + zend_execute_ex(call TSRMLS_CC); + } + } + } else { /* ZEND_OVERLOADED_FUNCTION */ + Z_OBJ(EG(This)) = call->object; + EG(scope) = fbc->common.scope; + + ZVAL_NULL(EX_VAR(opline->result.var)); + + /* Not sure what should be done here if it's a static method */ + if (EXPECTED(call->object != NULL)) { + call->prev_execute_data = execute_data; + EG(current_execute_data) = call; + call->object->handlers->call_method(fbc->common.function_name, call->object, call->num_args, EX_VAR(opline->result.var) TSRMLS_CC); + EG(current_execute_data) = call->prev_execute_data; + } else { + zend_error_noreturn(E_ERROR, "Cannot call overloaded function for non-object"); + } + + zend_vm_stack_free_args(call TSRMLS_CC); + + zend_vm_stack_free_call_frame(call TSRMLS_CC); + + if (fbc->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY) { + STR_RELEASE(fbc->common.function_name); + } + efree(fbc); + + if (!RETURN_VALUE_USED(opline)) { + zval_ptr_dtor(EX_VAR(opline->result.var)); + } else { + //??? Z_UNSET_ISREF_P(EX_T(opline->result.var).var.ptr); + //??? Z_SET_REFCOUNT_P(EX_T(opline->result.var).var.ptr, 1); + Z_VAR_FLAGS_P(EX_VAR(opline->result.var)) = 0; + } + } + + ZEND_VM_C_LABEL(fcall_end_change_scope): + if (Z_OBJ(EG(This))) { + if (UNEXPECTED(EG(exception) != NULL) && (opline->op1.num & ZEND_CALL_CTOR)) { + if (!(opline->op1.num & ZEND_CALL_CTOR_RESULT_UNUSED)) { + Z_DELREF(EG(This)); + } + if (Z_REFCOUNT(EG(This)) == 1) { + zend_object_store_ctor_failed(Z_OBJ(EG(This)) TSRMLS_CC); + } + } + if (!Z_DELREF(EG(This))) { + _zval_dtor_func_for_ptr(Z_COUNTED(EG(This)) ZEND_FILE_LINE_CC); + } else if (UNEXPECTED(!Z_GC_INFO(EG(This)))) { + gc_possible_root(Z_COUNTED(EG(This)) TSRMLS_CC); + } + } + Z_OBJ(EG(This)) = EX(object); + EG(scope) = EX(scope); + + ZEND_VM_C_LABEL(fcall_end): + if (UNEXPECTED(EG(exception) != NULL)) { + zend_throw_exception_internal(NULL TSRMLS_CC); + if (RETURN_VALUE_USED(opline)) { + zval_ptr_dtor(EX_VAR(opline->result.var)); + } + HANDLE_EXCEPTION(); + } + + ZEND_VM_NEXT_OPCODE(); } ZEND_VM_HANDLER(62, ZEND_RETURN, CONST|TMP|VAR|CV, ANY) diff --cc Zend/zend_vm_execute.h index 5efc0c7,c2e59c1..8126359 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@@ -509,10 -533,10 +533,10 @@@ static int ZEND_FASTCALL ZEND_DO_FCALL if (UNEXPECTED(EG(exception) != NULL)) { HANDLE_EXCEPTION(); } - } else { + } else if (!(fbc->common.fn_flags & ZEND_ACC_ALLOW_STATIC_EXPLICIT)){ /* FIXME: output identifiers properly */ /* An internal function assumes $this is present and won't check that. So PHP would crash by allowing the call. */ - zend_error_noreturn(E_ERROR, "Non-static method %s::%s() cannot be called statically", fbc->common.scope->name, fbc->common.function_name); + zend_error_noreturn(E_ERROR, "Non-static method %s::%s() cannot be called statically", fbc->common.scope->name->val, fbc->common.function_name->val); } }
diff --cc Zend/zend_closures.c index a5735f4,c10c1a4..d9d1179 --- a/Zend/zend_closures.c +++ b/Zend/zend_closures.c @@@ -70,71 -66,15 +66,71 @@@ ZEND_METHOD(Closure, __invoke) /* {{{ * } /* }}} */ -/* {{{ proto Closure Closure::bind(Closure $old, object $to [, mixed $scope = "static" ] ) +/* {{{ proto mixed Closure::call(object $to [, mixed $parameter] [, mixed $...] ) + Call closure, binding to a given object */ +ZEND_METHOD(Closure, call) /* {{{ */ +{ - zval *zclosure, *newthis, *closure_result_ptr = NULL; ++ zval *zclosure, *newthis, closure_result; + zend_closure *closure; + zend_fcall_info fci; + zend_fcall_info_cache fci_cache; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oo*", &zclosure, zend_ce_closure, &newthis, &fci.params, &fci.param_count) == FAILURE) { + RETURN_NULL(); + } + - closure = (zend_closure *)zend_object_store_get_object(zclosure TSRMLS_CC); ++ closure = (zend_closure *)Z_OBJ_P(zclosure); + + if (closure->func.common.fn_flags & ZEND_ACC_STATIC) { + zend_error(E_WARNING, "Cannot bind an instance to a static closure"); + RETURN_NULL(); + } + + if (closure->func.type == ZEND_INTERNAL_FUNCTION) { + /* verify that we aren't binding internal function to a wrong object */ + if((closure->func.common.fn_flags & ZEND_ACC_STATIC) == 0 && + !instanceof_function(Z_OBJCE_P(newthis), closure->func.common.scope TSRMLS_CC)) { + zend_error(E_WARNING, "Cannot bind function %s::%s to object of class %s", closure->func.common.scope->name, closure->func.common.function_name, Z_OBJCE_P(newthis)->name); + RETURN_NULL(); + } + } + + fci.size = sizeof(fci); + fci.function_table = CG(function_table); - fci.object_ptr = newthis; - fci.function_name = zclosure; - fci.retval_ptr_ptr = &closure_result_ptr; ++ fci.object = Z_OBJ_P(newthis); ++ fci.function_name = *zclosure; ++ fci.retval = &closure_result; + fci.no_separation = 1; + fci.symbol_table = NULL; + + fci_cache.initialized = 1; + fci_cache.function_handler = &closure->func; - fci_cache.object_ptr = newthis; ++ fci_cache.object = Z_OBJ_P(newthis); + fci_cache.calling_scope = fci_cache.called_scope = Z_OBJCE_P(newthis); + + if (zend_call_function(&fci, &fci_cache TSRMLS_CC) == FAILURE) { + RETVAL_FALSE; - } else if (closure_result_ptr) { ++ } else { + zval_ptr_dtor(&return_value); - *return_value_ptr = closure_result_ptr; ++ *return_value = closure_result; + } + + if (fci.param_count) { + efree(fci.params); + } +} +/* }}} */ + +/* {{{ proto Closure Closure::bind(Closure $old, object $to [, mixed $scope = "static" ] [, bool $unbound_scoped = false ] ) Create a closure from another one and bind to another object and scope */ ZEND_METHOD(Closure, bind) { zval *newthis, *zclosure, *scope_arg = NULL; zend_closure *closure; - zend_class_entry *ce, **ce_p; + zend_class_entry *ce; + zend_bool unbound_scoped = false; - if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oo!|z", &zclosure, zend_ce_closure, &newthis, &scope_arg) == FAILURE) { + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oo!|zb", &zclosure, zend_ce_closure, &newthis, &scope_arg, &unbound_scoped) == FAILURE) { RETURN_NULL(); } @@@ -549,26 -456,17 +525,25 @@@ ZEND_API void zend_create_closure_ex(zv } } - closure->this_ptr = NULL; + ZVAL_UNDEF(&closure->this_ptr); /* Invariants: - * If the closure is unscoped, it has no bound object. - * The the closure is scoped, it's either static or it's bound */ + * If the closure is unscoped, it is unbound. + * If the closure is scoped, it may be static, bound or unbound */ closure->func.common.scope = scope; if (scope) { closure->func.common.fn_flags |= ZEND_ACC_PUBLIC; - if (this_ptr && (closure->func.common.fn_flags & ZEND_ACC_STATIC) == 0) { - closure->this_ptr = this_ptr; - Z_ADDREF_P(this_ptr); - /* We assume that a static scoped closure is desired if given NULL to bind to - If an unbound scoped closure is desired, the parameter must be set to 1*/ - } else if (!unbound_scoped) { + if (this_ptr && Z_TYPE_P(this_ptr) == IS_OBJECT && (closure->func.common.fn_flags & ZEND_ACC_STATIC) == 0) { + ZVAL_COPY(&closure->this_ptr, this_ptr); - } else { ++ /* We assume that a static scoped closure is desired if given NULL to bind to ++ If an unbound scoped closure is desired, the parameter must be set to 1*/ ++ } else if (!unbound_scoped) { closure->func.common.fn_flags |= ZEND_ACC_STATIC; + /* Unbound but scoped was explicitly specified and we wish to avoid E_ERROR when calling without object + We don't do this if it has implict allowed static (i.e. is a method, should E_STRICT) + Nor do we do this if it's an internal function (which would blow up if $this was NULL) + (In that case, we're actually creating a closure which can't be called without apply) */ + } else if ((func->common.fn_flags & ZEND_ACC_ALLOW_STATIC) == 0 && func->type == ZEND_USER_FUNCTION) { + closure->func.common.fn_flags |= ZEND_ACC_ALLOW_STATIC_EXPLICIT; } } } diff --cc Zend/zend_closures.h index d916d5e,4f8ae6d..0f9ab5c --- a/Zend/zend_closures.h +++ b/Zend/zend_closures.h @@@ -29,8 -29,7 +29,8 @@@ void zend_register_closure_ce(TSRMLS_D) extern ZEND_API zend_class_entry *zend_ce_closure; ZEND_API void zend_create_closure(zval *res, zend_function *op_array, zend_class_entry *scope, zval *this_ptr TSRMLS_DC); +ZEND_API void zend_create_closure_ex(zval *res, zend_function *op_array, zend_class_entry *scope, zval *this_ptr, zend_bool unbound_scoped TSRMLS_DC); - ZEND_API zend_function *zend_get_closure_invoke_method(zval *obj TSRMLS_DC); + ZEND_API zend_function *zend_get_closure_invoke_method(zend_object *obj TSRMLS_DC); ZEND_API const zend_function *zend_get_closure_method_def(zval *obj TSRMLS_DC); ZEND_API zval* zend_get_closure_this_ptr(zval *obj TSRMLS_DC); diff --cc Zend/zend_vm_def.h index 5a93cdd,7749197..9e91663 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@@ -2804,39 -2582,234 +2582,235 @@@ ZEND_VM_HANDLER(69, ZEND_INIT_NS_FCALL_ ZEND_VM_NEXT_OPCODE(); } - ZEND_VM_HANDLER(61, ZEND_DO_FCALL_BY_NAME, ANY, ANY) + ZEND_VM_HANDLER(61, ZEND_INIT_FCALL, ANY, CONST) { - EX(function_state).function = EX(call)->fbc; - ZEND_VM_DISPATCH_TO_HELPER(zend_do_fcall_common_helper); + USE_OPLINE + zend_free_op free_op2; + zval *fname = GET_OP2_ZVAL_PTR(BP_VAR_R); + zval *func; + zend_function *fbc; + + if (CACHED_PTR(Z_CACHE_SLOT_P(fname))) { + fbc = CACHED_PTR(Z_CACHE_SLOT_P(fname)); + } else if (UNEXPECTED((func = zend_hash_find(EG(function_table), Z_STR_P(fname))) == NULL)) { + SAVE_OPLINE(); + zend_error_noreturn(E_ERROR, "Call to undefined function %s()", Z_STRVAL_P(fname)); + } else { + fbc = Z_FUNC_P(func); + CACHE_PTR(Z_CACHE_SLOT_P(fname), fbc); + } + + EX(call) = zend_vm_stack_push_call_frame( + fbc, opline->extended_value, 0, NULL, NULL, EX(call) TSRMLS_CC); + + FREE_OP2(); + + ZEND_VM_NEXT_OPCODE(); } - ZEND_VM_HANDLER(60, ZEND_DO_FCALL, CONST, ANY) + ZEND_VM_HANDLER(60, ZEND_DO_FCALL, ANY, ANY) { USE_OPLINE - zend_free_op free_op1; - zval *fname = GET_OP1_ZVAL_PTR(BP_VAR_R); - call_slot *call = EX(call_slots) + opline->op2.num; + zend_execute_data *call = EX(call); + zend_function *fbc = call->func; - if (CACHED_PTR(opline->op1.literal->cache_slot)) { - EX(function_state).function = CACHED_PTR(opline->op1.literal->cache_slot); - } else if (UNEXPECTED(zend_hash_quick_find(EG(function_table), Z_STRVAL_P(fname), Z_STRLEN_P(fname)+1, Z_HASH_P(fname), (void **) &EX(function_state).function)==FAILURE)) { - SAVE_OPLINE(); - zend_error_noreturn(E_ERROR, "Call to undefined function %s()", fname->value.str.val); - } else { - CACHE_PTR(opline->op1.literal->cache_slot, EX(function_state).function); + SAVE_OPLINE(); + EX(call) = call->prev_nested_call; + if (UNEXPECTED((fbc->common.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_DEPRECATED)) != 0)) { + if (UNEXPECTED((fbc->common.fn_flags & ZEND_ACC_ABSTRACT) != 0)) { + zend_error_noreturn(E_ERROR, "Cannot call abstract method %s::%s()", fbc->common.scope->name->val, fbc->common.function_name->val); + } + if (UNEXPECTED((fbc->common.fn_flags & ZEND_ACC_DEPRECATED) != 0)) { + zend_error(E_DEPRECATED, "Function %s%s%s() is deprecated", + fbc->common.scope ? fbc->common.scope->name->val : "", + fbc->common.scope ? "::" : "", + fbc->common.function_name->val); + if (UNEXPECTED(EG(exception) != NULL)) { + HANDLE_EXCEPTION(); + } + } + } + if (fbc->common.scope && + !(fbc->common.fn_flags & ZEND_ACC_STATIC) && + !call->object) { + + if (fbc->common.fn_flags & ZEND_ACC_ALLOW_STATIC) { + /* FIXME: output identifiers properly */ + zend_error(E_STRICT, "Non-static method %s::%s() should not be called statically", fbc->common.scope->name->val, fbc->common.function_name->val); + if (UNEXPECTED(EG(exception) != NULL)) { + HANDLE_EXCEPTION(); + } - } else { ++ } else if (!(fbc->common.fn_flags & ZEND_ACC_ALLOW_STATIC_EXPLICIT)){ + /* FIXME: output identifiers properly */ + /* An internal function assumes $this is present and won't check that. So PHP would crash by allowing the call. */ + zend_error_noreturn(E_ERROR, "Non-static method %s::%s() cannot be called statically", fbc->common.scope->name->val, fbc->common.function_name->val); + } } ++} - call->fbc = EX(function_state).function; - call->object = NULL; - call->called_scope = NULL; - call->num_additional_args = 0; - call->is_ctor_call = 0; - EX(call) = call; + LOAD_OPLINE(); - FREE_OP1(); + if (UNEXPECTED(fbc->type == ZEND_INTERNAL_FUNCTION)) { + int should_change_scope = 0; + zval *ret; + + if (fbc->common.scope) { + should_change_scope = 1; + Z_OBJ(EG(This)) = call->object; + /* TODO: we don't set scope if we call an object method ??? */ + /* See: ext/pdo_sqlite/tests/pdo_fetch_func_001.phpt */ + #if 1 + EG(scope) = (call->object) ? NULL : fbc->common.scope; + #else + EG(scope) = fbc->common.scope; + #endif + } else { + call->called_scope = EX(called_scope); + } + + call->prev_execute_data = execute_data; + EG(current_execute_data) = call; - ZEND_VM_DISPATCH_TO_HELPER(zend_do_fcall_common_helper); + if (fbc->common.fn_flags & ZEND_ACC_HAS_TYPE_HINTS) { + zend_uint i; + zval *p = ZEND_CALL_ARG(call, 1); + + for (i = 0; i < call->num_args; ++i) { + zend_verify_arg_type(fbc, i + 1, p, 0 TSRMLS_CC); + p++; + } + if (UNEXPECTED(EG(exception) != NULL)) { + EG(current_execute_data) = call->prev_execute_data; + zend_vm_stack_free_args(call TSRMLS_CC); + zend_vm_stack_free_call_frame(call TSRMLS_CC); + if (RETURN_VALUE_USED(opline)) { + ZVAL_UNDEF(EX_VAR(opline->result.var)); + } + if (UNEXPECTED(should_change_scope)) { + ZEND_VM_C_GOTO(fcall_end_change_scope); + } else { + ZEND_VM_C_GOTO(fcall_end); + } + } + } + + ret = EX_VAR(opline->result.var); + ZVAL_NULL(ret); + Z_VAR_FLAGS_P(ret) = (fbc->common.fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0 ? IS_VAR_RET_REF : 0; + + if (!zend_execute_internal) { + /* saves one function call if zend_execute_internal is not used */ + fbc->internal_function.handler(call->num_args, ret TSRMLS_CC); + } else { + zend_execute_internal(call, ret TSRMLS_CC); + } + EG(current_execute_data) = call->prev_execute_data; + zend_vm_stack_free_args(call TSRMLS_CC); + zend_vm_stack_free_call_frame(call TSRMLS_CC); + + if (!RETURN_VALUE_USED(opline)) { + zval_ptr_dtor(ret); + } + + if (UNEXPECTED(should_change_scope)) { + ZEND_VM_C_GOTO(fcall_end_change_scope); + } else { + ZEND_VM_C_GOTO(fcall_end); + } + } else if (EXPECTED(fbc->type == ZEND_USER_FUNCTION)) { + zval *return_value = NULL; + + Z_OBJ(EG(This)) = call->object; + EG(scope) = fbc->common.scope; + call->symbol_table = NULL; + if (RETURN_VALUE_USED(opline)) { + return_value = EX_VAR(opline->result.var); + + ZVAL_NULL(return_value); + Z_VAR_FLAGS_P(return_value) = (fbc->common.fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0 ? IS_VAR_RET_REF : 0; + } + + if (UNEXPECTED((fbc->common.fn_flags & ZEND_ACC_GENERATOR) != 0)) { + if (RETURN_VALUE_USED(opline)) { + zend_generator_create_zval(call, &fbc->op_array, EX_VAR(opline->result.var) TSRMLS_CC); + } else { + zend_vm_stack_free_args(call TSRMLS_CC); + } + + zend_vm_stack_free_call_frame(call TSRMLS_CC); + } else { + call->prev_execute_data = execute_data; + i_init_func_execute_data(call, &fbc->op_array, return_value, EXPECTED(zend_execute_ex == execute_ex) ? VM_FRAME_NESTED_FUNCTION : VM_FRAME_TOP_FUNCTION TSRMLS_CC); + + if (EXPECTED(zend_execute_ex == execute_ex)) { + ZEND_VM_ENTER(); + } else { + zend_execute_ex(call TSRMLS_CC); + } + } + } else { /* ZEND_OVERLOADED_FUNCTION */ + Z_OBJ(EG(This)) = call->object; + EG(scope) = fbc->common.scope; + + ZVAL_NULL(EX_VAR(opline->result.var)); + + /* Not sure what should be done here if it's a static method */ + if (EXPECTED(call->object != NULL)) { + call->prev_execute_data = execute_data; + EG(current_execute_data) = call; + call->object->handlers->call_method(fbc->common.function_name, call->object, call->num_args, EX_VAR(opline->result.var) TSRMLS_CC); + EG(current_execute_data) = call->prev_execute_data; + } else { + zend_error_noreturn(E_ERROR, "Cannot call overloaded function for non-object"); + } + + zend_vm_stack_free_args(call TSRMLS_CC); + + zend_vm_stack_free_call_frame(call TSRMLS_CC); + + if (fbc->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY) { + STR_RELEASE(fbc->common.function_name); + } + efree(fbc); + + if (!RETURN_VALUE_USED(opline)) { + zval_ptr_dtor(EX_VAR(opline->result.var)); + } else { + //??? Z_UNSET_ISREF_P(EX_T(opline->result.var).var.ptr); + //??? Z_SET_REFCOUNT_P(EX_T(opline->result.var).var.ptr, 1); + Z_VAR_FLAGS_P(EX_VAR(opline->result.var)) = 0; + } + } + + ZEND_VM_C_LABEL(fcall_end_change_scope): + if (Z_OBJ(EG(This))) { + if (UNEXPECTED(EG(exception) != NULL) && (opline->op1.num & ZEND_CALL_CTOR)) { + if (!(opline->op1.num & ZEND_CALL_CTOR_RESULT_UNUSED)) { + Z_DELREF(EG(This)); + } + if (Z_REFCOUNT(EG(This)) == 1) { + zend_object_store_ctor_failed(Z_OBJ(EG(This)) TSRMLS_CC); + } + } + if (!Z_DELREF(EG(This))) { + _zval_dtor_func_for_ptr(Z_COUNTED(EG(This)) ZEND_FILE_LINE_CC); + } else if (UNEXPECTED(!Z_GC_INFO(EG(This)))) { + gc_possible_root(Z_COUNTED(EG(This)) TSRMLS_CC); + } + } + Z_OBJ(EG(This)) = EX(object); + EG(scope) = EX(scope); + + ZEND_VM_C_LABEL(fcall_end): + if (UNEXPECTED(EG(exception) != NULL)) { + zend_throw_exception_internal(NULL TSRMLS_CC); + if (RETURN_VALUE_USED(opline)) { + zval_ptr_dtor(EX_VAR(opline->result.var)); + } + HANDLE_EXCEPTION(); + } + + ZEND_VM_NEXT_OPCODE(); } ZEND_VM_HANDLER(62, ZEND_RETURN, CONST|TMP|VAR|CV, ANY) diff --cc Zend/zend_vm_execute.h index 5efc0c7,c2e59c1..8126359 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@@ -509,10 -533,10 +533,10 @@@ static int ZEND_FASTCALL ZEND_DO_FCALL if (UNEXPECTED(EG(exception) != NULL)) { HANDLE_EXCEPTION(); } - } else { + } else if (!(fbc->common.fn_flags & ZEND_ACC_ALLOW_STATIC_EXPLICIT)){ /* FIXME: output identifiers properly */ /* An internal function assumes $this is present and won't check that. So PHP would crash by allowing the call. */ - zend_error_noreturn(E_ERROR, "Non-static method %s::%s() cannot be called statically", fbc->common.scope->name, fbc->common.function_name); + zend_error_noreturn(E_ERROR, "Non-static method %s::%s() cannot be called statically", fbc->common.scope->name->val, fbc->common.function_name->val); } }