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
185 static const zend_object_iterator_funcs zend_interface_iterator_funcs_iterator = {
186 zend_user_it_dtor,
187 zend_user_it_valid,
188 zend_user_it_get_current_data,
189 zend_user_it_get_current_key,
190 zend_user_it_move_forward,
191 zend_user_it_rewind,
192 zend_user_it_invalidate_current,
193 NULL, /* get_gc */
194 };
195
196 /* {{{ zend_user_it_get_iterator */
197 /* by_ref is int due to Iterator API */
zend_user_it_get_iterator(zend_class_entry * ce,zval * object,int by_ref)198 static zend_object_iterator *zend_user_it_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
199 {
200 zend_user_iterator *iterator;
201
202 if (by_ref) {
203 zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
204 return NULL;
205 }
206
207 iterator = emalloc(sizeof(zend_user_iterator));
208
209 zend_iterator_init((zend_object_iterator*)iterator);
210
211 ZVAL_OBJ_COPY(&iterator->it.data, Z_OBJ_P(object));
212 iterator->it.funcs = &zend_interface_iterator_funcs_iterator;
213 iterator->ce = Z_OBJCE_P(object);
214 ZVAL_UNDEF(&iterator->value);
215 return (zend_object_iterator*)iterator;
216 }
217 /* }}} */
218
219 /* {{{ zend_user_it_get_new_iterator */
220 /* by_ref is int due to Iterator API */
zend_user_it_get_new_iterator(zend_class_entry * ce,zval * object,int by_ref)221 ZEND_API zend_object_iterator *zend_user_it_get_new_iterator(zend_class_entry *ce, zval *object, int by_ref)
222 {
223 zval iterator;
224 zend_object_iterator *new_iterator;
225 zend_class_entry *ce_it;
226
227 zend_user_it_new_iterator(ce, object, &iterator);
228 ce_it = (Z_TYPE(iterator) == IS_OBJECT) ? Z_OBJCE(iterator) : NULL;
229
230 if (!ce_it || !ce_it->get_iterator || (ce_it->get_iterator == zend_user_it_get_new_iterator && Z_OBJ(iterator) == Z_OBJ_P(object))) {
231 if (!EG(exception)) {
232 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));
233 }
234 zval_ptr_dtor(&iterator);
235 return NULL;
236 }
237
238 new_iterator = ce_it->get_iterator(ce_it, &iterator, by_ref);
239 zval_ptr_dtor(&iterator);
240 return new_iterator;
241 }
242 /* }}} */
243
244 /* {{{ zend_implement_traversable */
zend_implement_traversable(zend_class_entry * interface,zend_class_entry * class_type)245 static int zend_implement_traversable(zend_class_entry *interface, zend_class_entry *class_type)
246 {
247 /* Abstract class can implement Traversable only, in which case the extending class must
248 * implement Iterator or IteratorAggregate. */
249 if (class_type->ce_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
250 return SUCCESS;
251 }
252
253 /* Check that class_type implements at least one of 'IteratorAggregate' or 'Iterator' */
254 if (class_type->num_interfaces) {
255 ZEND_ASSERT(class_type->ce_flags & ZEND_ACC_RESOLVED_INTERFACES);
256 for (uint32_t i = 0; i < class_type->num_interfaces; i++) {
257 if (class_type->interfaces[i] == zend_ce_aggregate || class_type->interfaces[i] == zend_ce_iterator) {
258 return SUCCESS;
259 }
260 }
261 }
262 zend_error_noreturn(E_CORE_ERROR, "Class %s must implement interface %s as part of either %s or %s",
263 ZSTR_VAL(class_type->name),
264 ZSTR_VAL(zend_ce_traversable->name),
265 ZSTR_VAL(zend_ce_iterator->name),
266 ZSTR_VAL(zend_ce_aggregate->name));
267 return FAILURE;
268 }
269 /* }}} */
270
271 /* {{{ zend_implement_aggregate */
zend_implement_aggregate(zend_class_entry * interface,zend_class_entry * class_type)272 static int zend_implement_aggregate(zend_class_entry *interface, zend_class_entry *class_type)
273 {
274 if (zend_class_implements_interface(class_type, zend_ce_iterator)) {
275 zend_error_noreturn(E_ERROR,
276 "Class %s cannot implement both Iterator and IteratorAggregate at the same time",
277 ZSTR_VAL(class_type->name));
278 }
279
280 zend_function *zf = zend_hash_str_find_ptr(
281 &class_type->function_table, "getiterator", sizeof("getiterator") - 1);
282 if (class_type->get_iterator && class_type->get_iterator != zend_user_it_get_new_iterator) {
283 /* get_iterator was explicitly assigned for an internal class. */
284 if (!class_type->parent || class_type->parent->get_iterator != class_type->get_iterator) {
285 ZEND_ASSERT(class_type->type == ZEND_INTERNAL_CLASS);
286 return SUCCESS;
287 }
288
289 /* The getIterator() method has not been overwritten, use inherited get_iterator(). */
290 if (zf->common.scope != class_type) {
291 return SUCCESS;
292 }
293
294 /* getIterator() has been overwritten, switch to zend_user_it_get_new_iterator. */
295 }
296
297 ZEND_ASSERT(!class_type->iterator_funcs_ptr && "Iterator funcs already set?");
298 zend_class_iterator_funcs *funcs_ptr = class_type->type == ZEND_INTERNAL_CLASS
299 ? pemalloc(sizeof(zend_class_iterator_funcs), 1)
300 : zend_arena_alloc(&CG(arena), sizeof(zend_class_iterator_funcs));
301 class_type->get_iterator = zend_user_it_get_new_iterator;
302 class_type->iterator_funcs_ptr = funcs_ptr;
303
304 memset(funcs_ptr, 0, sizeof(zend_class_iterator_funcs));
305 funcs_ptr->zf_new_iterator = zf;
306
307 return SUCCESS;
308 }
309 /* }}} */
310
311 /* {{{ zend_implement_iterator */
zend_implement_iterator(zend_class_entry * interface,zend_class_entry * class_type)312 static int zend_implement_iterator(zend_class_entry *interface, zend_class_entry *class_type)
313 {
314 if (zend_class_implements_interface(class_type, zend_ce_aggregate)) {
315 zend_error_noreturn(E_ERROR,
316 "Class %s cannot implement both Iterator and IteratorAggregate at the same time",
317 ZSTR_VAL(class_type->name));
318 }
319
320 if (class_type->get_iterator && class_type->get_iterator != zend_user_it_get_iterator) {
321 if (!class_type->parent || class_type->parent->get_iterator != class_type->get_iterator) {
322 /* get_iterator was explicitly assigned for an internal class. */
323 ZEND_ASSERT(class_type->type == ZEND_INTERNAL_CLASS);
324 return SUCCESS;
325 }
326 /* Otherwise get_iterator was inherited from the parent by default. */
327 }
328
329 if (class_type->parent && (class_type->parent->ce_flags & ZEND_ACC_REUSE_GET_ITERATOR)) {
330 /* Keep the inherited get_iterator handler. */
331 class_type->ce_flags |= ZEND_ACC_REUSE_GET_ITERATOR;
332 } else {
333 class_type->get_iterator = zend_user_it_get_iterator;
334 }
335
336 ZEND_ASSERT(!class_type->iterator_funcs_ptr && "Iterator funcs already set?");
337 zend_class_iterator_funcs *funcs_ptr = class_type->type == ZEND_INTERNAL_CLASS
338 ? pemalloc(sizeof(zend_class_iterator_funcs), 1)
339 : zend_arena_alloc(&CG(arena), sizeof(zend_class_iterator_funcs));
340 memset(funcs_ptr, 0, sizeof(zend_class_iterator_funcs));
341 class_type->iterator_funcs_ptr = funcs_ptr;
342
343 return SUCCESS;
344 }
345 /* }}} */
346
347 /* {{{ zend_user_serialize */
zend_user_serialize(zval * object,unsigned char ** buffer,size_t * buf_len,zend_serialize_data * data)348 ZEND_API int zend_user_serialize(zval *object, unsigned char **buffer, size_t *buf_len, zend_serialize_data *data)
349 {
350 zend_class_entry * ce = Z_OBJCE_P(object);
351 zval retval;
352 zend_result result;
353
354 zend_call_method_with_0_params(
355 Z_OBJ_P(object), Z_OBJCE_P(object), NULL, "serialize", &retval);
356
357 if (Z_TYPE(retval) == IS_UNDEF || EG(exception)) {
358 result = FAILURE;
359 } else {
360 switch(Z_TYPE(retval)) {
361 case IS_NULL:
362 /* we could also make this '*buf_len = 0' but this allows to skip variables */
363 zval_ptr_dtor(&retval);
364 return FAILURE;
365 case IS_STRING:
366 *buffer = (unsigned char*)estrndup(Z_STRVAL(retval), Z_STRLEN(retval));
367 *buf_len = Z_STRLEN(retval);
368 result = SUCCESS;
369 break;
370 default: /* failure */
371 result = FAILURE;
372 break;
373 }
374 zval_ptr_dtor(&retval);
375 }
376
377 if (result == FAILURE && !EG(exception)) {
378 zend_throw_exception_ex(NULL, 0, "%s::serialize() must return a string or NULL", ZSTR_VAL(ce->name));
379 }
380 return result;
381 }
382 /* }}} */
383
384 /* {{{ zend_user_unserialize */
zend_user_unserialize(zval * object,zend_class_entry * ce,const unsigned char * buf,size_t buf_len,zend_unserialize_data * data)385 ZEND_API int zend_user_unserialize(zval *object, zend_class_entry *ce, const unsigned char *buf, size_t buf_len, zend_unserialize_data *data)
386 {
387 zval zdata;
388
389 if (UNEXPECTED(object_init_ex(object, ce) != SUCCESS)) {
390 return FAILURE;
391 }
392
393 ZVAL_STRINGL(&zdata, (char*)buf, buf_len);
394 zend_call_method_with_1_params(
395 Z_OBJ_P(object), Z_OBJCE_P(object), NULL, "unserialize", NULL, &zdata);
396 zval_ptr_dtor(&zdata);
397
398 if (EG(exception)) {
399 return FAILURE;
400 } else {
401 return SUCCESS;
402 }
403 }
404 /* }}} */
405
zend_class_serialize_deny(zval * object,unsigned char ** buffer,size_t * buf_len,zend_serialize_data * data)406 ZEND_API int zend_class_serialize_deny(zval *object, unsigned char **buffer, size_t *buf_len, zend_serialize_data *data) /* {{{ */
407 {
408 zend_class_entry *ce = Z_OBJCE_P(object);
409 zend_throw_exception_ex(NULL, 0, "Serialization of '%s' is not allowed", ZSTR_VAL(ce->name));
410 return FAILURE;
411 }
412 /* }}} */
413
zend_class_unserialize_deny(zval * object,zend_class_entry * ce,const unsigned char * buf,size_t buf_len,zend_unserialize_data * data)414 ZEND_API int zend_class_unserialize_deny(zval *object, zend_class_entry *ce, const unsigned char *buf, size_t buf_len, zend_unserialize_data *data) /* {{{ */
415 {
416 zend_throw_exception_ex(NULL, 0, "Unserialization of '%s' is not allowed", ZSTR_VAL(ce->name));
417 return FAILURE;
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 return SUCCESS;
436 }
437 /* }}}*/
438
439 typedef struct {
440 zend_object std;
441 zend_object_iterator *iter;
442 zend_bool rewind_called;
443 } zend_internal_iterator;
444
zend_internal_iterator_create(zend_class_entry * ce)445 static zend_object *zend_internal_iterator_create(zend_class_entry *ce) {
446 zend_internal_iterator *intern = emalloc(sizeof(zend_internal_iterator));
447 zend_object_std_init(&intern->std, ce);
448 intern->std.handlers = &zend_internal_iterator_handlers;
449 intern->iter = NULL;
450 intern->rewind_called = 0;
451 return &intern->std;
452 }
453
zend_create_internal_iterator_zval(zval * return_value,zval * obj)454 ZEND_API zend_result zend_create_internal_iterator_zval(zval *return_value, zval *obj) {
455 zend_class_entry *scope = EG(current_execute_data)->func->common.scope;
456 ZEND_ASSERT(scope->get_iterator != zend_user_it_get_new_iterator);
457 zend_object_iterator *iter = scope->get_iterator(Z_OBJCE_P(obj), obj, /* by_ref */ 0);
458 if (!iter) {
459 return FAILURE;
460 }
461
462 zend_internal_iterator *intern =
463 (zend_internal_iterator *) zend_internal_iterator_create(zend_ce_internal_iterator);
464 intern->iter = iter;
465 intern->iter->index = 0;
466 ZVAL_OBJ(return_value, &intern->std);
467 return SUCCESS;
468 }
469
zend_internal_iterator_free(zend_object * obj)470 static void zend_internal_iterator_free(zend_object *obj) {
471 zend_internal_iterator *intern = (zend_internal_iterator *) obj;
472 if (intern->iter) {
473 zend_iterator_dtor(intern->iter);
474 }
475 zend_object_std_dtor(&intern->std);
476 }
477
zend_internal_iterator_fetch(zval * This)478 static zend_internal_iterator *zend_internal_iterator_fetch(zval *This) {
479 zend_internal_iterator *intern = (zend_internal_iterator *) Z_OBJ_P(This);
480 if (!intern->iter) {
481 zend_throw_error(NULL, "The InternalIterator object has not been properly initialized");
482 return NULL;
483 }
484 return intern;
485 }
486
487 /* Many iterators will not behave correctly if rewind() is not called, make sure it happens. */
zend_internal_iterator_ensure_rewound(zend_internal_iterator * intern)488 static zend_result zend_internal_iterator_ensure_rewound(zend_internal_iterator *intern) {
489 if (!intern->rewind_called) {
490 zend_object_iterator *iter = intern->iter;
491 intern->rewind_called = 1;
492 if (iter->funcs->rewind) {
493 iter->funcs->rewind(iter);
494 if (UNEXPECTED(EG(exception))) {
495 return FAILURE;
496 }
497 }
498 }
499 return SUCCESS;
500 }
501
502
ZEND_METHOD(InternalIterator,__construct)503 ZEND_METHOD(InternalIterator, __construct) {
504 zend_throw_error(NULL, "Cannot manually construct InternalIterator");
505 }
506
ZEND_METHOD(InternalIterator,current)507 ZEND_METHOD(InternalIterator, current) {
508 ZEND_PARSE_PARAMETERS_NONE();
509
510 zend_internal_iterator *intern = zend_internal_iterator_fetch(ZEND_THIS);
511 if (!intern) {
512 RETURN_THROWS();
513 }
514
515 if (zend_internal_iterator_ensure_rewound(intern) == FAILURE) {
516 RETURN_THROWS();
517 }
518
519 zval *data = intern->iter->funcs->get_current_data(intern->iter);
520 if (data) {
521 ZVAL_COPY_DEREF(return_value, data);
522 }
523 }
524
ZEND_METHOD(InternalIterator,key)525 ZEND_METHOD(InternalIterator, key) {
526 ZEND_PARSE_PARAMETERS_NONE();
527
528 zend_internal_iterator *intern = zend_internal_iterator_fetch(ZEND_THIS);
529 if (!intern) {
530 RETURN_THROWS();
531 }
532
533 if (zend_internal_iterator_ensure_rewound(intern) == FAILURE) {
534 RETURN_THROWS();
535 }
536
537 if (intern->iter->funcs->get_current_key) {
538 intern->iter->funcs->get_current_key(intern->iter, return_value);
539 } else {
540 RETURN_LONG(intern->iter->index);
541 }
542 }
543
ZEND_METHOD(InternalIterator,next)544 ZEND_METHOD(InternalIterator, next) {
545 ZEND_PARSE_PARAMETERS_NONE();
546
547 zend_internal_iterator *intern = zend_internal_iterator_fetch(ZEND_THIS);
548 if (!intern) {
549 RETURN_THROWS();
550 }
551
552 if (zend_internal_iterator_ensure_rewound(intern) == FAILURE) {
553 RETURN_THROWS();
554 }
555
556 /* Advance index first to match foreach behavior. */
557 intern->iter->index++;
558 intern->iter->funcs->move_forward(intern->iter);
559 }
560
ZEND_METHOD(InternalIterator,valid)561 ZEND_METHOD(InternalIterator, valid) {
562 ZEND_PARSE_PARAMETERS_NONE();
563
564 zend_internal_iterator *intern = zend_internal_iterator_fetch(ZEND_THIS);
565 if (!intern) {
566 RETURN_THROWS();
567 }
568
569 if (zend_internal_iterator_ensure_rewound(intern) == FAILURE) {
570 RETURN_THROWS();
571 }
572
573 RETURN_BOOL(intern->iter->funcs->valid(intern->iter) == SUCCESS);
574 }
575
ZEND_METHOD(InternalIterator,rewind)576 ZEND_METHOD(InternalIterator, rewind) {
577 ZEND_PARSE_PARAMETERS_NONE();
578
579 zend_internal_iterator *intern = zend_internal_iterator_fetch(ZEND_THIS);
580 if (!intern) {
581 RETURN_THROWS();
582 }
583
584 if (!intern->iter->funcs->rewind) {
585 /* Allow calling rewind() if no iteration has happened yet,
586 * even if the iterator does not support rewinding. */
587 if (intern->iter->index != 0) {
588 zend_throw_error(NULL, "Iterator does not support rewinding");
589 RETURN_THROWS();
590 }
591 intern->iter->index = 0;
592 return;
593 }
594
595 intern->iter->funcs->rewind(intern->iter);
596 intern->iter->index = 0;
597 }
598
599 /* {{{ zend_register_interfaces */
zend_register_interfaces(void)600 ZEND_API void zend_register_interfaces(void)
601 {
602 zend_class_entry ce;
603
604 REGISTER_MAGIC_INTERFACE(traversable, Traversable);
605
606 REGISTER_MAGIC_INTERFACE(aggregate, IteratorAggregate);
607 REGISTER_MAGIC_IMPLEMENT(aggregate, traversable);
608
609 REGISTER_MAGIC_INTERFACE(iterator, Iterator);
610 REGISTER_MAGIC_IMPLEMENT(iterator, traversable);
611
612 REGISTER_MAGIC_INTERFACE(serializable, Serializable);
613
614 INIT_CLASS_ENTRY(ce, "ArrayAccess", class_ArrayAccess_methods);
615 zend_ce_arrayaccess = zend_register_internal_interface(&ce);
616
617 INIT_CLASS_ENTRY(ce, "Countable", class_Countable_methods);
618 zend_ce_countable = zend_register_internal_interface(&ce);
619
620 INIT_CLASS_ENTRY(ce, "Stringable", class_Stringable_methods);
621 zend_ce_stringable = zend_register_internal_interface(&ce);
622
623 INIT_CLASS_ENTRY(ce, "InternalIterator", class_InternalIterator_methods);
624 zend_ce_internal_iterator = zend_register_internal_class(&ce);
625 zend_class_implements(zend_ce_internal_iterator, 1, zend_ce_iterator);
626 zend_ce_internal_iterator->ce_flags |= ZEND_ACC_FINAL;
627 zend_ce_internal_iterator->create_object = zend_internal_iterator_create;
628 zend_ce_internal_iterator->serialize = zend_class_serialize_deny;
629 zend_ce_internal_iterator->unserialize = zend_class_unserialize_deny;
630
631 memcpy(&zend_internal_iterator_handlers, zend_get_std_object_handlers(),
632 sizeof(zend_object_handlers));
633 zend_internal_iterator_handlers.free_obj = zend_internal_iterator_free;
634 }
635 /* }}} */
636