/src/php-src/Zend/zend_iterators.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Zend Engine | |
4 | | +----------------------------------------------------------------------+ |
5 | | | Copyright © Zend Technologies Ltd., a subsidiary company of | |
6 | | | Perforce Software, Inc., and Contributors. | |
7 | | +----------------------------------------------------------------------+ |
8 | | | This source file is subject to the Modified BSD License that is | |
9 | | | bundled with this package in the file LICENSE, and is available | |
10 | | | through the World Wide Web at <https://www.php.net/license/>. | |
11 | | | | |
12 | | | SPDX-License-Identifier: BSD-3-Clause | |
13 | | +----------------------------------------------------------------------+ |
14 | | | Author: Wez Furlong <wez@thebrainroom.com> | |
15 | | | Marcus Boerger <helly@php.net> | |
16 | | +----------------------------------------------------------------------+ |
17 | | */ |
18 | | |
19 | | #include "zend.h" |
20 | | #include "zend_API.h" |
21 | | |
22 | | static zend_class_entry zend_iterator_class_entry; |
23 | | |
24 | | static void iter_wrapper_free(zend_object *object); |
25 | | static void iter_wrapper_dtor(zend_object *object); |
26 | | static HashTable *iter_wrapper_get_gc(zend_object *object, zval **table, int *n); |
27 | | |
28 | | static const zend_object_handlers iterator_object_handlers = { |
29 | | 0, |
30 | | iter_wrapper_free, |
31 | | iter_wrapper_dtor, |
32 | | NULL, /* clone_obj */ |
33 | | NULL, /* clone_obj_with */ |
34 | | NULL, /* prop read */ |
35 | | NULL, /* prop write */ |
36 | | NULL, /* read dim */ |
37 | | NULL, /* write dim */ |
38 | | NULL, /* get_property_ptr_ptr */ |
39 | | NULL, /* has prop */ |
40 | | NULL, /* unset prop */ |
41 | | NULL, /* has dim */ |
42 | | NULL, /* unset dim */ |
43 | | NULL, /* props get */ |
44 | | NULL, /* method get */ |
45 | | NULL, /* get ctor */ |
46 | | NULL, /* get class name */ |
47 | | NULL, /* cast */ |
48 | | NULL, /* count */ |
49 | | NULL, /* get_debug_info */ |
50 | | NULL, /* get_closure */ |
51 | | iter_wrapper_get_gc, |
52 | | NULL, /* do_operation */ |
53 | | NULL, /* compare */ |
54 | | NULL /* get_properties_for */ |
55 | | }; |
56 | | |
57 | | ZEND_API void zend_register_iterator_wrapper(void) |
58 | 2 | { |
59 | 2 | INIT_CLASS_ENTRY(zend_iterator_class_entry, "__iterator_wrapper", NULL); |
60 | 2 | zend_iterator_class_entry.default_object_handlers = &iterator_object_handlers; |
61 | 2 | } |
62 | | |
63 | | static void iter_wrapper_free(zend_object *object) |
64 | 1.23k | { |
65 | 1.23k | zend_object_iterator *iter = (zend_object_iterator*)object; |
66 | 1.23k | iter->funcs->dtor(iter); |
67 | 1.23k | } |
68 | | |
69 | | static void iter_wrapper_dtor(zend_object *object) |
70 | 1.23k | { |
71 | 1.23k | } |
72 | | |
73 | 291 | static HashTable *iter_wrapper_get_gc(zend_object *object, zval **table, int *n) { |
74 | 291 | zend_object_iterator *iter = (zend_object_iterator*)object; |
75 | 291 | if (iter->funcs->get_gc) { |
76 | 291 | return iter->funcs->get_gc(iter, table, n); |
77 | 291 | } |
78 | | |
79 | 0 | *table = NULL; |
80 | 0 | *n = 0; |
81 | 0 | return NULL; |
82 | 291 | } |
83 | | |
84 | | ZEND_API void zend_iterator_init(zend_object_iterator *iter) |
85 | 1.23k | { |
86 | 1.23k | zend_object_std_init(&iter->std, &zend_iterator_class_entry); |
87 | 1.23k | } |
88 | | |
89 | | ZEND_API void zend_iterator_dtor(zend_object_iterator *iter) |
90 | 245 | { |
91 | 245 | if (GC_DELREF(&iter->std) > 0) { |
92 | 0 | return; |
93 | 0 | } |
94 | | |
95 | 245 | zend_objects_store_del(&iter->std); |
96 | 245 | } |
97 | | |
98 | | ZEND_API zend_object_iterator* zend_iterator_unwrap(zval *array_ptr) |
99 | 4.44k | { |
100 | 4.44k | ZEND_ASSERT(Z_TYPE_P(array_ptr) == IS_OBJECT); |
101 | 4.44k | if (Z_OBJ_HT_P(array_ptr) == &iterator_object_handlers) { |
102 | 3.64k | return (zend_object_iterator *)Z_OBJ_P(array_ptr); |
103 | 3.64k | } |
104 | 801 | return NULL; |
105 | 4.44k | } |