prepare v2.2.3
[m6w6/ext-pq] / src / php_pq_object.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: pq |
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 +--------------------------------------------------------------------+
11 */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include <php.h>
18
19 #include "php_pq_object.h"
20 #include "php_pq_misc.h"
21
22 void *php_pq_object_create(zend_class_entry *ce, void *intern, size_t obj_size, zend_object_handlers *oh, HashTable *ph)
23 {
24 php_pq_object_t *o = ecalloc(1, obj_size + zend_object_properties_size(ce));
25
26 zend_object_std_init(&o->zo, ce);
27 object_properties_init(&o->zo, ce);
28 o->zo.handlers = oh;
29 o->intern = intern;
30 o->prophandler = ph;
31
32 zend_hash_init(&o->gc, 0, NULL, NULL, 0);
33
34 return o;
35 }
36
37 void php_pq_object_dtor(zend_object *o)
38 {
39 php_pq_object_t *obj = PHP_PQ_OBJ(NULL, o);
40
41 zend_hash_destroy(&obj->gc);
42 zend_object_std_dtor(o);
43 }
44
45 void php_pq_object_to_zval(void *o, zval *zv)
46 {
47 php_pq_object_t *obj = o;
48
49 ZVAL_OBJ(zv, &obj->zo);
50 Z_ADDREF_P(zv);
51 }
52
53 void php_pq_object_to_zval_no_addref(void *o, zval *zv)
54 {
55 php_pq_object_t *obj = o;
56
57 ZVAL_OBJ(zv, &obj->zo);
58 }
59
60 void php_pq_object_addref(void *o)
61 {
62 php_pq_object_t *obj = o;
63 #ifdef GC_ADDREF
64 GC_ADDREF(&obj->zo);
65 #else
66 ++GC_REFCOUNT(&obj->zo);
67 #endif
68 }
69
70 void php_pq_object_delref(void *o)
71 {
72 php_pq_object_t *obj = o;
73 zval tmp;
74
75 /* this should gc immediately */
76 ZVAL_OBJ(&tmp, &obj->zo);
77 zval_ptr_dtor(&tmp);
78 }
79
80 struct apply_pi_to_ht_arg {
81 HashTable *ht;
82 php_pq_object_t *pq_obj;
83 unsigned gc:1;
84 };
85
86 static int apply_pi_to_ht(zval *p, void *a)
87 {
88 zend_property_info *pi = Z_PTR_P(p);
89 struct apply_pi_to_ht_arg *arg = a;
90
91 if (arg->gc) {
92 php_pq_object_prophandler_t *handler;
93
94 if ((handler = zend_hash_find_ptr(arg->pq_obj->prophandler, pi->name)) && handler->gc) {
95 zval return_value;
96
97 ZVAL_ARR(&return_value, arg->ht);
98 handler->gc(arg->pq_obj, &return_value);
99 }
100 } else {
101 zval tmp_prop, *property;
102 #if PHP_VERSION_ID < 80000
103 zval zobj;
104
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);
108 # else
109 property = zend_read_property_ex(arg->pq_obj->zo.ce, &zobj, pi->name, 0, &tmp_prop);
110 # endif
111 #else
112 property = zend_read_property_ex(arg->pq_obj->zo.ce, &arg->pq_obj->zo, pi->name, 0, &tmp_prop);
113 #endif
114 zend_hash_update(arg->ht, pi->name, property);
115 }
116
117 return ZEND_HASH_APPLY_KEEP;
118 }
119
120 static inline HashTable *php_pq_object_debug_info_ex(zend_object *object, int *temp)
121 {
122 struct apply_pi_to_ht_arg arg = {NULL};
123
124 *temp = 1;
125 ALLOC_HASHTABLE(arg.ht);
126 ZEND_INIT_SYMTABLE(arg.ht);
127
128 arg.pq_obj = PHP_PQ_OBJ(NULL, object);
129 arg.gc = 0;
130
131 zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg);
132
133 return arg.ht;
134 }
135 #if PHP_VERSION_ID >= 80000
136 HashTable *php_pq_object_debug_info(zend_object *object, int *temp)
137 {
138 return php_pq_object_debug_info_ex(object, temp);
139 }
140 #else
141 HashTable *php_pq_object_debug_info(zval *object, int *temp)
142 {
143 return php_pq_object_debug_info_ex(Z_OBJ_P(object), temp);
144 }
145 #endif
146
147 static inline HashTable *php_pq_object_properties_ex(zend_object *object, HashTable *props)
148 {
149 struct apply_pi_to_ht_arg arg = {NULL};
150
151 arg.ht = props;
152 arg.pq_obj = PHP_PQ_OBJ(NULL, object);
153 arg.gc = 0;
154
155 zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg);
156
157 return arg.ht;
158 }
159 #if PHP_VERSION_ID >= 80000
160 HashTable *php_pq_object_properties(zend_object *object)
161 {
162 return php_pq_object_properties_ex(object, zend_std_get_properties(object));
163 }
164 #else
165 HashTable *php_pq_object_properties(zval *object)
166 {
167 return php_pq_object_properties_ex(Z_OBJ_P(object), zend_std_get_properties(object));
168 }
169 #endif
170
171 static inline HashTable *php_pq_object_get_gc_ex(zend_object *object, HashTable *props, zval **table, int *n)
172 {
173 struct apply_pi_to_ht_arg arg = {NULL};
174
175 arg.pq_obj = PHP_PQ_OBJ(NULL, object);
176 arg.ht = &arg.pq_obj->gc;
177 arg.gc = 1;
178
179 if (GC_REFCOUNT(arg.ht) == 1) {
180 zend_hash_clean(arg.ht);
181 zend_hash_copy(arg.ht, props, NULL);
182 zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg);
183 }
184
185 *table = NULL;
186 *n = 0;
187
188 return arg.ht;
189 }
190 #if PHP_VERSION_ID >= 80000
191 HashTable *php_pq_object_get_gc(zend_object *object, zval **table, int *n)
192 {
193 return php_pq_object_get_gc_ex(object, zend_std_get_properties(object), table, n);
194 }
195 #else
196 HashTable *php_pq_object_get_gc(zval *object, zval **table, int *n)
197 {
198 return php_pq_object_get_gc_ex(Z_OBJ_P(object), zend_std_get_properties(object), table, n);
199 }
200 #endif
201
202 zend_class_entry *ancestor(zend_class_entry *ce)
203 {
204 while (ce->parent) {
205 ce = ce->parent;
206 }
207 return ce;
208 }
209
210 static inline int php_pq_object_read_prop_ex(zend_object *object, zend_string *member, int type, zval *return_value)
211 {
212 php_pq_object_t *obj = PHP_PQ_OBJ(NULL, object);
213 php_pq_object_prophandler_t *handler;
214
215 if (!obj->intern) {
216 php_error(E_RECOVERABLE_ERROR, "%s not initialized", ancestor(obj->zo.ce)->name->val);
217 } else if (!(handler = zend_hash_find_ptr(obj->prophandler, member)) || !handler->read) {
218 /* default handler */
219 } else if (type != BP_VAR_R) {
220 php_error(E_WARNING, "Cannot access %s properties by reference or array key/index", ancestor(obj->zo.ce)->name->val);
221 } else {
222 handler->read(obj, return_value);
223 return SUCCESS;
224 }
225
226 return FAILURE;
227 }
228 #if PHP_VERSION_ID >= 80000
229 zval *php_pq_object_read_prop(zend_object *object, zend_string *member, int type, void **cache_slot, zval *tmp)
230 {
231 if (SUCCESS != php_pq_object_read_prop_ex(object, member, type, tmp)) {
232 return zend_std_read_property(object, member, type, cache_slot, tmp);
233 }
234
235 zend_std_write_property(object, member, tmp, cache_slot);
236
237 if (cache_slot) {
238 *cache_slot = NULL;
239 }
240 return tmp;
241 }
242 #else
243 zval *php_pq_object_read_prop(zval *object, zval *member, int type, void **cache_slot, zval *tmp)
244 {
245 zend_string *member_str = zval_get_string(member);
246
247 if (SUCCESS != php_pq_object_read_prop_ex(Z_OBJ_P(object), member_str, type, tmp)) {
248 zend_string_release(member_str);
249 return zend_get_std_object_handlers()->read_property(object, member, type, cache_slot, tmp);
250 }
251 zend_string_release(member_str);
252
253 zend_std_write_property(object, member, tmp, cache_slot);
254
255 if (cache_slot) {
256 *cache_slot = NULL;
257 }
258 return tmp;
259 }
260 #endif
261
262 static inline int php_pq_object_write_prop_ex(zend_object *object, zend_string *member, zval *value)
263 {
264 php_pq_object_t *obj = PHP_PQ_OBJ(NULL, object);
265 php_pq_object_prophandler_t *handler;
266
267 if (!obj->intern) {
268 php_error(E_RECOVERABLE_ERROR, "%s not initialized", ancestor(obj->zo.ce)->name->val);
269 } else if (!(handler = zend_hash_find_ptr(obj->prophandler, member))) {
270 /* default handler */
271 } else {
272 if (handler->write) {
273 handler->write(obj, value);
274 }
275 return SUCCESS;
276 }
277 return FAILURE;
278 }
279 #if PHP_VERSION_ID >= 80000
280 zval *php_pq_object_write_prop(zend_object *object, zend_string *member, zval *value, void **cache_slot)
281 {
282 if (SUCCESS != php_pq_object_write_prop_ex(object, member, value)) {
283 return zend_std_write_property(object, member, value, cache_slot);
284 }
285 return value;
286 }
287 #elif PHP_VERSION_ID >= 70400
288 zval *php_pq_object_write_prop(zval *object, zval *member, zval *value, void **cache_slot)
289 {
290 zend_string *member_str = zval_get_string(member);
291 if (SUCCESS != php_pq_object_write_prop_ex(Z_OBJ_P(object), member_str, value)) {
292 value = zend_std_write_property(object, member, value, cache_slot);
293 }
294 zend_string_release(member_str);
295 return value;
296 }
297 #else
298 void php_pq_object_write_prop(zval *object, zval *member, zval *value, void **cache_slot)
299 {
300 zend_string *member_str = zval_get_string(member);
301 if (SUCCESS != php_pq_object_write_prop_ex(Z_OBJ_P(object), member_str, value)) {
302 zend_std_write_property(object, member, value, cache_slot);
303 }
304 zend_string_release(member_str);
305 }
306 #endif
307
308 #if PHP_VERSION_ID >= 80000
309 zval *php_pq_object_get_prop_ptr_null(zend_object *object, zend_string *member, int type, void **cache_slot)
310 {
311 return NULL;
312 }
313 #else
314 zval *php_pq_object_get_prop_ptr_null(zval *object, zval *member, int type, void **cache_slot)
315 {
316 return NULL;
317 }
318 #endif
319
320 void php_pq_object_prophandler_dtor(zval *zv) {
321 pefree(Z_PTR_P(zv), 1);
322 }
323