xref: /PHP-7.4/Zend/zend_iterators.c (revision 92ac598a)
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    | Author: Wez Furlong <wez@thebrainroom.com>                           |
16    |         Marcus Boerger <helly@php.net>                               |
17    +----------------------------------------------------------------------+
18 */
19 
20 #include "zend.h"
21 #include "zend_API.h"
22 
23 static zend_class_entry zend_iterator_class_entry;
24 
25 static void iter_wrapper_free(zend_object *object);
26 static void iter_wrapper_dtor(zend_object *object);
27 static HashTable *iter_wrapper_get_gc(zval *object, zval **table, int *n);
28 
29 static const zend_object_handlers iterator_object_handlers = {
30 	0,
31 	iter_wrapper_free,
32 	iter_wrapper_dtor,
33 	NULL,
34 	NULL, /* prop read */
35 	NULL, /* prop write */
36 	NULL, /* read dim */
37 	NULL, /* write dim */
38 	NULL,
39 	NULL, /* get */
40 	NULL, /* set */
41 	NULL, /* has prop */
42 	NULL, /* unset prop */
43 	NULL, /* has dim */
44 	NULL, /* unset dim */
45 	NULL, /* props get */
46 	NULL, /* method get */
47 	NULL, /* call */
48 	NULL, /* get ctor */
49 	NULL, /* get class name */
50 	NULL, /* compare */
51 	NULL, /* cast */
52 	NULL, /* count */
53 	NULL, /* get_debug_info */
54 	NULL, /* get_closure */
55 	iter_wrapper_get_gc,
56 	NULL, /* do_operation */
57 	NULL  /* compare */
58 };
59 
zend_register_iterator_wrapper(void)60 ZEND_API void zend_register_iterator_wrapper(void)
61 {
62 	INIT_CLASS_ENTRY(zend_iterator_class_entry, "__iterator_wrapper", NULL);
63 }
64 
iter_wrapper_free(zend_object * object)65 static void iter_wrapper_free(zend_object *object)
66 {
67 	zend_object_iterator *iter = (zend_object_iterator*)object;
68 	iter->funcs->dtor(iter);
69 }
70 
iter_wrapper_dtor(zend_object * object)71 static void iter_wrapper_dtor(zend_object *object)
72 {
73 }
74 
iter_wrapper_get_gc(zval * object,zval ** table,int * n)75 static HashTable *iter_wrapper_get_gc(zval *object, zval **table, int *n) {
76 	/* TODO: We need a get_gc iterator handler */
77 	*table = NULL;
78 	*n = 0;
79 	return NULL;
80 }
81 
zend_iterator_init(zend_object_iterator * iter)82 ZEND_API void zend_iterator_init(zend_object_iterator *iter)
83 {
84 	zend_object_std_init(&iter->std, &zend_iterator_class_entry);
85 	iter->std.handlers = &iterator_object_handlers;
86 }
87 
zend_iterator_dtor(zend_object_iterator * iter)88 ZEND_API void zend_iterator_dtor(zend_object_iterator *iter)
89 {
90 	if (GC_DELREF(&iter->std) > 0) {
91 		return;
92 	}
93 
94 	zend_objects_store_del(&iter->std);
95 }
96 
zend_iterator_unwrap(zval * array_ptr)97 ZEND_API zend_object_iterator* zend_iterator_unwrap(zval *array_ptr)
98 {
99 	ZEND_ASSERT(Z_TYPE_P(array_ptr) == IS_OBJECT);
100 	if (Z_OBJ_HT_P(array_ptr) == &iterator_object_handlers) {
101 		return (zend_object_iterator *)Z_OBJ_P(array_ptr);
102 	}
103 	return NULL;
104 }
105