| 1 |
// Copyright 2017 the V8 project authors. All rights reserved. |
| 2 |
// This code is governed by the BSD license found in the LICENSE file. |
| 3 |
|
| 4 |
/*--- |
| 5 |
description: > |
| 6 |
Test the groups object on RegExp subclass results that do not have their own. |
| 7 |
esid: sec-regexpbuiltinexec |
| 8 |
features: [regexp-named-groups] |
| 9 |
info: | |
| 10 |
Runtime Semantics: RegExpBuiltinExec ( R, S ) |
| 11 |
24. If _R_ contains any |GroupName|, then |
| 12 |
a. Let _groups_ be ObjectCreate(*null*). |
| 13 |
25. Else, |
| 14 |
a. Let _groups_ be *undefined*. |
| 15 |
26. Perform ! CreateDataProperty(_A_, `"groups"`, _groups_). |
| 16 |
---*/ |
| 17 |
|
| 18 |
class FakeRegExp extends RegExp { |
| 19 |
exec(subject) { |
| 20 |
const fakeResult = ["ab", "a"]; |
| 21 |
fakeResult.index = 0; |
| 22 |
// `groups` is not set, triggering prototype lookup. |
| 23 |
return fakeResult; |
| 24 |
} |
| 25 |
}; |
| 26 |
|
| 27 |
const re = new FakeRegExp(); |
| 28 |
const result = re.exec("ab"); |
| 29 |
assert.sameValue(Object.getPrototypeOf(result), Array.prototype); |
| 30 |
assert.sameValue(false, result.hasOwnProperty("groups")); |
| 31 |
|
| 32 |
Array.prototype.groups = { a: "b" }; |
| 33 |
Object.getPrototypeOf(Array.prototype.groups).b = "c"; |
| 34 |
assert.sameValue("b", "ab".replace(re, "$<a>")); |
| 35 |
assert.sameValue("c", "ab".replace(re, "$<b>")); |
| 36 |
Array.prototype.groups = undefined; |