Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 396 lines (346 sloc) 12.18 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) {
39df192 Correct destruction
Marcus Boerger authored
89 zval_ptr_dtor(&retval);
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
90 }
91 return NULL;
92 }
93 return *retval_ptr_ptr;
94 }
95 /* }}} */
96
97 /* iterator interface, c-level functions used by engine */
98
99 typedef struct _zend_user_iterator {
100 zend_object_iterator it;
101 zend_class_entry *ce;
102 zval *value;
103 } zend_user_iterator;
104
105 /* {{{ zend_user_new_iterator */
106 static zval *zend_user_new_iterator(zend_class_entry *ce, zval *object TSRMLS_DC)
107 {
108 zval *retval;
109
110 return zend_call_method_with_0_params(&object, ce, &ce->iterator_funcs.zf_new_iterator, "getiterator", &retval);
5824641 IteratorAggregate::getIterator() cannot return anythingy else than ob…
Marcus Boerger authored
111
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
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);
b29834a Use correct macro/function
Marcus Boerger authored
142 zval_ptr_dtor(&more);
f9d1309 Prevent some SEGV's when Exceptions are thorown inside iterators.
Marcus Boerger authored
143 return result ? SUCCESS : FAILURE;
144 }
145 }
146 return FAILURE;
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
147 }
148 /* }}} */
149
150 /* {{{ zend_user_get_current_data */
151 static void zend_user_get_current_data(zend_object_iterator *_iter, zval ***data TSRMLS_DC)
152 {
153 zend_user_iterator *iter = (zend_user_iterator*)_iter;
154 zval *object = (zval*)iter->it.data;
155
156 if (!iter->value) {
157 zend_call_method_with_0_params(&object, iter->ce, &iter->ce->iterator_funcs.zf_current, "current", &iter->value);
158 }
159 *data = &iter->value;
160 }
161 /* }}} */
162
163 /* {{{ zend_user_get_current_key_default */
164 #if 0
165 static int zend_user_get_current_key_default(zend_object_iterator *_iter, char **str_key, uint *str_key_len, ulong *int_key TSRMLS_DC)
166 {
da6d68d Little iterator improvement: ability to store index in iterator
Marcus Boerger authored
167 *int_key = _iter->index;
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
168 return HASH_KEY_IS_LONG;
169 }
170 #endif
171 /* }}} */
172
173 /* {{{ zend_user_get_current_key */
174 static int zend_user_get_current_key(zend_object_iterator *_iter, char **str_key, uint *str_key_len, ulong *int_key TSRMLS_DC)
175 {
176 zend_user_iterator *iter = (zend_user_iterator*)_iter;
177 zval *object = (zval*)iter->it.data;
178 zval *retval;
179
180 zend_call_method_with_0_params(&object, iter->ce, &iter->ce->iterator_funcs.zf_key, "key", &retval);
181
182 switch (retval->type) {
183 default:
184 zend_error(E_WARNING, "Illegal type returned from %s::key()", iter->ce->name);
185 case IS_NULL:
186 *str_key = "";
187 *str_key_len = 0;
188 *int_key = 0;
189 zval_ptr_dtor(&retval);
190 return HASH_KEY_IS_LONG;
191
192 case IS_STRING:
193 *str_key = estrndup(retval->value.str.val, retval->value.str.len);
194 *str_key_len = retval->value.str.len+1;
195 *int_key = 0;
196 zval_ptr_dtor(&retval);
197 return HASH_KEY_IS_STRING;
198
199 case IS_DOUBLE:
200 case IS_RESOURCE:
201 case IS_BOOL:
202 case IS_LONG: {
203 if (retval->type == IS_DOUBLE) {
204 *int_key = (long)retval->value.dval;
205 } else {
206 *int_key = retval->value.lval;
207 }
208 }
209 zval_ptr_dtor(&retval);
210 return HASH_KEY_IS_LONG;
211 }
212 }
213 /* }}} */
214
215 /* {{{ zend_user_move_forward */
216 static void zend_user_move_forward(zend_object_iterator *_iter TSRMLS_DC)
217 {
218 zend_user_iterator *iter = (zend_user_iterator*)_iter;
219 zval *object = (zval*)iter->it.data;
220
221 if (iter->value) {
222 zval_ptr_dtor(&iter->value);
223 iter->value = NULL;
224 }
225 zend_call_method_with_0_params(&object, iter->ce, &iter->ce->iterator_funcs.zf_next, "next", NULL);
226 }
227 /* }}} */
228
229 /* {{{ zend_user_rewind */
230 static void zend_user_rewind(zend_object_iterator *_iter TSRMLS_DC)
231 {
232 zend_user_iterator *iter = (zend_user_iterator*)_iter;
233 zval *object = (zval*)iter->it.data;
234
235 zend_call_method_with_0_params(&object, iter->ce, &iter->ce->iterator_funcs.zf_rewind, "rewind", NULL);
236 }
237 /* }}} */
238
239 zend_object_iterator_funcs zend_interface_iterator_funcs_iterator = {
240 zend_user_dtor,
241 zend_user_has_more,
242 zend_user_get_current_data,
243 zend_user_get_current_key,
244 zend_user_move_forward,
245 zend_user_rewind
246 };
247
248 /* {{{ zend_user_get_iterator */
249 static zend_object_iterator *zend_user_get_iterator(zend_class_entry *ce, zval *object TSRMLS_DC)
250 {
251 zend_user_iterator *iterator = emalloc(sizeof(zend_user_iterator));
252
253 object->refcount++;
254 iterator->it.data = (void*)object;
255 iterator->it.funcs = ce->iterator_funcs.funcs;
256 iterator->ce = Z_OBJCE_P(object);
257 iterator->value = NULL;
258 return (zend_object_iterator*)iterator;
259 }
260 /* }}} */
261
262 /* {{{ zend_user_get_new_iterator */
263 static zend_object_iterator *zend_user_get_new_iterator(zend_class_entry *ce, zval *object TSRMLS_DC)
264 {
265 zval *iterator = zend_user_new_iterator(ce, object TSRMLS_CC);
c978cc2 Use correct order
Marcus Boerger authored
266 zend_object_iterator *new_iterator;
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
267
5824641 IteratorAggregate::getIterator() cannot return anythingy else than ob…
Marcus Boerger authored
268 zend_class_entry *ce_it = Z_TYPE_P(iterator) == IS_OBJECT ? Z_OBJCE_P(iterator) : NULL;
269
270 if (!ce || !ce_it || !ce_it->get_iterator) {
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
271 zend_error(E_WARNING, "Objects returned by %s::getIterator() must be traversable or implement interface Iterator", ce->name);
5824641 IteratorAggregate::getIterator() cannot return anythingy else than ob…
Marcus Boerger authored
272 zval_ptr_dtor(&iterator);
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
273 return NULL;
274 }
c978cc2 Use correct order
Marcus Boerger authored
275 new_iterator = ce_it->get_iterator(ce_it, iterator TSRMLS_CC);
276 zval_ptr_dtor(&iterator);
277 return new_iterator;
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
278 }
279 /* }}} */
280
281 /* {{{ zend_implement_traversable */
282 static int zend_implement_traversable(zend_class_entry *interface, zend_class_entry *class_type TSRMLS_DC)
283 {
284 /* check that class_type is traversable at c-level or implements at least one of 'aggregate' and 'Iterator' */
285 int i;
286
287 if (class_type->get_iterator) {
288 return SUCCESS;
289 }
290 for (i = 0; i < class_type->num_interfaces; i++) {
291 if (class_type->interfaces[i] == zend_ce_aggregate || class_type->interfaces[i] == zend_ce_iterator) {
292 return SUCCESS;
293 }
294 }
295 zend_error(E_CORE_ERROR, "Class %s must implement interface %s as part of either %s or %s",
296 class_type->name,
297 zend_ce_traversable->name,
298 zend_ce_iterator->name,
299 zend_ce_aggregate->name);
300 return FAILURE;
301 }
302 /* }}} */
303
304 /* {{{ zend_implement_aggregate */
305 static int zend_implement_aggregate(zend_class_entry *interface, zend_class_entry *class_type TSRMLS_DC)
306 {
851a02e Give some freedon to c iterators but not in userspace.
Marcus Boerger authored
307 if (class_type->get_iterator) {
308 if (class_type->type == ZEND_INTERNAL_CLASS) {
309 /* inheritance ensures the class has necessary userland methods */
310 return SUCCESS;
311 } else if (class_type->get_iterator != zend_user_get_new_iterator) {
312 /* c-level get_iterator cannot be changed */
313 return FAILURE;
314 }
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
315 }
316 class_type->iterator_funcs.zf_new_iterator = NULL;
317 class_type->get_iterator = zend_user_get_new_iterator;
318 return SUCCESS;
319 }
320 /* }}} */
321
322 /* {{{ zend_implement_iterator */
323 static int zend_implement_iterator(zend_class_entry *interface, zend_class_entry *class_type TSRMLS_DC)
324 {
325 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
326 if (class_type->type == ZEND_INTERNAL_CLASS) {
327 /* inheritance ensures the class has the necessary userland methods */
328 return SUCCESS;
329 } else if (class_type->get_iterator != zend_user_get_new_iterator) {
330 /* c-level get_iterator cannot be changed */
331 return FAILURE;
332 }
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
333 }
334 class_type->get_iterator = zend_user_get_iterator;
335 class_type->iterator_funcs.zf_has_more = NULL;
336 class_type->iterator_funcs.zf_current = NULL;
337 class_type->iterator_funcs.zf_key = NULL;
338 class_type->iterator_funcs.zf_next = NULL;
339 class_type->iterator_funcs.zf_rewind = NULL;
340 if (!class_type->iterator_funcs.funcs) {
341 class_type->iterator_funcs.funcs = &zend_interface_iterator_funcs_iterator;
342 }
343 return SUCCESS;
344 }
345 /* }}} */
346
347 /* {{{ function tables */
348 zend_function_entry zend_funcs_aggregate[] = {
349 ZEND_ABSTRACT_ME(iterator, getIterator, NULL)
350 {NULL, NULL, NULL}
351 };
352
353 zend_function_entry zend_funcs_iterator[] = {
354 ZEND_ABSTRACT_ME(iterator, current, NULL)
355 ZEND_ABSTRACT_ME(iterator, next, NULL)
356 ZEND_ABSTRACT_ME(iterator, key, NULL)
357 ZEND_ABSTRACT_ME(iterator, hasMore, NULL)
da6d68d Little iterator improvement: ability to store index in iterator
Marcus Boerger authored
358 ZEND_ABSTRACT_ME(iterator, rewind, NULL)
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
359 {NULL, NULL, NULL}
360 };
361
362 zend_function_entry *zend_funcs_traversable = NULL;
363 /* }}} */
364
365 #define REGISTER_ITERATOR_INTERFACE(class_name, class_name_str) \
366 {\
367 zend_class_entry ce;\
368 INIT_CLASS_ENTRY(ce, # class_name_str, zend_funcs_ ## class_name) \
369 zend_ce_ ## class_name = zend_register_internal_interface(&ce TSRMLS_CC);\
370 zend_ce_ ## class_name->interface_gets_implemented = zend_implement_ ## class_name;\
371 }
372
373 #define REGISTER_ITERATOR_IMPLEMENT(class_name, interface_name) \
374 zend_class_implements(zend_ce_ ## class_name TSRMLS_CC, 1, zend_ce_ ## interface_name)
375
376 /* {{{ zend_register_interfaces */
377 ZEND_API void zend_register_interfaces(TSRMLS_D)
378 {
379 REGISTER_ITERATOR_INTERFACE(traversable, Traversable);
380
381 REGISTER_ITERATOR_INTERFACE(aggregate, IteratorAggregate);
382 REGISTER_ITERATOR_IMPLEMENT(aggregate, traversable);
383
384 REGISTER_ITERATOR_INTERFACE(iterator, Iterator);
385 REGISTER_ITERATOR_IMPLEMENT(iterator, traversable);
386 }
387 /* }}} */
388
389 /*
390 * Local variables:
391 * tab-width: 4
392 * c-basic-offset: 4
393 * indent-tabs-mode: t
394 * End:
395 */
Something went wrong with that request. Please try again.