@@ -984,18 +984,39 @@ RegExpCapture* RegExpParser::GetCapture(int index) {
984984 return captures_->at (index - 1 );
985985}
986986
987+ namespace {
988+
989+ struct RegExpCaptureIndexLess {
990+ bool operator ()(const RegExpCapture* lhs, const RegExpCapture* rhs) const {
991+ DCHECK_NOT_NULL (lhs);
992+ DCHECK_NOT_NULL (rhs);
993+ return lhs->index () < rhs->index ();
994+ }
995+ };
996+
997+ } // namespace
998+
987999Handle<FixedArray> RegExpParser::CreateCaptureNameMap () {
9881000 if (named_captures_ == nullptr || named_captures_->empty ()) {
9891001 return Handle<FixedArray>();
9901002 }
9911003
1004+ // Named captures are sorted by name (because the set is used to ensure
1005+ // name uniqueness). But the capture name map must to be sorted by index.
1006+
1007+ ZoneVector<RegExpCapture*> sorted_named_captures (
1008+ named_captures_->begin (), named_captures_->end (), zone ());
1009+ std::sort (sorted_named_captures.begin (), sorted_named_captures.end (),
1010+ RegExpCaptureIndexLess{});
1011+ DCHECK_EQ (sorted_named_captures.size (), named_captures_->size ());
1012+
9921013 Factory* factory = isolate ()->factory ();
9931014
994- int len = static_cast <int >(named_captures_-> size ()) * 2 ;
1015+ int len = static_cast <int >(sorted_named_captures. size ()) * 2 ;
9951016 Handle<FixedArray> array = factory->NewFixedArray (len);
9961017
9971018 int i = 0 ;
998- for (const auto & capture : *named_captures_ ) {
1019+ for (const auto & capture : sorted_named_captures ) {
9991020 Vector<const uc16> capture_name (capture->name ()->data (),
10001021 capture->name ()->size ());
10011022 // CSA code in ConstructNewResultFromMatchInfo requires these strings to be
0 commit comments