diff options
author | Nikita Popov <nikita.ppv@gmail.com> | 2018-10-04 13:58:35 +0200 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2018-10-10 10:39:10 +0200 |
commit | 7ec8087f8097955bfc6b97d1a916c6ffc39908f4 (patch) | |
tree | 7f56e3c0030c1135bdc84c1a2f1e26724e2f0f3b /ext | |
parent | 77c85b3119cc3aacbe2642c974bf88e512abd187 (diff) | |
download | php-git-7ec8087f8097955bfc6b97d1a916c6ffc39908f4.tar.gz |
Introduce get_properties_for() handler
This handler allows getting the object properties for a particular
purpose, such as array casting, serialization, etc.
Diffstat (limited to 'ext')
-rw-r--r-- | ext/json/json_encoder.c | 9 | ||||
-rw-r--r-- | ext/standard/var.c | 33 |
2 files changed, 21 insertions, 21 deletions
diff --git a/ext/json/json_encoder.c b/ext/json/json_encoder.c index c79e694f26..8ab190d075 100644 --- a/ext/json/json_encoder.c +++ b/ext/json/json_encoder.c @@ -130,19 +130,21 @@ static inline void php_json_encode_double(smart_str *buf, double d, int options) static int php_json_encode_array(smart_str *buf, zval *val, int options, php_json_encoder *encoder) /* {{{ */ { int i, r, need_comma = 0; - HashTable *myht; + HashTable *myht, *prop_ht; if (Z_TYPE_P(val) == IS_ARRAY) { myht = Z_ARRVAL_P(val); + prop_ht = NULL; r = (options & PHP_JSON_FORCE_OBJECT) ? PHP_JSON_OUTPUT_OBJECT : php_json_determine_array_type(val); } else { - myht = Z_OBJPROP_P(val); + prop_ht = myht = zend_get_properties_for(val, ZEND_PROP_PURPOSE_JSON); r = PHP_JSON_OUTPUT_OBJECT; } if (myht && GC_IS_RECURSIVE(myht)) { encoder->error_code = PHP_JSON_ERROR_RECURSION; smart_str_appendl(buf, "null", 4); + zend_release_properties(prop_ht); return FAILURE; } @@ -218,6 +220,7 @@ static int php_json_encode_array(smart_str *buf, zval *val, int options, php_jso if (php_json_encode_zval(buf, data, options, encoder) == FAILURE && !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) { PHP_JSON_HASH_UNPROTECT_RECURSION(myht); + zend_release_properties(prop_ht); return FAILURE; } } ZEND_HASH_FOREACH_END(); @@ -228,6 +231,7 @@ static int php_json_encode_array(smart_str *buf, zval *val, int options, php_jso if (encoder->depth > encoder->max_depth) { encoder->error_code = PHP_JSON_ERROR_DEPTH; if (!(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) { + zend_release_properties(prop_ht); return FAILURE; } } @@ -245,6 +249,7 @@ static int php_json_encode_array(smart_str *buf, zval *val, int options, php_jso smart_str_appendc(buf, '}'); } + zend_release_properties(prop_ht); return SUCCESS; } /* }}} */ diff --git a/ext/standard/var.c b/ext/standard/var.c index 77b290a6cc..f6b6bcf47f 100644 --- a/ext/standard/var.c +++ b/ext/standard/var.c @@ -82,7 +82,6 @@ PHPAPI void php_var_dump(zval *struc, int level) /* {{{ */ { HashTable *myht; zend_string *class_name; - int is_temp; int is_ref = 0; zend_ulong num; zend_string *key; @@ -145,7 +144,7 @@ again: } Z_PROTECT_RECURSION_P(struc); - myht = Z_OBJDEBUG_P(struc, is_temp); + myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_DEBUG); class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc)); php_printf("%sobject(%s)#%d (%d) {\n", COMMON, ZSTR_VAL(class_name), Z_OBJ_HANDLE_P(struc), myht ? zend_array_count(myht) : 0); zend_string_release_ex(class_name, 0); @@ -158,10 +157,7 @@ again: ZEND_HASH_FOREACH_KEY_VAL_IND(myht, num, key, val) { php_object_property_dump(val, num, key, level); } ZEND_HASH_FOREACH_END(); - if (is_temp) { - zend_hash_destroy(myht); - efree(myht); - } + zend_release_properties(myht); } if (level > 1) { php_printf("%*c", level-1, ' '); @@ -249,7 +245,6 @@ PHPAPI void php_debug_zval_dump(zval *struc, int level) /* {{{ */ { HashTable *myht = NULL; zend_string *class_name; - int is_temp = 0; int is_ref = 0; zend_ulong index; zend_string *key; @@ -299,20 +294,17 @@ again: if (level > 1 && !(GC_FLAGS(myht) & GC_IMMUTABLE)) { GC_UNPROTECT_RECURSION(myht); } - if (is_temp) { - zend_hash_destroy(myht); - efree(myht); - } if (level > 1) { php_printf("%*c", level - 1, ' '); } PUTS("}\n"); break; case IS_OBJECT: - myht = Z_OBJDEBUG_P(struc, is_temp); + myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_DEBUG); if (myht) { if (GC_IS_RECURSIVE(myht)) { PUTS("*RECURSION*\n"); + zend_release_properties(myht); return; } GC_PROTECT_RECURSION(myht); @@ -325,10 +317,7 @@ again: zval_object_property_dump(val, index, key, level); } ZEND_HASH_FOREACH_END(); GC_UNPROTECT_RECURSION(myht); - if (is_temp) { - zend_hash_destroy(myht); - efree(myht); - } + zend_release_properties(myht); } if (level > 1) { php_printf("%*c", level - 1, ' '); @@ -510,11 +499,12 @@ again: break; case IS_OBJECT: - myht = Z_OBJPROP_P(struc); + myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_VAR_EXPORT); if (myht) { if (GC_IS_RECURSIVE(myht)) { smart_str_appendl(buf, "NULL", 4); zend_error(E_WARNING, "var_export does not handle circular references"); + zend_release_properties(myht); return; } else { GC_PROTECT_RECURSION(myht); @@ -538,6 +528,7 @@ again: php_object_element_export(val, index, key, level, buf); } ZEND_HASH_FOREACH_END(); GC_UNPROTECT_RECURSION(myht); + zend_release_properties(myht); } if (level > 1) { buffer_append_spaces(buf, level - 1); @@ -743,7 +734,7 @@ static void php_var_serialize_class(smart_str *buf, zval *struc, zval *retval_pt smart_str_appendl(buf, ":{", 2); ZVAL_NULL(&nval); - propers = Z_OBJPROP_P(struc); + propers = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_SERIALIZE); ZEND_HASH_FOREACH_STR_KEY(&names, name) { zend_string *prot_name, *priv_name; @@ -809,6 +800,7 @@ undef_prop: smart_str_appendc(buf, '}'); zend_hash_destroy(&names); + zend_release_properties(propers); } /* }}} */ @@ -925,7 +917,7 @@ again: i = zend_array_count(myht); } else { incomplete_class = php_var_serialize_class_name(buf, struc); - myht = Z_OBJPROP_P(struc); + myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_SERIALIZE); /* count after serializing name, since php_var_serialize_class_name * changes the count if the variable is incomplete class */ i = zend_array_count(myht); @@ -978,6 +970,9 @@ again: } ZEND_HASH_FOREACH_END(); } smart_str_appendc(buf, '}'); + if (Z_TYPE_P(struc) == IS_OBJECT) { + zend_release_properties(myht); + } return; } case IS_REFERENCE: |