2008-07-14 17:49:03 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
2019-01-30 17:23:29 +08:00
|
|
|
| Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
|
2008-07-14 17:49:03 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 2.00 of the Zend license, |
|
2013-02-19 12:56:02 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2008-07-14 17:49:03 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
|
|
|
| http://www.zend.com/license/2_00.txt. |
|
|
|
|
| If you did not receive a copy of the Zend license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@zend.com so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Christian Seiler <chris_se@gmx.net> |
|
2018-11-01 23:20:07 +08:00
|
|
|
| Dmitry Stogov <dmitry@php.net> |
|
2009-01-02 00:22:44 +08:00
|
|
|
| Marcus Boerger <helly@php.net> |
|
2008-07-14 17:49:03 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "zend.h"
|
|
|
|
#include "zend_API.h"
|
|
|
|
#include "zend_closures.h"
|
2009-01-02 00:22:44 +08:00
|
|
|
#include "zend_exceptions.h"
|
2008-12-22 22:11:49 +08:00
|
|
|
#include "zend_interfaces.h"
|
2008-07-14 17:49:03 +08:00
|
|
|
#include "zend_objects.h"
|
|
|
|
#include "zend_objects_API.h"
|
|
|
|
#include "zend_globals.h"
|
|
|
|
|
|
|
|
#define ZEND_CLOSURE_PRINT_NAME "Closure object"
|
|
|
|
|
2008-07-22 15:29:31 +08:00
|
|
|
#define ZEND_CLOSURE_PROPERTY_ERROR() \
|
2015-07-08 01:10:22 +08:00
|
|
|
zend_throw_error(NULL, "Closure object cannot have properties")
|
2008-07-22 15:29:31 +08:00
|
|
|
|
2008-07-14 17:49:03 +08:00
|
|
|
typedef struct _zend_closure {
|
2015-05-06 03:06:34 +08:00
|
|
|
zend_object std;
|
|
|
|
zend_function func;
|
|
|
|
zval this_ptr;
|
|
|
|
zend_class_entry *called_scope;
|
2017-06-08 21:52:39 +08:00
|
|
|
zif_handler orig_internal_handler;
|
2008-07-14 17:49:03 +08:00
|
|
|
} zend_closure;
|
|
|
|
|
2008-08-11 16:49:00 +08:00
|
|
|
/* non-static since it needs to be referenced */
|
|
|
|
ZEND_API zend_class_entry *zend_ce_closure;
|
2008-07-14 17:49:03 +08:00
|
|
|
static zend_object_handlers closure_handlers;
|
|
|
|
|
|
|
|
ZEND_METHOD(Closure, __invoke) /* {{{ */
|
|
|
|
{
|
2014-10-03 23:32:46 +08:00
|
|
|
zend_function *func = EX(func);
|
2016-04-16 20:47:27 +08:00
|
|
|
zval *arguments = ZEND_CALL_ARG(execute_data, 1);
|
2008-07-14 17:49:03 +08:00
|
|
|
|
2018-11-16 00:54:19 +08:00
|
|
|
if (call_user_function(CG(function_table), NULL, ZEND_THIS, return_value, ZEND_NUM_ARGS(), arguments) == FAILURE) {
|
2008-07-26 21:14:04 +08:00
|
|
|
RETVAL_FALSE;
|
2008-07-14 17:49:03 +08:00
|
|
|
}
|
|
|
|
|
2008-07-26 21:14:04 +08:00
|
|
|
/* destruct the function also, then - we have allocated it in get_method */
|
2018-05-28 21:27:12 +08:00
|
|
|
zend_string_release_ex(func->internal_function.function_name, 0);
|
2008-07-26 21:14:04 +08:00
|
|
|
efree(func);
|
2015-02-21 22:44:51 +08:00
|
|
|
#if ZEND_DEBUG
|
|
|
|
execute_data->func = NULL;
|
|
|
|
#endif
|
2008-07-26 21:14:04 +08:00
|
|
|
}
|
2008-07-14 20:18:23 +08:00
|
|
|
/* }}} */
|
|
|
|
|
2015-10-10 19:39:26 +08:00
|
|
|
static zend_bool zend_valid_closure_binding(
|
|
|
|
zend_closure *closure, zval *newthis, zend_class_entry *scope) /* {{{ */
|
|
|
|
{
|
|
|
|
zend_function *func = &closure->func;
|
2016-03-30 05:48:07 +08:00
|
|
|
zend_bool is_fake_closure = (func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) != 0;
|
2015-10-10 19:39:26 +08:00
|
|
|
if (newthis) {
|
|
|
|
if (func->common.fn_flags & ZEND_ACC_STATIC) {
|
|
|
|
zend_error(E_WARNING, "Cannot bind an instance to a static closure");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-30 05:48:07 +08:00
|
|
|
if (is_fake_closure && func->common.scope &&
|
2015-10-10 19:39:26 +08:00
|
|
|
!instanceof_function(Z_OBJCE_P(newthis), func->common.scope)) {
|
|
|
|
/* Binding incompatible $this to an internal method is not supported. */
|
2016-03-30 05:48:07 +08:00
|
|
|
zend_error(E_WARNING, "Cannot bind method %s::%s() to object of class %s",
|
2015-10-10 19:39:26 +08:00
|
|
|
ZSTR_VAL(func->common.scope->name),
|
|
|
|
ZSTR_VAL(func->common.function_name),
|
|
|
|
ZSTR_VAL(Z_OBJCE_P(newthis)->name));
|
|
|
|
return 0;
|
|
|
|
}
|
2018-09-30 02:58:17 +08:00
|
|
|
} else if (is_fake_closure && func->common.scope
|
|
|
|
&& !(func->common.fn_flags & ZEND_ACC_STATIC)) {
|
2019-01-29 22:16:51 +08:00
|
|
|
zend_error(E_WARNING, "Cannot unbind $this of method");
|
|
|
|
return 0;
|
2019-08-16 18:55:28 +08:00
|
|
|
} else if (!is_fake_closure && !Z_ISUNDEF(closure->this_ptr)
|
|
|
|
&& (func->common.fn_flags & ZEND_ACC_USES_THIS)) {
|
2019-07-12 18:53:09 +08:00
|
|
|
// TODO: Only deprecate if it had $this *originally*?
|
|
|
|
zend_error(E_DEPRECATED, "Unbinding $this of closure is deprecated");
|
2015-10-10 19:39:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (scope && scope != func->common.scope && scope->type == ZEND_INTERNAL_CLASS) {
|
|
|
|
/* rebinding to internal class is not allowed */
|
|
|
|
zend_error(E_WARNING, "Cannot bind closure to scope of internal class %s",
|
|
|
|
ZSTR_VAL(scope->name));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-30 05:48:07 +08:00
|
|
|
if (is_fake_closure && scope != func->common.scope) {
|
2015-10-10 00:41:15 +08:00
|
|
|
zend_error(E_WARNING, "Cannot rebind scope of closure created by ReflectionFunctionAbstract::getClosure()");
|
2015-10-10 19:39:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2015-06-24 03:47:07 +08:00
|
|
|
/* {{{ proto mixed Closure::call(object to [, mixed parameter] [, mixed ...] )
|
2014-08-18 07:03:56 +08:00
|
|
|
Call closure, binding to a given object with its class as the scope */
|
2015-01-03 17:22:58 +08:00
|
|
|
ZEND_METHOD(Closure, call)
|
2014-07-29 09:36:17 +08:00
|
|
|
{
|
2018-11-14 07:44:25 +08:00
|
|
|
zval *newthis, closure_result;
|
2014-07-29 09:36:17 +08:00
|
|
|
zend_closure *closure;
|
|
|
|
zend_fcall_info fci;
|
|
|
|
zend_fcall_info_cache fci_cache;
|
2014-08-18 06:47:47 +08:00
|
|
|
zend_function my_function;
|
2015-04-28 12:43:43 +08:00
|
|
|
zend_object *newobj;
|
2014-07-29 09:36:17 +08:00
|
|
|
|
2018-01-12 02:23:52 +08:00
|
|
|
fci.param_count = 0;
|
|
|
|
fci.params = NULL;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o*", &newthis, &fci.params, &fci.param_count) == FAILURE) {
|
2014-08-20 07:11:11 +08:00
|
|
|
return;
|
2014-07-29 09:36:17 +08:00
|
|
|
}
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2018-11-16 00:54:19 +08:00
|
|
|
closure = (zend_closure *) Z_OBJ_P(ZEND_THIS);
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2015-04-28 12:43:43 +08:00
|
|
|
newobj = Z_OBJ_P(newthis);
|
|
|
|
|
2015-10-10 19:39:26 +08:00
|
|
|
if (!zend_valid_closure_binding(closure, newthis, Z_OBJCE_P(newthis))) {
|
2015-04-28 12:43:43 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-12 02:23:52 +08:00
|
|
|
if (closure->func.common.fn_flags & ZEND_ACC_GENERATOR) {
|
2015-09-02 00:04:00 +08:00
|
|
|
zval new_closure;
|
2018-01-12 02:23:52 +08:00
|
|
|
zend_create_closure(&new_closure, &closure->func, Z_OBJCE_P(newthis), closure->called_scope, newthis);
|
2015-09-02 00:04:00 +08:00
|
|
|
closure = (zend_closure *) Z_OBJ(new_closure);
|
|
|
|
fci_cache.function_handler = &closure->func;
|
|
|
|
} else {
|
2018-01-12 02:23:52 +08:00
|
|
|
memcpy(&my_function, &closure->func, closure->func.type == ZEND_USER_FUNCTION ? sizeof(zend_op_array) : sizeof(zend_internal_function));
|
|
|
|
my_function.common.fn_flags &= ~ZEND_ACC_CLOSURE;
|
2015-10-07 04:48:10 +08:00
|
|
|
/* use scope of passed object */
|
2015-10-07 04:48:15 +08:00
|
|
|
my_function.common.scope = Z_OBJCE_P(newthis);
|
|
|
|
fci_cache.function_handler = &my_function;
|
2015-09-02 00:04:00 +08:00
|
|
|
|
|
|
|
/* Runtime cache relies on bound scope to be immutable, hence we need a separate rt cache in case scope changed */
|
2018-10-02 14:10:04 +08:00
|
|
|
if (ZEND_USER_CODE(my_function.type)
|
|
|
|
&& (closure->func.common.scope != Z_OBJCE_P(newthis)
|
|
|
|
|| (closure->func.common.fn_flags & ZEND_ACC_HEAP_RT_CACHE))) {
|
2018-10-17 20:52:50 +08:00
|
|
|
void *ptr;
|
|
|
|
|
2018-10-02 14:10:04 +08:00
|
|
|
my_function.op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE;
|
2018-10-17 20:52:50 +08:00
|
|
|
ptr = emalloc(sizeof(void*) + my_function.op_array.cache_size);
|
|
|
|
ZEND_MAP_PTR_INIT(my_function.op_array.run_time_cache, ptr);
|
|
|
|
ptr = (char*)ptr + sizeof(void*);
|
|
|
|
ZEND_MAP_PTR_SET(my_function.op_array.run_time_cache, ptr);
|
|
|
|
memset(ptr, 0, my_function.op_array.cache_size);
|
2015-09-02 00:04:00 +08:00
|
|
|
}
|
2015-06-21 22:39:28 +08:00
|
|
|
}
|
|
|
|
|
2018-01-12 02:23:52 +08:00
|
|
|
fci_cache.called_scope = newobj->ce;
|
|
|
|
fci_cache.object = fci.object = newobj;
|
|
|
|
|
|
|
|
fci.size = sizeof(fci);
|
2018-11-14 07:44:25 +08:00
|
|
|
ZVAL_OBJ(&fci.function_name, &closure->std);
|
2018-01-12 02:23:52 +08:00
|
|
|
fci.retval = &closure_result;
|
|
|
|
fci.no_separation = 1;
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
if (zend_call_function(&fci, &fci_cache) == SUCCESS && Z_TYPE(closure_result) != IS_UNDEF) {
|
2016-10-01 04:08:08 +08:00
|
|
|
if (Z_ISREF(closure_result)) {
|
|
|
|
zend_unwrap_reference(&closure_result);
|
|
|
|
}
|
2014-08-18 03:20:23 +08:00
|
|
|
ZVAL_COPY_VALUE(return_value, &closure_result);
|
2014-07-29 09:36:17 +08:00
|
|
|
}
|
2015-06-21 22:39:28 +08:00
|
|
|
|
2015-09-02 00:04:00 +08:00
|
|
|
if (fci_cache.function_handler->common.fn_flags & ZEND_ACC_GENERATOR) {
|
|
|
|
/* copied upon generator creation */
|
2017-10-27 06:28:58 +08:00
|
|
|
GC_DELREF(&closure->std);
|
2019-09-02 19:05:28 +08:00
|
|
|
} else if (ZEND_USER_CODE(my_function.type)
|
|
|
|
&& fci_cache.function_handler->common.fn_flags & ZEND_ACC_HEAP_RT_CACHE) {
|
2018-10-17 20:52:50 +08:00
|
|
|
efree(ZEND_MAP_PTR(my_function.op_array.run_time_cache));
|
2015-06-21 22:39:28 +08:00
|
|
|
}
|
2014-07-29 09:36:17 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2015-06-24 03:47:07 +08:00
|
|
|
/* {{{ proto Closure Closure::bind(callable old, object to [, mixed scope])
|
2011-09-07 14:46:27 +08:00
|
|
|
Create a closure from another one and bind to another object and scope */
|
2012-09-02 16:52:53 +08:00
|
|
|
ZEND_METHOD(Closure, bind)
|
2010-04-20 03:45:03 +08:00
|
|
|
{
|
2011-09-07 14:46:27 +08:00
|
|
|
zval *newthis, *zclosure, *scope_arg = NULL;
|
2018-01-15 19:13:55 +08:00
|
|
|
zend_closure *closure;
|
2015-05-06 03:06:34 +08:00
|
|
|
zend_class_entry *ce, *called_scope;
|
2010-04-20 03:45:03 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oo!|z", &zclosure, zend_ce_closure, &newthis, &scope_arg) == FAILURE) {
|
2015-10-10 19:39:26 +08:00
|
|
|
return;
|
2010-04-20 03:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-10-07 04:59:36 +08:00
|
|
|
closure = (zend_closure *)Z_OBJ_P(zclosure);
|
2010-04-20 03:45:03 +08:00
|
|
|
|
2011-09-07 14:46:27 +08:00
|
|
|
if (scope_arg != NULL) { /* scope argument was given */
|
2014-10-09 19:58:14 +08:00
|
|
|
if (Z_TYPE_P(scope_arg) == IS_OBJECT) {
|
2011-09-07 14:46:27 +08:00
|
|
|
ce = Z_OBJCE_P(scope_arg);
|
|
|
|
} else if (Z_TYPE_P(scope_arg) == IS_NULL) {
|
|
|
|
ce = NULL;
|
|
|
|
} else {
|
2017-11-16 22:09:32 +08:00
|
|
|
zend_string *tmp_class_name;
|
|
|
|
zend_string *class_name = zval_get_tmp_string(scope_arg, &tmp_class_name);
|
2014-08-26 04:40:58 +08:00
|
|
|
if (zend_string_equals_literal(class_name, "static")) {
|
2011-09-07 14:46:27 +08:00
|
|
|
ce = closure->func.common.scope;
|
2019-05-24 20:41:38 +08:00
|
|
|
} else if ((ce = zend_lookup_class(class_name)) == NULL) {
|
2015-06-30 18:59:27 +08:00
|
|
|
zend_error(E_WARNING, "Class '%s' not found", ZSTR_VAL(class_name));
|
2018-05-28 21:27:12 +08:00
|
|
|
zend_string_release_ex(class_name, 0);
|
2011-09-07 14:46:27 +08:00
|
|
|
RETURN_NULL();
|
|
|
|
}
|
2017-11-16 22:09:32 +08:00
|
|
|
zend_tmp_string_release(tmp_class_name);
|
2011-09-07 14:46:27 +08:00
|
|
|
}
|
|
|
|
} else { /* scope argument not given; do not change the scope by default */
|
|
|
|
ce = closure->func.common.scope;
|
|
|
|
}
|
2010-04-20 03:45:03 +08:00
|
|
|
|
2015-10-10 19:39:26 +08:00
|
|
|
if (!zend_valid_closure_binding(closure, newthis, ce)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-06 03:06:34 +08:00
|
|
|
if (newthis) {
|
|
|
|
called_scope = Z_OBJCE_P(newthis);
|
|
|
|
} else {
|
|
|
|
called_scope = ce;
|
|
|
|
}
|
|
|
|
|
2015-10-07 04:48:15 +08:00
|
|
|
zend_create_closure(return_value, &closure->func, ce, called_scope, newthis);
|
2010-04-20 03:45:03 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2017-06-08 21:52:39 +08:00
|
|
|
static ZEND_NAMED_FUNCTION(zend_closure_call_magic) /* {{{ */ {
|
2016-05-16 00:39:47 +08:00
|
|
|
zend_fcall_info fci;
|
|
|
|
zend_fcall_info_cache fcc;
|
2016-07-05 21:43:27 +08:00
|
|
|
zval params[2];
|
2016-05-16 00:39:47 +08:00
|
|
|
|
|
|
|
memset(&fci, 0, sizeof(zend_fcall_info));
|
2017-12-13 13:39:28 +08:00
|
|
|
memset(&fcc, 0, sizeof(zend_fcall_info_cache));
|
2016-05-16 00:39:47 +08:00
|
|
|
|
|
|
|
fci.size = sizeof(zend_fcall_info);
|
|
|
|
fci.retval = return_value;
|
|
|
|
|
2018-08-22 15:43:51 +08:00
|
|
|
fcc.function_handler = (EX(func)->internal_function.fn_flags & ZEND_ACC_STATIC) ?
|
|
|
|
EX(func)->internal_function.scope->__callstatic : EX(func)->internal_function.scope->__call;
|
2016-07-05 21:43:27 +08:00
|
|
|
fci.params = params;
|
2016-05-16 00:39:47 +08:00
|
|
|
fci.param_count = 2;
|
|
|
|
ZVAL_STR(&fci.params[0], EX(func)->common.function_name);
|
2017-10-24 22:27:31 +08:00
|
|
|
if (ZEND_NUM_ARGS()) {
|
|
|
|
array_init_size(&fci.params[1], ZEND_NUM_ARGS());
|
|
|
|
zend_copy_parameters_array(ZEND_NUM_ARGS(), &fci.params[1]);
|
|
|
|
} else {
|
|
|
|
ZVAL_EMPTY_ARRAY(&fci.params[1]);
|
|
|
|
}
|
2016-05-16 00:39:47 +08:00
|
|
|
|
2018-11-16 00:54:19 +08:00
|
|
|
fcc.object = fci.object = Z_OBJ_P(ZEND_THIS);
|
2016-05-16 00:39:47 +08:00
|
|
|
|
|
|
|
zend_call_function(&fci, &fcc);
|
|
|
|
|
|
|
|
zval_ptr_dtor(&fci.params[0]);
|
|
|
|
zval_ptr_dtor(&fci.params[1]);
|
|
|
|
}
|
2016-07-05 15:31:46 +08:00
|
|
|
/* }}} */
|
2016-05-16 00:39:47 +08:00
|
|
|
|
2016-07-05 15:31:46 +08:00
|
|
|
static int zend_create_closure_from_callable(zval *return_value, zval *callable, char **error) /* {{{ */ {
|
2016-05-16 00:39:47 +08:00
|
|
|
zend_fcall_info_cache fcc;
|
|
|
|
zend_function *mptr;
|
|
|
|
zval instance;
|
2016-07-05 20:53:20 +08:00
|
|
|
zend_internal_function call;
|
2016-05-16 00:39:47 +08:00
|
|
|
|
|
|
|
if (!zend_is_callable_ex(callable, NULL, 0, NULL, &fcc, error)) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mptr = fcc.function_handler;
|
|
|
|
if (mptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
|
|
|
|
memset(&call, 0, sizeof(zend_internal_function));
|
|
|
|
|
2019-03-11 20:39:34 +08:00
|
|
|
if (!mptr->common.scope) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
if (mptr->common.fn_flags & ZEND_ACC_STATIC) {
|
|
|
|
if (!mptr->common.scope->__callstatic) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!mptr->common.scope->__call) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 00:39:47 +08:00
|
|
|
call.type = ZEND_INTERNAL_FUNCTION;
|
2018-08-22 15:43:51 +08:00
|
|
|
call.fn_flags = mptr->common.fn_flags & ZEND_ACC_STATIC;
|
2016-05-16 00:39:47 +08:00
|
|
|
call.handler = zend_closure_call_magic;
|
|
|
|
call.function_name = mptr->common.function_name;
|
|
|
|
call.scope = mptr->common.scope;
|
|
|
|
|
|
|
|
zend_free_trampoline(mptr);
|
|
|
|
mptr = (zend_function *) &call;
|
|
|
|
}
|
|
|
|
|
2016-07-05 15:31:46 +08:00
|
|
|
if (fcc.object) {
|
|
|
|
ZVAL_OBJ(&instance, fcc.object);
|
2016-07-05 21:43:27 +08:00
|
|
|
zend_create_fake_closure(return_value, mptr, mptr->common.scope, fcc.called_scope, &instance);
|
2016-07-05 15:31:46 +08:00
|
|
|
} else {
|
2016-07-05 21:43:27 +08:00
|
|
|
zend_create_fake_closure(return_value, mptr, mptr->common.scope, fcc.called_scope, NULL);
|
2016-07-05 15:31:46 +08:00
|
|
|
}
|
2016-05-16 00:39:47 +08:00
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
2016-07-05 15:31:46 +08:00
|
|
|
/* }}} */
|
2016-05-16 00:39:47 +08:00
|
|
|
|
|
|
|
/* {{{ proto Closure Closure::fromCallable(callable callable)
|
2016-05-16 04:08:52 +08:00
|
|
|
Create a closure from a callable using the current scope. */
|
2016-05-16 00:39:47 +08:00
|
|
|
ZEND_METHOD(Closure, fromCallable)
|
|
|
|
{
|
|
|
|
zval *callable;
|
|
|
|
int success;
|
|
|
|
char *error = NULL;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &callable) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Z_TYPE_P(callable) == IS_OBJECT && instanceof_function(Z_OBJCE_P(callable), zend_ce_closure)) {
|
2016-07-05 15:31:46 +08:00
|
|
|
/* It's already a closure */
|
2016-05-16 00:39:47 +08:00
|
|
|
RETURN_ZVAL(callable, 1, 0);
|
|
|
|
}
|
|
|
|
|
2016-07-05 15:31:46 +08:00
|
|
|
/* create closure as if it were called from parent scope */
|
2016-05-16 00:39:47 +08:00
|
|
|
EG(current_execute_data) = EX(prev_execute_data);
|
|
|
|
success = zend_create_closure_from_callable(return_value, callable, &error);
|
|
|
|
EG(current_execute_data) = execute_data;
|
|
|
|
|
2019-01-29 23:03:24 +08:00
|
|
|
if (success == FAILURE) {
|
2016-05-16 00:39:47 +08:00
|
|
|
if (error) {
|
2019-01-07 19:28:51 +08:00
|
|
|
zend_type_error("Failed to create closure from callable: %s", error);
|
2016-05-16 00:39:47 +08:00
|
|
|
efree(error);
|
|
|
|
} else {
|
2019-01-07 19:28:51 +08:00
|
|
|
zend_type_error("Failed to create closure from callable");
|
2016-05-16 00:39:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2015-08-19 19:40:56 +08:00
|
|
|
static ZEND_COLD zend_function *zend_closure_get_constructor(zend_object *object) /* {{{ */
|
2008-07-14 17:49:03 +08:00
|
|
|
{
|
2015-07-08 01:10:22 +08:00
|
|
|
zend_throw_error(NULL, "Instantiation of 'Closure' is not allowed");
|
2008-07-14 17:49:03 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static int zend_closure_compare_objects(zval *o1, zval *o2) /* {{{ */
|
2008-07-14 17:49:03 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
return (Z_OBJ_P(o1) != Z_OBJ_P(o2));
|
2008-07-14 17:49:03 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
ZEND_API zend_function *zend_get_closure_invoke_method(zend_object *object) /* {{{ */
|
2008-08-11 16:49:00 +08:00
|
|
|
{
|
2014-03-28 06:11:22 +08:00
|
|
|
zend_closure *closure = (zend_closure *)object;
|
2008-08-11 16:49:00 +08:00
|
|
|
zend_function *invoke = (zend_function*)emalloc(sizeof(zend_function));
|
2015-05-29 17:11:02 +08:00
|
|
|
const uint32_t keep_flags =
|
|
|
|
ZEND_ACC_RETURN_REFERENCE | ZEND_ACC_VARIADIC | ZEND_ACC_HAS_RETURN_TYPE;
|
2008-08-11 16:49:00 +08:00
|
|
|
|
|
|
|
invoke->common = closure->func.common;
|
2015-04-23 22:52:05 +08:00
|
|
|
/* We return ZEND_INTERNAL_FUNCTION, but arg_info representation is the
|
|
|
|
* same as for ZEND_USER_FUNCTION (uses zend_string* instead of char*).
|
|
|
|
* This is not a problem, because ZEND_ACC_HAS_TYPE_HINTS is never set,
|
2015-06-16 16:24:35 +08:00
|
|
|
* and we won't check arguments on internal function. We also set
|
|
|
|
* ZEND_ACC_USER_ARG_INFO flag to prevent invalid usage by Reflection */
|
2008-08-11 16:49:00 +08:00
|
|
|
invoke->type = ZEND_INTERNAL_FUNCTION;
|
2015-10-07 04:59:36 +08:00
|
|
|
invoke->internal_function.fn_flags =
|
|
|
|
ZEND_ACC_PUBLIC | ZEND_ACC_CALL_VIA_HANDLER | (closure->func.common.fn_flags & keep_flags);
|
2015-06-16 18:29:17 +08:00
|
|
|
if (closure->func.type != ZEND_INTERNAL_FUNCTION || (closure->func.common.fn_flags & ZEND_ACC_USER_ARG_INFO)) {
|
2015-10-07 04:59:36 +08:00
|
|
|
invoke->internal_function.fn_flags |=
|
|
|
|
ZEND_ACC_USER_ARG_INFO;
|
2015-06-16 18:29:17 +08:00
|
|
|
}
|
2008-08-11 16:49:00 +08:00
|
|
|
invoke->internal_function.handler = ZEND_MN(Closure___invoke);
|
|
|
|
invoke->internal_function.module = 0;
|
|
|
|
invoke->internal_function.scope = zend_ce_closure;
|
2017-03-04 17:39:13 +08:00
|
|
|
invoke->internal_function.function_name = ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE);
|
2008-08-11 16:49:00 +08:00
|
|
|
return invoke;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
ZEND_API const zend_function *zend_get_closure_method_def(zval *obj) /* {{{ */
|
2009-01-03 20:25:59 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
|
2009-01-03 20:25:59 +08:00
|
|
|
return &closure->func;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
ZEND_API zval* zend_get_closure_this_ptr(zval *obj) /* {{{ */
|
2010-04-20 03:45:03 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
|
|
|
|
return &closure->this_ptr;
|
2010-04-20 03:45:03 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static zend_function *zend_closure_get_method(zend_object **object, zend_string *method, const zval *key) /* {{{ */
|
2008-07-14 17:49:03 +08:00
|
|
|
{
|
2016-03-31 00:31:10 +08:00
|
|
|
if (zend_string_equals_literal_ci(method, ZEND_INVOKE_FUNC_NAME)) {
|
2014-12-14 06:06:14 +08:00
|
|
|
return zend_get_closure_invoke_method(*object);
|
2008-07-14 17:49:03 +08:00
|
|
|
}
|
2016-03-31 00:31:10 +08:00
|
|
|
|
2018-05-31 16:57:22 +08:00
|
|
|
return zend_std_get_method(object, method, key);
|
2008-07-14 17:49:03 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2019-05-29 16:50:27 +08:00
|
|
|
static ZEND_COLD zval *zend_closure_read_property(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv) /* {{{ */
|
2008-07-22 15:29:31 +08:00
|
|
|
{
|
2008-07-22 15:44:41 +08:00
|
|
|
ZEND_CLOSURE_PROPERTY_ERROR();
|
2008-09-17 21:08:54 +08:00
|
|
|
return &EG(uninitialized_zval);
|
2008-07-22 15:29:31 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2019-05-29 16:50:27 +08:00
|
|
|
static ZEND_COLD zval *zend_closure_write_property(zend_object *object, zend_string *member, zval *value, void **cache_slot) /* {{{ */
|
2008-07-22 15:29:31 +08:00
|
|
|
{
|
2008-07-22 15:44:41 +08:00
|
|
|
ZEND_CLOSURE_PROPERTY_ERROR();
|
2019-01-07 19:28:51 +08:00
|
|
|
return value;
|
2008-07-22 15:29:31 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2019-05-29 16:50:27 +08:00
|
|
|
static ZEND_COLD zval *zend_closure_get_property_ptr_ptr(zend_object *object, zend_string *member, int type, void **cache_slot) /* {{{ */
|
2008-07-22 15:29:31 +08:00
|
|
|
{
|
2008-07-22 15:44:41 +08:00
|
|
|
ZEND_CLOSURE_PROPERTY_ERROR();
|
2008-07-22 15:29:31 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2019-05-29 16:50:27 +08:00
|
|
|
static ZEND_COLD int zend_closure_has_property(zend_object *object, zend_string *member, int has_set_exists, void **cache_slot) /* {{{ */
|
2008-07-22 15:29:31 +08:00
|
|
|
{
|
2018-07-24 12:51:36 +08:00
|
|
|
if (has_set_exists != ZEND_PROPERTY_EXISTS) {
|
2009-11-12 02:59:37 +08:00
|
|
|
ZEND_CLOSURE_PROPERTY_ERROR();
|
|
|
|
}
|
2008-07-22 15:29:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2019-05-29 16:50:27 +08:00
|
|
|
static ZEND_COLD void zend_closure_unset_property(zend_object *object, zend_string *member, void **cache_slot) /* {{{ */
|
2008-07-22 15:29:31 +08:00
|
|
|
{
|
2008-07-22 15:44:41 +08:00
|
|
|
ZEND_CLOSURE_PROPERTY_ERROR();
|
2008-07-22 15:29:31 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static void zend_closure_free_storage(zend_object *object) /* {{{ */
|
2008-07-14 17:49:03 +08:00
|
|
|
{
|
|
|
|
zend_closure *closure = (zend_closure *)object;
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
zend_object_std_dtor(&closure->std);
|
2008-07-14 17:49:03 +08:00
|
|
|
|
|
|
|
if (closure->func.type == ZEND_USER_FUNCTION) {
|
2014-12-14 06:06:14 +08:00
|
|
|
destroy_op_array(&closure->func.op_array);
|
2008-07-14 17:49:03 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
|
2010-04-20 03:45:03 +08:00
|
|
|
zval_ptr_dtor(&closure->this_ptr);
|
|
|
|
}
|
2008-07-14 17:49:03 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static zend_object *zend_closure_new(zend_class_entry *class_type) /* {{{ */
|
2008-07-14 17:49:03 +08:00
|
|
|
{
|
|
|
|
zend_closure *closure;
|
|
|
|
|
|
|
|
closure = emalloc(sizeof(zend_closure));
|
|
|
|
memset(closure, 0, sizeof(zend_closure));
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
zend_object_std_init(&closure->std, class_type);
|
2014-02-10 14:04:30 +08:00
|
|
|
closure->std.handlers = &closure_handlers;
|
2008-07-22 15:44:41 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
return (zend_object*)closure;
|
2008-07-14 17:49:03 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2019-01-31 23:47:58 +08:00
|
|
|
static zend_object *zend_closure_clone(zend_object *zobject) /* {{{ */
|
2010-04-20 03:45:03 +08:00
|
|
|
{
|
2019-01-31 23:47:58 +08:00
|
|
|
zend_closure *closure = (zend_closure *)zobject;
|
2010-04-20 03:45:03 +08:00
|
|
|
zval result;
|
|
|
|
|
2015-10-07 04:48:15 +08:00
|
|
|
zend_create_closure(&result, &closure->func,
|
|
|
|
closure->func.common.scope, closure->called_scope, &closure->this_ptr);
|
2014-02-10 14:04:30 +08:00
|
|
|
return Z_OBJ(result);
|
2010-04-20 03:45:03 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2019-01-31 23:47:58 +08:00
|
|
|
int zend_closure_get_closure(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr) /* {{{ */
|
2008-08-15 05:36:56 +08:00
|
|
|
{
|
2019-01-31 23:47:58 +08:00
|
|
|
zend_closure *closure = (zend_closure *)obj;
|
2008-08-15 05:36:56 +08:00
|
|
|
*fptr_ptr = &closure->func;
|
2015-05-06 03:06:34 +08:00
|
|
|
*ce_ptr = closure->called_scope;
|
2008-08-15 05:36:56 +08:00
|
|
|
|
2016-04-16 21:15:44 +08:00
|
|
|
if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
|
|
|
|
*obj_ptr = Z_OBJ(closure->this_ptr);
|
|
|
|
} else {
|
|
|
|
*obj_ptr = NULL;
|
2008-08-15 05:36:56 +08:00
|
|
|
}
|
2016-04-16 21:15:44 +08:00
|
|
|
|
2008-08-15 05:36:56 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2019-01-31 23:47:58 +08:00
|
|
|
static HashTable *zend_closure_get_debug_info(zend_object *object, int *is_temp) /* {{{ */
|
2009-01-02 00:22:44 +08:00
|
|
|
{
|
2019-01-31 23:47:58 +08:00
|
|
|
zend_closure *closure = (zend_closure *)object;
|
2014-02-10 14:04:30 +08:00
|
|
|
zval val;
|
2009-01-02 00:22:44 +08:00
|
|
|
struct _zend_arg_info *arg_info = closure->func.common.arg_info;
|
2015-04-16 16:01:55 +08:00
|
|
|
HashTable *debug_info;
|
2017-09-30 04:38:08 +08:00
|
|
|
zend_bool zstr_args = (closure->func.type == ZEND_USER_FUNCTION) || (closure->func.common.fn_flags & ZEND_ACC_USER_ARG_INFO);
|
2015-04-16 16:01:55 +08:00
|
|
|
|
|
|
|
*is_temp = 1;
|
2009-01-02 00:22:44 +08:00
|
|
|
|
2017-09-20 07:25:56 +08:00
|
|
|
debug_info = zend_new_array(8);
|
2009-08-10 23:18:13 +08:00
|
|
|
|
2015-04-16 16:01:55 +08:00
|
|
|
if (closure->func.type == ZEND_USER_FUNCTION && closure->func.op_array.static_variables) {
|
2018-10-17 20:52:50 +08:00
|
|
|
HashTable *static_variables =
|
|
|
|
ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr);
|
2015-04-16 16:01:55 +08:00
|
|
|
ZVAL_ARR(&val, zend_array_dup(static_variables));
|
2017-03-04 17:39:13 +08:00
|
|
|
zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_STATIC), &val);
|
2009-01-02 00:22:44 +08:00
|
|
|
}
|
|
|
|
|
2015-04-16 16:01:55 +08:00
|
|
|
if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
|
|
|
|
Z_ADDREF(closure->this_ptr);
|
2017-03-04 17:39:13 +08:00
|
|
|
zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_THIS), &closure->this_ptr);
|
2015-04-16 16:01:55 +08:00
|
|
|
}
|
2010-04-20 03:45:03 +08:00
|
|
|
|
2015-04-16 16:01:55 +08:00
|
|
|
if (arg_info &&
|
|
|
|
(closure->func.common.num_args ||
|
|
|
|
(closure->func.common.fn_flags & ZEND_ACC_VARIADIC))) {
|
|
|
|
uint32_t i, num_args, required = closure->func.common.required_num_args;
|
2009-08-10 23:18:13 +08:00
|
|
|
|
2015-04-16 16:01:55 +08:00
|
|
|
array_init(&val);
|
2009-08-10 23:18:13 +08:00
|
|
|
|
2015-04-16 16:01:55 +08:00
|
|
|
num_args = closure->func.common.num_args;
|
|
|
|
if (closure->func.common.fn_flags & ZEND_ACC_VARIADIC) {
|
|
|
|
num_args++;
|
|
|
|
}
|
|
|
|
for (i = 0; i < num_args; i++) {
|
|
|
|
zend_string *name;
|
|
|
|
zval info;
|
|
|
|
if (arg_info->name) {
|
2017-09-30 04:38:08 +08:00
|
|
|
if (zstr_args) {
|
|
|
|
name = zend_strpprintf(0, "%s$%s",
|
|
|
|
arg_info->pass_by_reference ? "&" : "",
|
|
|
|
ZSTR_VAL(arg_info->name));
|
|
|
|
} else {
|
|
|
|
name = zend_strpprintf(0, "%s$%s",
|
|
|
|
arg_info->pass_by_reference ? "&" : "",
|
|
|
|
((zend_internal_arg_info*)arg_info)->name);
|
|
|
|
}
|
2015-04-16 16:01:55 +08:00
|
|
|
} else {
|
|
|
|
name = zend_strpprintf(0, "%s$param%d",
|
|
|
|
arg_info->pass_by_reference ? "&" : "",
|
|
|
|
i + 1);
|
2009-01-02 00:22:44 +08:00
|
|
|
}
|
2015-04-16 16:01:55 +08:00
|
|
|
ZVAL_NEW_STR(&info, zend_strpprintf(0, "%s", i >= required ? "<optional>" : "<required>"));
|
|
|
|
zend_hash_update(Z_ARRVAL(val), name, &info);
|
2018-05-28 21:27:12 +08:00
|
|
|
zend_string_release_ex(name, 0);
|
2015-04-16 16:01:55 +08:00
|
|
|
arg_info++;
|
2009-01-02 00:22:44 +08:00
|
|
|
}
|
2015-04-16 16:01:55 +08:00
|
|
|
zend_hash_str_update(debug_info, "parameter", sizeof("parameter")-1, &val);
|
2009-01-02 00:22:44 +08:00
|
|
|
}
|
2009-08-10 23:18:13 +08:00
|
|
|
|
2015-04-16 16:01:55 +08:00
|
|
|
return debug_info;
|
2009-01-02 00:22:44 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2019-01-31 23:47:58 +08:00
|
|
|
static HashTable *zend_closure_get_gc(zend_object *obj, zval **table, int *n) /* {{{ */
|
2011-11-02 14:31:33 +08:00
|
|
|
{
|
2019-01-31 23:47:58 +08:00
|
|
|
zend_closure *closure = (zend_closure *)obj;
|
2011-11-02 14:31:33 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
*table = Z_TYPE(closure->this_ptr) != IS_NULL ? &closure->this_ptr : NULL;
|
|
|
|
*n = Z_TYPE(closure->this_ptr) != IS_NULL ? 1 : 0;
|
2011-11-02 14:31:33 +08:00
|
|
|
return (closure->func.type == ZEND_USER_FUNCTION) ?
|
2018-10-17 20:52:50 +08:00
|
|
|
ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr) : NULL;
|
2011-11-02 14:31:33 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2018-02-28 00:40:03 +08:00
|
|
|
/* {{{ proto Closure::__construct()
|
2009-01-04 03:29:55 +08:00
|
|
|
Private constructor preventing instantiation */
|
2015-08-19 19:40:56 +08:00
|
|
|
ZEND_COLD ZEND_METHOD(Closure, __construct)
|
2009-01-04 03:29:55 +08:00
|
|
|
{
|
2015-07-08 01:10:22 +08:00
|
|
|
zend_throw_error(NULL, "Instantiation of 'Closure' is not allowed");
|
2009-01-04 03:29:55 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2011-09-07 14:46:27 +08:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_closure_bindto, 0, 0, 1)
|
2010-04-20 03:45:03 +08:00
|
|
|
ZEND_ARG_INFO(0, newthis)
|
2011-09-07 14:46:27 +08:00
|
|
|
ZEND_ARG_INFO(0, newscope)
|
2010-04-20 03:45:03 +08:00
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2011-09-07 14:46:27 +08:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_closure_bind, 0, 0, 2)
|
2010-04-20 03:45:03 +08:00
|
|
|
ZEND_ARG_INFO(0, closure)
|
2011-09-07 14:46:27 +08:00
|
|
|
ZEND_ARG_INFO(0, newthis)
|
|
|
|
ZEND_ARG_INFO(0, newscope)
|
2010-04-20 03:45:03 +08:00
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2014-07-30 08:26:53 +08:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_closure_call, 0, 0, 1)
|
2014-07-29 09:36:17 +08:00
|
|
|
ZEND_ARG_INFO(0, newthis)
|
|
|
|
ZEND_ARG_VARIADIC_INFO(0, parameters)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2016-05-16 00:39:47 +08:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_closure_fromcallable, 0, 0, 1)
|
2016-06-02 04:53:01 +08:00
|
|
|
ZEND_ARG_INFO(0, callable)
|
2016-05-16 00:39:47 +08:00
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2009-01-04 03:29:55 +08:00
|
|
|
static const zend_function_entry closure_functions[] = {
|
|
|
|
ZEND_ME(Closure, __construct, NULL, ZEND_ACC_PRIVATE)
|
2010-04-20 03:45:03 +08:00
|
|
|
ZEND_ME(Closure, bind, arginfo_closure_bind, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
|
2011-09-07 14:46:27 +08:00
|
|
|
ZEND_MALIAS(Closure, bindTo, bind, arginfo_closure_bindto, ZEND_ACC_PUBLIC)
|
2014-07-30 08:26:53 +08:00
|
|
|
ZEND_ME(Closure, call, arginfo_closure_call, ZEND_ACC_PUBLIC)
|
2016-05-16 00:39:47 +08:00
|
|
|
ZEND_ME(Closure, fromCallable, arginfo_closure_fromcallable, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
|
2014-12-13 02:57:34 +08:00
|
|
|
ZEND_FE_END
|
2009-01-04 03:29:55 +08:00
|
|
|
};
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
void zend_register_closure_ce(void) /* {{{ */
|
2008-07-14 17:49:03 +08:00
|
|
|
{
|
|
|
|
zend_class_entry ce;
|
|
|
|
|
2009-01-04 03:29:55 +08:00
|
|
|
INIT_CLASS_ENTRY(ce, "Closure", closure_functions);
|
2014-12-14 06:06:14 +08:00
|
|
|
zend_ce_closure = zend_register_internal_class(&ce);
|
2014-11-22 12:57:55 +08:00
|
|
|
zend_ce_closure->ce_flags |= ZEND_ACC_FINAL;
|
2008-07-14 17:49:03 +08:00
|
|
|
zend_ce_closure->create_object = zend_closure_new;
|
2008-12-22 22:11:49 +08:00
|
|
|
zend_ce_closure->serialize = zend_class_serialize_deny;
|
|
|
|
zend_ce_closure->unserialize = zend_class_unserialize_deny;
|
2008-07-14 17:49:03 +08:00
|
|
|
|
2018-05-31 16:57:22 +08:00
|
|
|
memcpy(&closure_handlers, &std_object_handlers, sizeof(zend_object_handlers));
|
2014-02-10 14:04:30 +08:00
|
|
|
closure_handlers.free_obj = zend_closure_free_storage;
|
2008-07-14 17:49:03 +08:00
|
|
|
closure_handlers.get_constructor = zend_closure_get_constructor;
|
|
|
|
closure_handlers.get_method = zend_closure_get_method;
|
2008-07-22 15:29:31 +08:00
|
|
|
closure_handlers.write_property = zend_closure_write_property;
|
|
|
|
closure_handlers.read_property = zend_closure_read_property;
|
|
|
|
closure_handlers.get_property_ptr_ptr = zend_closure_get_property_ptr_ptr;
|
|
|
|
closure_handlers.has_property = zend_closure_has_property;
|
|
|
|
closure_handlers.unset_property = zend_closure_unset_property;
|
2008-07-14 17:49:03 +08:00
|
|
|
closure_handlers.compare_objects = zend_closure_compare_objects;
|
2010-04-20 03:45:03 +08:00
|
|
|
closure_handlers.clone_obj = zend_closure_clone;
|
2009-01-02 00:22:44 +08:00
|
|
|
closure_handlers.get_debug_info = zend_closure_get_debug_info;
|
2008-08-15 05:36:56 +08:00
|
|
|
closure_handlers.get_closure = zend_closure_get_closure;
|
2011-11-02 14:31:33 +08:00
|
|
|
closure_handlers.get_gc = zend_closure_get_gc;
|
2008-07-14 17:49:03 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2017-06-08 21:52:39 +08:00
|
|
|
static ZEND_NAMED_FUNCTION(zend_closure_internal_handler) /* {{{ */
|
2015-10-09 05:45:02 +08:00
|
|
|
{
|
2018-01-12 03:15:45 +08:00
|
|
|
zend_closure *closure = (zend_closure*)ZEND_CLOSURE_OBJECT(EX(func));
|
2015-10-09 05:45:02 +08:00
|
|
|
closure->orig_internal_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU);
|
|
|
|
OBJ_RELEASE((zend_object*)closure);
|
2015-10-09 06:09:22 +08:00
|
|
|
EX(func) = NULL;
|
2015-10-09 05:45:02 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2015-05-06 03:06:34 +08:00
|
|
|
ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr) /* {{{ */
|
2008-07-14 17:49:03 +08:00
|
|
|
{
|
|
|
|
zend_closure *closure;
|
|
|
|
|
|
|
|
object_init_ex(res, zend_ce_closure);
|
|
|
|
|
2015-10-07 04:59:36 +08:00
|
|
|
closure = (zend_closure *)Z_OBJ_P(res);
|
2008-07-14 17:49:03 +08:00
|
|
|
|
2015-10-07 04:48:15 +08:00
|
|
|
if ((scope == NULL) && this_ptr && (Z_TYPE_P(this_ptr) != IS_UNDEF)) {
|
|
|
|
/* use dummy scope if we're binding an object without specifying a scope */
|
|
|
|
/* maybe it would be better to create one for this purpose */
|
|
|
|
scope = zend_ce_closure;
|
|
|
|
}
|
|
|
|
|
2015-10-09 05:45:02 +08:00
|
|
|
if (func->type == ZEND_USER_FUNCTION) {
|
|
|
|
memcpy(&closure->func, func, sizeof(zend_op_array));
|
|
|
|
closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;
|
2018-10-17 20:52:50 +08:00
|
|
|
closure->func.common.fn_flags &= ~ZEND_ACC_IMMUTABLE;
|
|
|
|
|
2008-07-14 17:49:03 +08:00
|
|
|
if (closure->func.op_array.static_variables) {
|
2015-12-29 18:16:08 +08:00
|
|
|
closure->func.op_array.static_variables =
|
|
|
|
zend_array_dup(closure->func.op_array.static_variables);
|
2008-07-14 17:49:03 +08:00
|
|
|
}
|
2018-10-17 20:52:50 +08:00
|
|
|
ZEND_MAP_PTR_INIT(closure->func.op_array.static_variables_ptr,
|
|
|
|
&closure->func.op_array.static_variables);
|
2018-01-15 19:13:55 +08:00
|
|
|
|
|
|
|
/* Runtime cache is scope-dependent, so we cannot reuse it if the scope changed */
|
2018-10-17 20:52:50 +08:00
|
|
|
if (!ZEND_MAP_PTR_GET(closure->func.op_array.run_time_cache)
|
2018-01-15 19:13:55 +08:00
|
|
|
|| func->common.scope != scope
|
2018-10-02 14:10:04 +08:00
|
|
|
|| (func->common.fn_flags & ZEND_ACC_HEAP_RT_CACHE)
|
2018-01-15 19:13:55 +08:00
|
|
|
) {
|
2018-10-17 20:52:50 +08:00
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
if (!ZEND_MAP_PTR_GET(func->op_array.run_time_cache)
|
|
|
|
&& (func->common.fn_flags & ZEND_ACC_CLOSURE)
|
|
|
|
&& (func->common.scope == scope ||
|
|
|
|
!(func->common.fn_flags & ZEND_ACC_IMMUTABLE))) {
|
2018-01-15 19:13:55 +08:00
|
|
|
/* If a real closure is used for the first time, we create a shared runtime cache
|
|
|
|
* and remember which scope it is for. */
|
2018-10-17 20:52:50 +08:00
|
|
|
if (func->common.scope != scope) {
|
|
|
|
func->common.scope = scope;
|
|
|
|
}
|
|
|
|
closure->func.op_array.fn_flags &= ~ZEND_ACC_HEAP_RT_CACHE;
|
|
|
|
ptr = zend_arena_alloc(&CG(arena), func->op_array.cache_size);
|
|
|
|
ZEND_MAP_PTR_SET(func->op_array.run_time_cache, ptr);
|
|
|
|
ZEND_MAP_PTR_SET(closure->func.op_array.run_time_cache, ptr);
|
2018-01-15 19:13:55 +08:00
|
|
|
} else {
|
|
|
|
/* Otherwise, we use a non-shared runtime cache */
|
2018-10-02 14:10:04 +08:00
|
|
|
closure->func.op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE;
|
2018-10-17 20:52:50 +08:00
|
|
|
ptr = emalloc(sizeof(void*) + func->op_array.cache_size);
|
|
|
|
ZEND_MAP_PTR_INIT(closure->func.op_array.run_time_cache, ptr);
|
|
|
|
ptr = (char*)ptr + sizeof(void*);
|
|
|
|
ZEND_MAP_PTR_SET(closure->func.op_array.run_time_cache, ptr);
|
2018-01-15 19:13:55 +08:00
|
|
|
}
|
2018-10-17 20:52:50 +08:00
|
|
|
memset(ptr, 0, func->op_array.cache_size);
|
2015-06-21 22:39:28 +08:00
|
|
|
}
|
2015-02-20 19:59:30 +08:00
|
|
|
if (closure->func.op_array.refcount) {
|
|
|
|
(*closure->func.op_array.refcount)++;
|
|
|
|
}
|
2015-10-07 04:59:36 +08:00
|
|
|
} else {
|
2015-10-09 05:45:02 +08:00
|
|
|
memcpy(&closure->func, func, sizeof(zend_internal_function));
|
|
|
|
closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;
|
|
|
|
/* wrap internal function handler to avoid memory leak */
|
2015-10-09 07:37:51 +08:00
|
|
|
if (UNEXPECTED(closure->func.internal_function.handler == zend_closure_internal_handler)) {
|
|
|
|
/* avoid infinity recursion, by taking handler from nested closure */
|
|
|
|
zend_closure *nested = (zend_closure*)((char*)func - XtOffsetOf(zend_closure, func));
|
|
|
|
ZEND_ASSERT(nested->std.ce == zend_ce_closure);
|
|
|
|
closure->orig_internal_handler = nested->orig_internal_handler;
|
|
|
|
} else {
|
|
|
|
closure->orig_internal_handler = closure->func.internal_function.handler;
|
|
|
|
}
|
2015-10-09 05:45:02 +08:00
|
|
|
closure->func.internal_function.handler = zend_closure_internal_handler;
|
2015-10-10 19:39:26 +08:00
|
|
|
if (!func->common.scope) {
|
2015-10-07 04:48:15 +08:00
|
|
|
/* if it's a free function, we won't set scope & this since they're meaningless */
|
|
|
|
this_ptr = NULL;
|
|
|
|
scope = NULL;
|
|
|
|
}
|
|
|
|
}
|
2008-07-14 17:49:03 +08:00
|
|
|
|
2014-06-12 09:07:33 +08:00
|
|
|
ZVAL_UNDEF(&closure->this_ptr);
|
2015-05-06 23:29:05 +08:00
|
|
|
/* Invariant:
|
|
|
|
* If the closure is unscoped or static, it has no bound object. */
|
2010-04-20 03:45:03 +08:00
|
|
|
closure->func.common.scope = scope;
|
2015-05-06 03:06:34 +08:00
|
|
|
closure->called_scope = called_scope;
|
2010-04-20 03:45:03 +08:00
|
|
|
if (scope) {
|
|
|
|
closure->func.common.fn_flags |= ZEND_ACC_PUBLIC;
|
2014-02-28 20:06:38 +08:00
|
|
|
if (this_ptr && Z_TYPE_P(this_ptr) == IS_OBJECT && (closure->func.common.fn_flags & ZEND_ACC_STATIC) == 0) {
|
2019-05-29 01:10:02 +08:00
|
|
|
Z_ADDREF_P(this_ptr);
|
|
|
|
ZVAL_OBJ(&closure->this_ptr, Z_OBJ_P(this_ptr));
|
2010-04-20 03:45:03 +08:00
|
|
|
}
|
|
|
|
}
|
2008-07-14 17:49:03 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2015-10-10 00:41:15 +08:00
|
|
|
ZEND_API void zend_create_fake_closure(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr) /* {{{ */
|
|
|
|
{
|
|
|
|
zend_closure *closure;
|
|
|
|
|
|
|
|
zend_create_closure(res, func, scope, called_scope, this_ptr);
|
|
|
|
|
|
|
|
closure = (zend_closure *)Z_OBJ_P(res);
|
|
|
|
closure->func.common.fn_flags |= ZEND_ACC_FAKE_CLOSURE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2015-12-29 18:16:08 +08:00
|
|
|
void zend_closure_bind_var(zval *closure_zv, zend_string *var_name, zval *var) /* {{{ */
|
|
|
|
{
|
|
|
|
zend_closure *closure = (zend_closure *) Z_OBJ_P(closure_zv);
|
2018-10-17 20:52:50 +08:00
|
|
|
HashTable *static_variables = ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr);
|
2015-12-29 18:16:08 +08:00
|
|
|
zend_hash_update(static_variables, var_name, var);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2018-08-20 21:10:09 +08:00
|
|
|
void zend_closure_bind_var_ex(zval *closure_zv, uint32_t offset, zval *val) /* {{{ */
|
|
|
|
{
|
|
|
|
zend_closure *closure = (zend_closure *) Z_OBJ_P(closure_zv);
|
2018-10-17 20:52:50 +08:00
|
|
|
HashTable *static_variables = ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr);
|
2018-08-20 21:10:09 +08:00
|
|
|
zval *var = (zval*)((char*)static_variables->arData + offset);
|
|
|
|
zval_ptr_dtor(var);
|
|
|
|
ZVAL_COPY_VALUE(var, val);
|
|
|
|
}
|
|
|
|
/* }}} */
|