Skip to content
Newer
Older
100755 393 lines (345 sloc) 11.8 KB
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1998-2003 Zend Technologies Ltd. (http://www.zend.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 2.00 of the Zend license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.zend.com/license/2_00.txt. |
11 | If you did not receive a copy of the Zend license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@zend.com so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Marcus Boerger <helly@php.net> |
16 +----------------------------------------------------------------------+
17 */
18
19 /* $Id$ */
20
21 #include "zend.h"
22 #include "zend_API.h"
23 #include "zend_interfaces.h"
24
25 zend_class_entry *zend_ce_traversable;
26 zend_class_entry *zend_ce_aggregate;
27 zend_class_entry *zend_ce_iterator;
28
29 /* {{{ zend_call_method
30 Only returns the returned zval if retval_ptr != NULL */
31 ZEND_API zval* zend_call_method(zval **object_pp, zend_class_entry *obj_ce, zend_function **fn_proxy, char *function_name, int function_name_len, zval **retval_ptr_ptr, int param_count, zval* arg1, zval* arg2 TSRMLS_DC)
32 {
33 int result;
34 zend_fcall_info fci;
35 zval z_fname;
36 zval *retval;
37
38 zval **params[2];
39
40 params[0] = &arg1;
41 params[1] = &arg2;
42
43 fci.size = sizeof(fci);
44 /*fci.function_table = NULL; will be read form zend_class_entry of object if needed */
45 fci.object_pp = object_pp;
46 fci.function_name = &z_fname;
47 fci.retval_ptr_ptr = retval_ptr_ptr ? retval_ptr_ptr : &retval;
48 fci.param_count = param_count;
49 fci.params = params;
50 fci.no_separation = 1;
51 fci.symbol_table = NULL;
52
53 if (!fn_proxy && !obj_ce) {
54 /* no interest in caching and no information already present that is
55 * needed later inside zend_call_function. */
56 ZVAL_STRINGL(&z_fname, function_name, function_name_len, 0);
57 result = zend_call_function(&fci, NULL TSRMLS_CC);
58 } else {
59 zend_fcall_info_cache fcic;
60
61 fcic.initialized = 1;
62 if (!obj_ce) {
63 obj_ce = Z_OBJCE_PP(object_pp);
64 }
65 if (!fn_proxy || !*fn_proxy) {
66 if (zend_hash_find(&obj_ce->function_table, function_name, function_name_len+1, (void **) &fcic.function_handler) == FAILURE) {
67 /* error at c-level */
68 zend_error(E_CORE_ERROR, "Couldn't find implementation for method %s::%s", obj_ce->name, function_name);
69 }
70 if (fn_proxy) {
71 *fn_proxy = fcic.function_handler;
72 }
73 } else {
74 fcic.function_handler = *fn_proxy;
75 }
76 fcic.calling_scope = obj_ce;
77 fcic.object_pp = object_pp;
78 result = zend_call_function(&fci, &fcic TSRMLS_CC);
79 }
80 if (result == FAILURE) {
81 /* error at c-level */
82 if (!obj_ce) {
83 obj_ce = Z_OBJCE_PP(object_pp);
84 }
85 zend_error(E_CORE_ERROR, "Couldn't execute method %s::%s", obj_ce->name, function_name);
86 }
87 if (!retval_ptr_ptr) {
88 if (retval) {
89 zval_dtor(retval);
90 FREE_ZVAL(retval);
91 }
92 return NULL;
93 }
94 return *retval_ptr_ptr;
95 }
96 /* }}} */
97
98 /* iterator interface, c-level functions used by engine */
99
100 typedef struct _zend_user_iterator {
101 zend_object_iterator it;
102 zend_class_entry *ce;
103 zval *value;
104 } zend_user_iterator;
105
106 /* {{{ zend_user_new_iterator */
107 static zval *zend_user_new_iterator(zend_class_entry *ce, zval *object TSRMLS_DC)
108 {
109 zval *retval;
110
111 return zend_call_method_with_0_params(&object, ce, &ce->iterator_funcs.zf_new_iterator, "getiterator", &retval);
112 }
113 /* }}} */
114
115 /* {{{ zend_user_dtor */
116 static void zend_user_dtor(zend_object_iterator *_iter TSRMLS_DC)
117 {
118 zend_user_iterator *iter = (zend_user_iterator*)_iter;
119 zval *object = (zval*)iter->it.data;
120
121 if (iter->value) {
122 zval_ptr_dtor(&iter->value);
123 iter->value = NULL;
124 }
125 zval_ptr_dtor(&object);
126 efree(iter);
127 }
128 /* }}} */
129
130 /* {{{ zend_user_has_more */
131 static int zend_user_has_more(zend_object_iterator *_iter TSRMLS_DC)
132 {
f9d1309 Prevent some SEGV's when Exceptions are thorown inside iterators.
Marcus Boerger authored
133 if (_iter) {
134 zend_user_iterator *iter = (zend_user_iterator*)_iter;
135 zval *object = (zval*)iter->it.data;
136 zval *more;
137 int result;
138
139 zend_call_method_with_0_params(&object, iter->ce, &iter->ce->iterator_funcs.zf_has_more, "hasmore", &more);
140 if (more) {
141 result = i_zend_is_true(more);
142 zval_dtor(more);
143 FREE_ZVAL(more);
144 return result ? SUCCESS : FAILURE;
145 }
146 }
147 return FAILURE;
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
148 }
149 /* }}} */
150
151 /* {{{ zend_user_get_current_data */
152 static void zend_user_get_current_data(zend_object_iterator *_iter, zval ***data TSRMLS_DC)
153 {
154 zend_user_iterator *iter = (zend_user_iterator*)_iter;
155 zval *object = (zval*)iter->it.data;
156
157 if (!iter->value) {
158 zend_call_method_with_0_params(&object, iter->ce, &iter->ce->iterator_funcs.zf_current, "current", &iter->value);
159 }
160 *data = &iter->value;
161 }
162 /* }}} */
163
164 /* {{{ zend_user_get_current_key_default */
165 #if 0
166 static int zend_user_get_current_key_default(zend_object_iterator *_iter, char **str_key, uint *str_key_len, ulong *int_key TSRMLS_DC)
167 {
da6d68d Little iterator improvement: ability to store index in iterator
Marcus Boerger authored
168 *int_key = _iter->index;
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
169 return HASH_KEY_IS_LONG;
170 }
171 #endif
172 /* }}} */
173
174 /* {{{ zend_user_get_current_key */
175 static int zend_user_get_current_key(zend_object_iterator *_iter, char **str_key, uint *str_key_len, ulong *int_key TSRMLS_DC)
176 {
177 zend_user_iterator *iter = (zend_user_iterator*)_iter;
178 zval *object = (zval*)iter->it.data;
179 zval *retval;
180
181 zend_call_method_with_0_params(&object, iter->ce, &iter->ce->iterator_funcs.zf_key, "key", &retval);
182
183 switch (retval->type) {
184 default:
185 zend_error(E_WARNING, "Illegal type returned from %s::key()", iter->ce->name);
186 case IS_NULL:
187 *str_key = "";
188 *str_key_len = 0;
189 *int_key = 0;
190 zval_ptr_dtor(&retval);
191 return HASH_KEY_IS_LONG;
192
193 case IS_STRING:
194 *str_key = estrndup(retval->value.str.val, retval->value.str.len);
195 *str_key_len = retval->value.str.len+1;
196 *int_key = 0;
197 zval_ptr_dtor(&retval);
198 return HASH_KEY_IS_STRING;
199
200 case IS_DOUBLE:
201 case IS_RESOURCE:
202 case IS_BOOL:
203 case IS_LONG: {
204 if (retval->type == IS_DOUBLE) {
205 *int_key = (long)retval->value.dval;
206 } else {
207 *int_key = retval->value.lval;
208 }
209 }
210 zval_ptr_dtor(&retval);
211 return HASH_KEY_IS_LONG;
212 }
213 }
214 /* }}} */
215
216 /* {{{ zend_user_move_forward */
217 static void zend_user_move_forward(zend_object_iterator *_iter TSRMLS_DC)
218 {
219 zend_user_iterator *iter = (zend_user_iterator*)_iter;
220 zval *object = (zval*)iter->it.data;
221
222 if (iter->value) {
223 zval_ptr_dtor(&iter->value);
224 iter->value = NULL;
225 }
226 zend_call_method_with_0_params(&object, iter->ce, &iter->ce->iterator_funcs.zf_next, "next", NULL);
227 }
228 /* }}} */
229
230 /* {{{ zend_user_rewind */
231 static void zend_user_rewind(zend_object_iterator *_iter TSRMLS_DC)
232 {
233 zend_user_iterator *iter = (zend_user_iterator*)_iter;
234 zval *object = (zval*)iter->it.data;
235
236 zend_call_method_with_0_params(&object, iter->ce, &iter->ce->iterator_funcs.zf_rewind, "rewind", NULL);
237 }
238 /* }}} */
239
240 zend_object_iterator_funcs zend_interface_iterator_funcs_iterator = {
241 zend_user_dtor,
242 zend_user_has_more,
243 zend_user_get_current_data,
244 zend_user_get_current_key,
245 zend_user_move_forward,
246 zend_user_rewind
247 };
248
249 /* {{{ zend_user_get_iterator */
250 static zend_object_iterator *zend_user_get_iterator(zend_class_entry *ce, zval *object TSRMLS_DC)
251 {
252 zend_user_iterator *iterator = emalloc(sizeof(zend_user_iterator));
253
254 object->refcount++;
255 iterator->it.data = (void*)object;
256 iterator->it.funcs = ce->iterator_funcs.funcs;
257 iterator->ce = Z_OBJCE_P(object);
258 iterator->value = NULL;
259 return (zend_object_iterator*)iterator;
260 }
261 /* }}} */
262
263 /* {{{ zend_user_get_new_iterator */
264 static zend_object_iterator *zend_user_get_new_iterator(zend_class_entry *ce, zval *object TSRMLS_DC)
265 {
266 zval *iterator = zend_user_new_iterator(ce, object TSRMLS_CC);
267
268 zend_class_entry *ce_it = Z_OBJCE_P(iterator);
269 if (!ce || !ce_it->get_iterator) {
270 zend_error(E_WARNING, "Objects returned by %s::getIterator() must be traversable or implement interface Iterator", ce->name);
271 return NULL;
272 }
273 iterator->refcount--; /* from return */
274 return ce_it->get_iterator(ce_it, iterator TSRMLS_CC);
275 }
276 /* }}} */
277
278 /* {{{ zend_implement_traversable */
279 static int zend_implement_traversable(zend_class_entry *interface, zend_class_entry *class_type TSRMLS_DC)
280 {
281 /* check that class_type is traversable at c-level or implements at least one of 'aggregate' and 'Iterator' */
282 int i;
283
284 if (class_type->get_iterator) {
285 return SUCCESS;
286 }
287 for (i = 0; i < class_type->num_interfaces; i++) {
288 if (class_type->interfaces[i] == zend_ce_aggregate || class_type->interfaces[i] == zend_ce_iterator) {
289 return SUCCESS;
290 }
291 }
292 zend_error(E_CORE_ERROR, "Class %s must implement interface %s as part of either %s or %s",
293 class_type->name,
294 zend_ce_traversable->name,
295 zend_ce_iterator->name,
296 zend_ce_aggregate->name);
297 return FAILURE;
298 }
299 /* }}} */
300
301 /* {{{ zend_implement_aggregate */
302 static int zend_implement_aggregate(zend_class_entry *interface, zend_class_entry *class_type TSRMLS_DC)
303 {
851a02e Give some freedon to c iterators but not in userspace.
Marcus Boerger authored
304 if (class_type->get_iterator) {
305 if (class_type->type == ZEND_INTERNAL_CLASS) {
306 /* inheritance ensures the class has necessary userland methods */
307 return SUCCESS;
308 } else if (class_type->get_iterator != zend_user_get_new_iterator) {
309 /* c-level get_iterator cannot be changed */
310 return FAILURE;
311 }
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
312 }
313 class_type->iterator_funcs.zf_new_iterator = NULL;
314 class_type->get_iterator = zend_user_get_new_iterator;
315 return SUCCESS;
316 }
317 /* }}} */
318
319 /* {{{ zend_implement_iterator */
320 static int zend_implement_iterator(zend_class_entry *interface, zend_class_entry *class_type TSRMLS_DC)
321 {
322 if (class_type->get_iterator && class_type->get_iterator != zend_user_get_iterator) {
851a02e Give some freedon to c iterators but not in userspace.
Marcus Boerger authored
323 if (class_type->type == ZEND_INTERNAL_CLASS) {
324 /* inheritance ensures the class has the necessary userland methods */
325 return SUCCESS;
326 } else if (class_type->get_iterator != zend_user_get_new_iterator) {
327 /* c-level get_iterator cannot be changed */
328 return FAILURE;
329 }
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
330 }
331 class_type->get_iterator = zend_user_get_iterator;
332 class_type->iterator_funcs.zf_has_more = NULL;
333 class_type->iterator_funcs.zf_current = NULL;
334 class_type->iterator_funcs.zf_key = NULL;
335 class_type->iterator_funcs.zf_next = NULL;
336 class_type->iterator_funcs.zf_rewind = NULL;
337 if (!class_type->iterator_funcs.funcs) {
338 class_type->iterator_funcs.funcs = &zend_interface_iterator_funcs_iterator;
339 }
340 return SUCCESS;
341 }
342 /* }}} */
343
344 /* {{{ function tables */
345 zend_function_entry zend_funcs_aggregate[] = {
346 ZEND_ABSTRACT_ME(iterator, getIterator, NULL)
347 {NULL, NULL, NULL}
348 };
349
350 zend_function_entry zend_funcs_iterator[] = {
351 ZEND_ABSTRACT_ME(iterator, current, NULL)
352 ZEND_ABSTRACT_ME(iterator, next, NULL)
353 ZEND_ABSTRACT_ME(iterator, key, NULL)
354 ZEND_ABSTRACT_ME(iterator, hasMore, NULL)
da6d68d Little iterator improvement: ability to store index in iterator
Marcus Boerger authored
355 ZEND_ABSTRACT_ME(iterator, rewind, NULL)
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
356 {NULL, NULL, NULL}
357 };
358
359 zend_function_entry *zend_funcs_traversable = NULL;
360 /* }}} */
361
362 #define REGISTER_ITERATOR_INTERFACE(class_name, class_name_str) \
363 {\
364 zend_class_entry ce;\
365 INIT_CLASS_ENTRY(ce, # class_name_str, zend_funcs_ ## class_name) \
366 zend_ce_ ## class_name = zend_register_internal_interface(&ce TSRMLS_CC);\
367 zend_ce_ ## class_name->interface_gets_implemented = zend_implement_ ## class_name;\
368 }
369
370 #define REGISTER_ITERATOR_IMPLEMENT(class_name, interface_name) \
371 zend_class_implements(zend_ce_ ## class_name TSRMLS_CC, 1, zend_ce_ ## interface_name)
372
373 /* {{{ zend_register_interfaces */
374 ZEND_API void zend_register_interfaces(TSRMLS_D)
375 {
376 REGISTER_ITERATOR_INTERFACE(traversable, Traversable);
377
378 REGISTER_ITERATOR_INTERFACE(aggregate, IteratorAggregate);
379 REGISTER_ITERATOR_IMPLEMENT(aggregate, traversable);
380
381 REGISTER_ITERATOR_INTERFACE(iterator, Iterator);
382 REGISTER_ITERATOR_IMPLEMENT(iterator, traversable);
383 }
384 /* }}} */
385
386 /*
387 * Local variables:
388 * tab-width: 4
389 * c-basic-offset: 4
390 * indent-tabs-mode: t
391 * End:
392 */
Something went wrong with that request. Please try again.