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"
20 #include "php_pq_misc.h"
22 void *php_pq_object_create(zend_class_entry
*ce
, void *intern
, size_t obj_size
, zend_object_handlers
*oh
, HashTable
*ph
)
24 php_pq_object_t
*o
= ecalloc(1, obj_size
+ zend_object_properties_size(ce
));
26 zend_object_std_init(&o
->zo
, ce
);
27 object_properties_init(&o
->zo
, ce
);
32 zend_hash_init(&o
->gc
, 0, NULL
, NULL
, 0);
37 void php_pq_object_dtor(zend_object
*o
)
39 php_pq_object_t
*obj
= PHP_PQ_OBJ(NULL
, o
);
41 zend_hash_destroy(&obj
->gc
);
42 zend_object_std_dtor(o
);
45 void php_pq_object_to_zval(void *o
, zval
*zv
)
47 php_pq_object_t
*obj
= o
;
49 ZVAL_OBJ(zv
, &obj
->zo
);
53 void php_pq_object_to_zval_no_addref(void *o
, zval
*zv
)
55 php_pq_object_t
*obj
= o
;
57 ZVAL_OBJ(zv
, &obj
->zo
);
60 void php_pq_object_addref(void *o
)
62 php_pq_object_t
*obj
= o
;
66 ++GC_REFCOUNT(&obj
->zo
);
70 void php_pq_object_delref(void *o
)
72 php_pq_object_t
*obj
= o
;
75 /* this should gc immediately */
76 ZVAL_OBJ(&tmp
, &obj
->zo
);
80 struct apply_pi_to_ht_arg
{
82 php_pq_object_t
*pq_obj
;
86 static int apply_pi_to_ht(zval
*p
, void *a
)
88 zend_property_info
*pi
= Z_PTR_P(p
);
89 struct apply_pi_to_ht_arg
*arg
= a
;
92 php_pq_object_prophandler_t
*handler
;
94 if ((handler
= zend_hash_find_ptr(arg
->pq_obj
->prophandler
, pi
->name
)) && handler
->gc
) {
97 ZVAL_ARR(&return_value
, arg
->ht
);
98 handler
->gc(arg
->pq_obj
, &return_value
);
101 zval tmp_prop
, *property
;
102 #if PHP_VERSION_ID < 80000
105 ZVAL_OBJ(&zobj
, &arg
->pq_obj
->zo
);
106 # if PHP_VERSION_ID < 70100
107 property
= zend_read_property(arg
->pq_obj
->zo
.ce
, &zobj
, pi
->name
->val
, pi
->name
->len
, 0, &tmp_prop
);
109 property
= zend_read_property_ex(arg
->pq_obj
->zo
.ce
, &zobj
, pi
->name
, 0, &tmp_prop
);
112 property
= zend_read_property_ex(arg
->pq_obj
->zo
.ce
, &arg
->pq_obj
->zo
, pi
->name
, 0, &tmp_prop
);
114 zend_hash_update(arg
->ht
, pi
->name
, property
);
117 return ZEND_HASH_APPLY_KEEP
;
120 static inline HashTable
*php_pq_object_debug_info_ex(zend_object
*object
, int *temp
)
122 struct apply_pi_to_ht_arg arg
= {NULL
};
125 ALLOC_HASHTABLE(arg
.ht
);
126 ZEND_INIT_SYMTABLE(arg
.ht
);
128 arg
.pq_obj
= PHP_PQ_OBJ(NULL
, object
);
131 zend_hash_apply_with_argument(&arg
.pq_obj
->zo
.ce
->properties_info
, apply_pi_to_ht
, &arg
);
135 #if PHP_VERSION_ID >= 80000
136 HashTable
*php_pq_object_debug_info(zend_object
*object
, int *temp
)
138 return php_pq_object_debug_info_ex(object
, temp
);
141 HashTable
*php_pq_object_debug_info(zval
*object
, int *temp
)
143 return php_pq_object_debug_info_ex(Z_OBJ_P(object
), temp
);
147 static inline HashTable
*php_pq_object_properties_ex(zend_object
*object
, HashTable
*props
)
149 struct apply_pi_to_ht_arg arg
= {NULL
};
152 arg
.pq_obj
= PHP_PQ_OBJ(NULL
, object
);
155 zend_hash_apply_with_argument(&arg
.pq_obj
->zo
.ce
->properties_info
, apply_pi_to_ht
, &arg
);
159 #if PHP_VERSION_ID >= 80000
160 HashTable
*php_pq_object_properties(zend_object
*object
)
162 return php_pq_object_properties_ex(object
, zend_std_get_properties(object
));
165 HashTable
*php_pq_object_properties(zval
*object
)
167 return php_pq_object_properties_ex(Z_OBJ_P(object
), zend_std_get_properties(object
));
171 static inline HashTable
*php_pq_object_get_gc_ex(zend_object
*object
, HashTable
*props
, zval
**table
, int *n
)
173 struct apply_pi_to_ht_arg arg
= {NULL
};
175 arg
.pq_obj
= PHP_PQ_OBJ(NULL
, object
);
176 arg
.ht
= &arg
.pq_obj
->gc
;
179 zend_hash_clean(arg
.ht
);
180 zend_hash_copy(arg
.ht
, props
, NULL
);
181 zend_hash_apply_with_argument(&arg
.pq_obj
->zo
.ce
->properties_info
, apply_pi_to_ht
, &arg
);
188 #if PHP_VERSION_ID >= 80000
189 HashTable
*php_pq_object_get_gc(zend_object
*object
, zval
**table
, int *n
)
191 return php_pq_object_get_gc_ex(object
, zend_std_get_properties(object
), table
, n
);
194 HashTable
*php_pq_object_get_gc(zval
*object
, zval
**table
, int *n
)
196 return php_pq_object_get_gc_ex(Z_OBJ_P(object
), zend_std_get_properties(object
), table
, n
);
200 zend_class_entry
*ancestor(zend_class_entry
*ce
)
208 static inline int php_pq_object_read_prop_ex(zend_object
*object
, zend_string
*member
, int type
, zval
*return_value
)
210 php_pq_object_t
*obj
= PHP_PQ_OBJ(NULL
, object
);
211 php_pq_object_prophandler_t
*handler
;
214 php_error(E_RECOVERABLE_ERROR
, "%s not initialized", ancestor(obj
->zo
.ce
)->name
->val
);
215 } else if (!(handler
= zend_hash_find_ptr(obj
->prophandler
, member
)) || !handler
->read
) {
216 /* default handler */
217 } else if (type
!= BP_VAR_R
) {
218 php_error(E_WARNING
, "Cannot access %s properties by reference or array key/index", ancestor(obj
->zo
.ce
)->name
->val
);
220 handler
->read(obj
, return_value
);
226 #if PHP_VERSION_ID >= 80000
227 zval
*php_pq_object_read_prop(zend_object
*object
, zend_string
*member
, int type
, void **cache_slot
, zval
*tmp
)
229 if (SUCCESS
!= php_pq_object_read_prop_ex(object
, member
, type
, tmp
)) {
230 return zend_std_read_property(object
, member
, type
, cache_slot
, tmp
);
233 zend_std_write_property(object
, member
, tmp
, cache_slot
);
241 zval
*php_pq_object_read_prop(zval
*object
, zval
*member
, int type
, void **cache_slot
, zval
*tmp
)
243 zend_string
*member_str
= zval_get_string(member
);
245 if (SUCCESS
!= php_pq_object_read_prop_ex(Z_OBJ_P(object
), member_str
, type
, tmp
)) {
246 zend_string_release(member_str
);
247 return zend_get_std_object_handlers()->read_property(object
, member
, type
, cache_slot
, tmp
);
249 zend_string_release(member_str
);
251 zend_std_write_property(object
, member
, tmp
, cache_slot
);
260 static inline int php_pq_object_write_prop_ex(zend_object
*object
, zend_string
*member
, zval
*value
)
262 php_pq_object_t
*obj
= PHP_PQ_OBJ(NULL
, object
);
263 php_pq_object_prophandler_t
*handler
;
266 php_error(E_RECOVERABLE_ERROR
, "%s not initialized", ancestor(obj
->zo
.ce
)->name
->val
);
267 } else if (!(handler
= zend_hash_find_ptr(obj
->prophandler
, member
))) {
268 /* default handler */
270 if (handler
->write
) {
271 handler
->write(obj
, value
);
277 #if PHP_VERSION_ID >= 80000
278 zval
*php_pq_object_write_prop(zend_object
*object
, zend_string
*member
, zval
*value
, void **cache_slot
)
280 if (SUCCESS
!= php_pq_object_write_prop_ex(object
, member
, value
)) {
281 return zend_std_write_property(object
, member
, value
, cache_slot
);
285 #elif PHP_VERSION_ID >= 70400
286 zval
*php_pq_object_write_prop(zval
*object
, zval
*member
, zval
*value
, void **cache_slot
)
288 zend_string
*member_str
= zval_get_string(member
);
289 if (SUCCESS
!= php_pq_object_write_prop_ex(Z_OBJ_P(object
), member_str
, value
)) {
290 value
= zend_std_write_property(object
, member
, value
, cache_slot
);
292 zend_string_release(member_str
);
296 void php_pq_object_write_prop(zval
*object
, zval
*member
, zval
*value
, void **cache_slot
)
298 zend_string
*member_str
= zval_get_string(member
);
299 if (SUCCESS
!= php_pq_object_write_prop_ex(Z_OBJ_P(object
), member_str
, value
)) {
300 zend_std_write_property(object
, member
, value
, cache_slot
);
302 zend_string_release(member_str
);
306 #if PHP_VERSION_ID >= 80000
307 zval
*php_pq_object_get_prop_ptr_null(zend_object
*object
, zend_string
*member
, int type
, void **cache_slot
)
312 zval
*php_pq_object_get_prop_ptr_null(zval
*object
, zval
*member
, int type
, void **cache_slot
)
318 void php_pq_object_prophandler_dtor(zval
*zv
) {
319 pefree(Z_PTR_P(zv
), 1);