Skip to content

Commit 553afa9

Browse files
targosMylesBorins
authored andcommittedJan 8, 2020
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@ed40ab1 Fixes: #29878 Backport-PR-URL: #30109 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 f364b50 commit 553afa9

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed
 

‎common.gypi

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
# Reset this number to 0 on major V8 upgrades.
4040
# Increment by one for each non-official patch applied to deps/v8.
41-
'v8_embedder_string': '-node.12',
41+
'v8_embedder_string': '-node.13',
4242

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ class RegExpCapture final : public RegExpTree {
477477
int max_match() override { return body_->max_match(); }
478478
RegExpTree* body() { return body_; }
479479
void set_body(RegExpTree* body) { body_ = body; }
480-
int index() { return index_; }
480+
int index() const { return index_; }
481481
const ZoneVector<uc16>* name() const { return name_; }
482482
void set_name(const ZoneVector<uc16>* name) { name_ = name; }
483483
static int StartRegister(int index) { return index * 2; }

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

+23-2
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+
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+
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 (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

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

+9
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+
const r = /(?<BKey>.+)\s(?<AKey>.+)/;
427+
const s = '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)