blob: 23e137c7aa5e0930276f58de43f1521b9f68d9b0 [file] [log] [blame]
Ben Murdoch097c5b22016-05-18 11:27:45 +01001// 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
5var log = [];
6
7var fake =
8 {
9 get source() {
10 log.push("p");
11 return {
12 toString: function() {
13 log.push("ps");
14 return "pattern";
15 }
16 };
17 },
18 get flags() {
19 log.push("f");
20 return {
21 toString: function() {
22 log.push("fs");
23 return "flags";
24 }
25 };
26 }
27 }
28
29function testThrows(x) {
30 try {
31 RegExp.prototype.toString.call(x);
32 } catch (e) {
33 assertTrue(/incompatible receiver/.test(e.message));
34 return;
35 }
36 assertUnreachable();
37}
38
39testThrows(1);
40testThrows(null);
41Number.prototype.source = "a";
42Number.prototype.flags = "b";
43testThrows(1);
44
45assertEquals("/pattern/flags", RegExp.prototype.toString.call(fake));
46assertEquals(["p", "ps", "f", "fs"], log);
Ben Murdochda12d292016-06-02 14:46:10 +010047
48// Monkey-patching is also possible on RegExp instances
49
50let weird = /foo/;
51Object.defineProperty(weird, 'flags', {value: 'bar'});
52Object.defineProperty(weird, 'source', {value: 'baz'});
53assertEquals('/baz/bar', weird.toString());
54
55assertEquals('/(?:)/', RegExp.prototype.toString());
56assertEquals('(?:)', RegExp.prototype.source);
57assertEquals('', RegExp.prototype.flags);