/src/php-src/ext/spl/spl_observer.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Authors: Marcus Boerger <helly@php.net> | |
12 | | | Etienne Kneuss <colder@php.net> | |
13 | | +----------------------------------------------------------------------+ |
14 | | */ |
15 | | |
16 | | #ifdef HAVE_CONFIG_H |
17 | | # include "config.h" |
18 | | #endif |
19 | | |
20 | | #include "php.h" |
21 | | #include "ext/standard/php_array.h" /* For PHP_COUNT_* constants */ |
22 | | #include "ext/standard/php_var.h" |
23 | | #include "zend_smart_str.h" |
24 | | #include "zend_interfaces.h" |
25 | | #include "zend_exceptions.h" |
26 | | |
27 | | #include "php_spl.h" /* For php_spl_object_hash() */ |
28 | | #include "spl_observer.h" |
29 | | #include "spl_observer_arginfo.h" |
30 | | #include "spl_iterators.h" |
31 | | #include "spl_exceptions.h" |
32 | | #include "spl_functions.h" /* For spl_set_private_debug_info_property() */ |
33 | | |
34 | | PHPAPI zend_class_entry *spl_ce_SplObserver; |
35 | | PHPAPI zend_class_entry *spl_ce_SplSubject; |
36 | | PHPAPI zend_class_entry *spl_ce_SplObjectStorage; |
37 | | PHPAPI zend_class_entry *spl_ce_MultipleIterator; |
38 | | |
39 | | static zend_object_handlers spl_handler_SplObjectStorage; |
40 | | static zend_object_handlers spl_handler_MultipleIterator; |
41 | | |
42 | | /* Bit flags for marking internal functionality overridden by SplObjectStorage subclasses. */ |
43 | 1 | #define SOS_OVERRIDDEN_READ_DIMENSION 1 |
44 | 1 | #define SOS_OVERRIDDEN_WRITE_DIMENSION 2 |
45 | 1 | #define SOS_OVERRIDDEN_UNSET_DIMENSION 4 |
46 | | |
47 | | ZEND_TLS uint32_t spl_object_storage_get_hash_depth; |
48 | | |
49 | | void spl_object_storage_reset_get_hash_depth(void) |
50 | 306k | { |
51 | 306k | spl_object_storage_get_hash_depth = 0; |
52 | 306k | } |
53 | | |
54 | | typedef struct _spl_SplObjectStorage { /* {{{ */ |
55 | | HashTable storage; |
56 | | zend_long index; |
57 | | HashPosition pos; |
58 | | /* In SplObjectStorage, flags is a hidden implementation detail to optimize ArrayAccess handlers. |
59 | | * In MultipleIterator on a different class hierarchy, flags is a user settable value controlling iteration behavior. */ |
60 | | zend_long flags; |
61 | | zend_function *fptr_get_hash; |
62 | | zend_object std; |
63 | | } spl_SplObjectStorage; /* }}} */ |
64 | | |
65 | | /* {{{ storage is an assoc array of [zend_object*]=>[zval *obj, zval *inf] */ |
66 | | typedef struct _spl_SplObjectStorageElement { |
67 | | zend_object *obj; |
68 | | zval inf; |
69 | | } spl_SplObjectStorageElement; /* }}} */ |
70 | | |
71 | 142k | #define spl_object_storage_from_obj(obj) ZEND_CONTAINER_OF(obj, spl_SplObjectStorage, std) |
72 | | |
73 | 25.6k | #define Z_SPLOBJSTORAGE_P(zv) spl_object_storage_from_obj(Z_OBJ_P((zv))) |
74 | | |
75 | | static zend_always_inline bool spl_object_storage_is_mutating_within_get_hash_call(void) |
76 | 2.48k | { |
77 | 2.48k | if (UNEXPECTED(spl_object_storage_get_hash_depth)) { |
78 | 0 | zend_throw_error(NULL, "Modification of SplObjectStorage during getHash() is prohibited"); |
79 | 0 | return true; |
80 | 0 | } |
81 | | |
82 | 2.48k | return false; |
83 | 2.48k | } |
84 | | |
85 | | static void spl_SplObjectStorage_free_storage(zend_object *object) /* {{{ */ |
86 | 37.5k | { |
87 | 37.5k | spl_SplObjectStorage *intern = spl_object_storage_from_obj(object); |
88 | | |
89 | 37.5k | zend_object_std_dtor(&intern->std); |
90 | | |
91 | 37.5k | zend_hash_destroy(&intern->storage); |
92 | 37.5k | } /* }}} */ |
93 | | |
94 | 115 | static zend_result spl_object_storage_get_hash(zend_hash_key *key, spl_SplObjectStorage *intern, zend_object *obj) { |
95 | 115 | if (UNEXPECTED(intern->fptr_get_hash)) { |
96 | 0 | zval param; |
97 | 0 | zval rv; |
98 | 0 | ZVAL_OBJ(¶m, obj); |
99 | 0 | ZVAL_UNDEF(&rv); |
100 | 0 | spl_object_storage_get_hash_depth++; |
101 | 0 | zend_call_known_function(intern->fptr_get_hash, &intern->std, intern->std.ce, &rv, 1, ¶m, NULL); |
102 | 0 | spl_object_storage_get_hash_depth--; |
103 | 0 | if (UNEXPECTED(Z_ISUNDEF(rv))) { |
104 | | /* An exception has occurred */ |
105 | 0 | return FAILURE; |
106 | 0 | } else { |
107 | | /* TODO PHP 9: Remove this as this will be enforced from the return type */ |
108 | 0 | if (UNEXPECTED(Z_TYPE(rv) != IS_STRING)) { |
109 | 0 | zend_type_error("%pS::getHash(): Return value must be of type string, %s returned", |
110 | 0 | intern->std.ce->name, zend_zval_value_name(&rv)); |
111 | 0 | zval_ptr_dtor(&rv); |
112 | 0 | return FAILURE; |
113 | 0 | } else { |
114 | 0 | key->key = Z_STR(rv); |
115 | 0 | return SUCCESS; |
116 | 0 | } |
117 | 0 | } |
118 | 115 | } else { |
119 | 115 | key->key = NULL; |
120 | 115 | key->h = obj->handle; |
121 | 115 | return SUCCESS; |
122 | 115 | } |
123 | 115 | } |
124 | | |
125 | 115 | static void spl_object_storage_free_hash(spl_SplObjectStorage *intern, zend_hash_key *key) { |
126 | 115 | if (key->key) { |
127 | 0 | zend_string_release_ex(key->key, 0); |
128 | 0 | } |
129 | 115 | } |
130 | | |
131 | | static void spl_object_storage_dtor(zval *element) /* {{{ */ |
132 | 2.44k | { |
133 | 2.44k | spl_SplObjectStorageElement *el = Z_PTR_P(element); |
134 | 2.44k | if (el) { |
135 | 2.44k | zend_object_release(el->obj); |
136 | 2.44k | zval_ptr_dtor(&el->inf); |
137 | 2.44k | efree(el); |
138 | 2.44k | } |
139 | 2.44k | } /* }}} */ |
140 | | |
141 | | static spl_SplObjectStorageElement* spl_object_storage_get(spl_SplObjectStorage *intern, zend_hash_key *key) /* {{{ */ |
142 | 115 | { |
143 | 115 | if (key->key) { |
144 | 0 | return zend_hash_find_ptr(&intern->storage, key->key); |
145 | 115 | } else { |
146 | 115 | return zend_hash_index_find_ptr(&intern->storage, key->h); |
147 | 115 | } |
148 | 115 | } /* }}} */ |
149 | | |
150 | | static spl_SplObjectStorageElement *spl_object_storage_create_element(zend_object *obj, zval *inf) /* {{{ */ |
151 | 2.44k | { |
152 | 2.44k | spl_SplObjectStorageElement *pelement = emalloc(sizeof(spl_SplObjectStorageElement)); |
153 | 2.44k | pelement->obj = zend_object_copy(obj); |
154 | 2.44k | if (inf) { |
155 | 2.37k | ZVAL_COPY(&pelement->inf, inf); |
156 | 2.37k | } else { |
157 | 71 | ZVAL_NULL(&pelement->inf); |
158 | 71 | } |
159 | 2.44k | return pelement; |
160 | 2.44k | } /* }}} */ |
161 | | |
162 | | /* A faster version of spl_object_storage_attach used when neither SplObjectStorage->getHash nor SplObjectStorage->offsetSet is overridden. */ |
163 | | static spl_SplObjectStorageElement *spl_object_storage_attach_handle(spl_SplObjectStorage *intern, zend_object *obj, zval *inf) /* {{{ */ |
164 | 2.48k | { |
165 | 2.48k | uint32_t handle = obj->handle; |
166 | 2.48k | zval *entry_zv = zend_hash_index_lookup(&intern->storage, handle); |
167 | 2.48k | spl_SplObjectStorageElement *pelement; |
168 | 2.48k | ZEND_ASSERT(!(intern->flags & SOS_OVERRIDDEN_WRITE_DIMENSION)); |
169 | | |
170 | 2.48k | if (Z_TYPE_P(entry_zv) != IS_NULL) { |
171 | 37 | zval zv_inf; |
172 | 37 | ZEND_ASSERT(Z_TYPE_P(entry_zv) == IS_PTR); |
173 | 37 | pelement = Z_PTR_P(entry_zv); |
174 | 37 | ZVAL_COPY_VALUE(&zv_inf, &pelement->inf); |
175 | 37 | if (inf) { |
176 | 10 | ZVAL_COPY(&pelement->inf, inf); |
177 | 27 | } else { |
178 | 27 | ZVAL_NULL(&pelement->inf); |
179 | 27 | } |
180 | | /* Call the old value's destructor last, in case it moves the entry */ |
181 | 37 | zval_ptr_dtor(&zv_inf); |
182 | 37 | return pelement; |
183 | 37 | } |
184 | | |
185 | | /* NULL initialization necessary because `spl_object_storage_create_element` could bail out due to OOM. */ |
186 | 2.44k | ZVAL_PTR(entry_zv, NULL); |
187 | 2.44k | pelement = spl_object_storage_create_element(obj, inf); |
188 | 2.44k | Z_PTR_P(entry_zv) = pelement; |
189 | 2.44k | return pelement; |
190 | 2.48k | } /* }}} */ |
191 | | |
192 | | static spl_SplObjectStorageElement *spl_object_storage_attach(spl_SplObjectStorage *intern, zend_object *obj, zval *inf) /* {{{ */ |
193 | 2.48k | { |
194 | 2.48k | if (UNEXPECTED(spl_object_storage_is_mutating_within_get_hash_call())) { |
195 | 0 | return NULL; |
196 | 0 | } |
197 | | |
198 | 2.48k | if (EXPECTED(!(intern->flags & SOS_OVERRIDDEN_WRITE_DIMENSION))) { |
199 | 2.48k | return spl_object_storage_attach_handle(intern, obj, inf); |
200 | 2.48k | } |
201 | | /* getHash or offsetSet is overridden. */ |
202 | | |
203 | 0 | spl_SplObjectStorageElement *pelement, element; |
204 | 0 | zend_hash_key key; |
205 | 0 | if (spl_object_storage_get_hash(&key, intern, obj) == FAILURE) { |
206 | 0 | return NULL; |
207 | 0 | } |
208 | | |
209 | 0 | pelement = spl_object_storage_get(intern, &key); |
210 | |
|
211 | 0 | if (pelement) { |
212 | 0 | zval zv_inf; |
213 | 0 | ZVAL_COPY_VALUE(&zv_inf, &pelement->inf); |
214 | 0 | if (inf) { |
215 | 0 | ZVAL_COPY(&pelement->inf, inf); |
216 | 0 | } else { |
217 | 0 | ZVAL_NULL(&pelement->inf); |
218 | 0 | } |
219 | 0 | spl_object_storage_free_hash(intern, &key); |
220 | | /* Call the old value's destructor last, in case it moves the entry */ |
221 | 0 | zval_ptr_dtor(&zv_inf); |
222 | 0 | return pelement; |
223 | 0 | } |
224 | | |
225 | 0 | element.obj = zend_object_copy(obj); |
226 | 0 | if (inf) { |
227 | 0 | ZVAL_COPY(&element.inf, inf); |
228 | 0 | } else { |
229 | 0 | ZVAL_NULL(&element.inf); |
230 | 0 | } |
231 | 0 | if (key.key) { |
232 | 0 | pelement = zend_hash_update_mem(&intern->storage, key.key, &element, sizeof(spl_SplObjectStorageElement)); |
233 | 0 | } else { |
234 | 0 | pelement = zend_hash_index_update_mem(&intern->storage, key.h, &element, sizeof(spl_SplObjectStorageElement)); |
235 | 0 | } |
236 | 0 | spl_object_storage_free_hash(intern, &key); |
237 | 0 | return pelement; |
238 | 0 | } /* }}} */ |
239 | | |
240 | | static zend_result spl_object_storage_detach(spl_SplObjectStorage *intern, zend_object *obj) /* {{{ */ |
241 | 0 | { |
242 | 0 | if (UNEXPECTED(spl_object_storage_is_mutating_within_get_hash_call())) { |
243 | 0 | return FAILURE; |
244 | 0 | } |
245 | | |
246 | 0 | if (EXPECTED(!(intern->flags & SOS_OVERRIDDEN_UNSET_DIMENSION))) { |
247 | 0 | return zend_hash_index_del(&intern->storage, obj->handle); |
248 | 0 | } |
249 | 0 | zend_result ret = FAILURE; |
250 | 0 | zend_hash_key key; |
251 | 0 | if (spl_object_storage_get_hash(&key, intern, obj) == FAILURE) { |
252 | 0 | return ret; |
253 | 0 | } |
254 | 0 | if (key.key) { |
255 | 0 | ret = zend_hash_del(&intern->storage, key.key); |
256 | 0 | } else { |
257 | 0 | ret = zend_hash_index_del(&intern->storage, key.h); |
258 | 0 | } |
259 | 0 | spl_object_storage_free_hash(intern, &key); |
260 | |
|
261 | 0 | return ret; |
262 | 0 | } /* }}}*/ |
263 | | |
264 | | /* TODO: make this an official Zend API? */ |
265 | 0 | #define SPL_SAFE_HASH_FOREACH_PTR(_ht, _ptr) do { \ |
266 | 0 | const HashTable *__ht = (_ht); \ |
267 | 0 | zval *_z = __ht->arPacked; \ |
268 | 0 | for (uint32_t _idx = 0; _idx < __ht->nNumUsed; _idx++, _z = ZEND_HASH_ELEMENT(__ht, _idx)) { \ |
269 | 0 | if (UNEXPECTED(Z_ISUNDEF_P(_z))) continue; \ |
270 | 0 | _ptr = Z_PTR_P(_z); |
271 | | |
272 | 0 | static zend_result spl_object_storage_addall(spl_SplObjectStorage *intern, spl_SplObjectStorage *other) { /* {{{ */ |
273 | 0 | spl_SplObjectStorageElement *element; |
274 | |
|
275 | 0 | SPL_SAFE_HASH_FOREACH_PTR(&other->storage, element) { |
276 | 0 | zval zv; |
277 | 0 | zend_object *obj = element->obj; |
278 | 0 | GC_ADDREF(obj); |
279 | 0 | ZVAL_COPY(&zv, &element->inf); |
280 | 0 | spl_SplObjectStorageElement *attached = spl_object_storage_attach(intern, obj, &zv); |
281 | 0 | zval_ptr_dtor(&zv); |
282 | 0 | OBJ_RELEASE(obj); |
283 | 0 | if (UNEXPECTED(!attached)) { |
284 | 0 | return FAILURE; |
285 | 0 | } |
286 | 0 | } ZEND_HASH_FOREACH_END(); |
287 | | |
288 | 0 | intern->index = 0; |
289 | 0 | return SUCCESS; |
290 | 0 | } /* }}} */ |
291 | | |
292 | | #define SPL_OBJECT_STORAGE_CLASS_HAS_OVERRIDE(class_type, zstr_method) \ |
293 | 4 | (class_type->arrayaccess_funcs_ptr && class_type->arrayaccess_funcs_ptr->zstr_method) |
294 | | |
295 | | static zend_object *spl_object_storage_new_ex(zend_class_entry *class_type, zend_object *orig) /* {{{ */ |
296 | 37.5k | { |
297 | 37.5k | spl_SplObjectStorage *intern; |
298 | 37.5k | const zend_class_entry *parent = class_type; |
299 | | |
300 | 37.5k | intern = zend_object_alloc(sizeof(spl_SplObjectStorage), parent); |
301 | 37.5k | intern->pos = 0; |
302 | | |
303 | 37.5k | zend_object_std_init(&intern->std, class_type); |
304 | 37.5k | object_properties_init(&intern->std, class_type); |
305 | | |
306 | 37.5k | zend_hash_init(&intern->storage, 0, NULL, spl_object_storage_dtor, 0); |
307 | | |
308 | 37.7k | while (parent) { |
309 | 37.5k | if (parent == spl_ce_SplObjectStorage) { |
310 | | /* Possible optimization: Cache these results with a map from class entry to IS_NULL/IS_PTR. |
311 | | * Or maybe just a single item with the result for the most recently loaded subclass. */ |
312 | 37.2k | if (class_type != spl_ce_SplObjectStorage) { |
313 | 1 | zend_function *get_hash = zend_hash_str_find_ptr(&class_type->function_table, "gethash", sizeof("gethash") - 1); |
314 | 1 | if (get_hash->common.scope != spl_ce_SplObjectStorage) { |
315 | 0 | intern->fptr_get_hash = get_hash; |
316 | 0 | } |
317 | 1 | if (intern->fptr_get_hash != NULL || |
318 | 1 | SPL_OBJECT_STORAGE_CLASS_HAS_OVERRIDE(class_type, zf_offsetget) || |
319 | 1 | SPL_OBJECT_STORAGE_CLASS_HAS_OVERRIDE(class_type, zf_offsetexists)) { |
320 | 1 | intern->flags |= SOS_OVERRIDDEN_READ_DIMENSION; |
321 | 1 | } |
322 | | |
323 | 1 | if (intern->fptr_get_hash != NULL || |
324 | 1 | SPL_OBJECT_STORAGE_CLASS_HAS_OVERRIDE(class_type, zf_offsetset)) { |
325 | 1 | intern->flags |= SOS_OVERRIDDEN_WRITE_DIMENSION; |
326 | 1 | } |
327 | | |
328 | 1 | if (intern->fptr_get_hash != NULL || |
329 | 1 | SPL_OBJECT_STORAGE_CLASS_HAS_OVERRIDE(class_type, zf_offsetunset)) { |
330 | 1 | intern->flags |= SOS_OVERRIDDEN_UNSET_DIMENSION; |
331 | 1 | } |
332 | 1 | } |
333 | 37.2k | break; |
334 | 37.2k | } |
335 | | |
336 | 250 | parent = parent->parent; |
337 | 250 | } |
338 | | |
339 | 37.5k | if (orig) { |
340 | 0 | spl_SplObjectStorage *other = spl_object_storage_from_obj(orig); |
341 | 0 | spl_object_storage_addall(intern, other); |
342 | 0 | } |
343 | | |
344 | 37.5k | return &intern->std; |
345 | 37.5k | } |
346 | | /* }}} */ |
347 | | |
348 | | /* {{{ spl_object_storage_clone */ |
349 | | static zend_object *spl_object_storage_clone(zend_object *old_object) |
350 | 0 | { |
351 | 0 | zend_object *new_object; |
352 | |
|
353 | 0 | new_object = spl_object_storage_new_ex(old_object->ce, old_object); |
354 | |
|
355 | 0 | zend_objects_clone_members(new_object, old_object); |
356 | |
|
357 | 0 | return new_object; |
358 | 0 | } |
359 | | /* }}} */ |
360 | | |
361 | | static inline HashTable* spl_object_storage_debug_info(zend_object *obj) /* {{{ */ |
362 | 0 | { |
363 | 0 | spl_SplObjectStorage *intern = spl_object_storage_from_obj(obj); |
364 | 0 | spl_SplObjectStorageElement *element; |
365 | 0 | HashTable *props; |
366 | 0 | zval tmp, storage; |
367 | 0 | HashTable *debug_info; |
368 | |
|
369 | 0 | props = obj->handlers->get_properties(obj); |
370 | |
|
371 | 0 | debug_info = zend_new_array(zend_hash_num_elements(props) + 1); |
372 | 0 | zend_hash_copy(debug_info, props, (copy_ctor_func_t)zval_add_ref); |
373 | |
|
374 | 0 | array_init(&storage); |
375 | |
|
376 | 0 | ZEND_HASH_FOREACH_PTR(&intern->storage, element) { |
377 | 0 | array_init(&tmp); |
378 | 0 | zval obj; |
379 | 0 | ZVAL_OBJ_COPY(&obj, element->obj); |
380 | 0 | add_assoc_zval_ex(&tmp, "obj", sizeof("obj") - 1, &obj); |
381 | 0 | Z_TRY_ADDREF(element->inf); |
382 | 0 | add_assoc_zval_ex(&tmp, "inf", sizeof("inf") - 1, &element->inf); |
383 | 0 | zend_hash_next_index_insert(Z_ARRVAL(storage), &tmp); |
384 | 0 | } ZEND_HASH_FOREACH_END(); |
385 | |
|
386 | 0 | spl_set_private_debug_info_property(spl_ce_SplObjectStorage, "storage", strlen("storage"), debug_info, &storage); |
387 | |
|
388 | 0 | return debug_info; |
389 | 0 | } |
390 | | /* }}} */ |
391 | | |
392 | | /* overridden for garbage collection */ |
393 | | static HashTable *spl_object_storage_get_gc(zend_object *obj, zval **table, int *n) /* {{{ */ |
394 | 79.0k | { |
395 | 79.0k | spl_SplObjectStorage *intern = spl_object_storage_from_obj(obj); |
396 | 79.0k | spl_SplObjectStorageElement *element; |
397 | 79.0k | zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create(); |
398 | | |
399 | 94.9k | ZEND_HASH_FOREACH_PTR(&intern->storage, element) { |
400 | 94.9k | zend_get_gc_buffer_add_obj(gc_buffer, element->obj); |
401 | 94.9k | zend_get_gc_buffer_add_zval(gc_buffer, &element->inf); |
402 | 94.9k | } ZEND_HASH_FOREACH_END(); |
403 | | |
404 | 79.0k | zend_get_gc_buffer_use(gc_buffer, table, n); |
405 | 79.0k | return zend_std_get_properties(obj); |
406 | 79.0k | } |
407 | | /* }}} */ |
408 | | |
409 | | static int spl_object_storage_compare_info(zval *e1, zval *e2) /* {{{ */ |
410 | 0 | { |
411 | 0 | spl_SplObjectStorageElement *s1 = (spl_SplObjectStorageElement*)Z_PTR_P(e1); |
412 | 0 | spl_SplObjectStorageElement *s2 = (spl_SplObjectStorageElement*)Z_PTR_P(e2); |
413 | |
|
414 | 0 | return zend_compare(&s1->inf, &s2->inf); |
415 | 0 | } |
416 | | /* }}} */ |
417 | | |
418 | | static int spl_object_storage_compare_objects(zval *o1, zval *o2) /* {{{ */ |
419 | 0 | { |
420 | 0 | zend_object *zo1; |
421 | 0 | zend_object *zo2; |
422 | |
|
423 | 0 | ZEND_COMPARE_OBJECTS_FALLBACK(o1, o2); |
424 | |
|
425 | 0 | zo1 = (zend_object *)Z_OBJ_P(o1); |
426 | 0 | zo2 = (zend_object *)Z_OBJ_P(o2); |
427 | |
|
428 | 0 | if (zo1->ce != spl_ce_SplObjectStorage || zo2->ce != spl_ce_SplObjectStorage) { |
429 | 0 | return ZEND_UNCOMPARABLE; |
430 | 0 | } |
431 | | |
432 | 0 | return zend_hash_compare(&(Z_SPLOBJSTORAGE_P(o1))->storage, &(Z_SPLOBJSTORAGE_P(o2))->storage, (compare_func_t)spl_object_storage_compare_info, 0); |
433 | 0 | } |
434 | | /* }}} */ |
435 | | |
436 | | /* {{{ spl_array_object_new */ |
437 | | static zend_object *spl_SplObjectStorage_new(zend_class_entry *class_type) |
438 | 37.5k | { |
439 | 37.5k | return spl_object_storage_new_ex(class_type, NULL); |
440 | 37.5k | } |
441 | | /* }}} */ |
442 | | |
443 | | /* Returns true if the SplObjectStorage contains an entry for getHash(obj), even if the corresponding value is null. */ |
444 | | static bool spl_object_storage_contains(spl_SplObjectStorage *intern, zend_object *obj) /* {{{ */ |
445 | 0 | { |
446 | 0 | if (EXPECTED(!intern->fptr_get_hash)) { |
447 | 0 | return zend_hash_index_find(&intern->storage, obj->handle) != NULL; |
448 | 0 | } |
449 | 0 | zend_hash_key key; |
450 | 0 | if (spl_object_storage_get_hash(&key, intern, obj) == FAILURE) { |
451 | 0 | return true; |
452 | 0 | } |
453 | | |
454 | 0 | ZEND_ASSERT(key.key); |
455 | 0 | bool found = zend_hash_exists(&intern->storage, key.key); |
456 | 0 | zend_string_release_ex(key.key, 0); |
457 | |
|
458 | 0 | return found; |
459 | 0 | } /* }}} */ |
460 | | |
461 | | /* {{{ Attaches an object to the storage if not yet contained */ |
462 | | PHP_METHOD(SplObjectStorage, attach) |
463 | 0 | { |
464 | 0 | zend_object *obj; |
465 | 0 | zval *inf = NULL; |
466 | |
|
467 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
468 | |
|
469 | 0 | ZEND_PARSE_PARAMETERS_START(1, 2) |
470 | 0 | Z_PARAM_OBJ(obj) |
471 | 0 | Z_PARAM_OPTIONAL |
472 | 0 | Z_PARAM_ZVAL(inf) |
473 | 0 | ZEND_PARSE_PARAMETERS_END(); |
474 | 0 | spl_object_storage_attach(intern, obj, inf); |
475 | 0 | if (UNEXPECTED(EG(exception))) { |
476 | 0 | RETURN_THROWS(); |
477 | 0 | } |
478 | 0 | } /* }}} */ |
479 | | |
480 | | // todo: make spl_object_storage_has_dimension return bool as well |
481 | | static int spl_object_storage_has_dimension(zend_object *object, zval *offset, int check_empty) |
482 | 0 | { |
483 | 0 | spl_SplObjectStorage *intern = spl_object_storage_from_obj(object); |
484 | 0 | if (UNEXPECTED(offset == NULL || Z_TYPE_P(offset) != IS_OBJECT || (intern->flags & SOS_OVERRIDDEN_READ_DIMENSION))) { |
485 | | /* Can't optimize empty()/isset() check if getHash, offsetExists, or offsetGet is overridden */ |
486 | 0 | return zend_std_has_dimension(object, offset, check_empty); |
487 | 0 | } |
488 | 0 | spl_SplObjectStorageElement *element = zend_hash_index_find_ptr(&intern->storage, Z_OBJ_HANDLE_P(offset)); |
489 | 0 | if (!element) { |
490 | 0 | return 0; |
491 | 0 | } |
492 | | |
493 | 0 | if (check_empty) { |
494 | 0 | return i_zend_is_true(&element->inf); |
495 | 0 | } |
496 | | /* NOTE: SplObjectStorage->offsetExists() is an alias of SplObjectStorage->contains(), so this returns true even if the value is null. */ |
497 | 0 | return 1; |
498 | 0 | } |
499 | | |
500 | | static zval *spl_object_storage_read_dimension(zend_object *object, zval *offset, int type, zval *rv) |
501 | 0 | { |
502 | 0 | spl_SplObjectStorage *intern = spl_object_storage_from_obj(object); |
503 | 0 | if (UNEXPECTED(offset == NULL || Z_TYPE_P(offset) != IS_OBJECT || (intern->flags & SOS_OVERRIDDEN_READ_DIMENSION))) { |
504 | | /* Can't optimize it if getHash, offsetExists, or offsetGet is overridden */ |
505 | 0 | return zend_std_read_dimension(object, offset, type, rv); |
506 | 0 | } |
507 | 0 | spl_SplObjectStorageElement *element = zend_hash_index_find_ptr(&intern->storage, Z_OBJ_HANDLE_P(offset)); |
508 | |
|
509 | 0 | if (!element) { |
510 | 0 | if (type == BP_VAR_IS) { |
511 | 0 | return &EG(uninitialized_zval); |
512 | 0 | } |
513 | 0 | zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Object not found"); |
514 | 0 | return NULL; |
515 | 0 | } else { |
516 | | /* This deliberately returns a non-reference, even for BP_VAR_W and BP_VAR_RW, to behave the same way as SplObjectStorage did when using the default zend_std_read_dimension behavior. |
517 | | * i.e. This prevents taking a reference to an entry of SplObjectStorage because offsetGet would return a non-reference. */ |
518 | 0 | ZVAL_COPY_DEREF(rv, &element->inf); |
519 | 0 | return rv; |
520 | 0 | } |
521 | 0 | } |
522 | | |
523 | | static void spl_object_storage_write_dimension(zend_object *object, zval *offset, zval *inf) |
524 | 0 | { |
525 | 0 | spl_SplObjectStorage *intern = spl_object_storage_from_obj(object); |
526 | 0 | if (UNEXPECTED(spl_object_storage_is_mutating_within_get_hash_call())) { |
527 | 0 | return; |
528 | 0 | } |
529 | | |
530 | 0 | if (UNEXPECTED(offset == NULL || Z_TYPE_P(offset) != IS_OBJECT || (intern->flags & SOS_OVERRIDDEN_WRITE_DIMENSION))) { |
531 | 0 | zend_std_write_dimension(object, offset, inf); |
532 | 0 | return; |
533 | 0 | } |
534 | 0 | spl_object_storage_attach_handle(intern, Z_OBJ_P(offset), inf); |
535 | 0 | } |
536 | | |
537 | | static void spl_multiple_iterator_write_dimension(zend_object *object, zval *offset, zval *inf) |
538 | 0 | { |
539 | 0 | spl_SplObjectStorage *intern = spl_object_storage_from_obj(object); |
540 | 0 | if (UNEXPECTED(spl_object_storage_is_mutating_within_get_hash_call())) { |
541 | 0 | return; |
542 | 0 | } |
543 | | |
544 | 0 | if (UNEXPECTED(offset == NULL || Z_TYPE_P(offset) != IS_OBJECT || (intern->flags & SOS_OVERRIDDEN_WRITE_DIMENSION))) { |
545 | 0 | zend_std_write_dimension(object, offset, inf); |
546 | 0 | return; |
547 | 0 | } |
548 | 0 | if (UNEXPECTED(!Z_OBJCE_P(offset)->iterator_funcs_ptr || !Z_OBJCE_P(offset)->iterator_funcs_ptr->zf_valid)) { |
549 | 0 | zend_type_error("Can only attach objects that implement the Iterator interface"); |
550 | 0 | return; |
551 | 0 | } |
552 | 0 | spl_object_storage_attach_handle(intern, Z_OBJ_P(offset), inf); |
553 | 0 | } |
554 | | |
555 | | static void spl_object_storage_unset_dimension(zend_object *object, zval *offset) |
556 | 0 | { |
557 | 0 | spl_SplObjectStorage *intern = spl_object_storage_from_obj(object); |
558 | 0 | if (UNEXPECTED(spl_object_storage_is_mutating_within_get_hash_call())) { |
559 | 0 | return; |
560 | 0 | } |
561 | | |
562 | 0 | if (UNEXPECTED(Z_TYPE_P(offset) != IS_OBJECT || (intern->flags & SOS_OVERRIDDEN_UNSET_DIMENSION))) { |
563 | 0 | zend_std_unset_dimension(object, offset); |
564 | 0 | return; |
565 | 0 | } |
566 | 0 | zend_hash_index_del(&intern->storage, Z_OBJ_HANDLE_P(offset)); |
567 | 0 | } |
568 | | |
569 | | /* {{{ Detaches an object from the storage */ |
570 | | PHP_METHOD(SplObjectStorage, detach) |
571 | 0 | { |
572 | 0 | zend_object *obj; |
573 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
574 | |
|
575 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
576 | 0 | Z_PARAM_OBJ(obj) |
577 | 0 | ZEND_PARSE_PARAMETERS_END(); |
578 | 0 | spl_object_storage_detach(intern, obj); |
579 | 0 | if (UNEXPECTED(EG(exception))) { |
580 | 0 | RETURN_THROWS(); |
581 | 0 | } |
582 | | |
583 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
584 | 0 | intern->index = 0; |
585 | 0 | } /* }}} */ |
586 | | |
587 | | /* {{{ Returns the hash of an object */ |
588 | | PHP_METHOD(SplObjectStorage, getHash) |
589 | 0 | { |
590 | 0 | zend_object *obj; |
591 | |
|
592 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
593 | 0 | Z_PARAM_OBJ(obj) |
594 | 0 | ZEND_PARSE_PARAMETERS_END(); |
595 | | |
596 | 0 | RETURN_NEW_STR(php_spl_object_hash(obj)); |
597 | |
|
598 | 0 | } /* }}} */ |
599 | | |
600 | | /* {{{ Returns associated information for a stored object */ |
601 | | PHP_METHOD(SplObjectStorage, offsetGet) |
602 | 0 | { |
603 | 0 | zend_object *obj; |
604 | 0 | spl_SplObjectStorageElement *element; |
605 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
606 | 0 | zend_hash_key key; |
607 | |
|
608 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
609 | 0 | Z_PARAM_OBJ(obj) |
610 | 0 | ZEND_PARSE_PARAMETERS_END(); |
611 | | |
612 | 0 | if (spl_object_storage_get_hash(&key, intern, obj) == FAILURE) { |
613 | | /* This may be the old NULL fallback, or an exception thrown by getHash(). */ |
614 | 0 | RETURN_NULL(); |
615 | 0 | } |
616 | | |
617 | 0 | element = spl_object_storage_get(intern, &key); |
618 | 0 | spl_object_storage_free_hash(intern, &key); |
619 | |
|
620 | 0 | if (!element) { |
621 | 0 | zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Object not found"); |
622 | 0 | } else { |
623 | 0 | RETURN_COPY_DEREF(&element->inf); |
624 | 0 | } |
625 | 0 | } /* }}} */ |
626 | | |
627 | | /* {{{ Add all elements contained in $os */ |
628 | | PHP_METHOD(SplObjectStorage, addAll) |
629 | 0 | { |
630 | 0 | zval *obj; |
631 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
632 | 0 | spl_SplObjectStorage *other; |
633 | |
|
634 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &obj, spl_ce_SplObjectStorage) == FAILURE) { |
635 | 0 | RETURN_THROWS(); |
636 | 0 | } |
637 | | |
638 | 0 | other = Z_SPLOBJSTORAGE_P(obj); |
639 | |
|
640 | 0 | if (UNEXPECTED(spl_object_storage_addall(intern, other) == FAILURE)) { |
641 | 0 | RETURN_THROWS(); |
642 | 0 | } |
643 | | |
644 | 0 | RETURN_LONG(zend_hash_num_elements(&intern->storage)); |
645 | 0 | } /* }}} */ |
646 | | |
647 | | /* {{{ Remove all elements contained in $os */ |
648 | | PHP_METHOD(SplObjectStorage, removeAll) |
649 | 0 | { |
650 | 0 | zval *obj; |
651 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
652 | 0 | spl_SplObjectStorage *other; |
653 | 0 | spl_SplObjectStorageElement *element; |
654 | |
|
655 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &obj, spl_ce_SplObjectStorage) == FAILURE) { |
656 | 0 | RETURN_THROWS(); |
657 | 0 | } |
658 | | |
659 | 0 | other = Z_SPLOBJSTORAGE_P(obj); |
660 | |
|
661 | 0 | zend_hash_internal_pointer_reset(&other->storage); |
662 | 0 | while ((element = zend_hash_get_current_data_ptr(&other->storage)) != NULL) { |
663 | 0 | if (spl_object_storage_detach(intern, element->obj) == FAILURE) { |
664 | 0 | if (UNEXPECTED(EG(exception))) { |
665 | 0 | RETURN_THROWS(); |
666 | 0 | } |
667 | 0 | zend_hash_move_forward(&other->storage); |
668 | 0 | } |
669 | 0 | } |
670 | | |
671 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
672 | 0 | intern->index = 0; |
673 | |
|
674 | 0 | RETURN_LONG(zend_hash_num_elements(&intern->storage)); |
675 | 0 | } /* }}} */ |
676 | | |
677 | | /* {{{ Remove elements not common to both this SplObjectStorage instance and $os */ |
678 | | PHP_METHOD(SplObjectStorage, removeAllExcept) |
679 | 0 | { |
680 | 0 | zval *obj; |
681 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
682 | 0 | spl_SplObjectStorage *other; |
683 | 0 | spl_SplObjectStorageElement *element; |
684 | |
|
685 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &obj, spl_ce_SplObjectStorage) == FAILURE) { |
686 | 0 | RETURN_THROWS(); |
687 | 0 | } |
688 | | |
689 | 0 | other = Z_SPLOBJSTORAGE_P(obj); |
690 | |
|
691 | 0 | SPL_SAFE_HASH_FOREACH_PTR(&intern->storage, element) { |
692 | 0 | zend_object *elem_obj = element->obj; |
693 | 0 | GC_ADDREF(elem_obj); |
694 | 0 | bool contains = spl_object_storage_contains(other, elem_obj); |
695 | 0 | if (UNEXPECTED(EG(exception))) { |
696 | 0 | OBJ_RELEASE(elem_obj); |
697 | 0 | RETURN_THROWS(); |
698 | 0 | } |
699 | 0 | if (!contains) { |
700 | 0 | if (spl_object_storage_detach(intern, elem_obj) == FAILURE) { |
701 | 0 | OBJ_RELEASE(elem_obj); |
702 | 0 | if (UNEXPECTED(EG(exception))) { |
703 | 0 | RETURN_THROWS(); |
704 | 0 | } |
705 | 0 | continue; |
706 | 0 | } |
707 | 0 | } |
708 | 0 | OBJ_RELEASE(elem_obj); |
709 | 0 | } ZEND_HASH_FOREACH_END(); |
710 | | |
711 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
712 | 0 | intern->index = 0; |
713 | |
|
714 | 0 | RETURN_LONG(zend_hash_num_elements(&intern->storage)); |
715 | 0 | } |
716 | | /* }}} */ |
717 | | |
718 | | /* {{{ Determine whether an object is contained in the storage */ |
719 | | PHP_METHOD(SplObjectStorage, contains) |
720 | 0 | { |
721 | 0 | zend_object *obj; |
722 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
723 | |
|
724 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
725 | 0 | Z_PARAM_OBJ(obj) |
726 | 0 | ZEND_PARSE_PARAMETERS_END(); |
727 | | |
728 | 0 | bool contains = spl_object_storage_contains(intern, obj); |
729 | 0 | if (UNEXPECTED(EG(exception))) { |
730 | 0 | RETURN_THROWS(); |
731 | 0 | } |
732 | | |
733 | 0 | RETURN_BOOL(contains); |
734 | 0 | } /* }}} */ |
735 | | |
736 | | /* {{{ Determine number of objects in storage */ |
737 | | PHP_METHOD(SplObjectStorage, count) |
738 | 0 | { |
739 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
740 | 0 | zend_long mode = PHP_COUNT_NORMAL; |
741 | |
|
742 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &mode) == FAILURE) { |
743 | 0 | RETURN_THROWS(); |
744 | 0 | } |
745 | | |
746 | 0 | if (mode == PHP_COUNT_RECURSIVE) { |
747 | 0 | zend_long count = php_count_recursive(&intern->storage); |
748 | 0 | if (UNEXPECTED(count < 0)) { |
749 | 0 | RETURN_THROWS(); |
750 | 0 | } |
751 | 0 | RETURN_LONG(count); |
752 | 0 | } |
753 | | |
754 | 0 | RETURN_LONG(zend_hash_num_elements(&intern->storage)); |
755 | 0 | } /* }}} */ |
756 | | |
757 | | /* {{{ Rewind to first position */ |
758 | | PHP_METHOD(SplObjectStorage, rewind) |
759 | 0 | { |
760 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
761 | |
|
762 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
763 | | |
764 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
765 | 0 | intern->index = 0; |
766 | 0 | } /* }}} */ |
767 | | |
768 | | /* {{{ Returns whether current position is valid */ |
769 | | PHP_METHOD(SplObjectStorage, valid) |
770 | 0 | { |
771 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
772 | |
|
773 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
774 | | |
775 | 0 | RETURN_BOOL(zend_hash_has_more_elements_ex(&intern->storage, &intern->pos) == SUCCESS); |
776 | 0 | } /* }}} */ |
777 | | |
778 | | /* {{{ Returns current key */ |
779 | | PHP_METHOD(SplObjectStorage, key) |
780 | 0 | { |
781 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
782 | |
|
783 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
784 | | |
785 | 0 | RETURN_LONG(intern->index); |
786 | 0 | } /* }}} */ |
787 | | |
788 | | /* {{{ Returns current element */ |
789 | | PHP_METHOD(SplObjectStorage, current) |
790 | 0 | { |
791 | 0 | spl_SplObjectStorageElement *element; |
792 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
793 | |
|
794 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
795 | | |
796 | 0 | if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) == NULL) { |
797 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Called current() on invalid iterator", 0); |
798 | 0 | RETURN_THROWS(); |
799 | 0 | } |
800 | 0 | ZVAL_OBJ_COPY(return_value, element->obj); |
801 | 0 | } /* }}} */ |
802 | | |
803 | | /* {{{ Returns associated information to current element */ |
804 | | PHP_METHOD(SplObjectStorage, getInfo) |
805 | 0 | { |
806 | 0 | spl_SplObjectStorageElement *element; |
807 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
808 | |
|
809 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
810 | | |
811 | 0 | if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) == NULL) { |
812 | 0 | RETURN_NULL(); |
813 | 0 | } |
814 | 0 | ZVAL_COPY(return_value, &element->inf); |
815 | 0 | } /* }}} */ |
816 | | |
817 | | /* {{{ Sets associated information of current element to $inf */ |
818 | | PHP_METHOD(SplObjectStorage, setInfo) |
819 | 0 | { |
820 | 0 | spl_SplObjectStorageElement *element; |
821 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
822 | 0 | zval *inf; |
823 | |
|
824 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &inf) == FAILURE) { |
825 | 0 | RETURN_THROWS(); |
826 | 0 | } |
827 | 0 | if (UNEXPECTED(spl_object_storage_is_mutating_within_get_hash_call())) { |
828 | 0 | RETURN_THROWS(); |
829 | 0 | } |
830 | | |
831 | 0 | if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) == NULL) { |
832 | 0 | RETURN_NULL(); |
833 | 0 | } |
834 | 0 | zval garbage; |
835 | 0 | ZVAL_COPY_VALUE(&garbage, &element->inf); |
836 | 0 | ZVAL_COPY(&element->inf, inf); |
837 | 0 | zval_ptr_dtor(&garbage); |
838 | 0 | } /* }}} */ |
839 | | |
840 | | /* {{{ Moves position forward */ |
841 | | PHP_METHOD(SplObjectStorage, next) |
842 | 0 | { |
843 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
844 | |
|
845 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
846 | | |
847 | 0 | zend_hash_move_forward_ex(&intern->storage, &intern->pos); |
848 | 0 | intern->index++; |
849 | 0 | } /* }}} */ |
850 | | |
851 | | /* {{{ Seek to position. */ |
852 | | PHP_METHOD(SplObjectStorage, seek) |
853 | 0 | { |
854 | 0 | zend_long position; |
855 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
856 | |
|
857 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &position) == FAILURE) { |
858 | 0 | RETURN_THROWS(); |
859 | 0 | } |
860 | | |
861 | 0 | if (position < 0 || position >= zend_hash_num_elements(&intern->storage)) { |
862 | 0 | zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position " ZEND_LONG_FMT " is out of range", position); |
863 | 0 | RETURN_THROWS(); |
864 | 0 | } |
865 | | |
866 | 0 | if (position == 0) { |
867 | | /* fast path */ |
868 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
869 | 0 | intern->index = 0; |
870 | 0 | } else if (position > intern->index) { |
871 | | /* unlike the optimization below, it's not cheap to go to the end */ |
872 | 0 | do { |
873 | 0 | zend_hash_move_forward_ex(&intern->storage, &intern->pos); |
874 | 0 | intern->index++; |
875 | 0 | } while (position > intern->index); |
876 | 0 | } else if (position < intern->index) { |
877 | | /* optimization: check if it's more profitable to reset and do a forwards seek instead, it's cheap to reset */ |
878 | 0 | if (intern->index - position > position) { |
879 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
880 | 0 | intern->index = 0; |
881 | 0 | do { |
882 | 0 | zend_hash_move_forward_ex(&intern->storage, &intern->pos); |
883 | 0 | intern->index++; |
884 | 0 | } while (position > intern->index); |
885 | 0 | } else { |
886 | 0 | do { |
887 | 0 | zend_hash_move_backwards_ex(&intern->storage, &intern->pos); |
888 | 0 | intern->index--; |
889 | 0 | } while (position < intern->index); |
890 | 0 | } |
891 | 0 | } |
892 | 0 | } /* }}} */ |
893 | | |
894 | | /* {{{ Serializes storage */ |
895 | | PHP_METHOD(SplObjectStorage, serialize) |
896 | 0 | { |
897 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
898 | |
|
899 | 0 | spl_SplObjectStorageElement *element; |
900 | 0 | zval members, flags; |
901 | 0 | HashPosition pos; |
902 | 0 | php_serialize_data_t var_hash; |
903 | 0 | smart_str buf = {0}; |
904 | |
|
905 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
906 | | |
907 | 0 | PHP_VAR_SERIALIZE_INIT(var_hash); |
908 | | |
909 | | /* storage */ |
910 | 0 | smart_str_appendl(&buf, "x:", 2); |
911 | 0 | ZVAL_LONG(&flags, zend_hash_num_elements(&intern->storage)); |
912 | 0 | php_var_serialize(&buf, &flags, &var_hash); |
913 | |
|
914 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &pos); |
915 | |
|
916 | 0 | while (zend_hash_has_more_elements_ex(&intern->storage, &pos) == SUCCESS) { |
917 | 0 | zval obj; |
918 | 0 | if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &pos)) == NULL) { |
919 | 0 | smart_str_free(&buf); |
920 | 0 | PHP_VAR_SERIALIZE_DESTROY(var_hash); |
921 | 0 | RETURN_NULL(); |
922 | 0 | } |
923 | 0 | ZVAL_OBJ(&obj, element->obj); |
924 | | |
925 | | /* Protect against modification; we need a full copy because the data may be refcounted. */ |
926 | 0 | zval inf_copy; |
927 | 0 | ZVAL_COPY(&inf_copy, &element->inf); |
928 | |
|
929 | 0 | php_var_serialize(&buf, &obj, &var_hash); |
930 | 0 | smart_str_appendc(&buf, ','); |
931 | 0 | php_var_serialize(&buf, &inf_copy, &var_hash); |
932 | 0 | smart_str_appendc(&buf, ';'); |
933 | 0 | zend_hash_move_forward_ex(&intern->storage, &pos); |
934 | |
|
935 | 0 | zval_ptr_dtor(&inf_copy); |
936 | 0 | } |
937 | | |
938 | | /* members */ |
939 | 0 | smart_str_appendl(&buf, "m:", 2); |
940 | |
|
941 | 0 | ZVAL_ARR(&members, zend_array_dup(zend_std_get_properties(Z_OBJ_P(ZEND_THIS)))); |
942 | 0 | php_var_serialize(&buf, &members, &var_hash); /* finishes the string */ |
943 | 0 | zval_ptr_dtor(&members); |
944 | | |
945 | | /* done */ |
946 | 0 | PHP_VAR_SERIALIZE_DESTROY(var_hash); |
947 | |
|
948 | 0 | RETURN_STR(smart_str_extract(&buf)); |
949 | 0 | } /* }}} */ |
950 | | |
951 | | /* {{{ Unserializes storage */ |
952 | | PHP_METHOD(SplObjectStorage, unserialize) |
953 | 176 | { |
954 | 176 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
955 | | |
956 | 176 | char *buf; |
957 | 176 | size_t buf_len; |
958 | 176 | const unsigned char *p, *s; |
959 | 176 | php_unserialize_data_t var_hash; |
960 | 176 | zval *pcount, *pmembers; |
961 | 176 | spl_SplObjectStorageElement *element; |
962 | 176 | zend_long count; |
963 | | |
964 | 176 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) { |
965 | 0 | RETURN_THROWS(); |
966 | 0 | } |
967 | | |
968 | 176 | if (buf_len == 0) { |
969 | 2 | return; |
970 | 2 | } |
971 | | |
972 | | /* storage */ |
973 | 174 | s = p = (const unsigned char*)buf; |
974 | 174 | PHP_VAR_UNSERIALIZE_INIT(var_hash); |
975 | | |
976 | 174 | if (*p!= 'x' || *++p != ':') { |
977 | 8 | goto outexcept; |
978 | 8 | } |
979 | 166 | ++p; |
980 | | |
981 | 166 | pcount = var_tmp_var(&var_hash); |
982 | 166 | if (!php_var_unserialize(pcount, &p, s + buf_len, &var_hash) || Z_TYPE_P(pcount) != IS_LONG) { |
983 | 3 | goto outexcept; |
984 | 3 | } |
985 | | |
986 | 163 | --p; /* for ';' */ |
987 | 163 | count = Z_LVAL_P(pcount); |
988 | 163 | if (count < 0) { |
989 | 37 | goto outexcept; |
990 | 37 | } |
991 | | |
992 | 241 | while (count-- > 0) { |
993 | 212 | spl_SplObjectStorageElement *pelement; |
994 | 212 | zend_hash_key key; |
995 | 212 | zval *entry = var_tmp_var(&var_hash); |
996 | 212 | zval inf; |
997 | 212 | ZVAL_UNDEF(&inf); |
998 | | |
999 | 212 | if (*p != ';') { |
1000 | 15 | goto outexcept; |
1001 | 15 | } |
1002 | 197 | ++p; |
1003 | 197 | if(*p != 'O' && *p != 'C' && *p != 'r') { |
1004 | 51 | goto outexcept; |
1005 | 51 | } |
1006 | | /* store reference to allow cross-references between different elements */ |
1007 | 146 | if (!php_var_unserialize(entry, &p, s + buf_len, &var_hash)) { |
1008 | 28 | goto outexcept; |
1009 | 28 | } |
1010 | 118 | if (*p == ',') { /* new version has inf */ |
1011 | 20 | ++p; |
1012 | 20 | if (!php_var_unserialize(&inf, &p, s + buf_len, &var_hash)) { |
1013 | 2 | zval_ptr_dtor(&inf); |
1014 | 2 | goto outexcept; |
1015 | 2 | } |
1016 | 20 | } |
1017 | 116 | if (Z_TYPE_P(entry) != IS_OBJECT) { |
1018 | 1 | zval_ptr_dtor(&inf); |
1019 | 1 | goto outexcept; |
1020 | 1 | } |
1021 | | |
1022 | 115 | if (spl_object_storage_get_hash(&key, intern, Z_OBJ_P(entry)) == FAILURE) { |
1023 | 0 | zval_ptr_dtor(&inf); |
1024 | 0 | if (EG(exception)) { |
1025 | 0 | PHP_VAR_UNSERIALIZE_DESTROY(var_hash); |
1026 | 0 | RETURN_THROWS(); |
1027 | 0 | } |
1028 | 0 | goto outexcept; |
1029 | 0 | } |
1030 | 115 | pelement = spl_object_storage_get(intern, &key); |
1031 | 115 | spl_object_storage_free_hash(intern, &key); |
1032 | 115 | if (pelement) { |
1033 | 37 | zval obj; |
1034 | 37 | if (!Z_ISUNDEF(pelement->inf)) { |
1035 | 37 | var_push_dtor(&var_hash, &pelement->inf); |
1036 | 37 | } |
1037 | 37 | ZVAL_OBJ(&obj, pelement->obj); |
1038 | 37 | var_push_dtor(&var_hash, &obj); |
1039 | 37 | } |
1040 | 115 | element = spl_object_storage_attach(intern, Z_OBJ_P(entry), Z_ISUNDEF(inf)?NULL:&inf); |
1041 | 115 | if (UNEXPECTED(!element)) { |
1042 | 0 | zval_ptr_dtor(&inf); |
1043 | 0 | if (EG(exception)) { |
1044 | 0 | PHP_VAR_UNSERIALIZE_DESTROY(var_hash); |
1045 | 0 | RETURN_THROWS(); |
1046 | 0 | } |
1047 | 0 | goto outexcept; |
1048 | 0 | } |
1049 | 115 | var_replace(&var_hash, &inf, &element->inf); |
1050 | 115 | zval_ptr_dtor(&inf); |
1051 | 115 | } |
1052 | | |
1053 | 29 | if (*p != ';') { |
1054 | 3 | goto outexcept; |
1055 | 3 | } |
1056 | 26 | ++p; |
1057 | | |
1058 | | /* members */ |
1059 | 26 | if (*p!= 'm' || *++p != ':') { |
1060 | 2 | goto outexcept; |
1061 | 2 | } |
1062 | 24 | ++p; |
1063 | | |
1064 | 24 | pmembers = var_tmp_var(&var_hash); |
1065 | 24 | if (!php_var_unserialize(pmembers, &p, s + buf_len, &var_hash) || Z_TYPE_P(pmembers) != IS_ARRAY) { |
1066 | 13 | goto outexcept; |
1067 | 13 | } |
1068 | | |
1069 | | /* copy members */ |
1070 | 11 | object_properties_load(&intern->std, Z_ARRVAL_P(pmembers)); |
1071 | | |
1072 | 11 | PHP_VAR_UNSERIALIZE_DESTROY(var_hash); |
1073 | 11 | return; |
1074 | | |
1075 | 163 | outexcept: |
1076 | 163 | PHP_VAR_UNSERIALIZE_DESTROY(var_hash); |
1077 | 163 | zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset %zd of %zd bytes", ((char*)p - buf), buf_len); |
1078 | 163 | RETURN_THROWS(); |
1079 | | |
1080 | 163 | } /* }}} */ |
1081 | | |
1082 | | /* {{{ */ |
1083 | | PHP_METHOD(SplObjectStorage, __serialize) |
1084 | 0 | { |
1085 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1086 | 0 | spl_SplObjectStorageElement *elem; |
1087 | 0 | zval tmp; |
1088 | |
|
1089 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
1090 | | |
1091 | 0 | array_init(return_value); |
1092 | | |
1093 | | /* storage */ |
1094 | 0 | array_init_size(&tmp, 2 * zend_hash_num_elements(&intern->storage)); |
1095 | 0 | ZEND_HASH_FOREACH_PTR(&intern->storage, elem) { |
1096 | 0 | zval obj; |
1097 | 0 | ZVAL_OBJ_COPY(&obj, elem->obj); |
1098 | 0 | zend_hash_next_index_insert(Z_ARRVAL(tmp), &obj); |
1099 | 0 | Z_TRY_ADDREF(elem->inf); |
1100 | 0 | zend_hash_next_index_insert(Z_ARRVAL(tmp), &elem->inf); |
1101 | 0 | } ZEND_HASH_FOREACH_END(); |
1102 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp); |
1103 | | |
1104 | | /* members */ |
1105 | 0 | ZVAL_ARR(&tmp, zend_proptable_to_symtable( |
1106 | 0 | zend_std_get_properties(&intern->std), /* always_duplicate */ 1)); |
1107 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp); |
1108 | 0 | } /* }}} */ |
1109 | | |
1110 | | /* {{{ */ |
1111 | | PHP_METHOD(SplObjectStorage, __unserialize) |
1112 | 25.4k | { |
1113 | 25.4k | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1114 | 25.4k | HashTable *data; |
1115 | 25.4k | zval *storage_zv, *members_zv, *key, *val; |
1116 | | |
1117 | 25.4k | if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) { |
1118 | 0 | RETURN_THROWS(); |
1119 | 0 | } |
1120 | | |
1121 | 25.4k | storage_zv = zend_hash_index_find(data, 0); |
1122 | 25.4k | members_zv = zend_hash_index_find(data, 1); |
1123 | 25.4k | if (!storage_zv || !members_zv || |
1124 | 25.4k | Z_TYPE_P(storage_zv) != IS_ARRAY || Z_TYPE_P(members_zv) != IS_ARRAY) { |
1125 | 72 | zend_throw_exception(spl_ce_UnexpectedValueException, |
1126 | 72 | "Incomplete or ill-typed serialization data", 0); |
1127 | 72 | RETURN_THROWS(); |
1128 | 72 | } |
1129 | | |
1130 | 25.3k | if (zend_hash_num_elements(Z_ARRVAL_P(storage_zv)) % 2 != 0) { |
1131 | 2 | zend_throw_exception(spl_ce_UnexpectedValueException, "Odd number of elements", 0); |
1132 | 2 | RETURN_THROWS(); |
1133 | 2 | } |
1134 | | |
1135 | 25.3k | key = NULL; |
1136 | 34.8k | ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(storage_zv), val) { |
1137 | 34.8k | if (key) { |
1138 | 2.37k | if (Z_TYPE_P(key) != IS_OBJECT) { |
1139 | 2 | zend_throw_exception(spl_ce_UnexpectedValueException, "Non-object key", 0); |
1140 | 2 | RETURN_THROWS(); |
1141 | 2 | } |
1142 | | |
1143 | 2.36k | ZVAL_DEREF(val); |
1144 | 2.36k | if (UNEXPECTED(!spl_object_storage_attach(intern, Z_OBJ_P(key), val))) { |
1145 | 0 | RETURN_THROWS(); |
1146 | 0 | } |
1147 | 2.36k | key = NULL; |
1148 | 2.37k | } else { |
1149 | 2.37k | key = val; |
1150 | 2.37k | } |
1151 | 34.8k | } ZEND_HASH_FOREACH_END(); |
1152 | | |
1153 | 25.3k | object_properties_load(&intern->std, Z_ARRVAL_P(members_zv)); |
1154 | 25.3k | } |
1155 | | |
1156 | | /* {{{ */ |
1157 | | PHP_METHOD(SplObjectStorage, __debugInfo) |
1158 | 0 | { |
1159 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
1160 | | |
1161 | 0 | RETURN_ARR(spl_object_storage_debug_info(Z_OBJ_P(ZEND_THIS))); |
1162 | 0 | } |
1163 | | /* }}} */ |
1164 | | |
1165 | 0 | #define SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT 1 |
1166 | 0 | #define SPL_MULTIPLE_ITERATOR_GET_ALL_KEY 2 |
1167 | | |
1168 | | /* {{{ Iterator that iterates over several iterators one after the other */ |
1169 | | PHP_METHOD(MultipleIterator, __construct) |
1170 | 0 | { |
1171 | 0 | spl_SplObjectStorage *intern; |
1172 | 0 | zend_long flags = MIT_NEED_ALL|MIT_KEYS_NUMERIC; |
1173 | |
|
1174 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &flags) == FAILURE) { |
1175 | 0 | RETURN_THROWS(); |
1176 | 0 | } |
1177 | | |
1178 | 0 | intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1179 | 0 | intern->flags = flags; |
1180 | 0 | } |
1181 | | /* }}} */ |
1182 | | |
1183 | | /* {{{ Return current flags */ |
1184 | | PHP_METHOD(MultipleIterator, getFlags) |
1185 | 0 | { |
1186 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1187 | |
|
1188 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
1189 | 0 | RETURN_LONG(intern->flags); |
1190 | 0 | } |
1191 | | /* }}} */ |
1192 | | |
1193 | | /* {{{ Set flags */ |
1194 | | PHP_METHOD(MultipleIterator, setFlags) |
1195 | 0 | { |
1196 | 0 | spl_SplObjectStorage *intern; |
1197 | 0 | intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1198 | |
|
1199 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &intern->flags) == FAILURE) { |
1200 | 0 | RETURN_THROWS(); |
1201 | 0 | } |
1202 | 0 | } |
1203 | | /* }}} */ |
1204 | | |
1205 | | /* {{{ Attach a new iterator */ |
1206 | | PHP_METHOD(MultipleIterator, attachIterator) |
1207 | 0 | { |
1208 | 0 | spl_SplObjectStorage *intern; |
1209 | 0 | zend_object *iterator = NULL; |
1210 | 0 | zval zinfo; |
1211 | 0 | zend_string *info_str; |
1212 | 0 | zend_long info_long; |
1213 | 0 | bool info_is_null = 1; |
1214 | |
|
1215 | 0 | ZEND_PARSE_PARAMETERS_START(1, 2) |
1216 | 0 | Z_PARAM_OBJ_OF_CLASS(iterator, zend_ce_iterator) |
1217 | 0 | Z_PARAM_OPTIONAL |
1218 | 0 | Z_PARAM_STR_OR_LONG_OR_NULL(info_str, info_long, info_is_null) |
1219 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1220 | | |
1221 | 0 | intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1222 | |
|
1223 | 0 | if (!info_is_null) { |
1224 | 0 | spl_SplObjectStorageElement *element; |
1225 | |
|
1226 | 0 | if (info_str) { |
1227 | 0 | ZVAL_STR(&zinfo, info_str); |
1228 | 0 | } else { |
1229 | 0 | ZVAL_LONG(&zinfo, info_long); |
1230 | 0 | } |
1231 | |
|
1232 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
1233 | 0 | while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL) { |
1234 | 0 | if (fast_is_identical_function(&zinfo, &element->inf)) { |
1235 | 0 | zend_throw_exception(spl_ce_InvalidArgumentException, "Key duplication error", 0); |
1236 | 0 | RETURN_THROWS(); |
1237 | 0 | } |
1238 | 0 | zend_hash_move_forward_ex(&intern->storage, &intern->pos); |
1239 | 0 | } |
1240 | | |
1241 | 0 | spl_object_storage_attach(intern, iterator, &zinfo); |
1242 | 0 | if (UNEXPECTED(EG(exception))) { |
1243 | 0 | RETURN_THROWS(); |
1244 | 0 | } |
1245 | 0 | } else { |
1246 | 0 | spl_object_storage_attach(intern, iterator, NULL); |
1247 | 0 | if (UNEXPECTED(EG(exception))) { |
1248 | 0 | RETURN_THROWS(); |
1249 | 0 | } |
1250 | 0 | } |
1251 | 0 | } |
1252 | | /* }}} */ |
1253 | | |
1254 | | /* {{{ Detaches an iterator */ |
1255 | | PHP_METHOD(MultipleIterator, detachIterator) |
1256 | 0 | { |
1257 | 0 | zval *iterator; |
1258 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1259 | |
|
1260 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &iterator, zend_ce_iterator) == FAILURE) { |
1261 | 0 | RETURN_THROWS(); |
1262 | 0 | } |
1263 | 0 | spl_object_storage_detach(intern, Z_OBJ_P(iterator)); |
1264 | 0 | if (UNEXPECTED(EG(exception))) { |
1265 | 0 | RETURN_THROWS(); |
1266 | 0 | } |
1267 | | |
1268 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
1269 | 0 | intern->index = 0; |
1270 | 0 | } /* }}} */ |
1271 | | |
1272 | | /* {{{ Determine whether the iterator exists */ |
1273 | | PHP_METHOD(MultipleIterator, containsIterator) |
1274 | 0 | { |
1275 | 0 | zval *iterator; |
1276 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1277 | |
|
1278 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &iterator, zend_ce_iterator) == FAILURE) { |
1279 | 0 | RETURN_THROWS(); |
1280 | 0 | } |
1281 | 0 | RETURN_BOOL(spl_object_storage_contains(intern, Z_OBJ_P(iterator))); |
1282 | 0 | } /* }}} */ |
1283 | | |
1284 | | PHP_METHOD(MultipleIterator, countIterators) |
1285 | 0 | { |
1286 | 0 | spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1287 | |
|
1288 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
1289 | | |
1290 | 0 | RETURN_LONG(zend_hash_num_elements(&intern->storage)); |
1291 | 0 | } |
1292 | | |
1293 | | /* {{{ Rewind all attached iterator instances */ |
1294 | | PHP_METHOD(MultipleIterator, rewind) |
1295 | 0 | { |
1296 | 0 | spl_SplObjectStorage *intern; |
1297 | 0 | spl_SplObjectStorageElement *element; |
1298 | |
|
1299 | 0 | intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1300 | |
|
1301 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
1302 | | |
1303 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
1304 | 0 | while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) { |
1305 | 0 | zend_object *it = element->obj; |
1306 | 0 | GC_ADDREF(it); |
1307 | 0 | zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_rewind, it, NULL); |
1308 | 0 | OBJ_RELEASE(it); |
1309 | 0 | zend_hash_move_forward_ex(&intern->storage, &intern->pos); |
1310 | 0 | } |
1311 | 0 | } |
1312 | | /* }}} */ |
1313 | | |
1314 | | /* {{{ Move all attached iterator instances forward */ |
1315 | | PHP_METHOD(MultipleIterator, next) |
1316 | 0 | { |
1317 | 0 | spl_SplObjectStorage *intern; |
1318 | 0 | spl_SplObjectStorageElement *element; |
1319 | |
|
1320 | 0 | intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1321 | |
|
1322 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
1323 | | |
1324 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
1325 | 0 | while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) { |
1326 | 0 | zend_object *it = element->obj; |
1327 | 0 | GC_ADDREF(it); |
1328 | 0 | zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_next, it, NULL); |
1329 | 0 | OBJ_RELEASE(it); |
1330 | 0 | zend_hash_move_forward_ex(&intern->storage, &intern->pos); |
1331 | 0 | } |
1332 | 0 | } |
1333 | | /* }}} */ |
1334 | | |
1335 | | /* {{{ Return whether all or one sub iterator is valid depending on flags */ |
1336 | | PHP_METHOD(MultipleIterator, valid) |
1337 | 0 | { |
1338 | 0 | spl_SplObjectStorage *intern; |
1339 | 0 | spl_SplObjectStorageElement *element; |
1340 | 0 | zval retval; |
1341 | 0 | zend_long expect, valid; |
1342 | |
|
1343 | 0 | intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1344 | |
|
1345 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
1346 | | |
1347 | 0 | if (!zend_hash_num_elements(&intern->storage)) { |
1348 | 0 | RETURN_FALSE; |
1349 | 0 | } |
1350 | | |
1351 | 0 | expect = (intern->flags & MIT_NEED_ALL) ? 1 : 0; |
1352 | |
|
1353 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
1354 | 0 | while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) { |
1355 | 0 | zend_object *it = element->obj; |
1356 | 0 | GC_ADDREF(it); |
1357 | 0 | zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_valid, it, &retval); |
1358 | 0 | OBJ_RELEASE(it); |
1359 | |
|
1360 | 0 | if (!Z_ISUNDEF(retval)) { |
1361 | 0 | valid = (Z_TYPE(retval) == IS_TRUE); |
1362 | 0 | zval_ptr_dtor(&retval); |
1363 | 0 | } else { |
1364 | 0 | valid = 0; |
1365 | 0 | } |
1366 | |
|
1367 | 0 | if (expect != valid) { |
1368 | 0 | RETURN_BOOL(!expect); |
1369 | 0 | } |
1370 | | |
1371 | 0 | zend_hash_move_forward_ex(&intern->storage, &intern->pos); |
1372 | 0 | } |
1373 | | |
1374 | 0 | RETURN_BOOL(expect); |
1375 | 0 | } |
1376 | | /* }}} */ |
1377 | | |
1378 | | static void spl_multiple_iterator_get_all(spl_SplObjectStorage *intern, int get_type, zval *return_value) /* {{{ */ |
1379 | 0 | { |
1380 | 0 | spl_SplObjectStorageElement *element; |
1381 | 0 | zval retval; |
1382 | 0 | int valid = 1, num_elements; |
1383 | |
|
1384 | 0 | num_elements = zend_hash_num_elements(&intern->storage); |
1385 | 0 | if (num_elements < 1) { |
1386 | 0 | zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Called %s() on an invalid iterator", |
1387 | 0 | get_type == SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT ? "current" : "key"); |
1388 | 0 | RETURN_THROWS(); |
1389 | 0 | } |
1390 | | |
1391 | 0 | array_init_size(return_value, num_elements); |
1392 | |
|
1393 | 0 | zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos); |
1394 | 0 | while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) { |
1395 | 0 | zend_object *it = element->obj; |
1396 | 0 | zval inf; |
1397 | 0 | GC_ADDREF(it); |
1398 | 0 | ZVAL_COPY(&inf, &element->inf); |
1399 | 0 | zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_valid, it, &retval); |
1400 | |
|
1401 | 0 | if (!Z_ISUNDEF(retval)) { |
1402 | 0 | valid = Z_TYPE(retval) == IS_TRUE; |
1403 | 0 | zval_ptr_dtor(&retval); |
1404 | 0 | } else { |
1405 | 0 | valid = 0; |
1406 | 0 | } |
1407 | |
|
1408 | 0 | if (valid) { |
1409 | 0 | if (SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT == get_type) { |
1410 | 0 | zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_current, it, &retval); |
1411 | 0 | } else { |
1412 | 0 | zend_call_known_instance_method_with_0_params(it->ce->iterator_funcs_ptr->zf_key, it, &retval); |
1413 | 0 | } |
1414 | 0 | if (Z_ISUNDEF(retval)) { |
1415 | 0 | OBJ_RELEASE(it); |
1416 | 0 | zval_ptr_dtor(&inf); |
1417 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Failed to call sub iterator method", 0); |
1418 | 0 | return; |
1419 | 0 | } |
1420 | 0 | } else if (intern->flags & MIT_NEED_ALL) { |
1421 | 0 | OBJ_RELEASE(it); |
1422 | 0 | zval_ptr_dtor(&inf); |
1423 | 0 | if (SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT == get_type) { |
1424 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Called current() with non valid sub iterator", 0); |
1425 | 0 | } else { |
1426 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Called key() with non valid sub iterator", 0); |
1427 | 0 | } |
1428 | 0 | return; |
1429 | 0 | } else { |
1430 | 0 | ZVAL_NULL(&retval); |
1431 | 0 | } |
1432 | | |
1433 | 0 | if (intern->flags & MIT_KEYS_ASSOC) { |
1434 | 0 | switch (Z_TYPE(inf)) { |
1435 | 0 | case IS_LONG: |
1436 | 0 | add_index_zval(return_value, Z_LVAL(inf), &retval); |
1437 | 0 | break; |
1438 | 0 | case IS_STRING: |
1439 | 0 | zend_symtable_update(Z_ARRVAL_P(return_value), Z_STR(inf), &retval); |
1440 | 0 | break; |
1441 | 0 | default: |
1442 | 0 | zval_ptr_dtor(&retval); |
1443 | 0 | OBJ_RELEASE(it); |
1444 | 0 | zval_ptr_dtor(&inf); |
1445 | 0 | zend_throw_exception(spl_ce_InvalidArgumentException, "Sub-Iterator is associated with NULL", 0); |
1446 | 0 | return; |
1447 | 0 | } |
1448 | 0 | } else { |
1449 | 0 | add_next_index_zval(return_value, &retval); |
1450 | 0 | } |
1451 | | |
1452 | 0 | OBJ_RELEASE(it); |
1453 | 0 | zval_ptr_dtor(&inf); |
1454 | 0 | zend_hash_move_forward_ex(&intern->storage, &intern->pos); |
1455 | 0 | } |
1456 | 0 | } |
1457 | | /* }}} */ |
1458 | | |
1459 | | /* {{{ Return an array of all registered Iterator instances current() result */ |
1460 | | PHP_METHOD(MultipleIterator, current) |
1461 | 0 | { |
1462 | 0 | spl_SplObjectStorage *intern; |
1463 | 0 | intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1464 | |
|
1465 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
1466 | | |
1467 | 0 | spl_multiple_iterator_get_all(intern, SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT, return_value); |
1468 | 0 | } |
1469 | | /* }}} */ |
1470 | | |
1471 | | /* {{{ Return an array of all registered Iterator instances key() result */ |
1472 | | PHP_METHOD(MultipleIterator, key) |
1473 | 0 | { |
1474 | 0 | spl_SplObjectStorage *intern; |
1475 | 0 | intern = Z_SPLOBJSTORAGE_P(ZEND_THIS); |
1476 | |
|
1477 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
1478 | | |
1479 | 0 | spl_multiple_iterator_get_all(intern, SPL_MULTIPLE_ITERATOR_GET_ALL_KEY, return_value); |
1480 | 0 | } |
1481 | | /* }}} */ |
1482 | | |
1483 | | /* {{{ PHP_MINIT_FUNCTION(spl_observer) */ |
1484 | | PHP_MINIT_FUNCTION(spl_observer) |
1485 | 16 | { |
1486 | 16 | spl_ce_SplObserver = register_class_SplObserver(); |
1487 | 16 | spl_ce_SplSubject = register_class_SplSubject(); |
1488 | | |
1489 | 16 | spl_ce_SplObjectStorage = register_class_SplObjectStorage(zend_ce_countable, spl_ce_SeekableIterator, zend_ce_serializable, zend_ce_arrayaccess); |
1490 | 16 | spl_ce_SplObjectStorage->create_object = spl_SplObjectStorage_new; |
1491 | 16 | spl_ce_SplObjectStorage->default_object_handlers = &spl_handler_SplObjectStorage; |
1492 | | |
1493 | 16 | memcpy(&spl_handler_SplObjectStorage, &std_object_handlers, sizeof(zend_object_handlers)); |
1494 | | |
1495 | 16 | spl_handler_SplObjectStorage.offset = offsetof(spl_SplObjectStorage, std); |
1496 | 16 | spl_handler_SplObjectStorage.compare = spl_object_storage_compare_objects; |
1497 | 16 | spl_handler_SplObjectStorage.clone_obj = spl_object_storage_clone; |
1498 | 16 | spl_handler_SplObjectStorage.get_gc = spl_object_storage_get_gc; |
1499 | 16 | spl_handler_SplObjectStorage.free_obj = spl_SplObjectStorage_free_storage; |
1500 | 16 | spl_handler_SplObjectStorage.read_dimension = spl_object_storage_read_dimension; |
1501 | 16 | spl_handler_SplObjectStorage.write_dimension = spl_object_storage_write_dimension; |
1502 | 16 | spl_handler_SplObjectStorage.has_dimension = spl_object_storage_has_dimension; |
1503 | 16 | spl_handler_SplObjectStorage.unset_dimension = spl_object_storage_unset_dimension; |
1504 | | |
1505 | 16 | memcpy(&spl_handler_MultipleIterator, &spl_handler_SplObjectStorage, sizeof(zend_object_handlers)); |
1506 | | |
1507 | 16 | spl_handler_MultipleIterator.write_dimension = spl_multiple_iterator_write_dimension; |
1508 | | |
1509 | 16 | spl_ce_MultipleIterator = register_class_MultipleIterator(zend_ce_iterator); |
1510 | 16 | spl_ce_MultipleIterator->create_object = spl_SplObjectStorage_new; |
1511 | 16 | spl_ce_MultipleIterator->default_object_handlers = &spl_handler_MultipleIterator; |
1512 | | |
1513 | 16 | return SUCCESS; |
1514 | 16 | } |
1515 | | /* }}} */ |