restore PHP 7 compatibility
[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
21 void *php_pq_object_create(zend_class_entry *ce, void *intern, size_t obj_size, zend_object_handlers *oh, HashTable *ph)
22 {
23 php_pq_object_t *o = ecalloc(1, obj_size + zend_object_properties_size(ce));
24
25 zend_object_std_init(&o->zo, ce);
26 object_properties_init(&o->zo, ce);
27 o->zo.handlers = oh;
28 o->intern = intern;
29 o->prophandler = ph;
30
31 zend_hash_init(&o->gc, 0, NULL, NULL, 0);
32
33 return o;
34 }
35
36 void php_pq_object_dtor(zend_object *o)
37 {
38 php_pq_object_t *obj = PHP_PQ_OBJ(NULL, o);
39
40 zend_hash_destroy(&obj->gc);
41 zend_object_std_dtor(o);
42 }
43
44 void php_pq_object_to_zval(void *o, zval *zv)
45 {
46 php_pq_object_t *obj = o;
47
48 ZVAL_OBJ(zv, &obj->zo);
49 Z_ADDREF_P(zv);
50 }
51
52 void php_pq_object_to_zval_no_addref(void *o, zval *zv)
53 {
54 php_pq_object_t *obj = o;
55
56 ZVAL_OBJ(zv, &obj->zo);
57 }
58
59 void php_pq_object_addref(void *o)
60 {
61 php_pq_object_t *obj = o;
62 #ifdef GC_ADDREF
63 GC_ADDREF(&obj->zo);
64 #else
65 ++GC_REFCOUNT(&obj->zo);
66 #endif
67 }
68
69 void php_pq_object_delref(void *o)
70 {
71 php_pq_object_t *obj = o;
72 zval tmp;
73
74 /* this should gc immediately */
75 ZVAL_OBJ(&tmp, &obj->zo);
76 zval_ptr_dtor(&tmp);
77 }
78
79 struct apply_pi_to_ht_arg {
80 HashTable *ht;
81 php_pq_object_t *pq_obj;
82 unsigned gc:1;
83 };
84
85 static int apply_pi_to_ht(zval *p, void *a)
86 {
87 zend_property_info *pi = Z_PTR_P(p);
88 struct apply_pi_to_ht_arg *arg = a;
89
90 if (arg->gc) {
91 php_pq_object_prophandler_t *handler;
92
93 if ((handler = zend_hash_find_ptr(arg->pq_obj->prophandler, pi->name)) && handler->gc) {
94 zval member, return_value;
95
96 ZVAL_STR(&member, pi->name);
97 ZVAL_ARR(&return_value, arg->ht);
98 handler->gc(arg->pq_obj, &return_value);
99 }
100 } else {
101 zval tmp_prop, *property = NULL;
102
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);
105 }
106
107 return ZEND_HASH_APPLY_KEEP;
108 }
109
110 HashTable *php_pq_object_debug_info_80(zend_object *object, int *temp)
111 {
112 struct apply_pi_to_ht_arg arg = {NULL};
113
114 *temp = 1;
115 ALLOC_HASHTABLE(arg.ht);
116 ZEND_INIT_SYMTABLE(arg.ht);
117
118 arg.pq_obj = PHP_PQ_OBJ(NULL, object);
119 arg.gc = 0;
120
121 zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg);
122
123 return arg.ht;
124 }
125 HashTable *php_pq_object_debug_info_70(zval *object, int *temp)
126 {
127 return php_pq_object_debug_info_80(Z_OBJ_P(object), temp);
128 }
129
130 static inline HashTable *php_pq_object_properties_ex(zend_object *object, HashTable *props)
131 {
132 struct apply_pi_to_ht_arg arg = {NULL};
133
134 arg.ht = props;
135 arg.pq_obj = PHP_PQ_OBJ(NULL, object);
136 arg.gc = 0;
137
138 zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg);
139
140 return arg.ht;
141 }
142 HashTable *php_pq_object_properties_80(zend_object *object)
143 {
144 return php_pq_object_properties_ex(object, zend_std_get_properties(object));
145 }
146 HashTable *php_pq_object_properties_70(zval *object)
147 {
148 return php_pq_object_properties_ex(Z_OBJ_P(object), zend_std_get_properties(object));
149 }
150
151 static inline HashTable *php_pq_object_get_gc_ex(zend_object *object, HashTable *props, zval **table, int *n)
152 {
153 struct apply_pi_to_ht_arg arg = {NULL};
154
155 arg.pq_obj = PHP_PQ_OBJ(NULL, object);
156 arg.ht = &arg.pq_obj->gc;
157 arg.gc = 1;
158
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);
162
163 *table = NULL;
164 *n = 0;
165
166 return arg.ht;
167 }
168 HashTable *php_pq_object_get_gc_80(zend_object *object, zval **table, int *n)
169 {
170 return php_pq_object_get_gc_ex(object, zend_std_get_properties(object), table, n);
171 }
172 HashTable *php_pq_object_get_gc_70(zval *object, zval **table, int *n)
173 {
174 return php_pq_object_get_gc_ex(Z_OBJ_P(object), zend_std_get_properties(object), table, n);
175 }
176
177 zend_class_entry *ancestor(zend_class_entry *ce)
178 {
179 while (ce->parent) {
180 ce = ce->parent;
181 }
182 return ce;
183 }
184
185 zval *php_pq_object_read_prop_ex(zend_object *object, zend_string *member, int type, void **cache_slot, zval *tmp, zval *def)
186 {
187 php_pq_object_t *obj = PHP_PQ_OBJ(NULL, object);
188 php_pq_object_prophandler_t *handler;
189 zval *return_value = def;
190
191 if (!obj->intern) {
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);
197 } else {
198 handler->read(obj, tmp);
199 zend_get_std_object_handlers()->write_property(object, member, tmp, cache_slot);
200 return_value = tmp;
201
202 if (cache_slot) {
203 *cache_slot = NULL;
204 }
205 }
206
207 return return_value;
208 }
209 zval *php_pq_object_read_prop_80(zend_object *object, zend_string *member, int type, void **cache_slot, zval *tmp)
210 {
211 return php_pq_object_read_prop_ex(object, member, type, cache_slot, tmp, zend_std_read_property(object, member, type, cache_slot, tmp));
212 }
213 zval *php_pq_object_read_prop_70(zval *object, zval *member, int type, void **cache_slot, zval *tmp)
214 {
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);
218 return return_value;
219 }
220
221 zval *php_pq_object_write_prop_ex(zend_object *object, zend_string *member, zval *value, void **cache_slot, int *update_std)
222 {
223 php_pq_object_t *obj = PHP_PQ_OBJ(NULL, object);
224 php_pq_object_prophandler_t *handler;
225
226 if (!obj->intern) {
227 php_error(E_RECOVERABLE_ERROR, "%s not initialized", ancestor(obj->zo.ce)->name->val);
228 *update_std = 1;
229 } else if ((handler = zend_hash_find_ptr(obj->prophandler, member))) {
230 if (handler->write) {
231 handler->write(obj, value);
232 }
233 *update_std = 0;
234 } else {
235 *update_std = 1;
236 }
237 return value;
238 }
239 zval *php_pq_object_write_prop_80(zend_object *object, zend_string *member, zval *value, void **cache_slot)
240 {
241 int update_std = 0;
242 zval *return_value = php_pq_object_write_prop_ex(object, member, value, cache_slot, &update_std);
243 if (update_std) {
244 return_value = zend_std_write_property(object, member, value, cache_slot);
245 }
246 return return_value;
247 }
248 zval *php_pq_object_write_prop_74(zval *object, zval *member, zval *value, void **cache_slot)
249 {
250 int update_std = 0;
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);
254 if (update_std) {
255 return_value = zend_std_write_property(object, member, value, cache_slot);
256 }
257 return return_value;
258 }
259 void php_pq_object_write_prop_70(zval *object, zval *member, zval *value, void **cache_slot)
260 {
261 (void) php_pq_object_write_prop_74(object, member, value, cache_slot);
262 }
263
264 zval *php_pq_object_get_prop_ptr_null_80(zend_object *object, zend_string *member, int type, void **cache_slot)
265 {
266 return NULL;
267 }
268 zval *php_pq_object_get_prop_ptr_null_70(zval *object, zval *member, int type, void **cache_slot)
269 {
270 return NULL;
271 }
272
273 void php_pq_object_prophandler_dtor(zval *zv) {
274 pefree(Z_PTR_P(zv), 1);
275 }
276