php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
spl_fixedarray.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Antony Dovgal <tony@daylessday.org> |
14 | Etienne Kneuss <colder@php.net> |
15 +----------------------------------------------------------------------+
16*/
17
18#ifdef HAVE_CONFIG_H
19#include <config.h>
20#endif
21
22#include "php.h"
23#include "zend_interfaces.h"
24#include "zend_exceptions.h"
25#include "zend_attributes.h"
26
28#include "spl_fixedarray.h"
29#include "spl_exceptions.h"
30#include "ext/json/php_json.h" /* For php_json_serializable_ce */
31
32static zend_object_handlers spl_handler_SplFixedArray;
34
35/* Check if the object is an instance of a subclass of SplFixedArray that overrides method's implementation.
36 * Expect subclassing SplFixedArray to be rare and check that first. */
37#define HAS_FIXEDARRAY_ARRAYACCESS_OVERRIDE(object, method) UNEXPECTED((object)->ce != spl_ce_SplFixedArray && (object)->ce->arrayaccess_funcs_ptr->method->common.scope != spl_ce_SplFixedArray)
38
39typedef struct _spl_fixedarray {
41 /* It is possible to resize this, so this can't be combined with the object */
43 /* If positive, it's a resize within a resize and the value gives the desired size. If -1, it's not. */
46
52
57
58static spl_fixedarray_object *spl_fixed_array_from_obj(zend_object *obj)
59{
60 return (spl_fixedarray_object*)((char*)(obj) - XtOffsetOf(spl_fixedarray_object, std));
61}
62
63#define Z_SPLFIXEDARRAY_P(zv) spl_fixed_array_from_obj(Z_OBJ_P((zv)))
64
65/* Helps enforce the invariants in debug mode:
66 * - if size == 0, then elements == NULL
67 * - if size > 0, then elements != NULL
68 * - size is not less than 0
69 */
70static bool spl_fixedarray_empty(spl_fixedarray *array)
71{
72 if (array->elements) {
73 ZEND_ASSERT(array->size > 0);
74 return false;
75 }
76 ZEND_ASSERT(array->size == 0);
77 return true;
78}
79
80static void spl_fixedarray_default_ctor(spl_fixedarray *array)
81{
82 array->size = 0;
83 array->elements = NULL;
84 array->cached_resize = -1;
85}
86
87/* Initializes the range [from, to) to null. Does not dtor existing elements. */
88static void spl_fixedarray_init_elems(spl_fixedarray *array, zend_long from, zend_long to)
89{
90 ZEND_ASSERT(from <= to);
91 zval *begin = array->elements + from, *end = array->elements + to;
92
93 while (begin != end) {
95 }
96}
97
98static void spl_fixedarray_init_non_empty_struct(spl_fixedarray *array, zend_long size)
99{
100 array->size = 0; /* reset size in case ecalloc() fails */
101 array->elements = size ? safe_emalloc(size, sizeof(zval), 0) : NULL;
102 array->size = size;
103 array->cached_resize = -1;
104}
105
106static void spl_fixedarray_init(spl_fixedarray *array, zend_long size)
107{
108 if (size > 0) {
109 spl_fixedarray_init_non_empty_struct(array, size);
110 spl_fixedarray_init_elems(array, 0, size);
111 } else {
112 spl_fixedarray_default_ctor(array);
113 }
114}
115
116/* Copies the range [begin, end) into the fixedarray, beginning at `offset`.
117 * Does not dtor the existing elements.
118 */
119static void spl_fixedarray_copy_range(spl_fixedarray *array, zend_long offset, zval *begin, zval *end)
120{
121 ZEND_ASSERT(offset >= 0);
122 ZEND_ASSERT(array->size - offset >= end - begin);
123
124 zval *to = &array->elements[offset];
125 while (begin != end) {
126 ZVAL_COPY(to++, begin++);
127 }
128}
129
130static void spl_fixedarray_copy_ctor(spl_fixedarray *to, spl_fixedarray *from)
131{
132 zend_long size = from->size;
133 spl_fixedarray_init(to, size);
134 if (size != 0) {
135 zval *begin = from->elements, *end = from->elements + size;
136 spl_fixedarray_copy_range(to, 0, begin, end);
137 }
138}
139
140/* Destructs the elements in the range [from, to).
141 * Caller is expected to bounds check.
142 */
143static void spl_fixedarray_dtor_range(spl_fixedarray *array, zend_long from, zend_long to)
144{
145 array->size = from;
146 zval *begin = array->elements + from, *end = array->elements + to;
147 while (begin != end) {
149 }
150}
151
152/* Destructs and frees contents but not the array itself.
153 * If you want to re-use the array then you need to re-initialize it.
154 */
155static void spl_fixedarray_dtor(spl_fixedarray *array)
156{
157 if (!spl_fixedarray_empty(array)) {
158 zval *begin = array->elements, *end = array->elements + array->size;
159 array->elements = NULL;
160 array->size = 0;
161 while (begin != end) {
163 }
164 efree(begin);
165 }
166}
167
168static void spl_fixedarray_resize(spl_fixedarray *array, zend_long size)
169{
170 if (size == array->size) {
171 /* nothing to do */
172 return;
173 }
174
175 /* first initialization */
176 if (array->size == 0) {
177 spl_fixedarray_init(array, size);
178 return;
179 }
180
181 if (UNEXPECTED(array->cached_resize >= 0)) {
182 /* We're already resizing, so just remember the desired size.
183 * The resize will happen later. */
184 array->cached_resize = size;
185 return;
186 }
187 array->cached_resize = size;
188
189 /* clearing the array */
190 if (size == 0) {
191 spl_fixedarray_dtor(array);
192 array->elements = NULL;
193 array->size = 0;
194 } else if (size > array->size) {
195 array->elements = safe_erealloc(array->elements, size, sizeof(zval), 0);
196 spl_fixedarray_init_elems(array, array->size, size);
197 array->size = size;
198 } else { /* size < array->size */
199 /* Size set in spl_fixedarray_dtor_range() */
200 spl_fixedarray_dtor_range(array, size, array->size);
201 array->elements = erealloc(array->elements, sizeof(zval) * size);
202 }
203
204 /* If resized within the destructor, take the last resize command and perform it */
205 zend_long cached_resize = array->cached_resize;
206 array->cached_resize = -1;
207 if (cached_resize != size) {
208 spl_fixedarray_resize(array, cached_resize);
209 }
210}
211
212static HashTable* spl_fixedarray_object_get_gc(zend_object *obj, zval **table, int *n)
213{
214 spl_fixedarray_object *intern = spl_fixed_array_from_obj(obj);
216
217 *table = intern->array.elements;
218 *n = (int)intern->array.size;
219
220 return ht;
221}
222
223static HashTable* spl_fixedarray_object_get_properties_for(zend_object *obj, zend_prop_purpose purpose)
224{
225 /* This has __serialize, so the purpose is not ZEND_PROP_PURPOSE_SERIALIZE, which would expect a non-null return value */
227
228 const spl_fixedarray_object *intern = spl_fixed_array_from_obj(obj);
229 /*
230 * SplFixedArray can be subclassed or have dynamic properties (With or without AllowDynamicProperties in subclasses).
231 * Instances of subclasses with declared properties may have properties but not yet have a property table.
232 */
233 HashTable *source_properties = obj->properties ? obj->properties : (obj->ce->default_properties_count ? zend_std_get_properties(obj) : NULL);
234
235 const zend_long size = intern->array.size;
236 if (size == 0 && (!source_properties || !zend_hash_num_elements(source_properties))) {
237 return NULL;
238 }
239 zval *const elements = intern->array.elements;
241
242 /* The array elements are not *real properties*. */
243 if (purpose != ZEND_PROP_PURPOSE_GET_OBJECT_VARS) {
244 for (zend_long i = 0; i < size; i++) {
245 Z_TRY_ADDREF_P(&elements[i]);
246 zend_hash_next_index_insert(ht, &elements[i]);
247 }
248 }
249
250 if (source_properties && zend_hash_num_elements(source_properties) > 0) {
251 zend_long nkey;
252 zend_string *skey;
253 zval *value;
254 ZEND_HASH_MAP_FOREACH_KEY_VAL_IND(source_properties, nkey, skey, value) {
256 if (skey) {
257 zend_hash_add_new(ht, skey, value);
258 } else {
260 }
262 }
263
264 return ht;
265}
266
267static void spl_fixedarray_object_free_storage(zend_object *object)
268{
269 spl_fixedarray_object *intern = spl_fixed_array_from_obj(object);
270 spl_fixedarray_dtor(&intern->array);
271 zend_object_std_dtor(&intern->std);
272}
273
274static zend_object *spl_fixedarray_object_new_ex(zend_class_entry *class_type, zend_object *orig, bool clone_orig)
275{
276 spl_fixedarray_object *intern;
277 zend_class_entry *parent = class_type;
278 bool inherited = false;
279
280 intern = zend_object_alloc(sizeof(spl_fixedarray_object), parent);
281
282 zend_object_std_init(&intern->std, class_type);
283 object_properties_init(&intern->std, class_type);
284
285 if (orig && clone_orig) {
286 spl_fixedarray_object *other = spl_fixed_array_from_obj(orig);
287 spl_fixedarray_copy_ctor(&intern->array, &other->array);
288 }
289
290 while (parent) {
291 if (parent == spl_ce_SplFixedArray) {
292 break;
293 }
294
295 parent = parent->parent;
296 inherited = true;
297 }
298
299 ZEND_ASSERT(parent);
300
301 if (UNEXPECTED(inherited)) {
302 /* Find count() method */
303 zend_function *fptr_count = zend_hash_find_ptr(&class_type->function_table, ZSTR_KNOWN(ZEND_STR_COUNT));
304 if (fptr_count->common.scope == parent) {
305 fptr_count = NULL;
306 }
307 intern->fptr_count = fptr_count;
308 }
309
310 return &intern->std;
311}
312
313static zend_object *spl_fixedarray_new(zend_class_entry *class_type)
314{
315 return spl_fixedarray_object_new_ex(class_type, NULL, 0);
316}
317
318static zend_object *spl_fixedarray_object_clone(zend_object *old_object)
319{
320 zend_object *new_object = spl_fixedarray_object_new_ex(old_object->ce, old_object, 1);
321
322 zend_objects_clone_members(new_object, old_object);
323
324 return new_object;
325}
326
327static zend_long spl_offset_convert_to_long(zval *offset) /* {{{ */
328{
329 try_again:
330 switch (Z_TYPE_P(offset)) {
331 case IS_STRING: {
332 zend_ulong index;
333 if (ZEND_HANDLE_NUMERIC(Z_STR_P(offset), index)) {
334 return (zend_long) index;
335 }
336 break;
337 }
338 case IS_DOUBLE:
339 return zend_dval_to_lval_safe(Z_DVAL_P(offset));
340 case IS_LONG:
341 return Z_LVAL_P(offset);
342 case IS_FALSE:
343 return 0;
344 case IS_TRUE:
345 return 1;
346 case IS_REFERENCE:
348 goto try_again;
349 case IS_RESOURCE:
351 return Z_RES_HANDLE_P(offset);
352 }
353
354 /* Use SplFixedArray name from the CE */
356 return 0;
357}
358
359static zval *spl_fixedarray_object_read_dimension_helper(spl_fixedarray_object *intern, zval *offset)
360{
361 zend_long index;
362
363 /* we have to return NULL on error here to avoid memleak because of
364 * ZE duplicating uninitialized_zval_ptr */
365 if (!offset) {
366 zend_throw_error(NULL, "[] operator not supported for SplFixedArray");
367 return NULL;
368 }
369
370 index = spl_offset_convert_to_long(offset);
371 if (EG(exception)) {
372 return NULL;
373 }
374
375 if (index < 0 || index >= intern->array.size) {
376 zend_throw_exception(spl_ce_OutOfBoundsException, "Index invalid or out of range", 0);
377 return NULL;
378 } else {
379 return &intern->array.elements[index];
380 }
381}
382
383static int spl_fixedarray_object_has_dimension(zend_object *object, zval *offset, int check_empty);
384
385static zval *spl_fixedarray_object_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
386{
387 if (type == BP_VAR_IS && !spl_fixedarray_object_has_dimension(object, offset, 0)) {
388 return &EG(uninitialized_zval);
389 }
390
391 if (HAS_FIXEDARRAY_ARRAYACCESS_OVERRIDE(object, zf_offsetget)) {
392 zval tmp;
393 if (!offset) {
394 ZVAL_NULL(&tmp);
395 offset = &tmp;
396 }
397 zend_call_known_instance_method_with_1_params(object->ce->arrayaccess_funcs_ptr->zf_offsetget, object, rv, offset);
398 if (!Z_ISUNDEF_P(rv)) {
399 return rv;
400 }
401 return &EG(uninitialized_zval);
402 }
403
404 spl_fixedarray_object *intern = spl_fixed_array_from_obj(object);
405 return spl_fixedarray_object_read_dimension_helper(intern, offset);
406}
407
408static void spl_fixedarray_object_write_dimension_helper(spl_fixedarray_object *intern, zval *offset, zval *value)
409{
410 zend_long index;
411
412 if (!offset) {
413 /* '$array[] = value' syntax is not supported */
414 zend_throw_error(NULL, "[] operator not supported for SplFixedArray");
415 return;
416 }
417
418 index = spl_offset_convert_to_long(offset);
419 if (EG(exception)) {
420 return;
421 }
422
423 if (index < 0 || index >= intern->array.size) {
424 zend_throw_exception(spl_ce_OutOfBoundsException, "Index invalid or out of range", 0);
425 return;
426 } else {
427 /* Fix #81429 */
428 zval *ptr = &(intern->array.elements[index]);
429 zval tmp;
430 ZVAL_COPY_VALUE(&tmp, ptr);
432 zval_ptr_dtor(&tmp);
433 }
434}
435
436static void spl_fixedarray_object_write_dimension(zend_object *object, zval *offset, zval *value)
437{
438 if (HAS_FIXEDARRAY_ARRAYACCESS_OVERRIDE(object, zf_offsetset)) {
439 zval tmp;
440
441 if (!offset) {
442 ZVAL_NULL(&tmp);
443 offset = &tmp;
444 }
445 zend_call_known_instance_method_with_2_params(object->ce->arrayaccess_funcs_ptr->zf_offsetset, object, NULL, offset, value);
446 return;
447 }
448
449 spl_fixedarray_object *intern = spl_fixed_array_from_obj(object);
450 spl_fixedarray_object_write_dimension_helper(intern, offset, value);
451}
452
453static void spl_fixedarray_object_unset_dimension_helper(spl_fixedarray_object *intern, zval *offset)
454{
455 zend_long index;
456
457 index = spl_offset_convert_to_long(offset);
458 if (EG(exception)) {
459 return;
460 }
461
462 if (index < 0 || index >= intern->array.size) {
463 zend_throw_exception(spl_ce_OutOfBoundsException, "Index invalid or out of range", 0);
464 return;
465 } else {
467 ZVAL_COPY_VALUE(&garbage, &intern->array.elements[index]);
468 ZVAL_NULL(&intern->array.elements[index]);
470 }
471}
472
473static void spl_fixedarray_object_unset_dimension(zend_object *object, zval *offset)
474{
475 if (UNEXPECTED(HAS_FIXEDARRAY_ARRAYACCESS_OVERRIDE(object, zf_offsetunset))) {
476 zend_call_known_instance_method_with_1_params(object->ce->arrayaccess_funcs_ptr->zf_offsetunset, object, NULL, offset);
477 return;
478 }
479
480 spl_fixedarray_object *intern = spl_fixed_array_from_obj(object);
481 spl_fixedarray_object_unset_dimension_helper(intern, offset);
482}
483
484static bool spl_fixedarray_object_has_dimension_helper(spl_fixedarray_object *intern, zval *offset, bool check_empty)
485{
486 zend_long index;
487
488 index = spl_offset_convert_to_long(offset);
489 if (EG(exception)) {
490 return false;
491 }
492
493 if (index < 0 || index >= intern->array.size) {
494 return false;
495 }
496
497 if (check_empty) {
498 return zend_is_true(&intern->array.elements[index]);
499 }
500
501 return Z_TYPE(intern->array.elements[index]) != IS_NULL;
502}
503
504static int spl_fixedarray_object_has_dimension(zend_object *object, zval *offset, int check_empty)
505{
506 if (HAS_FIXEDARRAY_ARRAYACCESS_OVERRIDE(object, zf_offsetexists)) {
507 zval rv;
508
509 zend_call_known_instance_method_with_1_params(object->ce->arrayaccess_funcs_ptr->zf_offsetexists, object, &rv, offset);
510 bool result = zend_is_true(&rv);
512 return result;
513 }
514
515 spl_fixedarray_object *intern = spl_fixed_array_from_obj(object);
516
517 return spl_fixedarray_object_has_dimension_helper(intern, offset, check_empty);
518}
519
520static zend_result spl_fixedarray_object_count_elements(zend_object *object, zend_long *count)
521{
522 spl_fixedarray_object *intern;
523
524 intern = spl_fixed_array_from_obj(object);
525 if (UNEXPECTED(intern->fptr_count)) {
526 zval rv;
527 zend_call_known_instance_method_with_0_params(intern->fptr_count, object, &rv);
528 if (!Z_ISUNDEF(rv)) {
529 *count = zval_get_long(&rv);
531 } else {
532 *count = 0;
533 }
534 } else {
535 *count = intern->array.size;
536 }
537 return SUCCESS;
538}
539
541{
542 zval *object = ZEND_THIS;
543 spl_fixedarray_object *intern;
544 zend_long size = 0;
545
548 }
549
550 if (size < 0) {
551 zend_argument_value_error(1, "must be greater than or equal to 0");
553 }
554
555 intern = Z_SPLFIXEDARRAY_P(object);
556
557 if (!spl_fixedarray_empty(&intern->array)) {
558 /* called __construct() twice, bail out */
559 return;
560 }
561
562 spl_fixedarray_init(&intern->array, size);
563}
564
566{
569 zval *data;
570
573 }
574
575 if (intern->array.size == 0) {
576 int index = 0;
577 int size = zend_hash_num_elements(intern_ht);
578
579 spl_fixedarray_init(&intern->array, size);
580
581 ZEND_HASH_FOREACH_VAL(intern_ht, data) {
582 ZVAL_COPY(&intern->array.elements[index], data);
583 index++;
585
586 /* Remove the unserialised properties, since we now have the elements
587 * within the spl_fixedarray_object structure. */
588 zend_hash_clean(intern_ht);
589 }
590}
591
593{
595 zval *current;
597
600 }
601
603 uint32_t num_properties = zend_hash_num_elements(ht);
604 array_init_size(return_value, intern->array.size + num_properties);
605
606 /* elements */
607 for (zend_long i = 0; i < intern->array.size; i++) {
608 current = &intern->array.elements[i];
611 }
612
613 /* members */
615 /* If the properties table was already rebuild, it will also contain the
616 * array elements. The array elements are already added in the above loop.
617 * We can detect array elements by the fact that their key == NULL. */
618 if (key != NULL) {
621 }
623}
624
626{
629 zval members_zv, *elem;
632
635 }
636
637 if (intern->array.size == 0) {
638 size = zend_hash_num_elements(data);
639 spl_fixedarray_init_non_empty_struct(&intern->array, size);
640 if (!size) {
641 return;
642 }
643 array_init(&members_zv);
644
645 intern->array.size = 0;
647 if (key == NULL) {
648 ZVAL_COPY(&intern->array.elements[intern->array.size], elem);
649 intern->array.size++;
650 } else {
651 Z_TRY_ADDREF_P(elem);
652 zend_hash_add(Z_ARRVAL(members_zv), key, elem);
653 }
655
656 if (intern->array.size != size) {
657 if (intern->array.size) {
658 intern->array.elements = erealloc(intern->array.elements, sizeof(zval) * intern->array.size);
659 } else {
660 efree(intern->array.elements);
661 intern->array.elements = NULL;
662 }
663 }
664
665 object_properties_load(&intern->std, Z_ARRVAL(members_zv));
666 zval_ptr_dtor(&members_zv);
667 }
668}
669
671{
672 zval *object = ZEND_THIS;
673 spl_fixedarray_object *intern;
674
677 }
678
679 intern = Z_SPLFIXEDARRAY_P(object);
680 RETURN_LONG(intern->array.size);
681}
682
684{
685 spl_fixedarray_object *intern;
686
689 }
690
692
693 if (!spl_fixedarray_empty(&intern->array)) {
695 for (zend_long i = 0; i < intern->array.size; i++) {
697 Z_TRY_ADDREF(intern->array.elements[i]);
698 }
699 } else {
701 }
702}
703
705{
706 zval *data;
707 spl_fixedarray array;
708 spl_fixedarray_object *intern;
709 int num;
710 bool save_indexes = 1;
711
712 if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|b", &data, &save_indexes) == FAILURE) {
714 }
715
716 num = zend_hash_num_elements(Z_ARRVAL_P(data));
717
718 if (num > 0 && save_indexes) {
719 zval *element;
720 zend_string *str_index;
721 zend_ulong num_index, max_index = 0;
722 zend_long tmp;
723
724 ZEND_HASH_FOREACH_KEY(Z_ARRVAL_P(data), num_index, str_index) {
725 if (str_index != NULL || (zend_long)num_index < 0) {
726 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "array must contain only positive integer keys");
728 }
729
730 if (num_index > max_index) {
731 max_index = num_index;
732 }
734
735 tmp = max_index + 1;
736 if (tmp <= 0) {
737 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "integer overflow detected");
739 }
740 spl_fixedarray_init(&array, tmp);
741
742 ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data), num_index, element) {
743 ZVAL_COPY_DEREF(&array.elements[num_index], element);
745
746 } else if (num > 0 && !save_indexes) {
747 zval *element;
748 zend_long i = 0;
749
750 spl_fixedarray_init(&array, num);
751
753 ZVAL_COPY_DEREF(&array.elements[i], element);
754 i++;
756 } else {
757 spl_fixedarray_init(&array, 0);
758 }
759
761
763 intern->array = array;
764}
765
767{
768 zval *object = ZEND_THIS;
769 spl_fixedarray_object *intern;
770
773 }
774
775 intern = Z_SPLFIXEDARRAY_P(object);
776 RETURN_LONG(intern->array.size);
777}
778
780{
781 zval *object = ZEND_THIS;
782 spl_fixedarray_object *intern;
784
787 }
788
789 if (size < 0) {
790 zend_argument_value_error(1, "must be greater than or equal to 0");
792 }
793
794 intern = Z_SPLFIXEDARRAY_P(object);
795
796 spl_fixedarray_resize(&intern->array, size);
798}
799
800/* Returns whether the requested $index exists. */
802{
803 zval *zindex;
804 spl_fixedarray_object *intern;
805
806 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
808 }
809
811
812 RETURN_BOOL(spl_fixedarray_object_has_dimension_helper(intern, zindex, 0));
813}
814
815/* Returns the value at the specified $index. */
817{
818 zval *zindex, *value;
819 spl_fixedarray_object *intern;
820
821 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
823 }
824
826 value = spl_fixedarray_object_read_dimension_helper(intern, zindex);
827
828 if (value) {
830 } else {
831 RETURN_NULL();
832 }
833}
834
835/* Sets the value at the specified $index to $newval. */
837{
838 zval *zindex, *value;
839 spl_fixedarray_object *intern;
840
841 if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &zindex, &value) == FAILURE) {
843 }
844
846 spl_fixedarray_object_write_dimension_helper(intern, zindex, value);
847
848}
849
850/* Unsets the value at the specified $index. */
852{
853 zval *zindex;
854 spl_fixedarray_object *intern;
855
856 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
858 }
859
861 spl_fixedarray_object_unset_dimension_helper(intern, zindex);
862
863}
864
865/* Create a new iterator from a SplFixedArray instance. */
874
876{
878
881 for (zend_long i = 0; i < intern->array.size; i++) {
883 Z_TRY_ADDREF(intern->array.elements[i]);
884 }
885}
886
887static void spl_fixedarray_it_dtor(zend_object_iterator *iter)
888{
889 zval_ptr_dtor(&iter->data);
890}
891
892static void spl_fixedarray_it_rewind(zend_object_iterator *iter)
893{
894 ((spl_fixedarray_it*)iter)->current = 0;
895}
896
897static zend_result spl_fixedarray_it_valid(zend_object_iterator *iter)
898{
899 spl_fixedarray_it *iterator = (spl_fixedarray_it*)iter;
901
902 if (iterator->current >= 0 && iterator->current < object->array.size) {
903 return SUCCESS;
904 }
905
906 return FAILURE;
907}
908
909static zval *spl_fixedarray_it_get_current_data(zend_object_iterator *iter)
910{
911 zval zindex, *data;
912 spl_fixedarray_it *iterator = (spl_fixedarray_it*)iter;
914
915 ZVAL_LONG(&zindex, iterator->current);
916 data = spl_fixedarray_object_read_dimension_helper(object, &zindex);
917
918 if (data == NULL) {
919 data = &EG(uninitialized_zval);
920 }
921 return data;
922}
923
924static void spl_fixedarray_it_get_current_key(zend_object_iterator *iter, zval *key)
925{
927}
928
929static void spl_fixedarray_it_move_forward(zend_object_iterator *iter)
930{
931 ((spl_fixedarray_it*)iter)->current++;
932}
933
934/* iterator handler table */
935static const zend_object_iterator_funcs spl_fixedarray_it_funcs = {
936 spl_fixedarray_it_dtor,
937 spl_fixedarray_it_valid,
938 spl_fixedarray_it_get_current_data,
939 spl_fixedarray_it_get_current_key,
940 spl_fixedarray_it_move_forward,
941 spl_fixedarray_it_rewind,
942 NULL,
943 NULL, /* get_gc */
944};
945
946static zend_object_iterator *spl_fixedarray_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
947{
948 spl_fixedarray_it *iterator;
949
950 if (by_ref) {
951 zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
952 return NULL;
953 }
954
955 iterator = emalloc(sizeof(spl_fixedarray_it));
956
958
959 ZVAL_OBJ_COPY(&iterator->intern.data, Z_OBJ_P(object));
960 iterator->intern.funcs = &spl_fixedarray_it_funcs;
961
962 return &iterator->intern;
963}
964
966{
967 spl_ce_SplFixedArray = register_class_SplFixedArray(
969 spl_ce_SplFixedArray->create_object = spl_fixedarray_new;
970 spl_ce_SplFixedArray->default_object_handlers = &spl_handler_SplFixedArray;
971 spl_ce_SplFixedArray->get_iterator = spl_fixedarray_get_iterator;
972
973 memcpy(&spl_handler_SplFixedArray, &std_object_handlers, sizeof(zend_object_handlers));
974
975 spl_handler_SplFixedArray.offset = XtOffsetOf(spl_fixedarray_object, std);
976 spl_handler_SplFixedArray.clone_obj = spl_fixedarray_object_clone;
977 spl_handler_SplFixedArray.read_dimension = spl_fixedarray_object_read_dimension;
978 spl_handler_SplFixedArray.write_dimension = spl_fixedarray_object_write_dimension;
979 spl_handler_SplFixedArray.unset_dimension = spl_fixedarray_object_unset_dimension;
980 spl_handler_SplFixedArray.has_dimension = spl_fixedarray_object_has_dimension;
981 spl_handler_SplFixedArray.count_elements = spl_fixedarray_object_count_elements;
982 spl_handler_SplFixedArray.get_properties_for = spl_fixedarray_object_get_properties_for;
983 spl_handler_SplFixedArray.get_gc = spl_fixedarray_object_get_gc;
984 spl_handler_SplFixedArray.free_obj = spl_fixedarray_object_free_storage;
985
986 return SUCCESS;
987}
bool exception
Definition assert.c:30
count(Countable|array $value, int $mode=COUNT_NORMAL)
int begin
Definition eaw_table.h:20
zend_ffi_type * type
Definition ffi.c:3812
zend_long n
Definition ffi.c:4979
new_type size
Definition ffi.c:4365
void * ptr
Definition ffi.c:3814
memcpy(ptr1, ptr2, size)
HashTable * ht
Definition ffi.c:4838
zend_long offset
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
PHP_JSON_API zend_class_entry * php_json_serializable_ce
Definition json.c:33
#define PHP_MINIT_FUNCTION
Definition php.h:400
#define PHP_METHOD
Definition php.h:365
#define PHPAPI
Definition php.h:71
unsigned const char * end
Definition php_ffi.h:51
unsigned char key[REFLECTION_KEY_LEN]
zend_constant * data
zval * current
Definition session.c:1024
zval rv
Definition session.c:1024
PHPAPI zend_class_entry * spl_ce_OutOfBoundsException
PHPAPI zend_class_entry * spl_ce_InvalidArgumentException
struct _spl_fixedarray_object spl_fixedarray_object
struct _spl_fixedarray spl_fixedarray
#define Z_SPLFIXEDARRAY_P(zv)
PHPAPI zend_class_entry * spl_ce_SplFixedArray
#define HAS_FIXEDARRAY_ARRAYACCESS_OVERRIDE(object, method)
struct _spl_fixedarray_it spl_fixedarray_it
zend_object_iterator intern
zend_function * fptr_count
zend_long cached_resize
int default_properties_count
Definition zend.h:158
HashTable function_table
Definition zend.h:163
const zend_object_iterator_funcs * funcs
zend_class_entry * ce
Definition zend_types.h:560
HashTable * properties
Definition zend_types.h:562
zend_class_entry * scope
struct _zend_function::@236135173067030250234125302313220025134003177336 common
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format,...)
Definition zend.c:1772
ZEND_API ZEND_COLD void zend_illegal_container_offset(const zend_string *container, const zval *offset, int type)
Definition zend.c:1802
ZEND_API zend_result object_init_ex(zval *arg, zend_class_entry *class_type)
Definition zend_API.c:1849
ZEND_API zend_result zend_parse_parameters(uint32_t num_args, const char *type_spec,...)
Definition zend_API.c:1300
ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type)
Definition zend_API.c:1688
ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format,...)
Definition zend_API.c:433
ZEND_API void object_properties_load(zend_object *object, HashTable *properties)
Definition zend_API.c:1728
#define RETURN_COPY_DEREF(zv)
Definition zend_API.h:1056
#define ZEND_NUM_ARGS()
Definition zend_API.h:530
ZEND_API void zend_call_known_instance_method_with_2_params(zend_function *fn, zend_object *object, zval *retval_ptr, zval *param1, zval *param2)
#define ZEND_PARSE_PARAMETERS_NONE()
Definition zend_API.h:1623
#define RETURN_NULL()
Definition zend_API.h:1036
#define array_init_size(arg, size)
Definition zend_API.h:538
#define zend_parse_parameters_none()
Definition zend_API.h:353
#define RETURN_LONG(l)
Definition zend_API.h:1037
#define RETURN_BOOL(b)
Definition zend_API.h:1035
#define RETURN_THROWS()
Definition zend_API.h:1060
#define ZEND_THIS
Definition zend_API.h:523
#define RETURN_EMPTY_ARRAY()
Definition zend_API.h:1051
#define RETURN_TRUE
Definition zend_API.h:1059
#define array_init(arg)
Definition zend_API.h:537
#define safe_erealloc(ptr, nmemb, size, offset)
Definition zend_alloc.h:161
#define efree(ptr)
Definition zend_alloc.h:155
#define erealloc(ptr, size)
Definition zend_alloc.h:159
#define safe_emalloc(nmemb, size, offset)
Definition zend_alloc.h:154
#define emalloc(size)
Definition zend_alloc.h:151
struct _zval_struct zval
#define BP_VAR_R
#define BP_VAR_IS
ZEND_API ZEND_COLD zend_object * zend_throw_exception(zend_class_entry *exception_ce, const char *message, zend_long code)
ZEND_API ZEND_COLD zend_object * zend_throw_exception_ex(zend_class_entry *exception_ce, zend_long code, const char *format,...)
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_use_resource_as_offset(const zval *dim)
union _zend_function zend_function
#define EG(v)
ZEND_API zval *ZEND_FASTCALL zend_hash_next_index_insert_new(HashTable *ht, zval *pData)
Definition zend_hash.c:1229
ZEND_API zval *ZEND_FASTCALL zend_hash_next_index_insert(HashTable *ht, zval *pData)
Definition zend_hash.c:1224
ZEND_API void ZEND_FASTCALL zend_hash_clean(HashTable *ht)
Definition zend_hash.c:1869
ZEND_API zval *ZEND_FASTCALL zend_hash_add_new(HashTable *ht, zend_string *key, zval *pData)
Definition zend_hash.c:1007
ZEND_API zval *ZEND_FASTCALL zend_hash_index_update(HashTable *ht, zend_ulong h, zval *pData)
Definition zend_hash.c:1219
ZEND_API zval *ZEND_FASTCALL zend_hash_add(HashTable *ht, zend_string *key, zval *pData)
Definition zend_hash.c:992
#define ZEND_HASH_FOREACH_STR_KEY_VAL_IND(ht, _key, _val)
Definition zend_hash.h:1193
#define ZEND_HASH_FOREACH_NUM_KEY_VAL(ht, _h, _val)
Definition zend_hash.h:1156
#define ZEND_HASH_FOREACH_KEY(ht, _h, _key)
Definition zend_hash.h:1146
#define zend_new_array(size)
Definition zend_hash.h:338
#define ZEND_HANDLE_NUMERIC(key, idx)
Definition zend_hash.h:420
#define ZEND_HASH_FOREACH_STR_KEY_VAL(ht, _key, _val)
Definition zend_hash.h:1166
#define ZEND_HASH_FOREACH_END()
Definition zend_hash.h:1086
#define ZEND_HASH_FOREACH_VAL(ht, _val)
Definition zend_hash.h:1102
#define ZEND_HASH_MAP_FOREACH_KEY_VAL_IND(ht, _h, _key, _val)
Definition zend_hash.h:1411
ZEND_API zend_class_entry * zend_ce_countable
ZEND_API zend_class_entry * zend_ce_arrayaccess
ZEND_API zend_class_entry * zend_ce_aggregate
ZEND_API zend_result zend_create_internal_iterator_zval(zval *return_value, zval *obj)
ZEND_API void zend_iterator_init(zend_object_iterator *iter)
struct _zend_object_iterator zend_object_iterator
struct _zend_object_iterator_funcs zend_object_iterator_funcs
int32_t zend_long
Definition zend_long.h:42
uint32_t zend_ulong
Definition zend_long.h:43
struct _zend_string zend_string
ZEND_API HashTable * zend_std_get_properties(zend_object *zobj)
ZEND_API const zend_object_handlers std_object_handlers
enum _zend_prop_purpose zend_prop_purpose
@ ZEND_PROP_PURPOSE_SERIALIZE
@ ZEND_PROP_PURPOSE_GET_OBJECT_VARS
ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object, zend_object *old_object)
ZEND_API void ZEND_FASTCALL zend_object_std_init(zend_object *object, zend_class_entry *ce)
ZEND_API void zend_object_std_dtor(zend_object *object)
ZEND_API bool ZEND_FASTCALL zend_is_true(const zval *op)
#define XtOffsetOf(s_type, field)
#define ZEND_ASSERT(c)
#define UNEXPECTED(condition)
struct _zend_class_entry zend_class_entry
struct _zend_object zend_object
#define ZSTR_KNOWN(idx)
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define IS_TRUE
Definition zend_types.h:603
#define Z_TRY_ADDREF_P(pz)
#define Z_REFVAL_P(zval_p)
#define IS_FALSE
Definition zend_types.h:602
#define Z_ISUNDEF_P(zval_p)
Definition zend_types.h:957
#define Z_ARRVAL_P(zval_p)
Definition zend_types.h:987
#define ZVAL_NULL(z)
#define ZVAL_LONG(z, l)
#define IS_STRING
Definition zend_types.h:606
struct _zend_array HashTable
Definition zend_types.h:386
#define IS_RESOURCE
Definition zend_types.h:609
#define Z_OBJ_P(zval_p)
Definition zend_types.h:990
#define ZVAL_COPY_DEREF(z, v)
#define IS_DOUBLE
Definition zend_types.h:605
#define Z_ISUNDEF(zval)
Definition zend_types.h:956
#define Z_STR_P(zval_p)
Definition zend_types.h:972
#define IS_NULL
Definition zend_types.h:601
@ FAILURE
Definition zend_types.h:61
#define Z_TRY_ADDREF(z)
#define IS_LONG
Definition zend_types.h:604
#define IS_REFERENCE
Definition zend_types.h:610
#define Z_RES_HANDLE_P(zval_p)
#define ZVAL_COPY(z, v)
#define ZVAL_OBJ_COPY(z, o)
ZEND_RESULT_CODE zend_result
Definition zend_types.h:64
struct _zend_object_handlers zend_object_handlers
Definition zend_types.h:88
#define Z_TYPE(zval)
Definition zend_types.h:659
#define Z_DVAL_P(zval_p)
Definition zend_types.h:969
#define Z_ARRVAL(zval)
Definition zend_types.h:986
#define Z_ARR_P(zval_p)
Definition zend_types.h:984
#define Z_LVAL_P(zval_p)
Definition zend_types.h:966
#define ZVAL_COPY_VALUE(z, v)
ZEND_API void zval_ptr_dtor(zval *zval_ptr)
zval * return_value
bool result
object
zend_refcounted * garbage
value