23e592918a3c16962e874a295abb3fb3e98bb019
[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 HashTable *php_pq_object_properties_80(zend_object *object)
131 {
132 struct apply_pi_to_ht_arg arg = {NULL};
133
134 arg.ht = zend_get_std_object_handlers()->get_properties(object);
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 }
143 HashTable *php_pq_object_properties_70(zval *object)
144 {
145 return php_pq_object_properties_80(Z_OBJ_P(object));
146 }
147
148 HashTable *php_pq_object_get_gc_80(zend_object *object, zval **table, int *n)
149 {
150 struct apply_pi_to_ht_arg arg = {NULL};
151
152 arg.pq_obj = PHP_PQ_OBJ(NULL, object);
153 arg.ht = &arg.pq_obj->gc;
154 arg.gc = 1;
155
156 zend_hash_clean(arg.ht);
157 zend_hash_copy(arg.ht, zend_std_get_properties(object), NULL);
158 zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg);
159
160 *table = NULL;
161 *n = 0;
162
163 return arg.ht;
164 }
165 HashTable *php_pq_object_get_gc_70(zval *object, zval **table, int *n)
166 {
167 return php_pq_object_get_gc_80(Z_OBJ_P(object), table, n);
168 }
169
170 zend_class_entry *ancestor(zend_class_entry *ce)
171 {
172 while (ce->parent) {
173 ce = ce->parent;
174 }
175 return ce;
176 }
177
178 zval *php_pq_object_read_prop_80(zend_object *object, zend_string *member, int type, void **cache_slot, zval *tmp)
179 {
180 php_pq_object_t *obj = PHP_PQ_OBJ(NULL, object);
181 php_pq_object_prophandler_t *handler;
182 zval *return_value = NULL;
183
184 return_value = zend_get_std_object_handlers()->read_property(object, member, type, cache_slot, tmp);
185
186 if (!obj->intern) {
187 php_error(E_RECOVERABLE_ERROR, "%s not initialized", ancestor(obj->zo.ce)->name->val);
188 } else if (!(handler = zend_hash_find_ptr(obj->prophandler, member)) || !handler->read) {
189 /* default handler */
190 } else if (type != BP_VAR_R) {
191 php_error(E_WARNING, "Cannot access %s properties by reference or array key/index", ancestor(obj->zo.ce)->name->val);
192 } else {
193 handler->read(obj, tmp);
194 zend_get_std_object_handlers()->write_property(object, member, tmp, cache_slot);
195 return_value = tmp;
196
197 if (cache_slot) {
198 *cache_slot = NULL;
199 }
200 }
201
202 return return_value;
203 }
204 zval *php_pq_object_read_prop_74(zval *object, zval *member, int type, void **cache_slot, zval *tmp)
205 {
206 zend_string *member_str = zval_get_string(member);
207 zval *return_value = php_pq_object_read_prop_80(Z_OBJ_P(object), member_str, type, cache_slot, tmp);
208 zend_string_release(member_str);
209 return return_value;
210 }
211 void php_pq_object_read_prop_70(zval *object, zval *member, int type, void **cache_slot, zval *tmp)
212 {
213 (void) php_pq_object_read_prop_74(object, member, type, cache_slot, tmp);
214 }
215
216 zval *php_pq_object_write_prop_80(zend_object *object, zend_string *member, zval *value, void **cache_slot)
217 {
218 php_pq_object_t *obj = PHP_PQ_OBJ(NULL, object);
219 php_pq_object_prophandler_t *handler;
220
221 if (!obj->intern) {
222 php_error(E_RECOVERABLE_ERROR, "%s not initialized", ancestor(obj->zo.ce)->name->val);
223 zend_get_std_object_handlers()->write_property(object, member, value, cache_slot);
224 } else if ((handler = zend_hash_find_ptr(obj->prophandler, member))) {
225 if (handler->write) {
226 handler->write(obj, value);
227 }
228 } else {
229 zend_get_std_object_handlers()->write_property(object, member, value, cache_slot);
230 }
231 return value;
232 }
233 zval *php_pq_object_write_prop_74(zval *object, zval *member, zval *value, void **cache_slot)
234 {
235 zend_string *member_str = zval_get_string(member);
236 zval *return_value = php_pq_object_write_prop_80(Z_OBJ_P(object), member_str, value, cache_slot);
237 zend_string_release(member_str);
238 return return_value;
239 }
240 void php_pq_object_write_prop_70(zval *object, zval *member, zval *value, void **cache_slot)
241 {
242 (void) php_pq_object_write_prop_74(object, member, value, cache_slot);
243 }
244
245 zval *php_pq_object_get_prop_ptr_null_80(zend_object *object, zend_string *member, int type, void **cache_slot)
246 {
247 return NULL;
248 }
249 zval *php_pq_object_get_prop_ptr_null_70(zval *object, zval *member, int type, void **cache_slot)
250 {
251 return NULL;
252 }
253
254 void php_pq_object_prophandler_dtor(zval *zv) {
255 pefree(Z_PTR_P(zv), 1);
256 }
257