Skip to content

New handler for retrieving properties when object is serialized #397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Zend/zend_object_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ ZEND_API HashTable *zend_std_get_debug_info(zval *object, int *is_temp TSRMLS_DC
}
/* }}} */

ZEND_API HashTable *zend_std_get_serialize_info(zval *object TSRMLS_DC) /* {{{ */
{
return zend_std_get_properties(object TSRMLS_CC);
}
/* }}} */

static zval *zend_std_call_getter(zval *object, zval *member TSRMLS_DC) /* {{{ */
{
zval *retval = NULL;
Expand Down Expand Up @@ -1649,6 +1655,7 @@ ZEND_API zend_object_handlers std_object_handlers = {
zend_std_cast_object_tostring, /* cast_object */
NULL, /* count_elements */
NULL, /* get_debug_info */
NULL, /* get_serialize_info */
zend_std_get_closure, /* get_closure */
zend_std_get_gc, /* get_gc */
NULL, /* do_operation */
Expand Down
3 changes: 3 additions & 0 deletions Zend/zend_object_handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ typedef void (*zend_object_unset_dimension_t)(zval *object, zval *offset TSRMLS_
typedef HashTable *(*zend_object_get_properties_t)(zval *object TSRMLS_DC);

typedef HashTable *(*zend_object_get_debug_info_t)(zval *object, int *is_temp TSRMLS_DC);
typedef HashTable *(*zend_object_get_serialize_info_t)(zval *objectp TSRMLS_DC);

/* Used to call methods */
/* args on stack! */
Expand Down Expand Up @@ -143,6 +144,7 @@ struct _zend_object_handlers {
zend_object_cast_t cast_object;
zend_object_count_elements_t count_elements;
zend_object_get_debug_info_t get_debug_info;
zend_object_get_serialize_info_t get_serialize_info;
zend_object_get_closure_t get_closure;
zend_object_get_gc_t get_gc;
zend_object_do_operation_t do_operation;
Expand All @@ -162,6 +164,7 @@ ZEND_API union _zend_function *zend_std_get_constructor(zval *object TSRMLS_DC);
ZEND_API struct _zend_property_info *zend_get_property_info(zend_class_entry *ce, zval *member, int silent TSRMLS_DC);
ZEND_API HashTable *zend_std_get_properties(zval *object TSRMLS_DC);
ZEND_API HashTable *zend_std_get_debug_info(zval *object, int *is_temp TSRMLS_DC);
ZEND_API HashTable *zend_std_get_serialize_info(zval *object TSRMLS_DC);
ZEND_API int zend_std_cast_object_tostring(zval *readobj, zval *writeobj, int type TSRMLS_DC);
ZEND_API void zend_std_write_property(zval *object, zval *member, zval *value, const struct _zend_literal *key TSRMLS_DC);
ZEND_API void rebuild_object_properties(zend_object *zobj);
Expand Down
3 changes: 3 additions & 0 deletions Zend/zend_operators.h
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ END_EXTERN_C()
#define Z_OBJ_HANDLER(zval, hf) Z_OBJ_HT((zval))->hf
#define Z_RESVAL(zval) (zval).value.lval
#define Z_OBJDEBUG(zval,is_tmp) (Z_OBJ_HANDLER((zval),get_debug_info)?Z_OBJ_HANDLER((zval),get_debug_info)(&(zval),&is_tmp TSRMLS_CC):(is_tmp=0,Z_OBJ_HANDLER((zval),get_properties)?Z_OBJPROP(zval):NULL))
#define Z_OBJSERIALIZE(zval) (Z_OBJ_HANDLER((zval),get_serialize_info)?Z_OBJ_HANDLER((zval),get_serialize_info)(&(zval) TSRMLS_CC):(Z_OBJ_HANDLER((zval),get_properties)?Z_OBJPROP(zval):NULL))

#define Z_LVAL_P(zval_p) Z_LVAL(*zval_p)
#define Z_BVAL_P(zval_p) Z_BVAL(*zval_p)
Expand All @@ -466,6 +467,7 @@ END_EXTERN_C()
#define Z_OBJ_HT_P(zval_p) Z_OBJ_HT(*zval_p)
#define Z_OBJ_HANDLER_P(zval_p, h) Z_OBJ_HANDLER(*zval_p, h)
#define Z_OBJDEBUG_P(zval_p,is_tmp) Z_OBJDEBUG(*zval_p,is_tmp)
#define Z_OBJSERIALIZE_P(zval) Z_OBJSERIALIZE(*zval)

#define Z_LVAL_PP(zval_pp) Z_LVAL(**zval_pp)
#define Z_BVAL_PP(zval_pp) Z_BVAL(**zval_pp)
Expand All @@ -481,6 +483,7 @@ END_EXTERN_C()
#define Z_OBJ_HT_PP(zval_p) Z_OBJ_HT(**zval_p)
#define Z_OBJ_HANDLER_PP(zval_p, h) Z_OBJ_HANDLER(**zval_p, h)
#define Z_OBJDEBUG_PP(zval_pp,is_tmp) Z_OBJDEBUG(**zval_pp,is_tmp)
#define Z_OBJSERIALIZE_PP(zval) Z_OBJSERIALIZE(**zval)

#define Z_TYPE(zval) (zval).type
#define Z_TYPE_P(zval_p) Z_TYPE(*zval_p)
Expand Down
13 changes: 7 additions & 6 deletions ext/standard/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -619,13 +619,14 @@ static inline zend_bool php_var_serialize_class_name(smart_str *buf, zval *struc

static void php_var_serialize_class(smart_str *buf, zval *struc, zval *retval_ptr, HashTable *var_hash TSRMLS_DC) /* {{{ */
{
HashTable *retval_props = Z_OBJSERIALIZE_P(retval_ptr);
int count;
zend_bool incomplete_class;

incomplete_class = php_var_serialize_class_name(buf, struc TSRMLS_CC);
/* count after serializing name, since php_var_serialize_class_name
* changes the count if the variable is incomplete class */
count = zend_hash_num_elements(HASH_OF(retval_ptr));
count = zend_hash_num_elements(retval_props);
if (incomplete_class) {
--count;
}
Expand All @@ -644,10 +645,10 @@ static void php_var_serialize_class(smart_str *buf, zval *struc, zval *retval_pt
ZVAL_NULL(&nval);
nvalp = &nval;

zend_hash_internal_pointer_reset_ex(HASH_OF(retval_ptr), &pos);
zend_hash_internal_pointer_reset_ex(retval_props, &pos);

for (;; zend_hash_move_forward_ex(HASH_OF(retval_ptr), &pos)) {
i = zend_hash_get_current_key_ex(HASH_OF(retval_ptr), &key, NULL, &index, 0, &pos);
for (;; zend_hash_move_forward_ex(retval_props, &pos)) {
i = zend_hash_get_current_key_ex(retval_props, &key, NULL, &index, 0, &pos);

if (i == HASH_KEY_NON_EXISTENT) {
break;
Expand All @@ -656,7 +657,7 @@ static void php_var_serialize_class(smart_str *buf, zval *struc, zval *retval_pt
if (incomplete_class && strcmp(key, MAGIC_MEMBER) == 0) {
continue;
}
zend_hash_get_current_data_ex(HASH_OF(retval_ptr), (void **) &name, &pos);
zend_hash_get_current_data_ex(retval_props, (void **) &name, &pos);

if (Z_TYPE_PP(name) != IS_STRING) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "__sleep should return an array only containing the names of instance-variables to serialize.");
Expand Down Expand Up @@ -840,7 +841,7 @@ static void php_var_serialize_intern(smart_str *buf, zval *struc, HashTable *var
myht = HASH_OF(struc);
} else {
incomplete_class = php_var_serialize_class_name(buf, struc TSRMLS_CC);
myht = Z_OBJPROP_P(struc);
myht = Z_OBJSERIALIZE_P(struc);
}
/* count after serializing name, since php_var_serialize_class_name
* changes the count if the variable is incomplete class */
Expand Down