OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Ensure that we have the correct number of accesses to exec in split, and |
| 6 // that exec is called at the correct point in time. |
| 7 |
| 8 var lastIndexHasBeenSet = false; |
| 9 var countOfExecGets = 0; |
| 10 |
| 11 // Force the slow path and make sure the created splitter object has our |
| 12 // overwritten exec method (@@split does not call exec on the original regexp |
| 13 // but on a newly-created splitter which is guaranteed to be sticky). |
| 14 class ObservableExecRegExp extends RegExp { |
| 15 constructor(pattern, flags) { |
| 16 super(pattern, flags); |
| 17 this.lastIndex = 42; |
| 18 |
| 19 const re = this; |
| 20 Object.defineProperty(this, "exec", { |
| 21 get: function() { |
| 22 // Ensure exec is first accessed after lastIndex has been reset to |
| 23 // satisfy the spec. |
| 24 assertTrue(re.lastIndex != 42); |
| 25 countOfExecGets++; |
| 26 return RegExp.prototype.exec; |
| 27 } |
| 28 }); |
| 29 } |
| 30 } |
| 31 |
| 32 |
| 33 |
| 34 var re = new ObservableExecRegExp(/x/); |
| 35 |
| 36 assertEquals(42, re.lastIndex); |
| 37 assertEquals(0, countOfExecGets); |
| 38 |
| 39 var result = "axbxc".split(re); |
| 40 |
| 41 assertEquals(5, countOfExecGets); |
| 42 assertEquals(["a", "b", "c"], result); |
OLD | NEW |