X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fphp_propro_api.c;h=631f707c3a9a77d9944c77240851a0df6ff4489e;hb=c5b306a3e265073a7d05a039e0470b8281c45f58;hp=9b5a79b1e95f5e59717c8a702712e5232e6da7d4;hpb=23538fb62e010a18c16b216f8e2dbd102e528303;p=m6w6%2Fext-propro diff --git a/src/php_propro_api.c b/src/php_propro_api.c index 9b5a79b..631f707 100644 --- a/src/php_propro_api.c +++ b/src/php_propro_api.c @@ -47,7 +47,7 @@ static const char *types[] = { "resource", "reference", "constant", - "constant AST", + "const AST", "_BOOL", "callable", "indirect", @@ -55,23 +55,46 @@ static const char *types[] = { "pointer" }; -static void _walk(php_property_proxy_object_t *obj) +static const char *_type(zval *zv) { + switch (Z_TYPE_P(zv)) { + case IS_OBJECT: + if (zv->value.obj->ce == php_property_proxy_get_class_entry()) { + return "PROPRO"; + } + /* no break */ + default: + return types[Z_TYPE_P(zv)]; + } +} + +static int _walk(php_property_proxy_object_t *obj) +{ + int p = 0; + if (obj) { if (!Z_ISUNDEF(obj->parent)) { - _walk(get_propro(&obj->parent)); + p += _walk(get_propro(&obj->parent)); } if (obj->proxy) { - fprintf(stderr, ".%s", obj->proxy->member->val); + p += fprintf(stderr, ".%s", obj->proxy->member->val); } } + + return p; } +static inline zval *get_container(zval *object, zval *tmp); +static inline zval *get_container_value(zval *container, zend_string *member, zval *return_value); + static void debug_propro(int inout, const char *f, php_property_proxy_object_t *obj, php_property_proxy_t *proxy, zval *offset, zval *value) { + int width; + zval tmp, *container = &tmp; + if (!proxy && obj) { proxy = obj->proxy; } @@ -81,22 +104,20 @@ static void debug_propro(int inout, const char *f, level += inout; if (proxy) { - fprintf(stderr, " container=%p (%s", Z_COUNTED(proxy->container), - types[Z_TYPE(proxy->container)]); - if (Z_REFCOUNTED(proxy->container) && Z_REFCOUNT(proxy->container) != 1) { - fprintf(stderr, " rc=%d", Z_REFCOUNT(proxy->container)); - } - if (Z_ISREF(proxy->container)) { - zval *ref = Z_REFVAL(proxy->container); - fprintf(stderr, " %s %p", types[Z_TYPE_P(ref)], ref->value.counted); - if (Z_REFCOUNTED_P(ref) && Z_REFCOUNT_P(ref) != 1) { - fprintf(stderr, " rc=%d", Z_REFCOUNT_P(ref)); - } + ZVAL_UNDEF(&tmp); + if (!obj) { + container = &proxy->container; + } else if (Z_ISUNDEF(obj->parent)) { + container = &obj->proxy->container; } - fprintf(stderr, ") "); + fprintf(stderr, " container= %-14p < %-10s rc=%-2d%s> ", + Z_REFCOUNTED_P(container) ? Z_COUNTED_P(container) : NULL, + _type(container), + Z_REFCOUNTED_P(container) ? Z_REFCOUNT_P(container) : 0, + Z_ISREF_P(container) ? "(ref) ":"(noref)"); } - _walk(obj); + width = _walk(obj); if (*f++=='d' && *f++=='i' @@ -110,24 +131,22 @@ static void debug_propro(int inout, const char *f, offset_str = Z_STRVAL_P(o); } - fprintf(stderr, ".%s", offset_str); + width += fprintf(stderr, ".%s", offset_str); if (o && o != offset) { zval_ptr_dtor(o); } } if (value) { + fprintf(stderr, "%.*s", 32-width, " "); if (Z_ISUNDEF_P(value)) { - fprintf(stderr, " =UNDEF"); + fprintf(stderr, " = UNDEF"); } else { - fprintf(stderr, " =%p (%s", Z_COUNTED_P(value), types[Z_TYPE_P(value)&0xf]); - if (Z_REFCOUNTED_P(value) && Z_REFCOUNT_P(value) != 1) { - fprintf(stderr, " rc=%d", Z_REFCOUNT_P(value)); - } - if (Z_ISREF_P(value)) { - fprintf(stderr, " ref"); - } - fprintf(stderr, ") "); + fprintf(stderr, " = %-14p < %-10s rc=%-2d%s> ", + Z_REFCOUNTED_P(value) ? Z_COUNTED_P(value) : NULL, + _type(value), + Z_REFCOUNTED_P(value) ? Z_REFCOUNT_P(value) : 0, + Z_ISREF_P(value) ? "(ref) " : "(noref)"); if (!Z_ISUNDEF_P(value) && Z_TYPE_P(value) != IS_INDIRECT) { zend_print_flat_zval_r(value TSRMLS_CC); } @@ -149,12 +168,12 @@ php_property_proxy_t *php_property_proxy_init(zval *container, zend_string *memb ZVAL_STR_COPY(&offset, member); #endif - debug_propro(1, "init", NULL, proxy, &offset, NULL); - - ZVAL_COPY(&proxy->container, container); + if (container) { + ZVAL_COPY(&proxy->container, container); + } proxy->member = zend_string_copy(member); - debug_propro(-1, "init", NULL, proxy, &offset, NULL); + debug_propro(0, "init", NULL, proxy, &offset, NULL); #if DEBUG_PROPRO zval_dtor(&offset); @@ -169,22 +188,20 @@ void php_property_proxy_free(php_property_proxy_t **proxy) zval offset; ZVAL_STR_COPY(&offset, (*proxy)->member); + debug_propro(0, "free", NULL, *proxy, &offset, NULL); #endif - debug_propro(1, "dtor", NULL, *proxy, &offset, NULL); - if (*proxy) { - zval_ptr_dtor(&(*proxy)->container); - - ZVAL_UNDEF(&(*proxy)->container); + if (!Z_ISUNDEF((*proxy)->container)) { + zval_ptr_dtor(&(*proxy)->container); + ZVAL_UNDEF(&(*proxy)->container); + } zend_string_release((*proxy)->member); (*proxy)->member = NULL; efree(*proxy); *proxy = NULL; } - debug_propro(-1, "dtor", NULL, NULL, &offset, NULL); - #if DEBUG_PROPRO zval_dtor(&offset); #endif @@ -236,6 +253,49 @@ static void destroy_obj(zend_object *object) zend_object_std_dtor(object); } +static inline php_property_proxy_object_t *get_propro(zval *object) +{ + ZEND_ASSERT(Z_TYPE_P(object) == IS_OBJECT); + return PHP_PROPRO_PTR(Z_OBJ_P(object)); +} + +static HashTable *get_gc(zval *object, zval **table, int *n) +{ + php_property_proxy_object_t *o = get_propro(object); + + if (Z_ISUNDEF(o->parent)) { + *table = &o->proxy->container; + } else { + *table = &o->parent; + } + *n = 1; + return NULL; +} + +static HashTable *get_debug_info(zval *object, int *is_temp) +{ + HashTable *ht; + zval *zmember; + php_property_proxy_object_t *obj = get_propro(object); + + ALLOC_HASHTABLE(ht); + zend_hash_init(ht, 3, NULL, NULL, 0); + + if (Z_ISUNDEF(obj->parent)) { + zend_hash_str_add_empty_element(ht, "parent", sizeof("parent")-1); + zend_hash_str_add(ht, "container", sizeof("container")-1, &obj->proxy->container); + } else { + zend_hash_str_add(ht, "parent", sizeof("parent")-1, &obj->parent); + zend_hash_str_add_empty_element(ht, "container", sizeof("container")-1); + } + + zmember = zend_hash_str_add_empty_element(ht, "member", sizeof("member")-1); + ZVAL_STR(zmember, obj->proxy->member); + + *is_temp = 1; + return ht; +} + static ZEND_RESULT_CODE cast_obj(zval *object, zval *return_value, int type) { zval tmp; @@ -267,43 +327,33 @@ static void set_obj(zval *object, zval *value) { set_proxied_value(object, value); } -static inline php_property_proxy_object_t *get_propro(zval *object) -{ - ZEND_ASSERT(Z_TYPE_P(object) == IS_OBJECT); - return PHP_PROPRO_PTR(Z_OBJ_P(object)); -} - -static inline zval *get_container(zval *object) +static inline zval *get_container(zval *object, zval *tmp) { php_property_proxy_object_t *obj = get_propro(object); + zval *container; - if (!Z_ISUNDEF(obj->parent)) { - zval *parent_value, tmp; - - ZVAL_UNDEF(&tmp); - parent_value = get_proxied_value(&obj->parent, &tmp); - ZVAL_DEREF(parent_value); - switch (Z_TYPE_P(parent_value)) { - case IS_OBJECT: - case IS_ARRAY: - zend_assign_to_variable(&obj->proxy->container, parent_value, IS_CV); - break; - default: - break; - } + if (Z_ISUNDEF(obj->parent)) { + container = &obj->proxy->container; + } else { + container = get_proxied_value(&obj->parent, tmp); } - - return &obj->proxy->container; + return container; } static inline void set_container(zval *object, zval *container) { php_property_proxy_object_t *obj = get_propro(object); - zend_assign_to_variable(&obj->proxy->container, container, IS_CV); + if (Z_ISUNDEF(obj->parent)) { + if (container != &obj->proxy->container) { + zval tmp; - if (!Z_ISUNDEF(obj->parent)) { - set_proxied_value(&obj->parent, &obj->proxy->container); + ZVAL_COPY_VALUE(&tmp, &obj->proxy->container); + ZVAL_COPY(&obj->proxy->container, container); + zval_ptr_dtor(&tmp); + } + } else { + set_proxied_value(&obj->parent, container); } } @@ -314,9 +364,9 @@ static inline zval *get_container_value(zval *container, zend_string *member, zv ZVAL_DEREF(container); switch (Z_TYPE_P(container)) { case IS_OBJECT: + ZVAL_UNDEF(&prop_tmp); found_value = zend_read_property(Z_OBJCE_P(container), container, member->val, member->len, 0, &prop_tmp); - break; case IS_ARRAY: @@ -326,13 +376,44 @@ static inline zval *get_container_value(zval *container, zend_string *member, zv if (found_value) { RETVAL_ZVAL(found_value, 0, 0); - //ZVAL_COPY_VALUE(return_value, found_value); } return return_value; } -static inline void set_container_value(zval *container, zend_string *member, zval *value) +static inline void cleanup_container(zval *object, zval *container, zend_bool separated) +{ + if (separated) { + zval_ptr_dtor(container); + } +} + +static inline zend_bool separate_container(zval *object, zval *container) +{ + switch (Z_TYPE_P(container)) { + case IS_OBJECT: + return 0; + + case IS_ARRAY: + /* always duplicate for PHP-7.0 and 7.1 on travis */ + ZVAL_ARR(container, zend_array_dup(Z_ARRVAL_P(container))); + break; + + case IS_UNDEF: + array_init(container); + break; + + default: + SEPARATE_ZVAL(container); + Z_TRY_ADDREF_P(container); + convert_to_array(container); + break; + } + + return 1; +} + +static inline zval *set_container_value(zval *container, zend_string *member, zval *value) { ZVAL_DEREF(container); switch (Z_TYPE_P(container)) { @@ -342,14 +423,20 @@ static inline void set_container_value(zval *container, zend_string *member, zva break; case IS_ARRAY: - SEPARATE_ARRAY(container); Z_TRY_ADDREF_P(value); - zend_symtable_update(Z_ARRVAL_P(container), member, value); + if (member) { + value = zend_symtable_update(Z_ARRVAL_P(container), member, value); + } else { + value = zend_hash_next_index_insert(Z_ARRVAL_P(container), value); + } break; default: ZEND_ASSERT(0); + break; } + + return value; } @@ -360,7 +447,10 @@ static zval *get_proxied_value(zval *object, zval *return_value) debug_propro(1, "get", obj, NULL, NULL, NULL); if (obj->proxy) { - zval *container = get_container(object); + zval tmp, *container; + + ZVAL_UNDEF(&tmp); + container = get_container(object, &tmp); return_value = get_container_value(container, obj->proxy->member, return_value); } @@ -377,10 +467,19 @@ static void set_proxied_value(zval *object, zval *value) debug_propro(1, "set", obj, NULL, NULL, value); if (obj->proxy) { - zval *container = get_container(object); + zval tmp, *container; + zend_bool separated; + Z_TRY_ADDREF_P(value); + + ZVAL_UNDEF(&tmp); + container = get_container(object, &tmp); + separated = separate_container(object, container); set_container_value(container, obj->proxy->member, value); set_container(object, container); + cleanup_container(object, container, separated); + + Z_TRY_DELREF_P(value); debug_propro(0, "set", obj, NULL, NULL, value); } @@ -399,7 +498,7 @@ static zval *read_dimension(zval *object, zval *offset, int type, zval *return_v ZVAL_UNDEF(&tmp); value = get_proxied_value(object, &tmp); - if (type == BP_VAR_R) { + if (type == BP_VAR_R || type == BP_VAR_IS) { ZEND_ASSERT(member); if (!Z_ISUNDEF_P(value)) { @@ -411,36 +510,26 @@ static zval *read_dimension(zval *object, zval *offset, int type, zval *return_v } else { php_property_proxy_t *proxy; php_property_proxy_object_t *proxy_obj; - zval *array = value; - zend_bool created = Z_ISUNDEF_P(value); - ZVAL_DEREF(array); - if (Z_REFCOUNTED_P(array) && Z_REFCOUNT_P(array) > 1) { - created = 1; - SEPARATE_ZVAL_NOREF(array); - } - if (Z_TYPE_P(array) != IS_ARRAY) { - created = 1; - if (Z_ISUNDEF_P(array)) { - array_init(array); - } else { - convert_to_array(array); - } + if (Z_ISUNDEF_P(value)) { + ZVAL_NULL(value); } if (!member) { - member = zend_long_to_str(zend_hash_next_free_element( - Z_ARRVAL_P(array))); + if (Z_TYPE_P(value) == IS_ARRAY) { + member = zend_long_to_str(zend_hash_next_free_element( + Z_ARRVAL_P(value))); + } else if (Z_TYPE_P(value) != IS_OBJECT){ + member = zend_long_to_str(0); + } } - proxy = php_property_proxy_init(value, member); + proxy = php_property_proxy_init(NULL, member); proxy_obj = php_property_proxy_object_new_ex(NULL, proxy); ZVAL_COPY(&proxy_obj->parent, object); RETVAL_OBJ(&proxy_obj->zo); - if (created) { - Z_DELREF_P(value); - } + debug_propro(0, "dim_R pp", get_propro(object), NULL, offset, return_value); } if (member) { @@ -463,7 +552,6 @@ static int has_dimension(zval *object, zval *offset, int check_empty) ZVAL_UNDEF(&tmp); value = get_proxied_value(object, &tmp); - exists = 0; if (!Z_ISUNDEF_P(value)) { zend_string *zs = zval_get_string(offset); @@ -471,9 +559,7 @@ static int has_dimension(zval *object, zval *offset, int check_empty) if (Z_TYPE_P(value) == IS_ARRAY) { zval *zentry = zend_symtable_find(Z_ARRVAL_P(value), zs); - if (!zentry) { - exists = 0; - } else { + if (zentry) { if (check_empty) { exists = !Z_ISNULL_P(zentry); } else { @@ -492,51 +578,25 @@ static int has_dimension(zval *object, zval *offset, int check_empty) static void write_dimension(zval *object, zval *offset, zval *input_value) { - zend_bool created = 0; - zval *array, *value, tmp; + zval *array, tmp; + zend_string *zs = NULL; + zend_bool separated; debug_propro(1, "dim_w", get_propro(object), NULL, offset, input_value); - ZVAL_UNDEF(&tmp); - value = get_proxied_value(object, &tmp); - array = value; - - if (Z_ISUNDEF_P(value)) { - array_init(array); - created = 1; - } else if (Z_TYPE_P(array) == IS_ARRAY) { - if (Z_REFCOUNT_P(array) > 1) { - SEPARATE_ARRAY(array); - created = 1; - } - } else { - ZVAL_DEREF(array); - if (Z_REFCOUNTED_P(array) && Z_REFCOUNT_P(array) > 1) { - SEPARATE_ZVAL(array); - created = 1; - } - if (Z_TYPE_P(array) != IS_ARRAY) { - convert_to_array(array); - created = 1; - } - } - - Z_TRY_ADDREF_P(input_value); - - debug_propro(0, "dim_w", get_propro(object), NULL, offset, input_value); - if (offset) { - zend_string *zs = zval_get_string(offset); - zend_symtable_update(Z_ARRVAL_P(array), zs, input_value); - zend_string_release(zs); - } else { - zend_hash_next_index_insert(Z_ARRVAL_P(array), input_value); + zs = zval_get_string(offset); } - set_proxied_value(object, value); + ZVAL_UNDEF(&tmp); + array = get_proxied_value(object, &tmp); + separated = separate_container(object, array); + set_container_value(array, zs, input_value); + set_proxied_value(object, array); + cleanup_container(object, array, separated); - if (created) { - Z_DELREF_P(array); + if (zs) { + zend_string_release(zs); } debug_propro(-1, "dim_w", get_propro(object), NULL, offset, input_value); @@ -568,40 +628,31 @@ static void unset_dimension(zval *object, zval *offset) } ZEND_BEGIN_ARG_INFO_EX(ai_propro_construct, 0, 0, 2) - ZEND_ARG_INFO(1, object) + ZEND_ARG_INFO(0, object) ZEND_ARG_INFO(0, member) ZEND_ARG_OBJ_INFO(0, parent, php\\PropertyProxy, 1) ZEND_END_ARG_INFO(); static PHP_METHOD(propro, __construct) { zend_error_handling zeh; - zval *reference, *container, *parent = NULL; + zval *reference, *parent = NULL; zend_string *member; zend_replace_error_handling(EH_THROW, NULL, &zeh); - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "zS|O!", + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "o!S|O!", &reference, &member, &parent, php_property_proxy_class_entry)) { php_property_proxy_object_t *obj; - container = reference; - ZVAL_DEREF(container); - switch (Z_TYPE_P(container)) { - case IS_ARRAY: - SEPARATE_ARRAY(container); - break; - case IS_OBJECT: - break; - default: - SEPARATE_ZVAL(container); - convert_to_array(container); - break; - } - obj = get_propro(getThis()); - obj->proxy = php_property_proxy_init(reference, member); if (parent) { ZVAL_COPY(&obj->parent, parent); + obj->proxy = php_property_proxy_init(NULL, member); + } else if (reference) { + zval *container = reference; + obj->proxy = php_property_proxy_init(container, member); + } else { + php_error(E_WARNING, "Either object or parent must be set"); } } zend_restore_error_handling(&zeh); @@ -626,6 +677,8 @@ static PHP_MINIT_FUNCTION(propro) sizeof(zend_object_handlers)); php_property_proxy_object_handlers.offset = XtOffsetOf(php_property_proxy_object_t, zo); php_property_proxy_object_handlers.free_obj = destroy_obj; + php_property_proxy_object_handlers.get_gc = get_gc; + php_property_proxy_object_handlers.get_debug_info = get_debug_info; php_property_proxy_object_handlers.set = set_obj; php_property_proxy_object_handlers.get = get_obj; php_property_proxy_object_handlers.cast_object = cast_obj;