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
128 zend_call_known_instance_method_with_0_params(iter->ce->iterator_funcs_ptr->zf_valid, Z_OBJ_P(object), &more);
129 bool result = i_zend_is_true(&more);
130 zval_ptr_dtor(&more);
131 return result ? SUCCESS : FAILURE;
132 }
133 return FAILURE;
134 }
135 /* }}} */
136
137 /* {{{ zend_user_it_get_current_data */
zend_user_it_get_current_data(zend_object_iterator * _iter)138 ZEND_API zval *zend_user_it_get_current_data(zend_object_iterator *_iter)
139 {
140 zend_user_iterator *iter = (zend_user_iterator*)_iter;
141 zval *object = &iter->it.data;
142
143 if (Z_ISUNDEF(iter->value)) {
144 zend_call_known_instance_method_with_0_params(iter->ce->iterator_funcs_ptr->zf_current, Z_OBJ_P(object), &iter->value);
145 }
146 return &iter->value;
147 }
148 /* }}} */
149
150 /* {{{ zend_user_it_get_current_key */
zend_user_it_get_current_key(zend_object_iterator * _iter,zval * key)151 ZEND_API void zend_user_it_get_current_key(zend_object_iterator *_iter, zval *key)
152 {
153 zend_user_iterator *iter = (zend_user_iterator*)_iter;
154 zval *object = &iter->it.data;
155 zend_call_known_instance_method_with_0_params(iter->ce->iterator_funcs_ptr->zf_key, Z_OBJ_P(object), key);
156 if (UNEXPECTED(Z_ISREF_P(key))) {
157 zend_unwrap_reference(key);
158 }
159 }
160 /* }}} */
161
162 /* {{{ zend_user_it_move_forward */
zend_user_it_move_forward(zend_object_iterator * _iter)163 ZEND_API void zend_user_it_move_forward(zend_object_iterator *_iter)
164 {
165 zend_user_iterator *iter = (zend_user_iterator*)_iter;
166 zval *object = &iter->it.data;
167
168 zend_user_it_invalidate_current(_iter);
169 zend_call_known_instance_method_with_0_params(iter->ce->iterator_funcs_ptr->zf_next, Z_OBJ_P(object), NULL);
170 }
171 /* }}} */
172
173 /* {{{ zend_user_it_rewind */
zend_user_it_rewind(zend_object_iterator * _iter)174 ZEND_API void zend_user_it_rewind(zend_object_iterator *_iter)
175 {
176 zend_user_iterator *iter = (zend_user_iterator*)_iter;
177 zval *object = &iter->it.data;
178
179 zend_user_it_invalidate_current(_iter);
180 zend_call_known_instance_method_with_0_params(iter->ce->iterator_funcs_ptr->zf_rewind, Z_OBJ_P(object), NULL);
181 }
182 /* }}} */
183
zend_user_it_get_gc(zend_object_iterator * _iter,zval ** table,int * n)184 ZEND_API HashTable *zend_user_it_get_gc(zend_object_iterator *_iter, zval **table, int *n)
185 {
186 zend_user_iterator *iter = (zend_user_iterator*)_iter;
187 if (Z_ISUNDEF(iter->value)) {
188 *table = &iter->it.data;
189 *n = 1;
190 } else {
191 zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
192 zend_get_gc_buffer_add_zval(gc_buffer, &iter->it.data);
193 zend_get_gc_buffer_add_zval(gc_buffer, &iter->value);
194 zend_get_gc_buffer_use(gc_buffer, table, n);
195 }
196 return NULL;
197 }
198
199 static const zend_object_iterator_funcs zend_interface_iterator_funcs_iterator = {
200 zend_user_it_dtor,
201 zend_user_it_valid,
202 zend_user_it_get_current_data,
203 zend_user_it_get_current_key,
204 zend_user_it_move_forward,
205 zend_user_it_rewind,
206 zend_user_it_invalidate_current,
207 zend_user_it_get_gc,
208 };
209
210 /* {{{ zend_user_it_get_iterator */
211 /* by_ref is int due to Iterator API */
zend_user_it_get_iterator(zend_class_entry * ce,zval * object,int by_ref)212 static zend_object_iterator *zend_user_it_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
213 {
214 zend_user_iterator *iterator;
215
216 if (by_ref) {
217 zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
218 return NULL;
219 }
220
221 iterator = emalloc(sizeof(zend_user_iterator));
222
223 zend_iterator_init((zend_object_iterator*)iterator);
224
225 ZVAL_OBJ_COPY(&iterator->it.data, Z_OBJ_P(object));
226 iterator->it.funcs = &zend_interface_iterator_funcs_iterator;
227 iterator->ce = Z_OBJCE_P(object);
228 ZVAL_UNDEF(&iterator->value);
229 return (zend_object_iterator*)iterator;
230 }
231 /* }}} */
232
233 /* {{{ zend_user_it_get_new_iterator */
234 /* by_ref is int due to Iterator API */
zend_user_it_get_new_iterator(zend_class_entry * ce,zval * object,int by_ref)235 ZEND_API zend_object_iterator *zend_user_it_get_new_iterator(zend_class_entry *ce, zval *object, int by_ref)
236 {
237 zval iterator;
238 zend_object_iterator *new_iterator;
239 zend_class_entry *ce_it;
240
241 zend_user_it_new_iterator(ce, object, &iterator);
242 ce_it = (Z_TYPE(iterator) == IS_OBJECT) ? Z_OBJCE(iterator) : NULL;
243
244 if (!ce_it || !ce_it->get_iterator || (ce_it->get_iterator == zend_user_it_get_new_iterator && Z_OBJ(iterator) == Z_OBJ_P(object))) {
245 if (!EG(exception)) {
246 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));
247 }
248 zval_ptr_dtor(&iterator);
249 return NULL;
250 }
251
252 new_iterator = ce_it->get_iterator(ce_it, &iterator, by_ref);
253 zval_ptr_dtor(&iterator);
254 return new_iterator;
255 }
256 /* }}} */
257
258 /* {{{ zend_implement_traversable */
zend_implement_traversable(zend_class_entry * interface,zend_class_entry * class_type)259 static int zend_implement_traversable(zend_class_entry *interface, zend_class_entry *class_type)
260 {
261 /* Abstract class can implement Traversable only, in which case the extending class must
262 * implement Iterator or IteratorAggregate. */
263 if (class_type->ce_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
264 return SUCCESS;
265 }
266
267 /* Check that class_type implements at least one of 'IteratorAggregate' or 'Iterator' */
268 if (class_type->num_interfaces) {
269 ZEND_ASSERT(class_type->ce_flags & ZEND_ACC_RESOLVED_INTERFACES);
270 for (uint32_t i = 0; i < class_type->num_interfaces; i++) {
271 if (class_type->interfaces[i] == zend_ce_aggregate || class_type->interfaces[i] == zend_ce_iterator) {
272 return SUCCESS;
273 }
274 }
275 }
276 zend_error_noreturn(E_CORE_ERROR, "%s %s must implement interface %s as part of either %s or %s",
277 zend_get_object_type_uc(class_type),
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 /* Always initialize iterator_funcs_ptr. */
296 ZEND_ASSERT(!class_type->iterator_funcs_ptr && "Iterator funcs already set?");
297 zend_class_iterator_funcs *funcs_ptr = class_type->type == ZEND_INTERNAL_CLASS
298 ? pemalloc(sizeof(zend_class_iterator_funcs), 1)
299 : zend_arena_alloc(&CG(arena), sizeof(zend_class_iterator_funcs));
300 class_type->iterator_funcs_ptr = funcs_ptr;
301
302 memset(funcs_ptr, 0, sizeof(zend_class_iterator_funcs));
303 funcs_ptr->zf_new_iterator = zend_hash_str_find_ptr(
304 &class_type->function_table, "getiterator", sizeof("getiterator") - 1);
305
306 if (class_type->get_iterator && class_type->get_iterator != zend_user_it_get_new_iterator) {
307 /* get_iterator was explicitly assigned for an internal class. */
308 if (!class_type->parent || class_type->parent->get_iterator != class_type->get_iterator) {
309 ZEND_ASSERT(class_type->type == ZEND_INTERNAL_CLASS);
310 return SUCCESS;
311 }
312
313 /* The getIterator() method has not been overwritten, use inherited get_iterator(). */
314 if (funcs_ptr->zf_new_iterator->common.scope != class_type) {
315 return SUCCESS;
316 }
317
318 /* getIterator() has been overwritten, switch to zend_user_it_get_new_iterator. */
319 }
320
321 class_type->get_iterator = zend_user_it_get_new_iterator;
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 ZEND_ASSERT(!class_type->iterator_funcs_ptr && "Iterator funcs already set?");
336 zend_class_iterator_funcs *funcs_ptr = class_type->type == ZEND_INTERNAL_CLASS
337 ? pemalloc(sizeof(zend_class_iterator_funcs), 1)
338 : zend_arena_alloc(&CG(arena), sizeof(zend_class_iterator_funcs));
339 class_type->iterator_funcs_ptr = funcs_ptr;
340
341 memset(funcs_ptr, 0, sizeof(zend_class_iterator_funcs));
342 funcs_ptr->zf_rewind = zend_hash_str_find_ptr(
343 &class_type->function_table, "rewind", sizeof("rewind") - 1);
344 funcs_ptr->zf_valid = zend_hash_str_find_ptr(
345 &class_type->function_table, "valid", sizeof("valid") - 1);
346 funcs_ptr->zf_key = zend_hash_str_find_ptr(
347 &class_type->function_table, "key", sizeof("key") - 1);
348 funcs_ptr->zf_current = zend_hash_str_find_ptr(
349 &class_type->function_table, "current", sizeof("current") - 1);
350 funcs_ptr->zf_next = zend_hash_str_find_ptr(
351 &class_type->function_table, "next", sizeof("next") - 1);
352
353 if (class_type->get_iterator && class_type->get_iterator != zend_user_it_get_iterator) {
354 if (!class_type->parent || class_type->parent->get_iterator != class_type->get_iterator) {
355 /* get_iterator was explicitly assigned for an internal class. */
356 ZEND_ASSERT(class_type->type == ZEND_INTERNAL_CLASS);
357 return SUCCESS;
358 }
359
360 /* None of the Iterator methods have been overwritten, use inherited get_iterator(). */
361 if (funcs_ptr->zf_rewind->common.scope != class_type &&
362 funcs_ptr->zf_valid->common.scope != class_type &&
363 funcs_ptr->zf_key->common.scope != class_type &&
364 funcs_ptr->zf_current->common.scope != class_type &&
365 funcs_ptr->zf_next->common.scope != class_type) {
366 return SUCCESS;
367 }
368
369 /* One of the Iterator methods has been overwritten,
370 * switch to zend_user_it_get_iterator. */
371 }
372
373 class_type->get_iterator = zend_user_it_get_iterator;
374 return SUCCESS;
375 }
376 /* }}} */
377
378 /* {{{ zend_implement_arrayaccess */
zend_implement_arrayaccess(zend_class_entry * interface,zend_class_entry * class_type)379 static int zend_implement_arrayaccess(zend_class_entry *interface, zend_class_entry *class_type)
380 {
381 ZEND_ASSERT(!class_type->arrayaccess_funcs_ptr && "ArrayAccess funcs already set?");
382 zend_class_arrayaccess_funcs *funcs_ptr = class_type->type == ZEND_INTERNAL_CLASS
383 ? pemalloc(sizeof(zend_class_arrayaccess_funcs), 1)
384 : zend_arena_alloc(&CG(arena), sizeof(zend_class_arrayaccess_funcs));
385 class_type->arrayaccess_funcs_ptr = funcs_ptr;
386
387 funcs_ptr->zf_offsetget = zend_hash_str_find_ptr(
388 &class_type->function_table, "offsetget", sizeof("offsetget") - 1);
389 funcs_ptr->zf_offsetexists = zend_hash_str_find_ptr(
390 &class_type->function_table, "offsetexists", sizeof("offsetexists") - 1);
391 funcs_ptr->zf_offsetset = zend_hash_str_find_ptr(
392 &class_type->function_table, "offsetset", sizeof("offsetset") - 1);
393 funcs_ptr->zf_offsetunset = zend_hash_str_find_ptr(
394 &class_type->function_table, "offsetunset", sizeof("offsetunset") - 1);
395
396 return SUCCESS;
397 }
398 /* }}} */
399
400 /* {{{ zend_user_serialize */
zend_user_serialize(zval * object,unsigned char ** buffer,size_t * buf_len,zend_serialize_data * data)401 ZEND_API int zend_user_serialize(zval *object, unsigned char **buffer, size_t *buf_len, zend_serialize_data *data)
402 {
403 zend_class_entry * ce = Z_OBJCE_P(object);
404 zval retval;
405 zend_result result;
406
407 zend_call_method_with_0_params(
408 Z_OBJ_P(object), Z_OBJCE_P(object), NULL, "serialize", &retval);
409
410 if (Z_TYPE(retval) == IS_UNDEF || EG(exception)) {
411 result = FAILURE;
412 } else {
413 switch(Z_TYPE(retval)) {
414 case IS_NULL:
415 /* we could also make this '*buf_len = 0' but this allows to skip variables */
416 zval_ptr_dtor(&retval);
417 return FAILURE;
418 case IS_STRING:
419 *buffer = (unsigned char*)estrndup(Z_STRVAL(retval), Z_STRLEN(retval));
420 *buf_len = Z_STRLEN(retval);
421 result = SUCCESS;
422 break;
423 default: /* failure */
424 result = FAILURE;
425 break;
426 }
427 zval_ptr_dtor(&retval);
428 }
429
430 if (result == FAILURE && !EG(exception)) {
431 zend_throw_exception_ex(NULL, 0, "%s::serialize() must return a string or NULL", ZSTR_VAL(ce->name));
432 }
433 return result;
434 }
435 /* }}} */
436
437 /* {{{ zend_user_unserialize */
zend_user_unserialize(zval * object,zend_class_entry * ce,const unsigned char * buf,size_t buf_len,zend_unserialize_data * data)438 ZEND_API int zend_user_unserialize(zval *object, zend_class_entry *ce, const unsigned char *buf, size_t buf_len, zend_unserialize_data *data)
439 {
440 zval zdata;
441
442 if (UNEXPECTED(object_init_ex(object, ce) != SUCCESS)) {
443 return FAILURE;
444 }
445
446 ZVAL_STRINGL(&zdata, (char*)buf, buf_len);
447 zend_call_method_with_1_params(
448 Z_OBJ_P(object), Z_OBJCE_P(object), NULL, "unserialize", NULL, &zdata);
449 zval_ptr_dtor(&zdata);
450
451 if (EG(exception)) {
452 return FAILURE;
453 } else {
454 return SUCCESS;
455 }
456 }
457 /* }}} */
458
459 /* {{{ zend_implement_serializable */
zend_implement_serializable(zend_class_entry * interface,zend_class_entry * class_type)460 static int zend_implement_serializable(zend_class_entry *interface, zend_class_entry *class_type)
461 {
462 if (class_type->parent
463 && (class_type->parent->serialize || class_type->parent->unserialize)
464 && !zend_class_implements_interface(class_type->parent, zend_ce_serializable)) {
465 return FAILURE;
466 }
467 if (!class_type->serialize) {
468 class_type->serialize = zend_user_serialize;
469 }
470 if (!class_type->unserialize) {
471 class_type->unserialize = zend_user_unserialize;
472 }
473 if (!(class_type->ce_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)
474 && (!class_type->__serialize || !class_type->__unserialize)) {
475 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));
476 if (EG(exception)) {
477 zend_exception_uncaught_error(
478 "During inheritance of %s, while implementing Serializable", ZSTR_VAL(class_type->name));
479 }
480 }
481 return SUCCESS;
482 }
483 /* }}}*/
484
485 typedef struct {
486 zend_object std;
487 zend_object_iterator *iter;
488 bool rewind_called;
489 } zend_internal_iterator;
490
zend_internal_iterator_create(zend_class_entry * ce)491 static zend_object *zend_internal_iterator_create(zend_class_entry *ce) {
492 zend_internal_iterator *intern = emalloc(sizeof(zend_internal_iterator));
493 zend_object_std_init(&intern->std, ce);
494 intern->std.handlers = &zend_internal_iterator_handlers;
495 intern->iter = NULL;
496 intern->rewind_called = 0;
497 return &intern->std;
498 }
499
zend_create_internal_iterator_zval(zval * return_value,zval * obj)500 ZEND_API zend_result zend_create_internal_iterator_zval(zval *return_value, zval *obj) {
501 zend_class_entry *scope = EG(current_execute_data)->func->common.scope;
502 ZEND_ASSERT(scope->get_iterator != zend_user_it_get_new_iterator);
503 zend_object_iterator *iter = scope->get_iterator(Z_OBJCE_P(obj), obj, /* by_ref */ 0);
504 if (!iter) {
505 return FAILURE;
506 }
507
508 zend_internal_iterator *intern =
509 (zend_internal_iterator *) zend_internal_iterator_create(zend_ce_internal_iterator);
510 intern->iter = iter;
511 intern->iter->index = 0;
512 ZVAL_OBJ(return_value, &intern->std);
513 return SUCCESS;
514 }
515
zend_internal_iterator_free(zend_object * obj)516 static void zend_internal_iterator_free(zend_object *obj) {
517 zend_internal_iterator *intern = (zend_internal_iterator *) obj;
518 if (intern->iter) {
519 zend_iterator_dtor(intern->iter);
520 }
521 zend_object_std_dtor(&intern->std);
522 }
523
zend_internal_iterator_fetch(zval * This)524 static zend_internal_iterator *zend_internal_iterator_fetch(zval *This) {
525 zend_internal_iterator *intern = (zend_internal_iterator *) Z_OBJ_P(This);
526 if (!intern->iter) {
527 zend_throw_error(NULL, "The InternalIterator object has not been properly initialized");
528 return NULL;
529 }
530 return intern;
531 }
532
533 /* Many iterators will not behave correctly if rewind() is not called, make sure it happens. */
zend_internal_iterator_ensure_rewound(zend_internal_iterator * intern)534 static zend_result zend_internal_iterator_ensure_rewound(zend_internal_iterator *intern) {
535 if (!intern->rewind_called) {
536 zend_object_iterator *iter = intern->iter;
537 intern->rewind_called = 1;
538 if (iter->funcs->rewind) {
539 iter->funcs->rewind(iter);
540 if (UNEXPECTED(EG(exception))) {
541 return FAILURE;
542 }
543 }
544 }
545 return SUCCESS;
546 }
547
548
ZEND_METHOD(InternalIterator,__construct)549 ZEND_METHOD(InternalIterator, __construct) {
550 zend_throw_error(NULL, "Cannot manually construct InternalIterator");
551 }
552
ZEND_METHOD(InternalIterator,current)553 ZEND_METHOD(InternalIterator, current) {
554 ZEND_PARSE_PARAMETERS_NONE();
555
556 zend_internal_iterator *intern = zend_internal_iterator_fetch(ZEND_THIS);
557 if (!intern) {
558 RETURN_THROWS();
559 }
560
561 if (zend_internal_iterator_ensure_rewound(intern) == FAILURE) {
562 RETURN_THROWS();
563 }
564
565 zval *data = intern->iter->funcs->get_current_data(intern->iter);
566 if (data) {
567 RETURN_COPY_DEREF(data);
568 }
569 }
570
ZEND_METHOD(InternalIterator,key)571 ZEND_METHOD(InternalIterator, key) {
572 ZEND_PARSE_PARAMETERS_NONE();
573
574 zend_internal_iterator *intern = zend_internal_iterator_fetch(ZEND_THIS);
575 if (!intern) {
576 RETURN_THROWS();
577 }
578
579 if (zend_internal_iterator_ensure_rewound(intern) == FAILURE) {
580 RETURN_THROWS();
581 }
582
583 if (intern->iter->funcs->get_current_key) {
584 intern->iter->funcs->get_current_key(intern->iter, return_value);
585 } else {
586 RETURN_LONG(intern->iter->index);
587 }
588 }
589
ZEND_METHOD(InternalIterator,next)590 ZEND_METHOD(InternalIterator, next) {
591 ZEND_PARSE_PARAMETERS_NONE();
592
593 zend_internal_iterator *intern = zend_internal_iterator_fetch(ZEND_THIS);
594 if (!intern) {
595 RETURN_THROWS();
596 }
597
598 if (zend_internal_iterator_ensure_rewound(intern) == FAILURE) {
599 RETURN_THROWS();
600 }
601
602 /* Advance index first to match foreach behavior. */
603 intern->iter->index++;
604 intern->iter->funcs->move_forward(intern->iter);
605 }
606
ZEND_METHOD(InternalIterator,valid)607 ZEND_METHOD(InternalIterator, valid) {
608 ZEND_PARSE_PARAMETERS_NONE();
609
610 zend_internal_iterator *intern = zend_internal_iterator_fetch(ZEND_THIS);
611 if (!intern) {
612 RETURN_THROWS();
613 }
614
615 if (zend_internal_iterator_ensure_rewound(intern) == FAILURE) {
616 RETURN_THROWS();
617 }
618
619 RETURN_BOOL(intern->iter->funcs->valid(intern->iter) == SUCCESS);
620 }
621
ZEND_METHOD(InternalIterator,rewind)622 ZEND_METHOD(InternalIterator, rewind) {
623 ZEND_PARSE_PARAMETERS_NONE();
624
625 zend_internal_iterator *intern = zend_internal_iterator_fetch(ZEND_THIS);
626 if (!intern) {
627 RETURN_THROWS();
628 }
629
630 intern->rewind_called = 1;
631 if (!intern->iter->funcs->rewind) {
632 /* Allow calling rewind() if no iteration has happened yet,
633 * even if the iterator does not support rewinding. */
634 if (intern->iter->index != 0) {
635 zend_throw_error(NULL, "Iterator does not support rewinding");
636 RETURN_THROWS();
637 }
638 intern->iter->index = 0;
639 return;
640 }
641
642 intern->iter->funcs->rewind(intern->iter);
643 intern->iter->index = 0;
644 }
645
646 /* {{{ zend_register_interfaces */
zend_register_interfaces(void)647 ZEND_API void zend_register_interfaces(void)
648 {
649 zend_ce_traversable = register_class_Traversable();
650 zend_ce_traversable->interface_gets_implemented = zend_implement_traversable;
651
652 zend_ce_aggregate = register_class_IteratorAggregate(zend_ce_traversable);
653 zend_ce_aggregate->interface_gets_implemented = zend_implement_aggregate;
654
655 zend_ce_iterator = register_class_Iterator(zend_ce_traversable);
656 zend_ce_iterator->interface_gets_implemented = zend_implement_iterator;
657
658 zend_ce_serializable = register_class_Serializable();
659 zend_ce_serializable->interface_gets_implemented = zend_implement_serializable;
660
661 zend_ce_arrayaccess = register_class_ArrayAccess();
662 zend_ce_arrayaccess->interface_gets_implemented = zend_implement_arrayaccess;
663
664 zend_ce_countable = register_class_Countable();
665
666 zend_ce_stringable = register_class_Stringable();
667
668 zend_ce_internal_iterator = register_class_InternalIterator(zend_ce_iterator);
669 zend_ce_internal_iterator->create_object = zend_internal_iterator_create;
670
671 memcpy(&zend_internal_iterator_handlers, zend_get_std_object_handlers(),
672 sizeof(zend_object_handlers));
673 zend_internal_iterator_handlers.clone_obj = NULL;
674 zend_internal_iterator_handlers.free_obj = zend_internal_iterator_free;
675 }
676 /* }}} */
677