summaryrefslogtreecommitdiff
path: root/test/built-ins/RegExp/prototype/exec/failure-g-lastindex-reset.js
blob: a6c26e1e5c3d35afd61822e1e86fdf8bcd066db2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: lastIndex is read and reset to 0 when global is set and the match
             fails.
es6id: 21.2.5.2.2
info: |
    21.2.5.2.2 Runtime Semantics: RegExpBuiltinExec ( R, S )

    [...]
    4. Let flags be R.[[OriginalFlags]].
    5. If flags contains "g", let global be true, else let global be false.
    [...]
    7. If global is false and sticky is false, let lastIndex be 0.
    8. Else, let lastIndex be ? ToLength(? Get(R, "lastIndex")).
    [...]
    12. Repeat, while matchSucceeded is false
        [...]
        c. If r is failure, then
           i. If sticky is true, then
              1. Perform ? Set(R, "lastIndex", 0, true).
              2. Return null.
           ii. Let lastIndex be AdvanceStringIndex(S, lastIndex, fullUnicode).
---*/

var lastIndexReads;
var result;

var r = /a/g;

function reset(value) {
  r.lastIndex = {
    valueOf: function() {
      lastIndexReads++;
      return value;
    }
  };
  lastIndexReads = 0;
}

reset(42);  // lastIndex beyond end of string.
result = r.exec('abc');
assert.sameValue(result, null);
assert.sameValue(r.lastIndex, 0);
assert.sameValue(lastIndexReads, 1);

reset(-1);  // No match.
result = r.exec('nbc');
assert.sameValue(result, null);
assert.sameValue(r.lastIndex, 0);
assert.sameValue(lastIndexReads, 1);