@@ -47,9 +47,12 @@ typedef struct {
4747 const pt_native_visitor *native;
4848} pt_visitor_plan;
4949
50- /* The splices recorded by one traverseArray() pass ($doNodes in the twin). */
50+ /* The splices recorded by one traverseArray() pass ($doNodes in the twin):
51+ * the element's position, and its key — what array_splice() is handed */
5152typedef struct {
5253 zend_ulong pos;
54+ zend_string *key; /* owned, NULL for an integer key */
55+ zend_ulong index;
5356 zval replacement; /* IS_ARRAY (owned) or IS_FALSE for remove */
5457} pt_do_node;
5558
@@ -59,13 +62,15 @@ typedef struct {
5962 uint32_t capacity;
6063} pt_do_nodes;
6164
62- static void pt_do_nodes_push (pt_do_nodes *dn, zend_ulong pos, zval *replacement)
65+ static void pt_do_nodes_push (pt_do_nodes *dn, zend_ulong pos, zend_string *key, zend_ulong index, zval *replacement)
6366{
6467 if (dn->count == dn->capacity ) {
6568 dn->capacity = dn->capacity == 0 ? 4 : dn->capacity * 2 ;
6669 dn->items = (pt_do_node *) erealloc (dn->items , dn->capacity * sizeof (pt_do_node));
6770 }
6871 dn->items [dn->count ].pos = pos;
72+ dn->items [dn->count ].key = key != NULL ? zend_string_copy (key) : NULL ;
73+ dn->items [dn->count ].index = index;
6974 if (replacement != NULL ) {
7075 ZVAL_COPY (&dn->items [dn->count ].replacement , replacement);
7176 } else {
@@ -78,6 +83,9 @@ static void pt_do_nodes_free(pt_do_nodes *dn)
7883{
7984 uint32_t i;
8085 for (i = 0 ; i < dn->count ; i++) {
86+ if (dn->items [i].key != NULL ) {
87+ zend_string_release (dn->items [i].key );
88+ }
8189 zval_ptr_dtor (&dn->items [i].replacement );
8290 }
8391 if (dn->items != NULL ) {
@@ -588,14 +596,14 @@ class NodeTraverser
588596 continue ;
589597 }
590598 if (retRef.isArray ()) {
591- pt_do_nodes_push (&doNodes, i, retRef.raw ());
599+ pt_do_nodes_push (&doNodes, i, key, index, retRef.raw ());
592600 skipToNext = true ;
593601 break ;
594602 }
595603 if (retRef.isLong ()) {
596604 zend_long code = retRef.asLong ();
597605 if (code == REMOVE_NODE ) {
598- pt_do_nodes_push (&doNodes, i, NULL );
606+ pt_do_nodes_push (&doNodes, i, key, index, NULL );
599607 skipToNext = true ;
600608 break ;
601609 }
@@ -660,13 +668,13 @@ class NodeTraverser
660668 continue ;
661669 }
662670 if (retRef.isArray ()) {
663- pt_do_nodes_push (&doNodes, i, retRef.raw ());
671+ pt_do_nodes_push (&doNodes, i, key, index, retRef.raw ());
664672 break ;
665673 }
666674 if (retRef.isLong ()) {
667675 zend_long code = retRef.asLong ();
668676 if (code == REMOVE_NODE ) {
669- pt_do_nodes_push (&doNodes, i, NULL );
677+ pt_do_nodes_push (&doNodes, i, key, index, NULL );
670678 break ;
671679 }
672680 if (code == STOP_TRAVERSAL ) {
@@ -693,8 +701,23 @@ class NodeTraverser
693701 }
694702
695703 zv::Val result = working.isUndef () ? std::move (original) : std::move (working);
696- if (doNodes.count > 0 ) {
697- /* apply the recorded splices in one rebuild pass */
704+ if (doNodes.count > 0 && !zend_array_is_list (Z_ARRVAL_P (result.raw ()))) {
705+ /* while (list($i, $replace) = array_pop($doNodes))
706+ * array_splice($nodes, $i, 1, $replace); */
707+ for (uint32_t k = doNodes.count ; k-- > 0 ; ) {
708+ pt_do_node &splice = doNodes.items [k];
709+ if (splice.key != NULL ) {
710+ zend_type_error (" array_splice(): Argument #2 ($offset) must be of type int, string given" );
711+ failed = true ;
712+ pt_do_nodes_free (&doNodes);
713+ return zv::Val ();
714+ }
715+ zval *replacement = &splice.replacement ;
716+ result = arraySplice (Z_ARRVAL_P (result.raw ()), (zend_long) splice.index , Z_TYPE_P (replacement) == IS_ARRAY ? Z_ARRVAL_P (replacement) : NULL );
717+ }
718+ } else if (doNodes.count > 0 ) {
719+ /* on a list every key is its position and each splice leaves
720+ * the positions below it alone: one rebuild pass does them all */
698721 zv::ArrRef nodes (result.raw ());
699722 zv::Arr rebuilt = zv::Arr::create (nodes.size ());
700723 uint32_t cursor = 0 ;
@@ -721,6 +744,49 @@ class NodeTraverser
721744 return result;
722745 }
723746
747+ /* array_splice($nodes, $offset, 1, $replacement) into a new array: the
748+ * offset clamped like array_splice() does, the integer keys renumbered,
749+ * the string keys kept; NULL replacement = [] */
750+ static zv::Val arraySplice (HashTable *nodes, zend_long offset, HashTable *replacement)
751+ {
752+ zend_long count = (zend_long) zend_hash_num_elements (nodes);
753+ if (offset > count) {
754+ offset = count;
755+ } else if (offset < 0 ) {
756+ offset = count + offset < 0 ? 0 : count + offset;
757+ }
758+ zend_long length = offset + 1 > count ? count - offset : 1 ;
759+
760+ zv::Arr spliced = zv::Arr::create ((uint32_t ) (count - length + (replacement != NULL ? zend_hash_num_elements (replacement) : 0 )));
761+ zv::ArrRef out (spliced.raw ());
762+ auto insertReplacement = [&]() {
763+ if (replacement == NULL ) return ;
764+ for (auto entry : zv::TableRef (replacement)) {
765+ out.push (entry.value ());
766+ }
767+ };
768+ zend_long position = 0 ;
769+ for (auto entry : zv::TableRef (nodes)) {
770+ if (position == offset) {
771+ insertReplacement ();
772+ }
773+ if (position < offset || position >= offset + length) {
774+ zend_string *key = entry.stringKeyOrNull ();
775+ if (key != NULL ) {
776+ Z_TRY_ADDREF_P (entry.value ().raw ());
777+ zend_hash_update (out.table (), key, entry.value ().raw ());
778+ } else {
779+ out.push (entry.value ());
780+ }
781+ }
782+ position++;
783+ }
784+ if (offset == count) {
785+ insertReplacement ();
786+ }
787+ return zv::Val (std::move (spliced));
788+ }
789+
724790 /* $old instanceof Stmt && $new instanceof Expr (and vice versa)
725791 * => LogicException; false means it threw */
726792 static bool ensureReplacementReasonable (zend_object *oldNode, zend_object *newNode)
0 commit comments