PHP-7.4 compat
[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 zval *object;
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 member, return_value;
96
97 ZVAL_STR(&member, pi->name);
98 ZVAL_ARR(&return_value, arg->ht);
99 handler->gc(arg->object, arg->pq_obj, &return_value);
100 }
101 } else {
102 zval tmp_prop, *property = NULL;
103
104 property = zend_read_property(arg->pq_obj->zo.ce, arg->object, pi->name->val, pi->name->len, 0, &tmp_prop);
105 zend_hash_update(arg->ht, pi->name, property);
106 }
107
108 return ZEND_HASH_APPLY_KEEP;
109 }
110
111 HashTable *php_pq_object_debug_info(zval *object, int *temp)
112 {
113 struct apply_pi_to_ht_arg arg = {NULL};
114
115 *temp = 1;
116 ALLOC_HASHTABLE(arg.ht);
117 ZEND_INIT_SYMTABLE(arg.ht);
118
119 arg.object = object;
120 arg.pq_obj = PHP_PQ_OBJ(object, NULL);
121 arg.gc = 0;
122
123 zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg);
124
125 return arg.ht;
126 }
127
128 HashTable *php_pq_object_properties(zval *object)
129 {
130 struct apply_pi_to_ht_arg arg = {NULL};
131
132 arg.ht = zend_get_std_object_handlers()->get_properties(object);
133 arg.object = object;
134 arg.pq_obj = PHP_PQ_OBJ(object, NULL);
135 arg.gc = 0;
136
137 zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg);
138
139 return arg.ht;
140 }
141
142 HashTable *php_pq_object_get_gc(zval *object, zval **table, int *n)
143 {
144 struct apply_pi_to_ht_arg arg = {NULL};
145
146 arg.object = object;
147 arg.pq_obj = PHP_PQ_OBJ(object, NULL);
148 arg.ht = &arg.pq_obj->gc;
149 arg.gc = 1;
150
151 zend_hash_clean(arg.ht);
152 zend_hash_copy(arg.ht, zend_std_get_properties(object), NULL);
153 zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg);
154
155 *table = NULL;
156 *n = 0;
157
158 return arg.ht;
159 }
160
161 zend_class_entry *ancestor(zend_class_entry *ce)
162 {
163 while (ce->parent) {
164 ce = ce->parent;
165 }
166 return ce;
167 }
168
169 zval *php_pq_object_read_prop(zval *object, zval *member, int type, void **cache_slot, zval *tmp)
170 {
171 php_pq_object_t *obj = PHP_PQ_OBJ(object, NULL);
172 php_pq_object_prophandler_t *handler;
173 zval *return_value = NULL;
174
175 return_value = zend_get_std_object_handlers()->read_property(object, member, type, cache_slot, tmp);
176
177 if (!obj->intern) {
178 php_error(E_RECOVERABLE_ERROR, "%s not initialized", ancestor(obj->zo.ce)->name->val);
179 } else if (!(handler = zend_hash_find_ptr(obj->prophandler, Z_STR_P(member))) || !handler->read) {
180 /* default handler */
181 } else if (type != BP_VAR_R) {
182 php_error(E_WARNING, "Cannot access %s properties by reference or array key/index", ancestor(obj->zo.ce)->name->val);
183 } else {
184 handler->read(object, obj, tmp);
185 zend_get_std_object_handlers()->write_property(object, member, tmp, cache_slot);
186 return_value = tmp;
187
188 /*
189 zval dtor;
190
191 ZVAL_COPY_VALUE(&dtor, return_value);
192
193 ZVAL_ZVAL(return_value, tmp, 0, 0);
194 zval_ptr_dtor(&dtor);
195
196 */
197
198 if (cache_slot) {
199 *cache_slot = NULL;
200 }
201 }
202
203 return return_value;
204 }
205
206 php_pq_object_write_prop_t php_pq_object_write_prop(zval *object, zval *member, zval *value, void **cache_slot)
207 {
208 php_pq_object_t *obj = PHP_PQ_OBJ(object, NULL);
209 php_pq_object_prophandler_t *handler;
210
211 if (!obj->intern) {
212 php_error(E_RECOVERABLE_ERROR, "%s not initialized", ancestor(obj->zo.ce)->name->val);
213 zend_get_std_object_handlers()->write_property(object, member, value, cache_slot);
214 } else if ((handler = zend_hash_find_ptr(obj->prophandler, Z_STR_P(member)))) {
215 if (handler->write) {
216 handler->write(object, obj, value);
217 }
218 } else {
219 zend_get_std_object_handlers()->write_property(object, member, value, cache_slot);
220 }
221 #if PHP_VERSION_ID >= 70400
222 return value;
223 #endif
224 }
225
226 zval *php_pq_object_get_prop_ptr_null(zval *object, zval *member, int type, void **cache_slot)
227 {
228 return NULL;
229 }
230
231 void php_pq_object_prophandler_dtor(zval *zv) {
232 pefree(Z_PTR_P(zv), 1);
233 }
234