Skip to content

Commit 5337172

Browse files
committed
ext/spl: Use ArrayObject object handlers for ArrayIterator
They are the same
1 parent 446b7fb commit 5337172

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

ext/spl/spl_array.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include "spl_functions.h" /* For spl_set_private_debug_info_property() */
3232

3333
/* Defined later in the file */
34-
static zend_object_handlers spl_handler_ArrayIterator;
3534
PHPAPI zend_class_entry *spl_ce_ArrayIterator;
3635
PHPAPI zend_class_entry *spl_ce_RecursiveArrayIterator;
3736

@@ -162,11 +161,18 @@ static zend_object *spl_array_object_new_ex(zend_class_entry *class_type, zend_o
162161
if (clone_orig) {
163162
if (other->ar_flags & SPL_ARRAY_IS_SELF) {
164163
ZVAL_UNDEF(&intern->array);
165-
} else if (orig->handlers == &spl_handler_ArrayObject) {
164+
} else if (instanceof_function(class_type, spl_ce_ArrayObject)) {
166165
ZVAL_ARR(&intern->array,
167166
zend_array_dup(spl_array_get_hash_table(other)));
168167
} else {
169-
ZEND_ASSERT(orig->handlers == &spl_handler_ArrayIterator);
168+
#if ZEND_DEBUG
169+
/* This is because the call to instanceof_function will remain because
170+
* the compiler can't prove in this compile unit that this function is
171+
* side-effect-free.
172+
* See https://github.com/php/php-src/pull/14518#discussion_r1638740932 */
173+
ZEND_ASSERT(instanceof_function(class_type, spl_ce_ArrayIterator));
174+
#endif
175+
170176
ZVAL_OBJ_COPY(&intern->array, orig);
171177
intern->ar_flags |= SPL_ARRAY_USE_OTHER;
172178
}
@@ -947,7 +953,7 @@ static void spl_array_set_array(zval *object, spl_array_object *intern, zval *ar
947953
}
948954
}
949955
} else {
950-
if (Z_OBJ_HT_P(array) == &spl_handler_ArrayObject || Z_OBJ_HT_P(array) == &spl_handler_ArrayIterator) {
956+
if (Z_OBJ_HT_P(array) == &spl_handler_ArrayObject) {
951957
zval_ptr_dtor(&intern->array);
952958
if (just_array) {
953959
spl_array_object *other = Z_SPLARRAY_P(array);
@@ -1895,11 +1901,9 @@ PHP_MINIT_FUNCTION(spl_array)
18951901

18961902
spl_ce_ArrayIterator = register_class_ArrayIterator(spl_ce_SeekableIterator, zend_ce_arrayaccess, zend_ce_serializable, zend_ce_countable);
18971903
spl_ce_ArrayIterator->create_object = spl_array_object_new;
1898-
spl_ce_ArrayIterator->default_object_handlers = &spl_handler_ArrayIterator;
1904+
spl_ce_ArrayIterator->default_object_handlers = &spl_handler_ArrayObject;
18991905
spl_ce_ArrayIterator->get_iterator = spl_array_get_iterator;
19001906

1901-
memcpy(&spl_handler_ArrayIterator, &spl_handler_ArrayObject, sizeof(zend_object_handlers));
1902-
19031907
spl_ce_RecursiveArrayIterator = register_class_RecursiveArrayIterator(spl_ce_ArrayIterator, spl_ce_RecursiveIterator);
19041908
spl_ce_RecursiveArrayIterator->create_object = spl_array_object_new;
19051909
spl_ce_RecursiveArrayIterator->get_iterator = spl_array_get_iterator;

0 commit comments

Comments
 (0)