Skip to content

Commit 0d9ae1b

Browse files
committed
deps: V8: cherry-pick ed40ab1
Original commit message: [regexp] Fix the order of named captures on the groups object Named capture properties on the groups object should be ordered by the capture index (and not alpha-sorted). This was accidentally broken in https://crrev.com/c/1687413. Bug: v8:9822,v8:9423 Change-Id: Iac6f866f077a1b7ce557ba47e8ba5d7e7014b3ce Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1864829 Auto-Submit: Jakob Gruber <[email protected]> Reviewed-by: Peter Marshall <[email protected]> Commit-Queue: Peter Marshall <[email protected]> Cr-Commit-Position: refs/heads/master@{#64306} Refs: v8/v8@ed40ab1Fixes: #29878 PR-URL: #30064 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jiawen Geng <[email protected]>
1 parent 8b75aab commit 0d9ae1b

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

‎common.gypi‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
# Reset this number to 0 on major V8 upgrades.
4141
# Increment by one for each non-official patch applied to deps/v8.
42-
'v8_embedder_string': '-node.14',
42+
'v8_embedder_string': '-node.15',
4343

4444
##### V8 defaults for Node.js #####
4545

‎deps/v8/src/regexp/regexp-ast.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ class RegExpCapture final : public RegExpTree{
477477
intmax_match() override{return body_->max_match()}
478478
RegExpTree* body(){return body_}
479479
voidset_body(RegExpTree* body){body_ = body}
480-
intindex(){return index_}
480+
intindex() const{return index_}
481481
const ZoneVector<uc16>* name() const{return name_}
482482
voidset_name(const ZoneVector<uc16>* name){name_ = name}
483483
staticintStartRegister(int index){return index * 2}

‎deps/v8/src/regexp/regexp-parser.cc‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -984,18 +984,39 @@ RegExpCapture* RegExpParser::GetCapture(int index){
984984
return captures_->at(index - 1);
985985
}
986986

987+
namespace{
988+
989+
structRegExpCaptureIndexLess{
990+
booloperator()(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+
987999
Handle<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 (constauto& capture : *named_captures_){
1019+
for (constauto& 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

‎deps/v8/test/mjsunit/harmony/regexp-named-captures.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,15 @@ function toSlowMode(re){
419419
assertEquals("cd","abcd".replace(re,"$<$1>"));
420420
}
421421

422+
// Named captures are ordered by capture index on the groups object.
423+
// https://crbug.com/v8/9822
424+
425+
{
426+
constr=/(?<BKey>.+)\s(?<AKey>.+)/;
427+
consts='example string';
428+
assertArrayEquals(["BKey","AKey"],Object.keys(r.exec(s).groups));
429+
}
430+
422431
// Tests for 'groups' semantics on the regexp result object.
423432
// https://crbug.com/v8/7192
424433

0 commit comments

Comments
(0)