[php-src] master: Zend: add zend_get_closure() API

From: Date: Sun, 22 Nov 2026 15:03:44 +0000
Subject: [php-src] master: Zend: add zend_get_closure() API
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-144028@lists.php.net to get a copy of this message
Author: Gina Peter Banyard (Girgias) Date: 2026-11-22T15:03:26Z Commit: https://github.com/php/php-src/commit/6a6d54faf305bb9193c12ff25be06a306a8b9811 Raw diff: https://github.com/php/php-src/commit/6a6d54faf305bb9193c12ff25be06a306a8b9811.diff Zend: add zend_get_closure() API Which is a small wrapper around zend_object->handlers->get_closure Changed paths: M Zend/zend_API.c M Zend/zend_closures.h M Zend/zend_execute.c M ext/reflection/php_reflection.c Diff: diff --git a/Zend/zend_API.c b/Zend/zend_API.c index c19e9d64e266..81991a1e8385 100644 --- a/Zend/zend_API.c +++ b/Zend/zend_API.c @@ -4212,9 +4212,9 @@ ZEND_API bool zend_is_callable_at_frame( } case IS_OBJECT: - if (!Z_OBJ_HANDLER_P(callable, get_closure) || Z_OBJ_HANDLER_P(callable, get_closure)(Z_OBJ_P(callable), &fcc->calling_scope, &fcc->function_handler, &fcc->object, 1) == FAILURE) { + if (UNEXPECTED(!zend_get_closure(Z_OBJ_P(callable), &fcc->calling_scope, &fcc->function_handler, &fcc->object, true))) { if (error) *error = estrdup("no array or string given"); - return 0; + return false; } fcc->called_scope = fcc->calling_scope; fcc->closure = Z_OBJ_P(callable); diff --git a/Zend/zend_closures.h b/Zend/zend_closures.h index 8d4572842662..36a402ac192a 100644 --- a/Zend/zend_closures.h +++ b/Zend/zend_closures.h @@ -46,6 +46,10 @@ ZEND_API bool zend_fn_closure_equals_ex( const zend_function *fn_b, const zend_object *closure_zobj_b ); +static zend_always_inline bool zend_get_closure(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **this_ptr, bool check_only) { + return obj->handlers->get_closure && obj->handlers->get_closure(obj, ce_ptr, fptr_ptr, this_ptr, check_only) == SUCCESS; +} + END_EXTERN_C() #endif diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index 37f5e9eedcaf..7d270c3e249d 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -5213,33 +5213,31 @@ static zend_never_inline zend_execute_data *zend_init_dynamic_call_object(zend_o zend_object *object; uint32_t call_info; - if (EXPECTED(function->handlers->get_closure) && - EXPECTED(function->handlers->get_closure(function, &called_scope, &fbc, &object, 0) == SUCCESS)) { - - object_or_called_scope = called_scope; - if (EXPECTED(fbc->common.fn_flags & ZEND_ACC_CLOSURE)) { - /* Delay closure destruction until its invocation */ - GC_ADDREF(ZEND_CLOSURE_OBJECT(fbc)); - ZEND_ASSERT(ZEND_ACC_FAKE_CLOSURE == ZEND_CALL_FAKE_CLOSURE); - call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC | ZEND_CALL_CLOSURE | - (fbc->common.fn_flags & ZEND_ACC_FAKE_CLOSURE); - if (object) { - call_info |= ZEND_CALL_HAS_THIS; - object_or_called_scope = object; - } - } else { - call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC; - if (object) { - call_info |= ZEND_CALL_RELEASE_THIS | ZEND_CALL_HAS_THIS; - /* For $this pointer */ - object_or_called_scope = zend_object_copy(object); - } - } - } else { + if (UNEXPECTED(!zend_get_closure(function, &called_scope, &fbc, &object, false))) { zend_throw_error(NULL, "Object of type %pS is not callable", function->ce->name); return NULL; } + object_or_called_scope = called_scope; + if (EXPECTED(fbc->common.fn_flags & ZEND_ACC_CLOSURE)) { + /* Delay closure destruction until its invocation */ + GC_ADDREF(ZEND_CLOSURE_OBJECT(fbc)); + ZEND_ASSERT(ZEND_ACC_FAKE_CLOSURE == ZEND_CALL_FAKE_CLOSURE); + call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC | ZEND_CALL_CLOSURE | + (fbc->common.fn_flags & ZEND_ACC_FAKE_CLOSURE); + if (object) { + call_info |= ZEND_CALL_HAS_THIS; + object_or_called_scope = object; + } + } else { + call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC; + if (object) { + call_info |= ZEND_CALL_RELEASE_THIS | ZEND_CALL_HAS_THIS; + GC_ADDREF(object); /* For $this pointer */ + object_or_called_scope = object; + } + } + if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) { init_func_run_time_cache(&fbc->op_array); } diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 171770258afd..1ddf8d839696 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -1813,11 +1813,12 @@ ZEND_METHOD(ReflectionFunctionAbstract, getClosureCalledClass) if (!Z_ISUNDEF(intern->obj)) { zend_class_entry *called_scope; zend_function *closure_func; - zend_object *object; - if (Z_OBJ_HANDLER(intern->obj, get_closure) - && Z_OBJ_HANDLER(intern->obj, get_closure)(Z_OBJ(intern->obj), &called_scope, &closure_func, &object, true) == SUCCESS - && closure_func && (called_scope || closure_func->common.scope) - ) { + zend_object *this_ptr; + + if (zend_get_closure(Z_OBJ(intern->obj), &called_scope, &closure_func, &this_ptr, true) + /* if zend_get_closure succeeds we have a non-NULL closure_func */ + && (called_scope || closure_func->common.scope)) { + ZEND_ASSERT(closure_func); zend_reflection_class_factory(called_scope ? called_scope : closure_func->common.scope, return_value); } } @@ -2070,8 +2071,7 @@ ZEND_METHOD(ReflectionFunction, invoke) fcc.object = NULL; if (!Z_ISUNDEF(intern->obj)) { - Z_OBJ_HT(intern->obj)->get_closure( - Z_OBJ(intern->obj), &fcc.called_scope, &fcc.function_handler, &fcc.object, false); + zend_get_closure(Z_OBJ(intern->obj), &fcc.calling_scope, &fcc.function_handler, &fcc.object, false); } zval retval; @@ -2103,8 +2103,7 @@ ZEND_METHOD(ReflectionFunction, invokeArgs) fcc.object = NULL; if (!Z_ISUNDEF(intern->obj)) { - Z_OBJ_HT(intern->obj)->get_closure( - Z_OBJ(intern->obj), &fcc.called_scope, &fcc.function_handler, &fcc.object, false); + zend_get_closure(Z_OBJ(intern->obj), &fcc.called_scope, &fcc.function_handler, &fcc.object, false); } zval retval;

« previous php.cvs (#144028) next »