diff options
author | Dmitry Stogov <dmitry@zend.com> | 2014-02-10 10:04:30 +0400 |
---|---|---|
committer | Dmitry Stogov <dmitry@zend.com> | 2014-02-10 10:04:30 +0400 |
commit | f4cfaf36e23ca47da3e352e1c60909104c059647 (patch) | |
tree | 0db3e2a323b12c5bbf1a958c857f92eb58c240d1 /Zend/zend_closures.c | |
parent | 89a9acea1f9d821a9805b3857bf4febbba08690d (diff) | |
download | php-git-f4cfaf36e23ca47da3e352e1c60909104c059647.tar.gz |
Use better data structures (incomplete)
Diffstat (limited to 'Zend/zend_closures.c')
-rw-r--r-- | Zend/zend_closures.c | 157 |
1 files changed, 70 insertions, 87 deletions
diff --git a/Zend/zend_closures.c b/Zend/zend_closures.c index d714b35b39..d0c0ebb4b3 100644 --- a/Zend/zend_closures.c +++ b/Zend/zend_closures.c @@ -37,7 +37,7 @@ typedef struct _zend_closure { zend_object std; zend_function func; - zval *this_ptr; + zval this_ptr; HashTable *debug_info; } zend_closure; @@ -48,24 +48,20 @@ static zend_object_handlers closure_handlers; ZEND_METHOD(Closure, __invoke) /* {{{ */ { zend_function *func = EG(current_execute_data)->function_state.function; - zval ***arguments; - zval *closure_result_ptr = NULL; + zval *arguments; - arguments = emalloc(sizeof(zval**) * ZEND_NUM_ARGS()); + arguments = emalloc(sizeof(zval) * ZEND_NUM_ARGS()); if (zend_get_parameters_array_ex(ZEND_NUM_ARGS(), arguments) == FAILURE) { efree(arguments); zend_error(E_RECOVERABLE_ERROR, "Cannot get arguments for calling closure"); RETVAL_FALSE; - } else if (call_user_function_ex(CG(function_table), NULL, this_ptr, &closure_result_ptr, ZEND_NUM_ARGS(), arguments, 1, NULL TSRMLS_CC) == FAILURE) { + } else if (call_user_function_ex(CG(function_table), NULL, this_ptr, return_value, ZEND_NUM_ARGS(), arguments, 1, NULL TSRMLS_CC) == FAILURE) { RETVAL_FALSE; - } else if (closure_result_ptr) { - zval_ptr_dtor(&return_value); - *return_value_ptr = closure_result_ptr; } efree(arguments); /* destruct the function also, then - we have allocated it in get_method */ - efree((char*)func->internal_function.function_name); + STR_RELEASE(func->internal_function.function_name); efree(func); } /* }}} */ @@ -76,13 +72,13 @@ ZEND_METHOD(Closure, bind) { zval *newthis, *zclosure, *scope_arg = NULL; zend_closure *closure; - zend_class_entry *ce, **ce_p; + zend_class_entry *ce; 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); + closure = (zend_closure *)Z_OBJ_P(zclosure); if ((newthis != NULL) && (closure->func.common.fn_flags & ZEND_ACC_STATIC)) { zend_error(E_WARNING, "Cannot bind an instance to a static closure"); @@ -94,32 +90,25 @@ ZEND_METHOD(Closure, bind) } else if (Z_TYPE_P(scope_arg) == IS_NULL) { ce = NULL; } else { - char *class_name; - int class_name_len; + zend_string *class_name; 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); + class_name = Z_STR_P(scope_arg); } else { - tmp_zval = *scope_arg; - zval_copy_ctor(&tmp_zval); + ZVAL_DUP(&tmp_zval, scope_arg); convert_to_string(&tmp_zval); - class_name = Z_STRVAL(tmp_zval); - class_name_len = Z_STRLEN(tmp_zval); + class_name = Z_STR(tmp_zval); } - if ((class_name_len == sizeof("static") - 1) && - (memcmp("static", class_name, sizeof("static") - 1) == 0)) { + if ((class_name->len == sizeof("static") - 1) && + (memcmp("static", class_name->val, 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); + else if ((ce = zend_lookup_class_ex(class_name, NULL, 1 TSRMLS_CC)) == NULL) { + zend_error(E_WARNING, "Class '%s' not found", class_name->val); zval_dtor(&tmp_zval); RETURN_NULL(); - } else { - ce = *ce_p; } zval_dtor(&tmp_zval); } @@ -140,13 +129,13 @@ static zend_function *zend_closure_get_constructor(zval *object TSRMLS_DC) /* {{ static int zend_closure_compare_objects(zval *o1, zval *o2 TSRMLS_DC) /* {{{ */ { - return (Z_OBJ_HANDLE_P(o1) != Z_OBJ_HANDLE_P(o2)); + return (Z_OBJ_P(o1) != Z_OBJ_P(o2)); } /* }}} */ ZEND_API zend_function *zend_get_closure_invoke_method(zval *obj TSRMLS_DC) /* {{{ */ { - zend_closure *closure = (zend_closure *)zend_object_store_get_object(obj TSRMLS_CC); + zend_closure *closure = (zend_closure *)Z_OBJ_P(obj); zend_function *invoke = (zend_function*)emalloc(sizeof(zend_function)); invoke->common = closure->func.common; @@ -155,47 +144,45 @@ ZEND_API zend_function *zend_get_closure_invoke_method(zval *obj TSRMLS_DC) /* { invoke->internal_function.handler = ZEND_MN(Closure___invoke); invoke->internal_function.module = 0; invoke->internal_function.scope = zend_ce_closure; - invoke->internal_function.function_name = estrndup(ZEND_INVOKE_FUNC_NAME, sizeof(ZEND_INVOKE_FUNC_NAME)-1); + invoke->internal_function.function_name = STR_INIT(ZEND_INVOKE_FUNC_NAME, sizeof(ZEND_INVOKE_FUNC_NAME)-1, 0); return invoke; } /* }}} */ ZEND_API const zend_function *zend_get_closure_method_def(zval *obj TSRMLS_DC) /* {{{ */ { - zend_closure *closure = (zend_closure *)zend_object_store_get_object(obj TSRMLS_CC); + zend_closure *closure = (zend_closure *)Z_OBJ_P(obj); return &closure->func; } /* }}} */ 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; + zend_closure *closure = (zend_closure *)Z_OBJ_P(obj); + 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) /* {{{ */ +static zend_function *zend_closure_get_method(zval *object_ptr, zend_string *method, const zend_literal *key TSRMLS_DC) /* {{{ */ { - char *lc_name; - ALLOCA_FLAG(use_heap) + zend_string *lc_name; - lc_name = do_alloca(method_len + 1, use_heap); - zend_str_tolower_copy(lc_name, method_name, method_len); - if ((method_len == sizeof(ZEND_INVOKE_FUNC_NAME)-1) && + lc_name = STR_ALLOC(method->len, 0); + zend_str_tolower_copy(lc_name->val, method->val, method->len); + if ((method->len == sizeof(ZEND_INVOKE_FUNC_NAME)-1) && memcmp(lc_name, ZEND_INVOKE_FUNC_NAME, sizeof(ZEND_INVOKE_FUNC_NAME)-1) == 0 ) { - free_alloca(lc_name, use_heap); - return zend_get_closure_invoke_method(*object_ptr TSRMLS_CC); + STR_FREE(lc_name); + return zend_get_closure_invoke_method(object_ptr TSRMLS_CC); } - free_alloca(lc_name, use_heap); - return std_object_handlers.get_method(object_ptr, method_name, method_len, key TSRMLS_CC); + STR_FREE(lc_name); + return std_object_handlers.get_method(object_ptr, method, key TSRMLS_CC); } /* }}} */ 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)); return &EG(uninitialized_zval); } /* }}} */ @@ -206,7 +193,7 @@ static void zend_closure_write_property(zval *object, zval *member, zval *value, } /* }}} */ -static zval **zend_closure_get_property_ptr_ptr(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC) /* {{{ */ +static zval *zend_closure_get_property_ptr_ptr(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC) /* {{{ */ { ZEND_CLOSURE_PROPERTY_ERROR(); return NULL; @@ -228,7 +215,7 @@ static void zend_closure_unset_property(zval *object, zval *member, const zend_l } /* }}} */ -static void zend_closure_free_storage(void *object TSRMLS_DC) /* {{{ */ +static void zend_closure_free_storage(zend_object *object TSRMLS_DC) /* {{{ */ { zend_closure *closure = (zend_closure *)object; @@ -250,7 +237,7 @@ static void zend_closure_free_storage(void *object TSRMLS_DC) /* {{{ */ efree(closure->debug_info); } - if (closure->this_ptr) { + if (Z_TYPE(closure->this_ptr) != IS_UNDEF) { zval_ptr_dtor(&closure->this_ptr); } @@ -258,34 +245,31 @@ static void zend_closure_free_storage(void *object TSRMLS_DC) /* {{{ */ } /* }}} */ -static zend_object_value zend_closure_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */ +static zend_object *zend_closure_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */ { zend_closure *closure; - zend_object_value object; closure = emalloc(sizeof(zend_closure)); memset(closure, 0, sizeof(zend_closure)); zend_object_std_init(&closure->std, class_type TSRMLS_CC); + closure->std.handlers = &closure_handlers; - object.handle = zend_objects_store_put(closure, (zend_objects_store_dtor_t)zend_objects_destroy_object, (zend_objects_free_object_storage_t) zend_closure_free_storage, NULL TSRMLS_CC); - object.handlers = &closure_handlers; - - return object; + return (zend_object*)closure; } /* }}} */ -static zend_object_value zend_closure_clone(zval *zobject TSRMLS_DC) /* {{{ */ +static zend_object *zend_closure_clone(zval *zobject TSRMLS_DC) /* {{{ */ { - zend_closure *closure = (zend_closure *)zend_object_store_get_object(zobject TSRMLS_CC); + zend_closure *closure = (zend_closure *)Z_OBJ_P(zobject); zval result; - zend_create_closure(&result, &closure->func, closure->func.common.scope, closure->this_ptr TSRMLS_CC); - return Z_OBJVAL(result); + zend_create_closure(&result, &closure->func, closure->func.common.scope, &closure->this_ptr TSRMLS_CC); + return Z_OBJ(result); } /* }}} */ -int zend_closure_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zval **zobj_ptr TSRMLS_DC) /* {{{ */ +int zend_closure_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zval *zobj_ptr TSRMLS_DC) /* {{{ */ { zend_closure *closure; @@ -293,17 +277,17 @@ int zend_closure_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function return FAILURE; } - closure = (zend_closure *)zend_object_store_get_object(obj TSRMLS_CC); + closure = (zend_closure *)Z_OBJ_P(obj); *fptr_ptr = &closure->func; - if (closure->this_ptr) { + if (Z_TYPE(closure->this_ptr) != IS_UNDEF) { if (zobj_ptr) { - *zobj_ptr = closure->this_ptr; + ZVAL_COPY_VALUE(zobj_ptr, &closure->this_ptr); } - *ce_ptr = Z_OBJCE_P(closure->this_ptr); + *ce_ptr = Z_OBJCE(closure->this_ptr); } else { if (zobj_ptr) { - *zobj_ptr = NULL; + ZVAL_UNDEF(zobj_ptr); } *ce_ptr = closure->func.common.scope; } @@ -313,8 +297,8 @@ int zend_closure_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function static HashTable *zend_closure_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */ { - zend_closure *closure = (zend_closure *)zend_object_store_get_object(object TSRMLS_CC); - zval *val; + zend_closure *closure = (zend_closure *)Z_OBJ_P(object); + zval val; struct _zend_arg_info *arg_info = closure->func.common.arg_info; *is_temp = 0; @@ -326,22 +310,20 @@ static HashTable *zend_closure_get_debug_info(zval *object, int *is_temp TSRMLS_ if (closure->debug_info->nApplyCount == 0) { if (closure->func.type == ZEND_USER_FUNCTION && closure->func.op_array.static_variables) { HashTable *static_variables = closure->func.op_array.static_variables; - 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_hash_update(closure->debug_info, "static", sizeof("static"), (void *) &val, sizeof(zval *), NULL); + array_init(&val); + zend_hash_copy(Z_ARRVAL(val), static_variables, zval_add_ref); + zend_hash_str_update(closure->debug_info, "static", sizeof("static")-1, &val); } - 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 (Z_TYPE(closure->this_ptr) != IS_UNDEF) { + Z_ADDREF(closure->this_ptr); + zend_hash_str_update(closure->debug_info, "this", sizeof("this")-1, &closure->this_ptr); } if (arg_info) { zend_uint i, required = closure->func.common.required_num_args; - MAKE_STD_ZVAL(val); - array_init(val); + array_init(&val); for (i = 0; i < closure->func.common.num_args; i++) { char *name, *info; @@ -356,12 +338,12 @@ static HashTable *zend_closure_get_debug_info(zval *object, int *is_temp TSRMLS_ i + 1); } info_len = zend_spprintf(&info, 0, "%s", - i >= required ? "<optional>" : "<required>"); - add_assoc_stringl_ex(val, name, name_len + 1, info, info_len, 0); + i >= required ? "<optional>" : "<required>"); + add_assoc_stringl_ex(&val, name, name_len, info, info_len, 0); efree(name); arg_info++; } - zend_hash_update(closure->debug_info, "parameter", sizeof("parameter"), (void *) &val, sizeof(zval *), NULL); + zend_hash_str_update(closure->debug_info, "parameter", sizeof("parameter")-1, &val); } } @@ -369,12 +351,12 @@ static HashTable *zend_closure_get_debug_info(zval *object, int *is_temp TSRMLS_ } /* }}} */ -static HashTable *zend_closure_get_gc(zval *obj, zval ***table, int *n 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); + zend_closure *closure = (zend_closure *)Z_OBJ_P(obj); - *table = closure->this_ptr ? &closure->this_ptr : NULL; - *n = closure->this_ptr ? 1 : 0; + *table = Z_TYPE(closure->this_ptr) != IS_NULL ? &closure->this_ptr : NULL; + *n = Z_TYPE(closure->this_ptr) != IS_NULL ? 1 : 0; return (closure->func.type == ZEND_USER_FUNCTION) ? closure->func.op_array.static_variables : NULL; } @@ -418,6 +400,8 @@ void zend_register_closure_ce(TSRMLS_D) /* {{{ */ zend_ce_closure->unserialize = zend_class_unserialize_deny; memcpy(&closure_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); + closure_handlers.free_obj = zend_closure_free_storage; + closure_handlers.clone_obj = NULL; closure_handlers.get_constructor = zend_closure_get_constructor; closure_handlers.get_method = zend_closure_get_method; closure_handlers.write_property = zend_closure_write_property; @@ -439,7 +423,7 @@ ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_ent object_init_ex(res, zend_ce_closure); - closure = (zend_closure *)zend_object_store_get_object(res TSRMLS_CC); + closure = (zend_closure *)Z_OBJ_P(res); closure->func = *func; closure->func.common.prototype = NULL; @@ -464,12 +448,12 @@ ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_ent /* 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); + zend_error(E_WARNING, "Cannot bind function %s::%s to scope class %s", func->common.scope->name->val, func->common.function_name->val, scope->name->val); 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); + zend_error(E_WARNING, "Cannot bind function %s::%s to object of class %s", func->common.scope->name->val, func->common.function_name->val, Z_OBJCE_P(this_ptr)->name->val); scope = NULL; this_ptr = NULL; } @@ -487,14 +471,13 @@ ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_ent 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); + ZVAL_COPY(&closure->this_ptr, this_ptr); } else { closure->func.common.fn_flags |= ZEND_ACC_STATIC; - closure->this_ptr = NULL; + ZVAL_UNDEF(&closure->this_ptr); } } else { - closure->this_ptr = NULL; + ZVAL_UNDEF(&closure->this_ptr); } } /* }}} */ |