- proper fallthrough in zend_update_static_property()
[m6w6/ext-http] / missing.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #include "php.h"
19 #include "missing.h"
20
21 #ifdef ZEND_ENGINE_2
22
23 static inline zval *new_zval(zend_class_entry *ce)
24 {
25 zval *z;
26 if (ce->type & ZEND_INTERNAL_CLASS) {
27 z = malloc(sizeof(zval));
28 } else {
29 ALLOC_ZVAL(z);
30 }
31 INIT_PZVAL(z);
32 return z;
33 }
34
35 static inline zval *tmp_zval(void)
36 {
37 zval *z;
38 ALLOC_ZVAL(z);
39 z->is_ref = 0;
40 z->refcount = 0;
41 return z;
42 }
43
44
45 #if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION == 0)
46
47 int zend_declare_property_double(zend_class_entry *ce, char *name, int name_length, double value, int access_type TSRMLS_DC)
48 {
49 zval *property = new_zval(ce);
50 ZVAL_DOUBLE(property, value);
51 return zend_declare_property(ce, name, name_length, property, access_type TSRMLS_CC);
52 }
53
54 void zend_update_property_double(zend_class_entry *scope, zval *object, char *name, int name_length, double value TSRMLS_DC)
55 {
56 zval *tmp = tmp_zval();
57 ZVAL_DOUBLE(tmp, value);
58 zend_update_property(scope, object, name, name_length, tmp TSRMLS_CC);
59 }
60
61 int zend_declare_property_bool(zend_class_entry *ce, char *name, int name_length, long value, int access_type TSRMLS_DC)
62 {
63 zval *property = new_zval(ce);
64 ZVAL_BOOL(property, value);
65 return zend_declare_property(ce, name, name_length, property, access_type TSRMLS_CC);
66 }
67
68 void zend_update_property_bool(zend_class_entry *scope, zval *object, char *name, int name_length, long value TSRMLS_DC)
69 {
70 zval *tmp = tmp_zval();
71 ZVAL_BOOL(tmp, value);
72 zend_update_property(scope, object, name, name_length, tmp TSRMLS_CC);
73 }
74
75 #endif /* PHP_VERSION == 5.0 */
76
77 #if (PHP_MAJOR_VERSION >= 5)
78
79 int zend_declare_class_constant(zend_class_entry *ce, char *name, size_t name_length, zval *value TSRMLS_DC)
80 {
81 return zend_hash_add(&ce->constants_table, name, name_length, &value, sizeof(zval *), NULL);
82 }
83
84 int zend_declare_class_constant_long(zend_class_entry *ce, char *name, size_t name_length, long value TSRMLS_DC)
85 {
86 zval *constant = new_zval(ce);
87 ZVAL_LONG(constant, value);
88 return zend_declare_class_constant(ce, name, name_length, constant TSRMLS_CC);
89 }
90
91 int zend_declare_class_constant_bool(zend_class_entry *ce, char *name, size_t name_length, zend_bool value TSRMLS_DC)
92 {
93 zval *constant = new_zval(ce);
94 ZVAL_BOOL(constant, value);
95 return zend_declare_class_constant(ce, name, name_length, constant TSRMLS_CC);
96 }
97
98 int zend_declare_class_constant_double(zend_class_entry *ce, char *name, size_t name_length, double value TSRMLS_DC)
99 {
100 zval *constant = new_zval(ce);
101 ZVAL_DOUBLE(constant, value);
102 return zend_declare_class_constant(ce, name, name_length, constant TSRMLS_CC);
103 }
104
105 int zend_declare_class_constant_string(zend_class_entry *ce, char *name, size_t name_length, char *value TSRMLS_DC)
106 {
107 return zend_declare_class_constant_stringl(ce, name, name_length, value, strlen(value) TSRMLS_CC);
108 }
109
110 int zend_declare_class_constant_stringl(zend_class_entry *ce, char *name, size_t name_length, char *value, size_t value_length TSRMLS_DC)
111 {
112 zval *constant = new_zval(ce);
113 if (ce->type & ZEND_INTERNAL_CLASS) {
114 ZVAL_STRINGL(constant, zend_strndup(value, value_length), value_length, 0);
115 } else {
116 ZVAL_STRINGL(constant, value, value_length, 1);
117 }
118 return zend_declare_class_constant(ce, name, name_length, constant TSRMLS_CC);
119 }
120
121
122 int zend_update_static_property(zend_class_entry *scope, char *name, size_t name_len, zval *value TSRMLS_DC)
123 {
124 int retval;
125 zval **property = NULL;
126 zend_class_entry *old_scope = EG(scope);
127
128 EG(scope) = scope;
129
130 if (!(property = zend_std_get_static_property(scope, name, name_len, 0 TSRMLS_CC))) {
131 retval = FAILURE;
132 } else if (*property == value) {
133 retval = SUCCESS;
134 } else if (scope->type & ZEND_INTERNAL_CLASS) {
135 int refcount;
136 zend_uchar is_ref;
137
138 refcount = (*property)->refcount;
139 is_ref = (*property)->is_ref;
140
141 /* clean */
142 switch (Z_TYPE_PP(property))
143 {
144 case IS_BOOL: case IS_LONG: case IS_NULL:
145 break;
146
147 case IS_RESOURCE:
148 zend_list_delete(Z_LVAL_PP(property));
149 break;
150
151 case IS_STRING: case IS_CONSTANT:
152 free(Z_STRVAL_PP(property));
153 break;
154
155 case IS_OBJECT:
156 if (Z_OBJ_HT_PP(property)->del_ref) {
157 Z_OBJ_HT_PP(property)->del_ref(*property TSRMLS_CC);
158 }
159 break;
160
161 case IS_ARRAY: case IS_CONSTANT_ARRAY:
162 if (Z_ARRVAL_PP(property) && Z_ARRVAL_PP(property) != &EG(symbol_table)) {
163 zend_hash_destroy(Z_ARRVAL_PP(property));
164 free(Z_ARRVAL_PP(property));
165 }
166 break;
167 }
168
169 /* copy */
170 **property = *value;
171
172 /* ctor */
173 switch (Z_TYPE_PP(property))
174 {
175 case IS_BOOL: case IS_LONG: case IS_NULL:
176 break;
177
178 case IS_RESOURCE:
179 zend_list_addref(Z_LVAL_PP(property));
180 break;
181
182 case IS_STRING: case IS_CONSTANT:
183 Z_STRVAL_PP(property) = (char *) zend_strndup(Z_STRVAL_PP(property), Z_STRLEN_PP(property));
184 break;
185
186 case IS_OBJECT:
187 if (Z_OBJ_HT_PP(property)->add_ref) {
188 Z_OBJ_HT_PP(property)->add_ref(*property TSRMLS_CC);
189 }
190 break;
191
192 case IS_ARRAY: case IS_CONSTANT_ARRAY:
193 {
194 if (Z_ARRVAL_PP(property) != &EG(symbol_table)) {
195 zval *tmp;
196 HashTable *old = Z_ARRVAL_PP(property);
197
198 Z_ARRVAL_PP(property) = (HashTable *) malloc(sizeof(HashTable));
199 zend_hash_init(Z_ARRVAL_PP(property), 0, NULL, ZVAL_PTR_DTOR, 0);
200 zend_hash_copy(Z_ARRVAL_PP(property), old, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
201 }
202 }
203 break;
204 }
205
206 (*property)->refcount = refcount;
207 (*property)->is_ref = is_ref;
208
209 retval = SUCCESS;
210
211 } else {
212 if (PZVAL_IS_REF(*property)) {
213 zval_dtor(*property);
214 (*property)->type = value->type;
215 (*property)->value = value->value;
216
217 if (value->refcount) {
218 zval_copy_ctor(*property);
219 }
220
221 retval = SUCCESS;
222 } else {
223 value->refcount++;
224 if (PZVAL_IS_REF(value)) {
225 SEPARATE_ZVAL(&value);
226 }
227
228 retval = zend_hash_update(scope->static_members, name, name_len+1, &value, sizeof(zval *), NULL);
229 }
230 }
231
232 if (!value->refcount) {
233 zval_dtor(value);
234 FREE_ZVAL(value);
235 }
236
237 EG(scope) = old_scope;
238
239 return retval;
240 }
241
242 int zend_update_static_property_bool(zend_class_entry *scope, char *name, size_t name_len, zend_bool value TSRMLS_DC)
243 {
244 zval *tmp = tmp_zval();
245 ZVAL_BOOL(tmp, value);
246 return zend_update_static_property(scope, name, name_len, tmp TSRMLS_CC);
247 }
248
249 int zend_update_static_property_long(zend_class_entry *scope, char *name, size_t name_len, long value TSRMLS_DC)
250 {
251 zval *tmp = tmp_zval();
252 ZVAL_LONG(tmp, value);
253 return zend_update_static_property(scope, name, name_len, tmp TSRMLS_CC);
254 }
255
256 int zend_update_static_property_double(zend_class_entry *scope, char *name, size_t name_len, double value TSRMLS_DC)
257 {
258 zval *tmp = tmp_zval();
259 ZVAL_DOUBLE(tmp, value);
260 return zend_update_static_property(scope, name, name_len, tmp TSRMLS_CC);
261 }
262
263 int zend_update_static_property_string(zend_class_entry *scope, char *name, size_t name_len, char *value TSRMLS_DC)
264 {
265 zval *tmp = tmp_zval();
266 ZVAL_STRING(tmp, value, 1);
267 return zend_update_static_property(scope, name, name_len, tmp TSRMLS_CC);
268 }
269
270 int zend_update_static_property_stringl(zend_class_entry *scope, char *name, size_t name_len, char *value, size_t value_len TSRMLS_DC)
271 {
272 zval *tmp = tmp_zval();
273 ZVAL_STRINGL(tmp, value, value_len, 1);
274 return zend_update_static_property(scope, name, name_len, tmp TSRMLS_CC);
275 }
276
277 #endif /* PHP_MAJOR_VERSION >= 5 */
278 #endif /* ZEND_ENGINE_2 */
279
280 /*
281 * Local variables:
282 * tab-width: 4
283 * c-basic-offset: 4
284 * End:
285 * vim600: noet sw=4 ts=4 fdm=marker
286 * vim<600: noet sw=4 ts=4
287 */
288