blob: 480222d95a815b0821a0537d3206464143fce9d5 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 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
Ben Murdochda12d292016-06-02 14:46:10 +01005// Flags: --harmony-unicode-regexps
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006
7var r1 = /abc/gi;
8assertEquals("abc", r1.source);
9assertTrue(r1.global);
10assertTrue(r1.ignoreCase);
11assertFalse(r1.multiline);
12assertFalse(r1.sticky);
13assertFalse(r1.unicode);
14
15// Internal slot of prototype is not read.
16var r2 = { __proto__: r1 };
17assertThrows(function() { r2.source; }, TypeError);
18assertThrows(function() { r2.global; }, TypeError);
19assertThrows(function() { r2.ignoreCase; }, TypeError);
20assertThrows(function() { r2.multiline; }, TypeError);
21assertThrows(function() { r2.sticky; }, TypeError);
22assertThrows(function() { r2.unicode; }, TypeError);
23
24var r3 = /I/;
25var string = "iIiIi";
26var expected = "iXiIi";
27assertFalse(r3.global);
28assertFalse(r3.ignoreCase);
29assertEquals("", r3.flags);
30assertEquals(expected, string.replace(r3, "X"));
31
32var get_count = 0;
33Object.defineProperty(r3, "global", {
34 get: function() { get_count++; return true; }
35});
36Object.defineProperty(r3, "ignoreCase", {
37 get: function() { get_count++; return true; }
38});
39
40assertTrue(r3.global);
41assertEquals(1, get_count);
42assertTrue(r3.ignoreCase);
43assertEquals(2, get_count);
44// Overridden flag getters affects the flags getter.
45assertEquals("gi", r3.flags);
46assertEquals(4, get_count);
Ben Murdochda12d292016-06-02 14:46:10 +010047// Overridden flag getters affect string.replace
48// TODO(adamk): Add more tests here once we've switched
49// to use [[OriginalFlags]] in more cases.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000050assertEquals(expected, string.replace(r3, "X"));
Ben Murdochda12d292016-06-02 14:46:10 +010051assertEquals(5, get_count);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000052
53
54function testName(name) {
Ben Murdochda12d292016-06-02 14:46:10 +010055 // Test for ES2017 RegExp web compatibility semantics
56 // https://github.com/tc39/ecma262/pull/511
57 assertEquals(name === "source" ? "(?:)" : undefined,
58 RegExp.prototype[name]);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000059 assertEquals(
60 "get " + name,
61 Object.getOwnPropertyDescriptor(RegExp.prototype, name).get.name);
62}
63
64testName("global");
65testName("ignoreCase");
66testName("multiline");
67testName("source");
68testName("sticky");
69testName("unicode");
Ben Murdochda12d292016-06-02 14:46:10 +010070
71
72RegExp.prototype.flags = 'setter should be undefined';
73
74assertEquals('', RegExp('').flags);
75assertEquals('', /./.flags);
76assertEquals('gimuy', RegExp('', 'yugmi').flags);
77assertEquals('gimuy', /foo/yumig.flags);
78
79var descriptor = Object.getOwnPropertyDescriptor(RegExp.prototype, 'flags');
80assertTrue(descriptor.configurable);
81assertFalse(descriptor.enumerable);
82assertInstanceof(descriptor.get, Function);
83assertEquals(undefined, descriptor.set);
84
85function testGenericFlags(object) {
86 return descriptor.get.call(object);
87}
88
89assertEquals('', testGenericFlags({}));
90assertEquals('i', testGenericFlags({ ignoreCase: true }));
91assertEquals('uy', testGenericFlags({ global: 0, sticky: 1, unicode: 1 }));
92assertEquals('m', testGenericFlags({ __proto__: { multiline: true } }));
93assertThrows(function() { testGenericFlags(); }, TypeError);
94assertThrows(function() { testGenericFlags(undefined); }, TypeError);
95assertThrows(function() { testGenericFlags(null); }, TypeError);
96assertThrows(function() { testGenericFlags(true); }, TypeError);
97assertThrows(function() { testGenericFlags(false); }, TypeError);
98assertThrows(function() { testGenericFlags(''); }, TypeError);
99assertThrows(function() { testGenericFlags(42); }, TypeError);
100
101var counter = 0;
102var map = {};
103var object = {
104 get global() {
105 map.g = counter++;
106 },
107 get ignoreCase() {
108 map.i = counter++;
109 },
110 get multiline() {
111 map.m = counter++;
112 },
113 get unicode() {
114 map.u = counter++;
115 },
116 get sticky() {
117 map.y = counter++;
118 }
119};
120testGenericFlags(object);
121assertEquals({ g: 0, i: 1, m: 2, u: 3, y: 4 }, map);