1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 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 #include "zend.h"
20 #include "zend_API.h"
21 #include "zend_interfaces.h"
22 #include "zend_exceptions.h"
23 #include "zend_interfaces_arginfo.h"
24
25 ZEND_API zend_class_entry *zend_ce_traversable;
26 ZEND_API zend_class_entry *zend_ce_aggregate;
27 ZEND_API zend_class_entry *zend_ce_iterator;
28 ZEND_API zend_class_entry *zend_ce_arrayaccess;
29 ZEND_API zend_class_entry *zend_ce_serializable;
30 ZEND_API zend_class_entry *zend_ce_countable;
31 ZEND_API zend_class_entry *zend_ce_stringable;
32 ZEND_API zend_class_entry *zend_ce_internal_iterator;
33
34 static zend_object_handlers zend_internal_iterator_handlers;
35
36 /* {{{ zend_call_method
37 Only returns the returned zval if retval_ptr != NULL */
zend_call_method(zend_object * object,zend_class_entry * obj_ce,zend_function ** fn_proxy,const char * function_name,size_t function_name_len,zval * retval_ptr,uint32_t param_count,zval * arg1,zval * arg2)38 ZEND_API zval* zend_call_method(zend_object *object, zend_class_entry *obj_ce, zend_function **fn_proxy, const char *function_name, size_t function_name_len, zval *retval_ptr, uint32_t param_count, zval* arg1, zval* arg2)
39 {
40 zend_function *fn;
41 zend_class_entry *called_scope;
42 zval params[2];
43
44 if (param_count > 0) {
45 ZVAL_COPY_VALUE(¶ms[0], arg1);
46 }
47 if (param_count > 1) {
48 ZVAL_COPY_VALUE(¶ms[1], arg2);
49 }
50
51 if (!obj_ce) {
52 obj_ce = object ? object->ce : NULL;
53 }
54 if (!fn_proxy || !*fn_proxy) {
55 if (EXPECTED(obj_ce)) {
56 fn = zend_hash_str_find_ptr_lc(
57 &obj_ce->function_table, function_name, function_name_len);
58 if (UNEXPECTED(fn == NULL)) {
59 /* error at c-level */
60 zend_error_noreturn(E_CORE_ERROR, "Couldn't find implementation for method %s::%s", ZSTR_VAL(obj_ce->name), function_name);
61 }
62 } else {
63 fn = zend_fetch_function_str(function_name, function_name_len);
64 if (UNEXPECTED(fn == NULL)) {
65 /* error at c-level */
66 zend_error_noreturn(E_CORE_ERROR, "Couldn't find implementation for function %s", function_name);
67 }
68 }
69 if (fn_proxy) {
70 *fn_proxy = fn;
71 }
72 } else {
73 fn = *fn_proxy;
74 }
75
76 if (object) {
77 called_scope = object->ce;
78 } else {
79 called_scope = obj_ce;
80 }
81
82 zend_call_known_function(fn, object, called_scope, retval_ptr, param_count, params, NULL);
83 return retval_ptr;
84 }
85 /* }}} */
86
87 /* iterator interface, c-level functions used by engine */
88
89 /* {{{ zend_user_it_new_iterator */
zend_user_it_new_iterator(zend_class_entry * ce,zval * object,zval * retval)90 ZEND_API void zend_user_it_new_iterator(zend_class_entry *ce, zval *object, zval *retval)
91 {
92 zend_call_known_instance_method_with_0_params(
93 ce->iterator_funcs_ptr->zf_new_iterator, Z_OBJ_P(object), retval);
94 }
95 /* }}} */
96
97 /* {{{ zend_user_it_invalidate_current */
zend_user_it_invalidate_current(zend_object_iterator * _iter)98 ZEND_API void zend_user_it_invalidate_current(zend_object_iterator *_iter)
99 {
100 zend_user_iterator *iter = (zend_user_iterator*)_iter;
101
102 if (!Z_ISUNDEF(iter->value)) {
103 zval_ptr_dtor(&iter->value);
104 ZVAL_UNDEF(&iter->value);
105 }
106 }
107 /* }}} */
108
109 /* {{{ zend_user_it_dtor */
zend_user_it_dtor(zend_object_iterator * _iter)110 static void zend_user_it_dtor(zend_object_iterator *_iter)
111 {
112 zend_user_iterator *iter = (zend_user_iterator*)_iter;
113 zval *object = &iter->it.data;
114
115 zend_user_it_invalidate_current(_iter);
116 zval_ptr_dtor(object);
117 }
118 /* }}} */
119
120 /* {{{ zend_user_it_valid */
zend_user_it_valid(zend_object_iterator * _iter)121 ZEND_API int zend_user_it_valid(zend_object_iterator *_iter)
122 {
123 if (_iter) {
124 zend_user_iterator *iter = (zend_user_iterator*)_iter;
125 zval *object = &iter->it.data;
126 zval more;
127 bool result;
128
129 zend_call_method_with_0_params(Z_OBJ_P(object), iter->ce, &iter->ce->iterator_funcs_ptr->zf_valid, "valid", &more);
130 result = i_zend_is_true(&more);
131 zval_ptr_dtor(&more);
132 return result ? SUCCESS : FAILURE;
133 }
134 return FAILURE;
135 }
136 /* }}} */
137
138 /* {{{ zend_user_it_get_current_data */
zend_user_it_get_current_data(zend_object_iterator * _iter)139 ZEND_API zval *zend_user_it_get_current_data(zend_object_iterator *_iter)
140 {
141 zend_user_iterator *iter = (zend_user_iterator*)_iter;
142 zval *object = &iter->it.data;
143
144 if (Z_ISUNDEF(iter->value)) {
145 zend_call_method_with_0_params(Z_OBJ_P(object), iter->ce, &iter->ce->iterator_funcs_ptr->zf_current, "current", &iter->value);
146 }
147 return &iter->value;
148 }
149 /* }}} */
150
151 /* {{{ zend_user_it_get_current_key */
zend_user_it_get_current_key(zend_object_iterator * _iter,zval * key)152 ZEND_API void zend_user_it_get_current_key(zend_object_iterator *_iter, zval *key)
153 {
154 zend_user_iterator *iter = (zend_user_iterator*)_iter;
155 zval *object = &iter->it.data;
156 zend_call_method_with_0_params(Z_OBJ_P(object), iter->ce, &iter->ce->iterator_funcs_ptr->zf_key, "key", key);
157 if (UNEXPECTED(Z_ISREF_P(key))) {
158 zend_unwrap_reference(key);
159 }
160 }
161 /* }}} */
162
163 /* {{{ zend_user_it_move_forward */
zend_user_it_move_forward(zend_object_iterator * _iter)164 ZEND_API void zend_user_it_move_forward(zend_object_iterator *_iter)
165 {
166 zend_user_iterator *iter = (zend_user_iterator*)_iter;
167 zval *object = &iter->it.data;
168
169 zend_user_it_invalidate_current(_iter);
170 zend_call_method_with_0_params(Z_OBJ_P(object), iter->ce, &iter->ce->iterator_funcs_ptr->zf_next, "next", NULL);
171 }
172 /* }}} */
173
174 /* {{{ zend_user_it_rewind */
zend_user_it_rewind(zend_object_iterator * _iter)175 ZEND_API void zend_user_it_rewind(zend_object_iterator *_iter)
176 {
177 zend_user_iterator *iter = (zend_user_iterator*)_iter;
178 zval *object = &iter->it.data;
179
180 zend_user_it_invalidate_current(_iter);
181 zend_call_method_with_0_params(Z_OBJ_P(object), iter->ce, &iter->ce->iterator_funcs_ptr->zf_rewind, "rewind", NULL);
182 }
183 /* }}} */
184
zend_user_it_get_gc(zend_object_iterator * _iter,zval ** table,int * n)185 ZEND_API HashTable *zend_user_it_get_gc(zend_object_iterator *_iter, zval **table, int *n)
186 {
187 zend_user_iterator *iter = (zend_user_iterator*)_iter;
188 if (Z_ISUNDEF(iter->value)) {
189 *table = &iter->it.data;
190 *n = 1;
191 } else {
192 zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
193 zend_get_gc_buffer_add_zval(gc_buffer, &iter->it.data);
194 zend_get_gc_buffer_add_zval(gc_buffer, &iter->value);
195 zend_get_gc_buffer_use(gc_buffer, table, n);
196 }
197 return NULL;
198 }
199
200 static const zend_object_iterator_funcs zend_interface_iterator_funcs_iterator = {
201 zend_user_it_dtor,
202 zend_user_it_valid,
203 zend_user_it_get_current_data,
204 zend_user_it_get_current_key,
205 zend_user_it_move_forward,
206 zend_user_it_rewind,
207 zend_user_it_invalidate_current,
208 zend_user_it_get_gc,
209 };
210
211 /* {{{ zend_user_it_get_iterator */
212 /* by_ref is int due to Iterator API */
zend_user_it_get_iterator(zend_class_entry * ce,zval * object,int by_ref)213 static zend_object_iterator *zend_user_it_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
214 {
215 zend_user_iterator *iterator;
216
217 if (by_ref) {
218 zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
219 return NULL;
220 }
221
222 iterator = emalloc(sizeof(zend_user_iterator));
223
224 zend_iterator_init((zend_object_iterator*)iterator);
225
226 ZVAL_OBJ_COPY(&iterator->it.data, Z_OBJ_P(object));
227 iterator->it.funcs = &zend_interface_iterator_funcs_iterator;
228 iterator->ce = Z_OBJCE_P(object);
229 ZVAL_UNDEF(&iterator->value);
230 return (zend_object_iterator*)iterator;
231 }
232 /* }}} */
233
234 /* {{{ zend_user_it_get_new_iterator */
235 /* by_ref is int due to Iterator API */
zend_user_it_get_new_iterator(zend_class_entry * ce,zval * object,int by_ref)236 ZEND_API zend_object_iterator *zend_user_it_get_new_iterator(zend_class_entry *ce, zval *object, int by_ref)
237 {
238 zval iterator;
239 zend_object_iterator *new_iterator;
240 zend_class_entry *ce_it;
241
242 zend_user_it_new_iterator(ce, object, &iterator);
243 ce_it = (Z_TYPE(iterator) == IS_OBJECT) ? Z_OBJCE(iterator) : NULL;
244
245 if (!ce_it || !ce_it->get_iterator || (ce_it->get_iterator == zend_user_it_get_new_iterator && Z_OBJ(iterator) == Z_OBJ_P(object))) {
246 if (!EG(exception)) {
247 zend_throw_exception_ex(NULL, 0, "Objects returned by %s::getIterator() must be traversable or implement interface Iterator", ce ? ZSTR_VAL(ce->name) : ZSTR_VAL(Z_OBJCE_P(object)->name));
248 }
249 zval_ptr_dtor(&iterator);
250 return NULL;
251 }
252
253 new_iterator = ce_it->get_iterator(ce_it, &iterator, by_ref);
254 zval_ptr_dtor(&iterator);
255 return new_iterator;
256 }
257 /* }}} */
258
259 /* {{{ zend_implement_traversable */
zend_implement_traversable(zend_class_entry * interface,zend_class_entry * class_type)260 static int zend_implement_traversable(zend_class_entry *interface, zend_class_entry *class_type)
261 {
262 /* Abstract class can implement Traversable only, in which case the extending class must
263 * implement Iterator or IteratorAggregate. */
264 if (class_type->ce_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
265 return SUCCESS;
266 }
267
268 /* Check that class_type implements at least one of 'IteratorAggregate' or 'Iterator' */
269 if (class_type->num_interfaces) {
270 ZEND_ASSERT(class_type->ce_flags & ZEND_ACC_RESOLVED_INTERFACES);
271 for (uint32_t i = 0; i < class_type->num_interfaces; i++) {
272 if (class_type->interfaces[i] == zend_ce_aggregate || class_type->interfaces[i] == zend_ce_iterator) {
273 return SUCCESS;
274 }
275 }
276 }
277 zend_error_noreturn(E_CORE_ERROR, "Class %s must implement interface %s as part of either %s or %s",
278 ZSTR_VAL(class_type->name),
279 ZSTR_VAL(zend_ce_traversable->name),
280 ZSTR_VAL(zend_ce_iterator->name),
281 ZSTR_VAL(zend_ce_aggregate->name));
282 return FAILURE;
283 }
284 /* }}} */
285
286 /* {{{ zend_implement_aggregate */
zend_implement_aggregate(zend_class_entry * interface,zend_class_entry * class_type)287 static int zend_implement_aggregate(zend_class_entry *interface, zend_class_entry *class_type)
288 {
289 if (zend_class_implements_interface(class_type, zend_ce_iterator)) {
290 zend_error_noreturn(E_ERROR,
291 "Class %s cannot implement both Iterator and IteratorAggregate at the same time",
292 ZSTR_VAL(class_type->name));
293 }
294
295 zend_function *zf = zend_hash_str_find_ptr(
296 &class_type->function_table, "getiterator", sizeof("getiterator") - 1);
297 if (class_type->get_iterator && class_type->get_iterator != zend_user_it_get_new_iterator) {
298 /* get_iterator was explicitly assigned for an internal class. */
299 if (!class_type->parent || class_type->parent->get_iterator != class_type->get_iterator) {
300 ZEND_ASSERT(class_type->type == ZEND_INTERNAL_CLASS);
301 return SUCCESS;
302 }
303
304 /* The getIterator() method has not been overwritten, use inherited get_iterator(). */
305 if (zf->common.scope != class_type) {
306 return SUCCESS;
307 }
308
309 /* getIterator() has been overwritten, switch to zend_user_it_get_new_iterator. */
310 }
311
312 ZEND_ASSERT(!class_type->iterator_funcs_ptr && "Iterator funcs already set?");
313 zend_class_iterator_funcs *funcs_ptr = class_type->type == ZEND_INTERNAL_CLASS
314 ? pemalloc(sizeof(zend_class_iterator_funcs), 1)
315 : zend_arena_alloc(&CG(arena), sizeof(zend_class_iterator_funcs));
316 class_type->get_iterator = zend_user_it_get_new_iterator;
317 class_type->iterator_funcs_ptr = funcs_ptr;
318
319 memset(funcs_ptr, 0, sizeof(zend_class_iterator_funcs));
320 funcs_ptr->zf_new_iterator = zf;
321
322 return SUCCESS;
323 }
324 /* }}} */
325
326 /* {{{ zend_implement_iterator */
zend_implement_iterator(zend_class_entry * interface,zend_class_entry * class_type)327 static int zend_implement_iterator(zend_class_entry *interface, zend_class_entry *class_type)
328 {
329 if (zend_class_implements_interface(class_type, zend_ce_aggregate)) {
330 zend_error_noreturn(E_ERROR,
331 "Class %s cannot implement both Iterator and IteratorAggregate at the same time",
332 ZSTR_VAL(class_type->name));
333 }
334
335 if (class_type->get_iterator && class_type->get_iterator != zend_user_it_get_iterator) {
336 if (!class_type->parent || class_type->parent->get_iterator != class_type->get_iterator) {
337 /* get_iterator was explicitly assigned for an internal class. */
338 ZEND_ASSERT(class_type->type == ZEND_INTERNAL_CLASS);
339 return SUCCESS;
340 }
341 /* Otherwise get_iterator was inherited from the parent by default. */
342 }
343
344 if (class_type->parent && (class_type->parent->ce_flags & ZEND_ACC_REUSE_GET_ITERATOR)) {
345 /* Keep the inherited get_iterator handler. */
346 class_type->ce_flags |= ZEND_ACC_REUSE_GET_ITERATOR;
347 } else {
348 class_type->get_iterator = zend_user_it_get_iterator;
349 }
350
351 ZEND_ASSERT(!class_type->iterator_funcs_ptr && "Iterator funcs already set?");
352 zend_class_iterator_funcs *funcs_ptr = class_type->type == ZEND_INTERNAL_CLASS
353 ? pemalloc(sizeof(zend_class_iterator_funcs), 1)
354 : zend_arena_alloc(&CG(arena), sizeof(zend_class_iterator_funcs));
355 memset(funcs_ptr, 0, sizeof(zend_class_iterator_funcs));
356 class_type->iterator_funcs_ptr = funcs_ptr;
357
358 return SUCCESS;
359 }
360 /* }}} */
361
362 /* {{{ zend_user_serialize */
zend_user_serialize(zval * object,unsigned char ** buffer,size_t * buf_len,zend_serialize_data * data)363 ZEND_API int zend_user_serialize(zval *object, unsigned char **buffer, size_t *buf_len, zend_serialize_data *data)
364 {
365 zend_class_entry * ce = Z_OBJCE_P(object);
366 zval retval;
367 zend_result result;
368
369 zend_call_method_with_0_params(
370 Z_OBJ_P(object), Z_OBJCE_P(object), NULL, "serialize", &retval);
371
372 if (Z_TYPE(retval) == IS_UNDEF || EG(exception)) {
373 result = FAILURE;
374 } else {
375 switch(Z_TYPE(retval)) {
376 case IS_NULL:
377 /* we could also make this '*buf_len = 0' but this allows to skip variables */
378 zval_ptr_dtor(&retval);
379 return FAILURE;
380 case IS_STRING:
381 *buffer = (unsigned char*)estrndup(Z_STRVAL(retval), Z_STRLEN(retval));
382 *buf_len = Z_STRLEN(retval);
383 result = SUCCESS;
384 break;
385 default: /* failure */
386 result = FAILURE;
387 break;
388 }
389 zval_ptr_dtor(&retval);
390 }
391
392 if (result == FAILURE && !EG(exception)) {
393 zend_throw_exception_ex(NULL, 0, "%s::serialize() must return a string or NULL", ZSTR_VAL(ce->name));
394 }
395 return result;
396 }
397 /* }}} */
398
399 /* {{{ zend_user_unserialize */
zend_user_unserialize(zval * object,zend_class_entry * ce,const unsigned char * buf,size_t buf_len,zend_unserialize_data * data)400 ZEND_API int zend_user_unserialize(zval *object, zend_class_entry *ce, const unsigned char *buf, size_t buf_len, zend_unserialize_data *data)
401 {
402 zval zdata;
403
404 if (UNEXPECTED(object_init_ex(object, ce) != SUCCESS)) {
405 return FAILURE;
406 }
407
408 ZVAL_STRINGL(&zdata, (char*)buf, buf_len);
409 zend_call_method_with_1_params(
410 Z_OBJ_P(object), Z_OBJCE_P(object), NULL, "unserialize", NULL, &zdata);
411 zval_ptr_dtor(&zdata);
412
413 if (EG(exception)) {
414 return FAILURE;
415 } else {
416 return SUCCESS;
417 }
418 }
419 /* }}} */
420
421 /* {{{ zend_implement_serializable */
zend_implement_serializable(zend_class_entry * interface,zend_class_entry * class_type)422 static int zend_implement_serializable(zend_class_entry *interface, zend_class_entry *class_type)
423 {
424 if (class_type->parent
425 && (class_type->parent->serialize || class_type->parent->unserialize)
426 && !zend_class_implements_interface(class_type->parent, zend_ce_serializable)) {
427 return FAILURE;
428 }
429 if (!class_type->serialize) {
430 class_type->serialize = zend_user_serialize;
431 }
432 if (!class_type->unserialize) {
433 class_type->unserialize = zend_user_unserialize;
434 }
435 if (!(class_type->ce_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)
436 && (!class_type->__serialize || !class_type->__unserialize)) {
437 zend_error(E_DEPRECATED, "%s implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary)", ZSTR_VAL(class_type->name));
438 }
439 return SUCCESS;
440 }
441 /* }}}*/
442
443 typedef struct {
444 zend_object std;
445 zend_object_iterator *iter;
446 bool rewind_called;
447 } zend_internal_iterator;
448
zend_internal_iterator_create(zend_class_entry * ce)449 static zend_object *zend_internal_iterator_create(zend_class_entry *ce) {
450 zend_internal_iterator *intern = emalloc(sizeof(zend_internal_iterator));
451 zend_object_std_init(&intern->std, ce);
452 intern->std.handlers = &zend_internal_iterator_handlers;
453 intern->iter = NULL;
454 intern->rewind_called = 0;
455 return &intern->std;
456 }
457
zend_create_internal_iterator_zval(zval * return_value,zval * obj)458 ZEND_API zend_result zend_create_internal_iterator_zval(zval *return_value, zval *obj) {
459 zend_class_entry *scope = EG(current_execute_data)->func->common.scope;
460 ZEND_ASSERT(scope->get_iterator != zend_user_it_get_new_iterator);
461 zend_object_iterator *iter = scope->get_iterator(Z_OBJCE_P(obj), obj, /* by_ref */ 0);
462 if (!iter) {
463 return FAILURE;
464 }
465
466 zend_internal_iterator *intern =
467 (zend_internal_iterator *) zend_internal_iterator_create(zend_ce_internal_iterator);
468 intern->iter = iter;
469 intern->iter->index = 0;
470 ZVAL_OBJ(return_value, &intern->std);
471 return SUCCESS;
472 }
473
zend_internal_iterator_free(zend_object * obj)474 static void zend_internal_iterator_free(zend_object *obj) {
475 zend_internal_iterator *intern = (zend_internal_iterator *) obj;
476 if (intern->iter) {
477 zend_iterator_dtor(intern->iter);
478 }
479 zend_object_std_dtor(&intern->std);
480 }
481
zend_internal_iterator_fetch(zval * This)482 static zend_internal_iterator *zend_internal_iterator_fetch(zval *This) {
483 zend_internal_iterator *intern = (zend_internal_iterator *) Z_OBJ_P(This);
484 if (!intern->iter) {
485 zend_throw_error(NULL, "The InternalIterator object has not been properly initialized");
486 return NULL;
487 }
488 return intern;
489 }
490
491 /* Many iterators will not behave correctly if rewind() is not called, make sure it happens. */
zend_internal_iterator_ensure_rewound(zend_internal_iterator * intern)492 static zend_result zend_internal_iterator_ensure_rewound(zend_internal_iterator *intern) {
493 if (!intern->rewind_called) {
494 zend_object_iterator *iter = intern->iter;
495 intern->rewind_called = 1;
496 if (iter->funcs->rewind) {
497 iter->funcs->rewind(iter);
498 if (UNEXPECTED(EG(exception))) {
499 return FAILURE;
500 }
501 }
502 }
503 return SUCCESS;
504 }
505
506
ZEND_METHOD(InternalIterator,__construct)507 ZEND_METHOD(InternalIterator, __construct) {
508 zend_throw_error(NULL, "Cannot manually construct InternalIterator");
509 }
510
ZEND_METHOD(InternalIterator,current)511 ZEND_METHOD(InternalIterator, current) {
512 ZEND_PARSE_PARAMETERS_NONE();
513
514 zend_internal_iterator *intern = zend_internal_iterator_fetch(ZEND_THIS);
515 if (!intern) {
516 RETURN_THROWS();
517 }
518
519 if (zend_internal_iterator_ensure_rewound(intern) == FAILURE) {
520 RETURN_THROWS();
521 }
522
523 zval *data = intern->iter->funcs->get_current_data(intern->iter);
524 if (data) {
525 RETURN_COPY_DEREF(data);
526 }
527 }
528
ZEND_METHOD(InternalIterator,key)529 ZEND_METHOD(InternalIterator, key) {
530 ZEND_PARSE_PARAMETERS_NONE();
531
532 zend_internal_iterator *intern = zend_internal_iterator_fetch(ZEND_THIS);
533 if (!intern) {
534 RETURN_THROWS();
535 }
536
537 if (zend_internal_iterator_ensure_rewound(intern) == FAILURE) {
538 RETURN_THROWS();
539 }
540
541 if (intern->iter->funcs->get_current_key) {
542 intern->iter->funcs->get_current_key(intern->iter, return_value);
543 } else {
544 RETURN_LONG(intern->iter->index);
545 }
546 }
547
ZEND_METHOD(InternalIterator,next)548 ZEND_METHOD(InternalIterator, next) {
549 ZEND_PARSE_PARAMETERS_NONE();
550
551 zend_internal_iterator *intern = zend_internal_iterator_fetch(ZEND_THIS);
552 if (!intern) {
553 RETURN_THROWS();
554 }
555
556 if (zend_internal_iterator_ensure_rewound(intern) == FAILURE) {
557 RETURN_THROWS();
558 }
559
560 /* Advance index first to match foreach behavior. */
561 intern->iter->index++;
562 intern->iter->funcs->move_forward(intern->iter);
563 }
564
ZEND_METHOD(InternalIterator,valid)565 ZEND_METHOD(InternalIterator, valid) {
566 ZEND_PARSE_PARAMETERS_NONE();
567
568 zend_internal_iterator *intern = zend_internal_iterator_fetch(ZEND_THIS);
569 if (!intern) {
570 RETURN_THROWS();
571 }
572
573 if (zend_internal_iterator_ensure_rewound(intern) == FAILURE) {
574 RETURN_THROWS();
575 }
576
577 RETURN_BOOL(intern->iter->funcs->valid(intern->iter) == SUCCESS);
578 }
579
ZEND_METHOD(InternalIterator,rewind)580 ZEND_METHOD(InternalIterator, rewind) {
581 ZEND_PARSE_PARAMETERS_NONE();
582
583 zend_internal_iterator *intern = zend_internal_iterator_fetch(ZEND_THIS);
584 if (!intern) {
585 RETURN_THROWS();
586 }
587
588 intern->rewind_called = 1;
589 if (!intern->iter->funcs->rewind) {
590 /* Allow calling rewind() if no iteration has happened yet,
591 * even if the iterator does not support rewinding. */
592 if (intern->iter->index != 0) {
593 zend_throw_error(NULL, "Iterator does not support rewinding");
594 RETURN_THROWS();
595 }
596 intern->iter->index = 0;
597 return;
598 }
599
600 intern->iter->funcs->rewind(intern->iter);
601 intern->iter->index = 0;
602 }
603
604 /* {{{ zend_register_interfaces */
zend_register_interfaces(void)605 ZEND_API void zend_register_interfaces(void)
606 {
607 zend_ce_traversable = register_class_Traversable();
608 zend_ce_traversable->interface_gets_implemented = zend_implement_traversable;
609
610 zend_ce_aggregate = register_class_IteratorAggregate(zend_ce_traversable);
611 zend_ce_aggregate->interface_gets_implemented = zend_implement_aggregate;
612
613 zend_ce_iterator = register_class_Iterator(zend_ce_traversable);
614 zend_ce_iterator->interface_gets_implemented = zend_implement_iterator;
615
616 zend_ce_serializable = register_class_Serializable();
617 zend_ce_serializable->interface_gets_implemented = zend_implement_serializable;
618
619 zend_ce_arrayaccess = register_class_ArrayAccess();
620
621 zend_ce_countable = register_class_Countable();
622
623 zend_ce_stringable = register_class_Stringable();
624
625 zend_ce_internal_iterator = register_class_InternalIterator(zend_ce_iterator);
626 zend_ce_internal_iterator->create_object = zend_internal_iterator_create;
627
628 memcpy(&zend_internal_iterator_handlers, zend_get_std_object_handlers(),
629 sizeof(zend_object_handlers));
630 zend_internal_iterator_handlers.free_obj = zend_internal_iterator_free;
631 }
632 /* }}} */
633