summaryrefslogtreecommitdiff
path: root/Zend/zend_closures.c
diff options
context:
space:
mode:
Diffstat (limited to 'Zend/zend_closures.c')
-rw-r--r--Zend/zend_closures.c241
1 files changed, 172 insertions, 69 deletions
diff --git a/Zend/zend_closures.c b/Zend/zend_closures.c
index dbaade4c52..c7527b4ff9 100644
--- a/Zend/zend_closures.c
+++ b/Zend/zend_closures.c
@@ -37,6 +37,7 @@
typedef struct _zend_closure {
zend_object std;
zend_function func;
+ zval *this_ptr;
HashTable *debug_info;
} zend_closure;
@@ -70,11 +71,72 @@ ZEND_METHOD(Closure, __invoke) /* {{{ */
efree(arguments);
/* destruct the function also, then - we have allocated it in get_method */
- efree(func->internal_function.function_name);
+ efree((char*)func->internal_function.function_name);
efree(func);
}
/* }}} */
+/* {{{ proto Closure Closure::bind(Closure $old, object $to [, mixed $scope = "static" ] )
+ 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;
+
+ if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oo!|z", &zclosure, zend_ce_closure, &newthis, &scope_arg) == FAILURE) {
+ RETURN_NULL();
+ }
+
+ closure = (zend_closure *)zend_object_store_get_object(zclosure TSRMLS_CC);
+
+ if ((newthis != NULL) && (closure->func.common.fn_flags & ZEND_ACC_STATIC)) {
+ zend_error(E_WARNING, "Cannot bind an instance to a static closure");
+ }
+
+ if (scope_arg != NULL) { /* scope argument was given */
+ if (IS_ZEND_STD_OBJECT(*scope_arg)) {
+ ce = Z_OBJCE_P(scope_arg);
+ } else if (Z_TYPE_P(scope_arg) == IS_NULL) {
+ ce = NULL;
+ } else {
+ char *class_name;
+ int class_name_len;
+ zval tmp_zval;
+ INIT_ZVAL(tmp_zval);
+
+ if (Z_TYPE_P(scope_arg) == IS_STRING) {
+ class_name = Z_STRVAL_P(scope_arg);
+ class_name_len = Z_STRLEN_P(scope_arg);
+ } else {
+ tmp_zval = *scope_arg;
+ zval_copy_ctor(&tmp_zval);
+ convert_to_string(&tmp_zval);
+ class_name = Z_STRVAL(tmp_zval);
+ class_name_len = Z_STRLEN(tmp_zval);
+ }
+
+ if ((class_name_len == sizeof("static") - 1) &&
+ (memcmp("static", class_name, sizeof("static") - 1) == 0)) {
+ ce = closure->func.common.scope;
+ }
+ else if (zend_lookup_class_ex(class_name, class_name_len, NULL, 1, &ce_p TSRMLS_CC) == FAILURE) {
+ zend_error(E_WARNING, "Class '%s' not found", class_name);
+ zval_dtor(&tmp_zval);
+ RETURN_NULL();
+ } else {
+ ce = *ce_p;
+ }
+ zval_dtor(&tmp_zval);
+ }
+ } else { /* scope argument not given; do not change the scope by default */
+ ce = closure->func.common.scope;
+ }
+
+ zend_create_closure(return_value, &closure->func, ce, newthis TSRMLS_CC);
+}
+/* }}} */
+
static zend_function *zend_closure_get_constructor(zval *object TSRMLS_DC) /* {{{ */
{
zend_error(E_RECOVERABLE_ERROR, "Instantiation of 'Closure' is not allowed");
@@ -95,7 +157,7 @@ ZEND_API zend_function *zend_get_closure_invoke_method(zval *obj TSRMLS_DC) /* {
invoke->common = closure->func.common;
invoke->type = ZEND_INTERNAL_FUNCTION;
- invoke->internal_function.fn_flags = ZEND_ACC_PUBLIC | ZEND_ACC_CALL_VIA_HANDLER;
+ invoke->internal_function.fn_flags = ZEND_ACC_PUBLIC | ZEND_ACC_CALL_VIA_HANDLER | (closure->func.common.fn_flags & ZEND_ACC_RETURN_REFERENCE);
invoke->internal_function.handler = ZEND_MN(Closure___invoke);
invoke->internal_function.module = 0;
invoke->internal_function.scope = zend_ce_closure;
@@ -111,7 +173,14 @@ ZEND_API const zend_function *zend_get_closure_method_def(zval *obj TSRMLS_DC) /
}
/* }}} */
-static zend_function *zend_closure_get_method(zval **object_ptr, char *method_name, int method_len TSRMLS_DC) /* {{{ */
+ZEND_API zval* zend_get_closure_this_ptr(zval *obj TSRMLS_DC) /* {{{ */
+{
+ zend_closure *closure = (zend_closure *)zend_object_store_get_object(obj TSRMLS_CC);
+ return closure->this_ptr;
+}
+/* }}} */
+
+static zend_function *zend_closure_get_method(zval **object_ptr, char *method_name, int method_len, const zend_literal *key TSRMLS_DC) /* {{{ */
{
char *lc_name;
ALLOCA_FLAG(use_heap)
@@ -125,11 +194,11 @@ static zend_function *zend_closure_get_method(zval **object_ptr, char *method_na
return zend_get_closure_invoke_method(*object_ptr TSRMLS_CC);
}
free_alloca(lc_name, use_heap);
- return NULL;
+ return std_object_handlers.get_method(object_ptr, method_name, method_len, key TSRMLS_CC);
}
/* }}} */
-static zval *zend_closure_read_property(zval *object, zval *member, int type TSRMLS_DC) /* {{{ */
+static zval *zend_closure_read_property(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC) /* {{{ */
{
ZEND_CLOSURE_PROPERTY_ERROR();
Z_ADDREF(EG(uninitialized_zval));
@@ -137,20 +206,20 @@ static zval *zend_closure_read_property(zval *object, zval *member, int type TSR
}
/* }}} */
-static void zend_closure_write_property(zval *object, zval *member, zval *value TSRMLS_DC) /* {{{ */
+static void zend_closure_write_property(zval *object, zval *member, zval *value, const zend_literal *key TSRMLS_DC) /* {{{ */
{
ZEND_CLOSURE_PROPERTY_ERROR();
}
/* }}} */
-static zval **zend_closure_get_property_ptr_ptr(zval *object, zval *member TSRMLS_DC) /* {{{ */
+static zval **zend_closure_get_property_ptr_ptr(zval *object, zval *member, const zend_literal *key TSRMLS_DC) /* {{{ */
{
ZEND_CLOSURE_PROPERTY_ERROR();
return NULL;
}
/* }}} */
-static int zend_closure_has_property(zval *object, zval *member, int has_set_exists TSRMLS_DC) /* {{{ */
+static int zend_closure_has_property(zval *object, zval *member, int has_set_exists, const zend_literal *key TSRMLS_DC) /* {{{ */
{
if (has_set_exists != 2) {
ZEND_CLOSURE_PROPERTY_ERROR();
@@ -159,7 +228,7 @@ static int zend_closure_has_property(zval *object, zval *member, int has_set_exi
}
/* }}} */
-static void zend_closure_unset_property(zval *object, zval *member TSRMLS_DC) /* {{{ */
+static void zend_closure_unset_property(zval *object, zval *member, const zend_literal *key TSRMLS_DC) /* {{{ */
{
ZEND_CLOSURE_PROPERTY_ERROR();
}
@@ -187,6 +256,10 @@ static void zend_closure_free_storage(void *object TSRMLS_DC) /* {{{ */
efree(closure->debug_info);
}
+ if (closure->this_ptr) {
+ zval_ptr_dtor(&closure->this_ptr);
+ }
+
efree(closure);
}
/* }}} */
@@ -208,6 +281,17 @@ static zend_object_value zend_closure_new(zend_class_entry *class_type TSRMLS_DC
}
/* }}} */
+static zend_object_value zend_closure_clone(zval *zobject TSRMLS_DC) /* {{{ */
+{
+ zend_closure *closure = (zend_closure *)zend_object_store_get_object(zobject TSRMLS_CC);
+ zval result;
+
+ zend_create_closure(&result, &closure->func, closure->func.common.scope, closure->this_ptr TSRMLS_CC);
+ return Z_OBJVAL(result);
+}
+/* }}} */
+
+
int zend_closure_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zval **zobj_ptr TSRMLS_DC) /* {{{ */
{
zend_closure *closure;
@@ -219,10 +303,17 @@ int zend_closure_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function
closure = (zend_closure *)zend_object_store_get_object(obj TSRMLS_CC);
*fptr_ptr = &closure->func;
- if (zobj_ptr) {
- *zobj_ptr = NULL;
+ if (closure->this_ptr) {
+ if (zobj_ptr) {
+ *zobj_ptr = closure->this_ptr;
+ }
+ *ce_ptr = Z_OBJCE_P(closure->this_ptr);
+ } else {
+ if (zobj_ptr) {
+ *zobj_ptr = NULL;
+ }
+ *ce_ptr = closure->func.common.scope;
}
- *ce_ptr = NULL;
return SUCCESS;
}
/* }}} */
@@ -245,7 +336,12 @@ static HashTable *zend_closure_get_debug_info(zval *object, int *is_temp TSRMLS_
MAKE_STD_ZVAL(val);
array_init(val);
zend_hash_copy(Z_ARRVAL_P(val), static_variables, (copy_ctor_func_t)zval_add_ref, NULL, sizeof(zval*));
- zend_symtable_update(closure->debug_info, "static", sizeof("static"), (void *) &val, sizeof(zval *), NULL);
+ zend_hash_update(closure->debug_info, "static", sizeof("static"), (void *) &val, sizeof(zval *), NULL);
+ }
+
+ if (closure->this_ptr) {
+ Z_ADDREF_P(closure->this_ptr);
+ zend_symtable_update(closure->debug_info, "this", sizeof("this"), (void *) &closure->this_ptr, sizeof(zval *), NULL);
}
if (arg_info) {
@@ -272,7 +368,7 @@ static HashTable *zend_closure_get_debug_info(zval *object, int *is_temp TSRMLS_
efree(name);
arg_info++;
}
- zend_symtable_update(closure->debug_info, "parameter", sizeof("parameter"), (void *) &val, sizeof(zval *), NULL);
+ zend_hash_update(closure->debug_info, "parameter", sizeof("parameter"), (void *) &val, sizeof(zval *), NULL);
}
}
@@ -280,15 +376,14 @@ static HashTable *zend_closure_get_debug_info(zval *object, int *is_temp TSRMLS_
}
/* }}} */
-static HashTable *zend_closure_get_properties(zval *obj TSRMLS_DC) /* {{{ */
+static HashTable *zend_closure_get_gc(zval *obj, zval ***table, int *n TSRMLS_DC) /* {{{ */
{
zend_closure *closure = (zend_closure *)zend_object_store_get_object(obj TSRMLS_CC);
- if (GC_G(gc_active)) {
- return (closure->func.type == ZEND_USER_FUNCTION) ? closure->func.op_array.static_variables : NULL;
- }
-
- return closure->std.properties;
+ *table = closure->this_ptr ? &closure->this_ptr : NULL;
+ *n = closure->this_ptr ? 1 : 0;
+ return (closure->func.type == ZEND_USER_FUNCTION) ?
+ closure->func.op_array.static_variables : NULL;
}
/* }}} */
@@ -300,8 +395,21 @@ ZEND_METHOD(Closure, __construct)
}
/* }}} */
+ZEND_BEGIN_ARG_INFO_EX(arginfo_closure_bindto, 0, 0, 1)
+ ZEND_ARG_INFO(0, newthis)
+ ZEND_ARG_INFO(0, newscope)
+ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO_EX(arginfo_closure_bind, 0, 0, 2)
+ ZEND_ARG_INFO(0, closure)
+ ZEND_ARG_INFO(0, newthis)
+ ZEND_ARG_INFO(0, newscope)
+ZEND_END_ARG_INFO()
+
static const zend_function_entry closure_functions[] = {
ZEND_ME(Closure, __construct, NULL, ZEND_ACC_PRIVATE)
+ ZEND_ME(Closure, bind, arginfo_closure_bind, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
+ ZEND_MALIAS(Closure, bindTo, bind, arginfo_closure_bindto, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
@@ -325,59 +433,14 @@ void zend_register_closure_ce(TSRMLS_D) /* {{{ */
closure_handlers.has_property = zend_closure_has_property;
closure_handlers.unset_property = zend_closure_unset_property;
closure_handlers.compare_objects = zend_closure_compare_objects;
- closure_handlers.clone_obj = NULL;
+ closure_handlers.clone_obj = zend_closure_clone;
closure_handlers.get_debug_info = zend_closure_get_debug_info;
closure_handlers.get_closure = zend_closure_get_closure;
- closure_handlers.get_properties = zend_closure_get_properties;
+ closure_handlers.get_gc = zend_closure_get_gc;
}
/* }}} */
-static int zval_copy_static_var(zval **p TSRMLS_DC, int num_args, va_list args, zend_hash_key *key) /* {{{ */
-{
- HashTable *target = va_arg(args, HashTable*);
- zend_bool is_ref;
- zval *tmp;
-
- if (Z_TYPE_PP(p) & (IS_LEXICAL_VAR|IS_LEXICAL_REF)) {
- is_ref = Z_TYPE_PP(p) & IS_LEXICAL_REF;
-
- if (!EG(active_symbol_table)) {
- zend_rebuild_symbol_table(TSRMLS_C);
- }
- if (zend_hash_quick_find(EG(active_symbol_table), key->arKey, key->nKeyLength, key->h, (void **) &p) == FAILURE) {
- if (is_ref) {
- ALLOC_INIT_ZVAL(tmp);
- Z_SET_ISREF_P(tmp);
- zend_hash_quick_add(EG(active_symbol_table), key->arKey, key->nKeyLength, key->h, &tmp, sizeof(zval*), (void**)&p);
- } else {
- tmp = EG(uninitialized_zval_ptr);
- zend_error(E_NOTICE,"Undefined variable: %s", key->arKey);
- }
- } else {
- if (is_ref) {
- SEPARATE_ZVAL_TO_MAKE_IS_REF(p);
- tmp = *p;
- } else if (Z_ISREF_PP(p)) {
- ALLOC_INIT_ZVAL(tmp);
- *tmp = **p;
- zval_copy_ctor(tmp);
- Z_SET_REFCOUNT_P(tmp, 0);
- Z_UNSET_ISREF_P(tmp);
- } else {
- tmp = *p;
- }
- }
- } else {
- tmp = *p;
- }
- if (zend_hash_quick_add(target, key->arKey, key->nKeyLength, key->h, &tmp, sizeof(zval*), NULL) == SUCCESS) {
- Z_ADDREF_P(tmp);
- }
- return ZEND_HASH_APPLY_KEEP;
-}
-/* }}} */
-
-ZEND_API void zend_create_closure(zval *res, zend_function *func TSRMLS_DC) /* {{{ */
+ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_entry *scope, zval *this_ptr TSRMLS_DC) /* {{{ */
{
zend_closure *closure;
@@ -388,6 +451,12 @@ ZEND_API void zend_create_closure(zval *res, zend_function *func TSRMLS_DC) /* {
closure->func = *func;
closure->func.common.prototype = NULL;
+ if ((scope == NULL) && (this_ptr != NULL)) {
+ /* 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;
+ }
+
if (closure->func.type == ZEND_USER_FUNCTION) {
if (closure->func.op_array.static_variables) {
HashTable *static_variables = closure->func.op_array.static_variables;
@@ -396,10 +465,44 @@ ZEND_API void zend_create_closure(zval *res, zend_function *func TSRMLS_DC) /* {
zend_hash_init(closure->func.op_array.static_variables, zend_hash_num_elements(static_variables), NULL, ZVAL_PTR_DTOR, 0);
zend_hash_apply_with_arguments(static_variables TSRMLS_CC, (apply_func_args_t)zval_copy_static_var, 1, closure->func.op_array.static_variables);
}
+ closure->func.op_array.run_time_cache = NULL;
(*closure->func.op_array.refcount)++;
+ } else {
+ /* verify that we aren't binding internal function to a wrong scope */
+ if(func->common.scope != NULL) {
+ if(scope && !instanceof_function(scope, func->common.scope TSRMLS_CC)) {
+ zend_error(E_WARNING, "Cannot bind function %s::%s to scope class %s", func->common.scope->name, func->common.function_name, scope->name);
+ scope = NULL;
+ }
+ if(scope && this_ptr && (func->common.fn_flags & ZEND_ACC_STATIC) == 0 &&
+ !instanceof_function(Z_OBJCE_P(this_ptr), closure->func.common.scope TSRMLS_CC)) {
+ zend_error(E_WARNING, "Cannot bind function %s::%s to object of class %s", func->common.scope->name, func->common.function_name, Z_OBJCE_P(this_ptr)->name);
+ scope = NULL;
+ this_ptr = NULL;
+ }
+ } else {
+ /* if it's a free function, we won't set scope & this since they're meaningless */
+ this_ptr = NULL;
+ scope = NULL;
+ }
}
- closure->func.common.scope = NULL;
+ /* Invariants:
+ * If the closure is unscoped, it has no bound object.
+ * The the closure is scoped, it's either static or it's bound */
+ 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);
+ } else {
+ closure->func.common.fn_flags |= ZEND_ACC_STATIC;
+ closure->this_ptr = NULL;
+ }
+ } else {
+ closure->this_ptr = NULL;
+ }
}
/* }}} */