@@ -458,6 +458,12 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
458
458
459
459
if (!isConverted)
460
460
return result;
461
+
462
+ auto ProtectedValue = [&baseVal](InternalValue value) {
463
+ if (baseVal.ShouldExtendLifetime ())
464
+ value.SetParentData (baseVal);
465
+ return value;
466
+ };
461
467
462
468
InternalValue attrName = GetArgumentValue (" attribute" , context);
463
469
InternalValue isCsVal = GetArgumentValue (" case_sensitive" , context, InternalValue (false ));
@@ -482,23 +488,23 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
482
488
{
483
489
case FirstItemMode:
484
490
if (listSize)
485
- result = list.GetValueByIndex (0 );
491
+ result = ProtectedValue ( list.GetValueByIndex (0 ) );
486
492
else
487
493
{
488
494
auto it = list.begin ();
489
495
if (it != list.end ())
490
- result = *it;
496
+ result = ProtectedValue ( *it) ;
491
497
}
492
498
break ;
493
499
case LastItemMode:
494
500
if (listSize)
495
- result = list.GetValueByIndex (listSize.value () - 1 );
501
+ result = ProtectedValue ( list.GetValueByIndex (listSize.value () - 1 ) );
496
502
else
497
503
{
498
504
auto it = list.begin ();
499
505
auto end = list.end ();
500
506
for (; it != end; ++it)
501
- result = *it;
507
+ result = ProtectedValue ( *it) ;
502
508
}
503
509
break ;
504
510
case LengthMode:
@@ -514,7 +520,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
514
520
if (listSize)
515
521
{
516
522
std::uniform_int_distribution<> dis (0 , static_cast <int >(listSize.value ()) - 1 );
517
- result = list.GetValueByIndex (dis (gen));
523
+ result = ProtectedValue ( list.GetValueByIndex (dis (gen) ));
518
524
}
519
525
else
520
526
{
@@ -525,7 +531,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
525
531
{
526
532
bool doCopy = count == 0 || std::uniform_int_distribution<size_t >(0 , count)(gen) == 0 ;
527
533
if (doCopy)
528
- result = *it;
534
+ result = ProtectedValue ( *it) ;
529
535
}
530
536
}
531
537
break ;
@@ -535,15 +541,15 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
535
541
auto b = list.begin ();
536
542
auto e = list.end ();
537
543
auto p = std::max_element (list.begin (), list.end (), lessComparator);
538
- result = p != e ? *p : InternalValue ();
544
+ result = p != e ? ProtectedValue (*p) : InternalValue ();
539
545
break ;
540
546
}
541
547
case MinItemMode:
542
548
{
543
549
auto b = list.begin ();
544
550
auto e = list.end ();
545
551
auto p = std::min_element (b, e, lessComparator);
546
- result = p != e ? *p : InternalValue ();
552
+ result = p != e ? ProtectedValue (*p) : InternalValue ();
547
553
break ;
548
554
}
549
555
case ReverseMode:
@@ -553,12 +559,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
553
559
auto size = listSize.value ();
554
560
InternalValueList resultList (size);
555
561
for (std::size_t n = 0 ; n < size; ++n)
556
- {
557
- auto resultVal = InternalValue (std::move ( list.GetValueByIndex (n) ));
558
- if (baseVal.ShouldExtendLifetime ())
559
- resultVal.SetParentData (baseVal);
560
- resultList[size - n - 1 ] = std::move (resultVal);
561
- }
562
+ resultList[size - n - 1 ] = ProtectedValue ( list.GetValueByIndex (n) );
562
563
result = ListAdapter::CreateAdapter (std::move (resultList));
563
564
}
564
565
else
@@ -567,12 +568,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
567
568
auto it = list.begin ();
568
569
auto end = list.end ();
569
570
for (; it != end; ++it)
570
- {
571
- auto resultVal = InternalValue (std::move ( *it ));
572
- if (baseVal.ShouldExtendLifetime ())
573
- resultVal.SetParentData (baseVal);
574
- resultList.push_back (std::move (resultVal));
575
- }
571
+ resultList.push_back ( ProtectedValue (*it) );
576
572
577
573
std::reverse (resultList.begin (), resultList.end ());
578
574
result = ListAdapter::CreateAdapter (std::move (resultList));
@@ -635,7 +631,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
635
631
std::stable_sort (items.begin (), items.end (), [](auto & i1, auto & i2) { return i1.idx < i2.idx ; });
636
632
637
633
for (auto & i : items)
638
- resultList.push_back (list.GetValueByIndex (i.idx ));
634
+ resultList.push_back ( ProtectedValue ( list.GetValueByIndex (i.idx ) ));
639
635
640
636
result = ListAdapter::CreateAdapter (std::move (resultList));
641
637
break ;
@@ -666,6 +662,12 @@ InternalValue Slice::Filter(const InternalValue& baseVal, RenderContext& context
666
662
if (!isConverted)
667
663
return result;
668
664
665
+ auto ProtectedValue = [&baseVal](InternalValue value) {
666
+ if (baseVal.ShouldExtendLifetime ())
667
+ value.SetParentData (baseVal);
668
+ return value;
669
+ };
670
+
669
671
InternalValue sliceLengthValue = GetArgumentValue (" slices" , context);
670
672
int64_t sliceLength = ConvertToInt (sliceLengthValue);
671
673
InternalValue fillWith = GetArgumentValue (" fill_with" , context);
@@ -683,7 +685,7 @@ InternalValue Slice::Filter(const InternalValue& baseVal, RenderContext& context
683
685
sublist.clear ();
684
686
sublistItemIndex %= sliceLength;
685
687
}
686
- sublist.push_back (item);
688
+ sublist.push_back ( ProtectedValue ( item) );
687
689
++sublistItemIndex;
688
690
}
689
691
if (!IsEmpty (fillWith))
@@ -717,7 +719,13 @@ InternalValue Slice::Batch(const InternalValue& baseVal, RenderContext& context)
717
719
718
720
InternalValueList resultList;
719
721
resultList.reserve (linecount);
720
-
722
+
723
+ auto ProtectedValue = [&baseVal](InternalValue value) {
724
+ if (baseVal.ShouldExtendLifetime ())
725
+ value.SetParentData (baseVal);
726
+ return value;
727
+ };
728
+
721
729
const auto remainder = elementsCount % linecount;
722
730
const auto columns = elementsCount / linecount + (remainder > 0 ? 1 : 0 );
723
731
for (std::size_t line = 0 , idx = 0 ; line < linecount; ++line)
@@ -728,7 +736,7 @@ InternalValue Slice::Batch(const InternalValue& baseVal, RenderContext& context)
728
736
std::fill_n (std::back_inserter (row), columns, fillWith);
729
737
730
738
for (std::size_t column = 0 ; column < elems; ++column)
731
- row[column] = list.GetValueByIndex (idx++);
739
+ row[column] = ProtectedValue ( list.GetValueByIndex (idx++) );
732
740
733
741
resultList.push_back (ListAdapter::CreateAdapter (std::move (row)));
734
742
}
0 commit comments