Skip to content
Newer
Older
100755 426 lines (370 sloc) 12.8 KB
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
ccfc46b - Happy new year and PHP 5 for rest of the files too..
foobar authored
5 | Copyright (c) 1998-2004 Zend Technologies Ltd. (http://www.zend.com) |
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
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;
4dba05d Add new interface ArrayAccess to use objects as Arrays
Marcus Boerger authored
28 zend_class_entry *zend_ce_arrayaccess;
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
29
30 /* {{{ zend_call_method
31 Only returns the returned zval if retval_ptr != NULL */
32 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)
33 {
34 int result;
35 zend_fcall_info fci;
36 zval z_fname;
37 zval *retval;
38
39 zval **params[2];
40
41 params[0] = &arg1;
42 params[1] = &arg2;
43
44 fci.size = sizeof(fci);
45 /*fci.function_table = NULL; will be read form zend_class_entry of object if needed */
46 fci.object_pp = object_pp;
47 fci.function_name = &z_fname;
48 fci.retval_ptr_ptr = retval_ptr_ptr ? retval_ptr_ptr : &retval;
49 fci.param_count = param_count;
50 fci.params = params;
51 fci.no_separation = 1;
52 fci.symbol_table = NULL;
53
54 if (!fn_proxy && !obj_ce) {
55 /* no interest in caching and no information already present that is
56 * needed later inside zend_call_function. */
57 ZVAL_STRINGL(&z_fname, function_name, function_name_len, 0);
58 result = zend_call_function(&fci, NULL TSRMLS_CC);
59 } else {
60 zend_fcall_info_cache fcic;
61
62 fcic.initialized = 1;
63 if (!obj_ce) {
64 obj_ce = Z_OBJCE_PP(object_pp);
65 }
66 if (!fn_proxy || !*fn_proxy) {
67 if (zend_hash_find(&obj_ce->function_table, function_name, function_name_len+1, (void **) &fcic.function_handler) == FAILURE) {
68 /* error at c-level */
69 zend_error(E_CORE_ERROR, "Couldn't find implementation for method %s::%s", obj_ce->name, function_name);
70 }
71 if (fn_proxy) {
72 *fn_proxy = fcic.function_handler;
73 }
74 } else {
75 fcic.function_handler = *fn_proxy;
76 }
77 fcic.calling_scope = obj_ce;
78 fcic.object_pp = object_pp;
79 result = zend_call_function(&fci, &fcic TSRMLS_CC);
80 }
81 if (result == FAILURE) {
82 /* error at c-level */
83 if (!obj_ce) {
84 obj_ce = Z_OBJCE_PP(object_pp);
85 }
86 zend_error(E_CORE_ERROR, "Couldn't execute method %s::%s", obj_ce->name, function_name);
87 }
88 if (!retval_ptr_ptr) {
89 if (retval) {
39df192 Correct destruction
Marcus Boerger authored
90 zval_ptr_dtor(&retval);
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
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);
5824641 IteratorAggregate::getIterator() cannot return anythingy else than ob…
Marcus Boerger authored
112
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
113 }
114 /* }}} */
115
116 /* {{{ zend_user_dtor */
117 static void zend_user_dtor(zend_object_iterator *_iter TSRMLS_DC)
118 {
119 zend_user_iterator *iter = (zend_user_iterator*)_iter;
120 zval *object = (zval*)iter->it.data;
121
122 if (iter->value) {
123 zval_ptr_dtor(&iter->value);
124 iter->value = NULL;
125 }
126 zval_ptr_dtor(&object);
127 efree(iter);
128 }
129 /* }}} */
130
131 /* {{{ zend_user_has_more */
132 static int zend_user_has_more(zend_object_iterator *_iter TSRMLS_DC)
133 {
f9d1309 Prevent some SEGV's when Exceptions are thorown inside iterators.
Marcus Boerger authored
134 if (_iter) {
135 zend_user_iterator *iter = (zend_user_iterator*)_iter;
136 zval *object = (zval*)iter->it.data;
137 zval *more;
138 int result;
139
140 zend_call_method_with_0_params(&object, iter->ce, &iter->ce->iterator_funcs.zf_has_more, "hasmore", &more);
141 if (more) {
142 result = i_zend_is_true(more);
b29834a Use correct macro/function
Marcus Boerger authored
143 zval_ptr_dtor(&more);
f9d1309 Prevent some SEGV's when Exceptions are thorown inside iterators.
Marcus Boerger authored
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);
c978cc2 Use correct order
Marcus Boerger authored
267 zend_object_iterator *new_iterator;
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
268
5824641 IteratorAggregate::getIterator() cannot return anythingy else than ob…
Marcus Boerger authored
269 zend_class_entry *ce_it = Z_TYPE_P(iterator) == IS_OBJECT ? Z_OBJCE_P(iterator) : NULL;
270
271 if (!ce || !ce_it || !ce_it->get_iterator) {
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
272 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
273 zval_ptr_dtor(&iterator);
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
274 return NULL;
275 }
c978cc2 Use correct order
Marcus Boerger authored
276 new_iterator = ce_it->get_iterator(ce_it, iterator TSRMLS_CC);
277 zval_ptr_dtor(&iterator);
278 return new_iterator;
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
279 }
280 /* }}} */
281
282 /* {{{ zend_implement_traversable */
283 static int zend_implement_traversable(zend_class_entry *interface, zend_class_entry *class_type TSRMLS_DC)
284 {
285 /* check that class_type is traversable at c-level or implements at least one of 'aggregate' and 'Iterator' */
286 int i;
287
64c7645 #where has my bracket been gone?
Marcus Boerger authored
288 if (class_type->get_iterator || (class_type->parent && class_type->parent->get_iterator)) {
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
289 return SUCCESS;
290 }
291 for (i = 0; i < class_type->num_interfaces; i++) {
292 if (class_type->interfaces[i] == zend_ce_aggregate || class_type->interfaces[i] == zend_ce_iterator) {
293 return SUCCESS;
294 }
295 }
296 zend_error(E_CORE_ERROR, "Class %s must implement interface %s as part of either %s or %s",
297 class_type->name,
298 zend_ce_traversable->name,
299 zend_ce_iterator->name,
300 zend_ce_aggregate->name);
301 return FAILURE;
302 }
303 /* }}} */
304
305 /* {{{ zend_implement_aggregate */
306 static int zend_implement_aggregate(zend_class_entry *interface, zend_class_entry *class_type TSRMLS_DC)
307 {
851a02e Give some freedon to c iterators but not in userspace.
Marcus Boerger authored
308 if (class_type->get_iterator) {
309 if (class_type->type == ZEND_INTERNAL_CLASS) {
310 /* inheritance ensures the class has necessary userland methods */
311 return SUCCESS;
312 } else if (class_type->get_iterator != zend_user_get_new_iterator) {
313 /* c-level get_iterator cannot be changed */
314 return FAILURE;
315 }
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
316 }
317 class_type->iterator_funcs.zf_new_iterator = NULL;
318 class_type->get_iterator = zend_user_get_new_iterator;
319 return SUCCESS;
320 }
321 /* }}} */
322
323 /* {{{ zend_implement_iterator */
324 static int zend_implement_iterator(zend_class_entry *interface, zend_class_entry *class_type TSRMLS_DC)
325 {
326 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
327 if (class_type->type == ZEND_INTERNAL_CLASS) {
328 /* inheritance ensures the class has the necessary userland methods */
329 return SUCCESS;
330 } else if (class_type->get_iterator != zend_user_get_new_iterator) {
331 /* c-level get_iterator cannot be changed */
332 return FAILURE;
333 }
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
334 }
335 class_type->get_iterator = zend_user_get_iterator;
336 class_type->iterator_funcs.zf_has_more = NULL;
337 class_type->iterator_funcs.zf_current = NULL;
338 class_type->iterator_funcs.zf_key = NULL;
339 class_type->iterator_funcs.zf_next = NULL;
340 class_type->iterator_funcs.zf_rewind = NULL;
341 if (!class_type->iterator_funcs.funcs) {
342 class_type->iterator_funcs.funcs = &zend_interface_iterator_funcs_iterator;
343 }
344 return SUCCESS;
345 }
346 /* }}} */
347
4dba05d Add new interface ArrayAccess to use objects as Arrays
Marcus Boerger authored
348 /* {{{ zend_implement_arrayaccess */
349 static int zend_implement_arrayaccess(zend_class_entry *interface, zend_class_entry *class_type TSRMLS_DC)
350 {
351 return SUCCESS;
352 }
353 /* }}}*/
354
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
355 /* {{{ function tables */
356 zend_function_entry zend_funcs_aggregate[] = {
357 ZEND_ABSTRACT_ME(iterator, getIterator, NULL)
358 {NULL, NULL, NULL}
359 };
360
361 zend_function_entry zend_funcs_iterator[] = {
362 ZEND_ABSTRACT_ME(iterator, current, NULL)
363 ZEND_ABSTRACT_ME(iterator, next, NULL)
364 ZEND_ABSTRACT_ME(iterator, key, NULL)
365 ZEND_ABSTRACT_ME(iterator, hasMore, NULL)
da6d68d Little iterator improvement: ability to store index in iterator
Marcus Boerger authored
366 ZEND_ABSTRACT_ME(iterator, rewind, NULL)
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
367 {NULL, NULL, NULL}
368 };
369
370 zend_function_entry *zend_funcs_traversable = NULL;
4dba05d Add new interface ArrayAccess to use objects as Arrays
Marcus Boerger authored
371
372 static
373 ZEND_BEGIN_ARG_INFO(arginfo_arrayaccess_offset, 0)
374 ZEND_ARG_INFO(0, offset)
375 ZEND_END_ARG_INFO();
376
377 static
378 ZEND_BEGIN_ARG_INFO(arginfo_arrayaccess_offset_value, 0)
379 ZEND_ARG_INFO(0, offset)
380 ZEND_ARG_INFO(0, value)
381 ZEND_END_ARG_INFO();
382
383 zend_function_entry zend_funcs_arrayaccess[] = {
384 ZEND_ABSTRACT_ME(arrayaccess, offsetExists, arginfo_arrayaccess_offset)
385 ZEND_ABSTRACT_ME(arrayaccess, offsetGet, arginfo_arrayaccess_offset)
386 ZEND_ABSTRACT_ME(arrayaccess, offsetSet, arginfo_arrayaccess_offset_value)
387 ZEND_ABSTRACT_ME(arrayaccess, offsetUnset, arginfo_arrayaccess_offset)
388 {NULL, NULL, NULL}
389 };
390
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
391 /* }}} */
392
393 #define REGISTER_ITERATOR_INTERFACE(class_name, class_name_str) \
394 {\
395 zend_class_entry ce;\
396 INIT_CLASS_ENTRY(ce, # class_name_str, zend_funcs_ ## class_name) \
397 zend_ce_ ## class_name = zend_register_internal_interface(&ce TSRMLS_CC);\
398 zend_ce_ ## class_name->interface_gets_implemented = zend_implement_ ## class_name;\
399 }
400
401 #define REGISTER_ITERATOR_IMPLEMENT(class_name, interface_name) \
402 zend_class_implements(zend_ce_ ## class_name TSRMLS_CC, 1, zend_ce_ ## interface_name)
403
404 /* {{{ zend_register_interfaces */
405 ZEND_API void zend_register_interfaces(TSRMLS_D)
406 {
407 REGISTER_ITERATOR_INTERFACE(traversable, Traversable);
408
409 REGISTER_ITERATOR_INTERFACE(aggregate, IteratorAggregate);
410 REGISTER_ITERATOR_IMPLEMENT(aggregate, traversable);
411
412 REGISTER_ITERATOR_INTERFACE(iterator, Iterator);
413 REGISTER_ITERATOR_IMPLEMENT(iterator, traversable);
4dba05d Add new interface ArrayAccess to use objects as Arrays
Marcus Boerger authored
414
415 REGISTER_ITERATOR_INTERFACE(arrayaccess, ArrayAccess);
8abb3bd Impement userspace iterator interfaces and tests. See tests for details
Marcus Boerger authored
416 }
417 /* }}} */
418
419 /*
420 * Local variables:
421 * tab-width: 4
422 * c-basic-offset: 4
423 * indent-tabs-mode: t
424 * End:
425 */
Something went wrong with that request. Please try again.