ae22738fc4f37afa4c240d26914280acfae4f0cc
[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 inline int php_pq_object_read_prop_ex(zend_object *object, zend_string *member, int type, zval *return_value);
87
88 static int apply_pi_to_ht(zval *p, void *a)
89 {
90 zend_property_info *pi = Z_PTR_P(p);
91 struct apply_pi_to_ht_arg *arg = a;
92
93 if (arg->gc) {
94 php_pq_object_prophandler_t *handler;
95
96 if ((handler = zend_hash_find_ptr(arg->pq_obj->prophandler, pi->name)) && handler->gc) {
97 zval return_value;
98
99 ZVAL_ARR(&return_value, arg->ht);
100 handler->gc(arg->pq_obj, &return_value);
101 }
102 } else {
103 zval tmp_prop, *property;
104 #if PHP_VERSION_ID < 80000
105 zval zobj, zprop;
106
107 ZVAL_OBJ(&zobj, &arg->pq_obj->zo);
108 ZVAL_STR(&zprop, pi->name);
109 property = php_pq_object_read_prop(&zobj, &zprop, BP_VAR_R, NULL, &tmp_prop);
110 #else
111 property = php_pq_object_read_prop(&arg->pq_obj->zo, pi->name, BP_VAR_R, NULL, &tmp_prop);
112 #endif
113 zend_hash_update(arg->ht, pi->name, property);
114 }
115
116 return ZEND_HASH_APPLY_KEEP;
117 }
118
119 static inline HashTable *php_pq_object_debug_info_ex(zend_object *object, int *temp)
120 {
121 struct apply_pi_to_ht_arg arg = {NULL};
122
123 *temp = 1;
124 ALLOC_HASHTABLE(arg.ht);
125 ZEND_INIT_SYMTABLE(arg.ht);
126
127 arg.pq_obj = PHP_PQ_OBJ(NULL, object);
128 arg.gc = 0;
129
130 zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg);
131
132 return arg.ht;
133 }
134 #if PHP_VERSION_ID >= 80000
135 HashTable *php_pq_object_debug_info(zend_object *object, int *temp)
136 {
137 return php_pq_object_debug_info_ex(object, temp);
138 }
139 #else
140 HashTable *php_pq_object_debug_info(zval *object, int *temp)
141 {
142 return php_pq_object_debug_info_ex(Z_OBJ_P(object), temp);
143 }
144 #endif
145
146 static inline HashTable *php_pq_object_properties_ex(zend_object *object, HashTable *props)
147 {
148 struct apply_pi_to_ht_arg arg = {NULL};
149
150 arg.ht = props;
151 arg.pq_obj = PHP_PQ_OBJ(NULL, object);
152 arg.gc = 0;
153
154 zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg);
155
156 return arg.ht;
157 }
158 #if PHP_VERSION_ID >= 80000
159 HashTable *php_pq_object_properties(zend_object *object)
160 {
161 return php_pq_object_properties_ex(object, zend_std_get_properties(object));
162 }
163 #else
164 HashTable *php_pq_object_properties(zval *object)
165 {
166 return php_pq_object_properties_ex(Z_OBJ_P(object), zend_std_get_properties(object));
167 }
168 #endif
169
170 static inline HashTable *php_pq_object_get_gc_ex(zend_object *object, HashTable *props, zval **table, int *n)
171 {
172 struct apply_pi_to_ht_arg arg = {NULL};
173
174 arg.pq_obj = PHP_PQ_OBJ(NULL, object);
175 arg.ht = &arg.pq_obj->gc;
176 arg.gc = 1;
177
178 zend_hash_clean(arg.ht);
179 zend_hash_copy(arg.ht, props, NULL);
180 zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg);
181
182 *table = NULL;
183 *n = 0;
184
185 return arg.ht;
186 }
187 #if PHP_VERSION_ID >= 80000
188 HashTable *php_pq_object_get_gc(zend_object *object, zval **table, int *n)
189 {
190 return php_pq_object_get_gc_ex(object, zend_std_get_properties(object), table, n);
191 }
192 #else
193 HashTable *php_pq_object_get_gc(zval *object, zval **table, int *n)
194 {
195 return php_pq_object_get_gc_ex(Z_OBJ_P(object), zend_std_get_properties(object), table, n);
196 }
197 #endif
198
199 zend_class_entry *ancestor(zend_class_entry *ce)
200 {
201 while (ce->parent) {
202 ce = ce->parent;
203 }
204 return ce;
205 }
206
207 static inline int php_pq_object_read_prop_ex(zend_object *object, zend_string *member, int type, zval *return_value)
208 {
209 php_pq_object_t *obj = PHP_PQ_OBJ(NULL, object);
210 php_pq_object_prophandler_t *handler;
211
212 if (!obj->intern) {
213 php_error(E_RECOVERABLE_ERROR, "%s not initialized", ancestor(obj->zo.ce)->name->val);
214 } else if (!(handler = zend_hash_find_ptr(obj->prophandler, member)) || !handler->read) {
215 /* default handler */
216 } else if (type != BP_VAR_R) {
217 php_error(E_WARNING, "Cannot access %s properties by reference or array key/index", ancestor(obj->zo.ce)->name->val);
218 } else {
219 handler->read(obj, return_value);
220 return SUCCESS;
221 }
222
223 return FAILURE;
224 }
225 #if PHP_VERSION_ID >= 80000
226 zval *php_pq_object_read_prop(zend_object *object, zend_string *member, int type, void **cache_slot, zval *tmp)
227 {
228 if (SUCCESS != php_pq_object_read_prop_ex(object, member, type, tmp)) {
229 return zend_std_read_property(object, member, type, cache_slot, tmp);
230 }
231
232 tmp = zend_std_write_property(object, member, tmp, cache_slot);
233 if (cache_slot) {
234 *cache_slot = NULL;
235 }
236 return tmp;
237 }
238 #else
239 zval *php_pq_object_read_prop(zval *object, zval *member, int type, void **cache_slot, zval *tmp)
240 {
241 zend_string *member_str = zval_get_string(member);
242
243 if (SUCCESS != php_pq_object_read_prop_ex(Z_OBJ_P(object), member_str, type, tmp)) {
244 zend_string_release(member_str);
245 return zend_std_read_property(object, member, type, cache_slot, tmp);
246 }
247 zend_string_release(member_str);
248
249 #if PHP_VERSON_ID >= 70400
250 tmp =
251 #endif
252 zend_std_write_property(object, member, tmp, cache_slot);
253 if (cache_slot) {
254 *cache_slot = NULL;
255 }
256 return tmp;
257 }
258 #endif
259
260 static inline int php_pq_object_write_prop_ex(zend_object *object, zend_string *member, zval *value)
261 {
262 php_pq_object_t *obj = PHP_PQ_OBJ(NULL, object);
263 php_pq_object_prophandler_t *handler;
264
265 if (!obj->intern) {
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 */
269 } else {
270 if (handler->write) {
271 handler->write(obj, value);
272 }
273 return SUCCESS;
274 }
275 return FAILURE;
276 }
277 #if PHP_VERSION_ID >= 80000
278 zval *php_pq_object_write_prop(zend_object *object, zend_string *member, zval *value, void **cache_slot)
279 {
280 if (SUCCESS != php_pq_object_write_prop_ex(object, member, value)) {
281 return zend_std_write_property(object, member, value, cache_slot);
282 }
283 return value;
284 }
285 #elif PHP_VERSION_ID >= 70400
286 zval *php_pq_object_write_prop(zval *object, zval *member, zval *value, void **cache_slot)
287 {
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);
291 }
292 zend_string_release(member_str);
293 return value;
294 }
295 #else
296 void php_pq_object_write_prop(zval *object, zval *member, zval *value, void **cache_slot)
297 {
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);
301 }
302 zend_string_release(member_str);
303 }
304 #endif
305
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)
308 {
309 return NULL;
310 }
311 #else
312 zval *php_pq_object_get_prop_ptr_null(zval *object, zval *member, int type, void **cache_slot)
313 {
314 return NULL;
315 }
316 #endif
317
318 void php_pq_object_prophandler_dtor(zval *zv) {
319 pefree(Z_PTR_P(zv), 1);
320 }
321