Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 27 | // |
| 28 | // Flags: --harmony-function-name |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 29 | |
| 30 | var obj = { |
| 31 | a: 7, |
| 32 | b: { x: 12, y: 24 }, |
| 33 | c: 'Zebra' |
| 34 | } |
| 35 | |
| 36 | assertEquals(7, obj.a); |
| 37 | assertEquals(12, obj.b.x); |
| 38 | assertEquals(24, obj.b.y); |
| 39 | assertEquals('Zebra', obj.c); |
| 40 | |
| 41 | var z = 24; |
| 42 | |
| 43 | var obj2 = { |
| 44 | a: 7, |
| 45 | b: { x: 12, y: z }, |
| 46 | c: 'Zebra' |
| 47 | } |
| 48 | |
| 49 | assertEquals(7, obj2.a); |
| 50 | assertEquals(12, obj2.b.x); |
| 51 | assertEquals(24, obj2.b.y); |
| 52 | assertEquals('Zebra', obj2.c); |
| 53 | |
| 54 | var arr = []; |
| 55 | for (var i = 0; i < 2; i++) { |
| 56 | arr[i] = { |
| 57 | a: 7, |
| 58 | b: { x: 12, y: 24 }, |
| 59 | c: 'Zebra' |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | arr[0].b.x = 2; |
| 64 | assertEquals(2, arr[0].b.x); |
| 65 | assertEquals(12, arr[1].b.x); |
| 66 | |
| 67 | |
| 68 | function makeSparseArray() { |
| 69 | return { |
| 70 | '0': { x: 12, y: 24 }, |
| 71 | '1000000': { x: 0, y: 0 } |
| 72 | }; |
| 73 | } |
| 74 | |
| 75 | var sa1 = makeSparseArray(); |
| 76 | sa1[0].x = 0; |
| 77 | var sa2 = makeSparseArray(); |
| 78 | assertEquals(12, sa2[0].x); |
| 79 | |
| 80 | // Test that non-constant literals work. |
| 81 | var n = new Object(); |
| 82 | |
| 83 | function makeNonConstantArray() { return [ [ n ] ]; } |
| 84 | |
| 85 | var a = makeNonConstantArray(); |
| 86 | a[0][0].foo = "bar"; |
| 87 | assertEquals("bar", n.foo); |
| 88 | |
| 89 | function makeNonConstantObject() { return { a: { b: n } }; } |
| 90 | |
| 91 | a = makeNonConstantObject(); |
| 92 | a.a.b.bar = "foo"; |
| 93 | assertEquals("foo", n.bar); |
| 94 | |
| 95 | // Test that exceptions for regexps still hold. |
| 96 | function makeRegexpInArray() { return [ [ /a*/, {} ] ]; } |
| 97 | |
| 98 | a = makeRegexpInArray(); |
| 99 | var b = makeRegexpInArray(); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 100 | assertFalse(a[0][0] === b[0][0]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 101 | assertFalse(a[0][1] === b[0][1]); |
| 102 | |
| 103 | function makeRegexpInObject() { return { a: { b: /b*/, c: {} } }; } |
| 104 | a = makeRegexpInObject(); |
| 105 | b = makeRegexpInObject(); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 106 | assertFalse(a.a.b === b.a.b); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 107 | assertFalse(a.a.c === b.a.c); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 108 | |
| 109 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 110 | // Test keywords are valid as property names in initializers and dot-access. |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 111 | var keywords = [ |
| 112 | "break", |
| 113 | "case", |
| 114 | "catch", |
| 115 | "const", |
| 116 | "continue", |
| 117 | "debugger", |
| 118 | "default", |
| 119 | "delete", |
| 120 | "do", |
| 121 | "else", |
| 122 | "false", |
| 123 | "finally", |
| 124 | "for", |
| 125 | "function", |
| 126 | "if", |
| 127 | "in", |
| 128 | "instanceof", |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 129 | "new", |
| 130 | "null", |
| 131 | "return", |
| 132 | "switch", |
| 133 | "this", |
| 134 | "throw", |
| 135 | "true", |
| 136 | "try", |
| 137 | "typeof", |
| 138 | "var", |
| 139 | "void", |
| 140 | "while", |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 141 | "with" |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 142 | ]; |
| 143 | |
| 144 | function testKeywordProperty(keyword) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 145 | var exception = false; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 146 | try { |
| 147 | // Sanity check that what we get is a keyword. |
| 148 | eval("var " + keyword + " = 42;"); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 149 | } catch (e) { |
| 150 | exception = true; |
| 151 | } |
| 152 | assertTrue(exception); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 153 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 154 | // Simple property, read and write. |
| 155 | var x = eval("({" + keyword + ": 42})"); |
| 156 | assertEquals(42, x[keyword]); |
| 157 | assertEquals(42, eval("x." + keyword)); |
| 158 | eval("x." + keyword + " = 37"); |
| 159 | assertEquals(37, x[keyword]); |
| 160 | assertEquals(37, eval("x." + keyword)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 161 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 162 | // Getter/setter property, read and write. |
| 163 | var y = eval("({value : 42, get " + keyword + "(){return this.value}," + |
| 164 | " set " + keyword + "(v) { this.value = v; }})"); |
| 165 | assertEquals(42, y[keyword]); |
| 166 | assertEquals(42, eval("y." + keyword)); |
| 167 | eval("y." + keyword + " = 37"); |
| 168 | assertEquals(37, y[keyword]); |
| 169 | assertEquals(37, eval("y." + keyword)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 170 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 171 | // Quoted keyword works is read back by unquoted as well. |
| 172 | var z = eval("({\"" + keyword + "\": 42})"); |
| 173 | assertEquals(42, z[keyword]); |
| 174 | assertEquals(42, eval("z." + keyword)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 175 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 176 | // Function property, called. |
| 177 | var was_called; |
| 178 | function test_call() { this.was_called = true; was_called = true; } |
| 179 | var w = eval("({" + keyword + ": test_call, was_called: false})"); |
| 180 | eval("w." + keyword + "();"); |
| 181 | assertTrue(was_called); |
| 182 | assertTrue(w.was_called); |
| 183 | |
| 184 | // Function property, constructed. |
| 185 | function construct() { this.constructed = true; } |
| 186 | var v = eval("({" + keyword + ": construct})"); |
| 187 | var vo = eval("new v." + keyword + "()"); |
| 188 | assertTrue(vo instanceof construct); |
| 189 | assertTrue(vo.constructed); |
| 190 | } |
| 191 | |
| 192 | for (var i = 0; i < keywords.length; i++) { |
| 193 | testKeywordProperty(keywords[i]); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 194 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 195 | |
| 196 | |
| 197 | (function TestNumericNames() { |
| 198 | var o = { |
| 199 | 1: 1, |
| 200 | 2.: 2, |
| 201 | 3.0: 3, |
| 202 | 4e0: 4, |
| 203 | 5E0: 5, |
| 204 | 6e-0: 6, |
| 205 | 7E-0: 7, |
| 206 | 0x8: 8, |
| 207 | 0X9: 9, |
| 208 | } |
| 209 | assertEquals(['1', '2', '3', '4', '5', '6', '7', '8', '9'], Object.keys(o)); |
| 210 | |
| 211 | o = { |
| 212 | 1.2: 1.2, |
| 213 | 1.30: 1.3 |
| 214 | }; |
| 215 | assertEquals(['1.2', '1.3'], Object.keys(o)); |
| 216 | })(); |
| 217 | |
| 218 | |
| 219 | function TestNumericNamesGetter(expectedKeys, object) { |
| 220 | assertEquals(expectedKeys, Object.keys(object)); |
| 221 | expectedKeys.forEach(function(key) { |
| 222 | var descr = Object.getOwnPropertyDescriptor(object, key); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 223 | assertEquals('get ' + key, descr.get.name); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 224 | }); |
| 225 | } |
| 226 | TestNumericNamesGetter(['1', '2', '3', '4', '5', '6', '7', '8', '9'], { |
| 227 | get 1() {}, |
| 228 | get 2.() {}, |
| 229 | get 3.0() {}, |
| 230 | get 4e0() {}, |
| 231 | get 5E0() {}, |
| 232 | get 6e-0() {}, |
| 233 | get 7E-0() {}, |
| 234 | get 0x8() {}, |
| 235 | get 0X9() {}, |
| 236 | }); |
| 237 | TestNumericNamesGetter(['1.2', '1.3'], { |
| 238 | get 1.2() {}, |
| 239 | get 1.30() {} |
| 240 | }); |
| 241 | |
| 242 | |
| 243 | function TestNumericNamesSetter(expectedKeys, object) { |
| 244 | assertEquals(expectedKeys, Object.keys(object)); |
| 245 | expectedKeys.forEach(function(key) { |
| 246 | var descr = Object.getOwnPropertyDescriptor(object, key); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 247 | assertEquals('set ' + key, descr.set.name); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 248 | }); |
| 249 | } |
| 250 | TestNumericNamesSetter(['1', '2', '3', '4', '5', '6', '7', '8', '9'], { |
| 251 | set 1(_) {}, |
| 252 | set 2.(_) {}, |
| 253 | set 3.0(_) {}, |
| 254 | set 4e0(_) {}, |
| 255 | set 5E0(_) {}, |
| 256 | set 6e-0(_) {}, |
| 257 | set 7E-0(_) {}, |
| 258 | set 0x8(_) {}, |
| 259 | set 0X9(_) {}, |
| 260 | }); |
| 261 | TestNumericNamesSetter(['1.2', '1.3'], { |
| 262 | set 1.2(_) {; }, |
| 263 | set 1.30(_) {; } |
| 264 | }); |