aboutsummaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/es6/regexp-tostring.js
blob: 23e137c7aa5e0930276f58de43f1521b9f68d9b0 (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
53
54
55
56
57
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var log = [];

var fake =
    {
      get source() {
        log.push("p");
        return {
          toString: function() {
            log.push("ps");
            return "pattern";
          }
        };
      },
      get flags() {
        log.push("f");
        return {
          toString: function() {
            log.push("fs");
            return "flags";
          }
        };
      }
    }

function testThrows(x) {
  try {
    RegExp.prototype.toString.call(x);
  } catch (e) {
    assertTrue(/incompatible receiver/.test(e.message));
    return;
  }
  assertUnreachable();
}

testThrows(1);
testThrows(null);
Number.prototype.source = "a";
Number.prototype.flags = "b";
testThrows(1);

assertEquals("/pattern/flags", RegExp.prototype.toString.call(fake));
assertEquals(["p", "ps", "f", "fs"], log);

// Monkey-patching is also possible on RegExp instances

let weird = /foo/;
Object.defineProperty(weird, 'flags', {value: 'bar'});
Object.defineProperty(weird, 'source', {value: 'baz'});
assertEquals('/baz/bar', weird.toString());

assertEquals('/(?:)/', RegExp.prototype.toString());
assertEquals('(?:)', RegExp.prototype.source);
assertEquals('', RegExp.prototype.flags);