Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 6 additions & 4 deletions Zend/zend_closures.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ typedef struct _zend_closure {
ZEND_API zend_class_entry *zend_ce_closure;
static zend_object_handlers closure_handlers;

static zend_result zend_closure_get_closure(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr, bool check_only);
static void zend_create_closure_ex(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zend_object *this_ptr, bool is_fake, uint32_t flags);

static inline uint32_t zend_closure_flags(const zend_closure *closure)
Expand All @@ -68,11 +67,14 @@ ZEND_METHOD(Closure, __invoke) /* {{{ */
Z_PARAM_VARIADIC_WITH_NAMED(args, num_args, named_args)
ZEND_PARSE_PARAMETERS_END();

zend_fcall_info_cache fcc = {
zend_closure *closure = (zend_closure*)Z_OBJ_P(ZEND_THIS);
const zend_fcall_info_cache fcc = {
.function_handler = &closure->func,
.calling_scope = closure->called_scope,
.called_scope = closure->called_scope,
.object = closure->this_ptr,
.closure = Z_OBJ_P(ZEND_THIS),
};
zend_closure_get_closure(Z_OBJ_P(ZEND_THIS), &fcc.calling_scope, &fcc.function_handler, &fcc.object, false);
fcc.called_scope = fcc.calling_scope;
zend_call_known_fcc(&fcc, return_value, num_args, args, named_args);
zend_return_unwrap_ref(execute_data, return_value);

Expand Down
4 changes: 4 additions & 0 deletions Zend/zend_closures.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ ZEND_API bool zend_fn_closure_equals_ex(
const zend_function *fn_b, const zend_object *closure_zobj_b
);

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);
}

END_EXTERN_C()

#endif
44 changes: 21 additions & 23 deletions Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
17 changes: 8 additions & 9 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Loading