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