Merge branch 'v1.1.x'
[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 ++GC_REFCOUNT(&obj->zo);
63 }
64
65 void php_pq_object_delref(void *o)
66 {
67 php_pq_object_t *obj = o;
68 zval tmp;
69
70 /* this should gc immediately */
71 ZVAL_OBJ(&tmp, &obj->zo);
72 zval_ptr_dtor(&tmp);
73 }
74
75 struct apply_pi_to_ht_arg {
76 HashTable *ht;
77 zval *object;
78 php_pq_object_t *pq_obj;
79 unsigned gc:1;
80 };
81
82 static int apply_pi_to_ht(zval *p, void *a)
83 {
84 zend_property_info *pi = Z_PTR_P(p);
85 struct apply_pi_to_ht_arg *arg = a;
86
87 if (arg->gc) {
88 php_pq_object_prophandler_t *handler;
89
90 if ((handler = zend_hash_find_ptr(arg->pq_obj->prophandler, pi->name)) && handler->gc) {
91 zval member, return_value;
92
93 ZVAL_STR(&member, pi->name);
94 ZVAL_ARR(&return_value, arg->ht);
95 handler->gc(arg->object, arg->pq_obj, &return_value);
96 }
97 } else {
98 zval tmp_prop, *property = NULL;
99
100 property = zend_read_property(arg->pq_obj->zo.ce, arg->object, pi->name->val, pi->name->len, 0, &tmp_prop);
101 zend_hash_update(arg->ht, pi->name, property);
102 }
103
104 return ZEND_HASH_APPLY_KEEP;
105 }
106
107 HashTable *php_pq_object_debug_info(zval *object, int *temp)
108 {
109 struct apply_pi_to_ht_arg arg = {NULL};
110
111 *temp = 1;
112 ALLOC_HASHTABLE(arg.ht);
113 ZEND_INIT_SYMTABLE(arg.ht);
114
115 arg.object = object;
116 arg.pq_obj = PHP_PQ_OBJ(object, NULL);
117 arg.gc = 0;
118
119 zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg);
120
121 return arg.ht;
122 }
123
124 HashTable *php_pq_object_properties(zval *object)
125 {
126 struct apply_pi_to_ht_arg arg = {NULL};
127
128 arg.ht = zend_get_std_object_handlers()->get_properties(object);
129 arg.object = object;
130 arg.pq_obj = PHP_PQ_OBJ(object, NULL);
131 arg.gc = 0;
132
133 zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg);
134
135 return arg.ht;
136 }
137
138 HashTable *php_pq_object_get_gc(zval *object, zval **table, int *n)
139 {
140 struct apply_pi_to_ht_arg arg = {NULL};
141
142 arg.object = object;
143 arg.pq_obj = PHP_PQ_OBJ(object, NULL);
144 arg.ht = &arg.pq_obj->gc;
145 arg.gc = 1;
146
147 zend_hash_clean(arg.ht);
148 zend_hash_copy(arg.ht, zend_std_get_properties(object), NULL);
149 zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg);
150
151 *table = NULL;
152 *n = 0;
153
154 return arg.ht;
155 }
156
157 zend_class_entry *ancestor(zend_class_entry *ce)
158 {
159 while (ce->parent) {
160 ce = ce->parent;
161 }
162 return ce;
163 }
164
165 zval *php_pq_object_read_prop(zval *object, zval *member, int type, void **cache_slot, zval *tmp)
166 {
167 php_pq_object_t *obj = PHP_PQ_OBJ(object, NULL);
168 php_pq_object_prophandler_t *handler;
169 zval *return_value = NULL;
170
171 return_value = zend_get_std_object_handlers()->read_property(object, member, type, cache_slot, tmp);
172
173 if (!obj->intern) {
174 php_error(E_RECOVERABLE_ERROR, "%s not initialized", ancestor(obj->zo.ce)->name->val);
175 } else if (!(handler = zend_hash_find_ptr(obj->prophandler, Z_STR_P(member))) || !handler->read) {
176 /* default handler */
177 } else if (type != BP_VAR_R) {
178 php_error(E_WARNING, "Cannot access %s properties by reference or array key/index", ancestor(obj->zo.ce)->name->val);
179 } else {
180 handler->read(object, obj, tmp);
181 zend_get_std_object_handlers()->write_property(object, member, tmp, cache_slot);
182 return_value = tmp;
183
184 /*
185 zval dtor;
186
187 ZVAL_COPY_VALUE(&dtor, return_value);
188
189 ZVAL_ZVAL(return_value, tmp, 0, 0);
190 zval_ptr_dtor(&dtor);
191
192 */
193
194 if (cache_slot) {
195 *cache_slot = NULL;
196 }
197 }
198
199 return return_value;
200 }
201
202 void php_pq_object_write_prop(zval *object, zval *member, zval *value, void **cache_slot)
203 {
204 php_pq_object_t *obj = PHP_PQ_OBJ(object, NULL);
205 php_pq_object_prophandler_t *handler;
206
207 if (!obj->intern) {
208 php_error(E_RECOVERABLE_ERROR, "%s not initialized", ancestor(obj->zo.ce)->name->val);
209 zend_get_std_object_handlers()->write_property(object, member, value, cache_slot);
210 } else if ((handler = zend_hash_find_ptr(obj->prophandler, Z_STR_P(member)))) {
211 if (handler->write) {
212 handler->write(object, obj, value);
213 }
214 } else {
215 zend_get_std_object_handlers()->write_property(object, member, value, cache_slot);
216 }
217 }
218
219 void php_pq_object_prophandler_dtor(zval *zv) {
220 pefree(Z_PTR_P(zv), 1);
221 }
222