2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2013, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
19 #include "php_pq_object.h"
21 void *php_pq_object_create(zend_class_entry
*ce
, void *intern
, size_t obj_size
, zend_object_handlers
*oh
, HashTable
*ph
)
23 php_pq_object_t
*o
= ecalloc(1, obj_size
+ zend_object_properties_size(ce
));
25 zend_object_std_init(&o
->zo
, ce
);
26 object_properties_init(&o
->zo
, ce
);
31 zend_hash_init(&o
->gc
, 0, NULL
, NULL
, 0);
36 void php_pq_object_dtor(zend_object
*o
)
38 php_pq_object_t
*obj
= PHP_PQ_OBJ(NULL
, o
);
40 zend_hash_destroy(&obj
->gc
);
41 zend_object_std_dtor(o
);
44 void php_pq_object_to_zval(void *o
, zval
*zv
)
46 php_pq_object_t
*obj
= o
;
48 ZVAL_OBJ(zv
, &obj
->zo
);
52 void php_pq_object_to_zval_no_addref(void *o
, zval
*zv
)
54 php_pq_object_t
*obj
= o
;
56 ZVAL_OBJ(zv
, &obj
->zo
);
59 void php_pq_object_addref(void *o
)
61 php_pq_object_t
*obj
= o
;
65 ++GC_REFCOUNT(&obj
->zo
);
69 void php_pq_object_delref(void *o
)
71 php_pq_object_t
*obj
= o
;
74 /* this should gc immediately */
75 ZVAL_OBJ(&tmp
, &obj
->zo
);
79 struct apply_pi_to_ht_arg
{
81 php_pq_object_t
*pq_obj
;
85 static int apply_pi_to_ht(zval
*p
, void *a
)
87 zend_property_info
*pi
= Z_PTR_P(p
);
88 struct apply_pi_to_ht_arg
*arg
= a
;
91 php_pq_object_prophandler_t
*handler
;
93 if ((handler
= zend_hash_find_ptr(arg
->pq_obj
->prophandler
, pi
->name
)) && handler
->gc
) {
94 zval member
, return_value
;
96 ZVAL_STR(&member
, pi
->name
);
97 ZVAL_ARR(&return_value
, arg
->ht
);
98 handler
->gc(arg
->pq_obj
, &return_value
);
101 zval tmp_prop
, *property
= NULL
;
103 property
= php_pq_object_read_prop_80(&arg
->pq_obj
->zo
, pi
->name
, BP_VAR_R
, NULL
, &tmp_prop
);
104 zend_hash_update(arg
->ht
, pi
->name
, property
);
107 return ZEND_HASH_APPLY_KEEP
;
110 HashTable
*php_pq_object_debug_info_80(zend_object
*object
, int *temp
)
112 struct apply_pi_to_ht_arg arg
= {NULL
};
115 ALLOC_HASHTABLE(arg
.ht
);
116 ZEND_INIT_SYMTABLE(arg
.ht
);
118 arg
.pq_obj
= PHP_PQ_OBJ(NULL
, object
);
121 zend_hash_apply_with_argument(&arg
.pq_obj
->zo
.ce
->properties_info
, apply_pi_to_ht
, &arg
);
125 HashTable
*php_pq_object_debug_info_70(zval
*object
, int *temp
)
127 return php_pq_object_debug_info_80(Z_OBJ_P(object
), temp
);
130 static inline HashTable
*php_pq_object_properties_ex(zend_object
*object
, HashTable
*props
)
132 struct apply_pi_to_ht_arg arg
= {NULL
};
135 arg
.pq_obj
= PHP_PQ_OBJ(NULL
, object
);
138 zend_hash_apply_with_argument(&arg
.pq_obj
->zo
.ce
->properties_info
, apply_pi_to_ht
, &arg
);
142 HashTable
*php_pq_object_properties_80(zend_object
*object
)
144 return php_pq_object_properties_ex(object
, zend_std_get_properties(object
));
146 HashTable
*php_pq_object_properties_70(zval
*object
)
148 return php_pq_object_properties_ex(Z_OBJ_P(object
), zend_std_get_properties(object
));
151 static inline HashTable
*php_pq_object_get_gc_ex(zend_object
*object
, HashTable
*props
, zval
**table
, int *n
)
153 struct apply_pi_to_ht_arg arg
= {NULL
};
155 arg
.pq_obj
= PHP_PQ_OBJ(NULL
, object
);
156 arg
.ht
= &arg
.pq_obj
->gc
;
159 zend_hash_clean(arg
.ht
);
160 zend_hash_copy(arg
.ht
, props
, NULL
);
161 zend_hash_apply_with_argument(&arg
.pq_obj
->zo
.ce
->properties_info
, apply_pi_to_ht
, &arg
);
168 HashTable
*php_pq_object_get_gc_80(zend_object
*object
, zval
**table
, int *n
)
170 return php_pq_object_get_gc_ex(object
, zend_std_get_properties(object
), table
, n
);
172 HashTable
*php_pq_object_get_gc_70(zval
*object
, zval
**table
, int *n
)
174 return php_pq_object_get_gc_ex(Z_OBJ_P(object
), zend_std_get_properties(object
), table
, n
);
177 zend_class_entry
*ancestor(zend_class_entry
*ce
)
185 zval
*php_pq_object_read_prop_ex(zend_object
*object
, zend_string
*member
, int type
, void **cache_slot
, zval
*tmp
, zval
*def
)
187 php_pq_object_t
*obj
= PHP_PQ_OBJ(NULL
, object
);
188 php_pq_object_prophandler_t
*handler
;
189 zval
*return_value
= def
;
192 php_error(E_RECOVERABLE_ERROR
, "%s not initialized", ancestor(obj
->zo
.ce
)->name
->val
);
193 } else if (!(handler
= zend_hash_find_ptr(obj
->prophandler
, member
)) || !handler
->read
) {
194 /* default handler */
195 } else if (type
!= BP_VAR_R
) {
196 php_error(E_WARNING
, "Cannot access %s properties by reference or array key/index", ancestor(obj
->zo
.ce
)->name
->val
);
198 handler
->read(obj
, tmp
);
199 zend_get_std_object_handlers()->write_property(object
, member
, tmp
, cache_slot
);
209 zval
*php_pq_object_read_prop_80(zend_object
*object
, zend_string
*member
, int type
, void **cache_slot
, zval
*tmp
)
211 return php_pq_object_read_prop_ex(object
, member
, type
, cache_slot
, tmp
, zend_std_read_property(object
, member
, type
, cache_slot
, tmp
));
213 zval
*php_pq_object_read_prop_70(zval
*object
, zval
*member
, int type
, void **cache_slot
, zval
*tmp
)
215 zend_string
*member_str
= zval_get_string(member
);
216 zval
*return_value
= php_pq_object_read_prop_ex(Z_OBJ_P(object
), member_str
, type
, cache_slot
, tmp
, zend_std_read_property(object
, member
, type
, cache_slot
, tmp
));
217 zend_string_release(member_str
);
221 zval
*php_pq_object_write_prop_ex(zend_object
*object
, zend_string
*member
, zval
*value
, void **cache_slot
, int *update_std
)
223 php_pq_object_t
*obj
= PHP_PQ_OBJ(NULL
, object
);
224 php_pq_object_prophandler_t
*handler
;
227 php_error(E_RECOVERABLE_ERROR
, "%s not initialized", ancestor(obj
->zo
.ce
)->name
->val
);
229 } else if ((handler
= zend_hash_find_ptr(obj
->prophandler
, member
))) {
230 if (handler
->write
) {
231 handler
->write(obj
, value
);
239 zval
*php_pq_object_write_prop_80(zend_object
*object
, zend_string
*member
, zval
*value
, void **cache_slot
)
242 zval
*return_value
= php_pq_object_write_prop_ex(object
, member
, value
, cache_slot
, &update_std
);
244 return_value
= zend_std_write_property(object
, member
, value
, cache_slot
);
248 zval
*php_pq_object_write_prop_74(zval
*object
, zval
*member
, zval
*value
, void **cache_slot
)
251 zend_string
*member_str
= zval_get_string(member
);
252 zval
*return_value
= php_pq_object_write_prop_ex(Z_OBJ_P(object
), member_str
, value
, cache_slot
, &update_std
);
253 zend_string_release(member_str
);
255 return_value
= zend_std_write_property(object
, member
, value
, cache_slot
);
259 void php_pq_object_write_prop_70(zval
*object
, zval
*member
, zval
*value
, void **cache_slot
)
261 (void) php_pq_object_write_prop_74(object
, member
, value
, cache_slot
);
264 zval
*php_pq_object_get_prop_ptr_null_80(zend_object
*object
, zend_string
*member
, int type
, void **cache_slot
)
268 zval
*php_pq_object_get_prop_ptr_null_70(zval
*object
, zval
*member
, int type
, void **cache_slot
)
273 void php_pq_object_prophandler_dtor(zval
*zv
) {
274 pefree(Z_PTR_P(zv
), 1);