| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 | |
| 5 'use strict'; | |
| 6 | |
| 7 // Flags: --harmony-computed-property-names --harmony-classes | |
| 8 | |
| 9 | |
| 10 function ID(x) { | |
| 11 return x; | |
| 12 } | |
| 13 | |
| 14 | |
| 15 (function TestClassMethodString() { | |
| 16 class C { | |
| 17 a() { return 'A'} | |
| 18 ['b']() { return 'B'; } | |
| 19 c() { return 'C'; } | |
| 20 [ID('d')]() { return 'D'; } | |
| 21 } | |
| 22 assertEquals('A', new C().a()); | |
| 23 assertEquals('B', new C().b()); | |
| 24 assertEquals('C', new C().c()); | |
| 25 assertEquals('D', new C().d()); | |
| 26 assertArrayEquals(['a', 'b', 'c', 'd'], Object.keys(C.prototype)); | |
| 27 })(); | |
| 28 | |
| 29 | |
| 30 (function TestClassMethodNumber() { | |
| 31 class C { | |
| 32 a() { return 'A'; } | |
| 33 [1]() { return 'B'; } | |
| 34 c() { return 'C'; } | |
| 35 [ID(2)]() { return 'D'; } | |
| 36 } | |
| 37 assertEquals('A', new C().a()); | |
| 38 assertEquals('B', new C()[1]()); | |
| 39 assertEquals('C', new C().c()); | |
| 40 assertEquals('D', new C()[2]()); | |
| 41 // Array indexes first. | |
| 42 assertArrayEquals(['1', '2', 'a', 'c'], Object.keys(C.prototype)); | |
| 43 })(); | |
| 44 | |
| 45 | |
| 46 (function TestClassMethodSymbol() { | |
| 47 var sym1 = Symbol(); | |
| 48 var sym2 = Symbol(); | |
| 49 class C { | |
| 50 a() { return 'A'; } | |
| 51 [sym1]() { return 'B'; } | |
| 52 c() { return 'C'; } | |
| 53 [ID(sym2)]() { return 'D'; } | |
| 54 } | |
| 55 assertEquals('A', new C().a()); | |
| 56 assertEquals('B', new C()[sym1]()); | |
| 57 assertEquals('C', new C().c()); | |
| 58 assertEquals('D', new C()[sym2]()); | |
| 59 assertArrayEquals(['a', 'c'], Object.keys(C.prototype)); | |
| 60 assertArrayEquals([sym1, sym2], Object.getOwnPropertySymbols(C.prototype)); | |
| 61 })(); | |
| 62 | |
| 63 | |
| 64 | |
| 65 (function TestStaticClassMethodString() { | |
| 66 class C { | |
| 67 static a() { return 'A'} | |
| 68 static ['b']() { return 'B'; } | |
| 69 static c() { return 'C'; } | |
| 70 static ['d']() { return 'D'; } | |
| 71 } | |
| 72 assertEquals('A', C.a()); | |
| 73 assertEquals('B', C.b()); | |
| 74 assertEquals('C', C.c()); | |
| 75 assertEquals('D', C.d()); | |
| 76 assertArrayEquals(['a', 'b', 'c', 'd'], Object.keys(C)); | |
| 77 })(); | |
| 78 | |
| 79 | |
| 80 (function TestStaticClassMethodNumber() { | |
| 81 class C { | |
| 82 static a() { return 'A'; } | |
| 83 static [1]() { return 'B'; } | |
| 84 static c() { return 'C'; } | |
| 85 static [2]() { return 'D'; } | |
| 86 } | |
| 87 assertEquals('A', C.a()); | |
| 88 assertEquals('B', C[1]()); | |
| 89 assertEquals('C', C.c()); | |
| 90 assertEquals('D', C[2]()); | |
| 91 // Array indexes first. | |
| 92 assertArrayEquals(['1', '2', 'a', 'c'], Object.keys(C)); | |
| 93 })(); | |
| 94 | |
| 95 | |
| 96 (function TestStaticClassMethodSymbol() { | |
| 97 var sym1 = Symbol(); | |
| 98 var sym2 = Symbol(); | |
| 99 class C { | |
| 100 static a() { return 'A'; } | |
| 101 static [sym1]() { return 'B'; } | |
| 102 static c() { return 'C'; } | |
| 103 static [sym2]() { return 'D'; } | |
| 104 } | |
| 105 assertEquals('A', C.a()); | |
| 106 assertEquals('B', C[sym1]()); | |
| 107 assertEquals('C', C.c()); | |
| 108 assertEquals('D', C[sym2]()); | |
| 109 assertArrayEquals(['a', 'c'], Object.keys(C)); | |
| 110 assertArrayEquals([sym1, sym2], Object.getOwnPropertySymbols(C)); | |
| 111 })(); | |
| 112 | |
| 113 | |
| 114 | |
| 115 function assertIteratorResult(value, done, result) { | |
| 116 assertEquals({ value: value, done: done}, result); | |
| 117 } | |
| 118 | |
| 119 | |
| 120 (function TestGeneratorComputedName() { | |
| 121 class C { | |
| 122 *['a']() { | |
| 123 yield 1; | |
| 124 yield 2; | |
| 125 } | |
| 126 } | |
| 127 var iter = new C().a(); | |
| 128 assertIteratorResult(1, false, iter.next()); | |
| 129 assertIteratorResult(2, false, iter.next()); | |
| 130 assertIteratorResult(undefined, true, iter.next()); | |
| 131 assertArrayEquals(['a'], Object.keys(C.prototype)); | |
| 132 })(); | |
| 133 | |
| 134 | |
| 135 (function TestToNameSideEffects() { | |
| 136 var counter = 0; | |
| 137 var key1 = { | |
| 138 toString: function() { | |
| 139 assertEquals(0, counter++); | |
| 140 return 'b'; | |
| 141 } | |
| 142 }; | |
| 143 var key2 = { | |
| 144 toString: function() { | |
| 145 assertEquals(1, counter++); | |
| 146 return 'd'; | |
| 147 } | |
| 148 }; | |
| 149 class C { | |
| 150 a() { return 'A'; } | |
| 151 [key1]() { return 'B'; } | |
| 152 c() { return 'C'; } | |
| 153 [key2]() { return 'D'; } | |
| 154 } | |
| 155 assertEquals(2, counter); | |
| 156 assertEquals('A', new C().a()); | |
| 157 assertEquals('B', new C().b()); | |
| 158 assertEquals('C', new C().c()); | |
| 159 assertEquals('D', new C().d()); | |
| 160 assertArrayEquals(['a', 'b', 'c', 'd'], Object.keys(C.prototype)); | |
| 161 })(); | |
| 162 | |
| 163 | |
| 164 (function TestToNameSideEffectsNumbers() { | |
| 165 var counter = 0; | |
| 166 var key1 = { | |
| 167 valueOf: function() { | |
| 168 assertEquals(0, counter++); | |
| 169 return 1; | |
| 170 }, | |
| 171 toString: null | |
| 172 }; | |
| 173 var key2 = { | |
| 174 valueOf: function() { | |
| 175 assertEquals(1, counter++); | |
| 176 return 2; | |
| 177 }, | |
| 178 toString: null | |
| 179 }; | |
| 180 | |
| 181 class C { | |
| 182 a() { return 'A'; } | |
| 183 [key1]() { return 'B'; } | |
| 184 c() { return 'C'; } | |
| 185 [key2]() { return 'D'; } | |
| 186 } | |
| 187 assertEquals(2, counter); | |
| 188 assertEquals('A', new C().a()); | |
| 189 assertEquals('B', new C()[1]()); | |
| 190 assertEquals('C', new C().c()); | |
| 191 assertEquals('D', new C()[2]()); | |
| 192 // Array indexes first. | |
| 193 assertArrayEquals(['1', '2', 'a', 'c'], Object.keys(C.prototype)); | |
| 194 })(); | |
| 195 | |
| 196 | |
| 197 (function TestGetter() { | |
| 198 class C { | |
| 199 get ['a']() { | |
| 200 return 'A'; | |
| 201 } | |
| 202 } | |
| 203 assertEquals('A', new C().a); | |
| 204 | |
| 205 class C2 { | |
| 206 get b() { | |
| 207 assertUnreachable(); | |
| 208 } | |
| 209 get ['b']() { | |
| 210 return 'B'; | |
| 211 } | |
| 212 } | |
| 213 assertEquals('B', new C2().b); | |
| 214 | |
| 215 class C3 { | |
| 216 get c() { | |
| 217 assertUnreachable(); | |
| 218 } | |
| 219 get ['c']() { | |
| 220 assertUnreachable(); | |
| 221 } | |
| 222 get ['c']() { | |
| 223 return 'C'; | |
| 224 } | |
| 225 } | |
| 226 assertEquals('C', new C3().c); | |
| 227 | |
| 228 class C4 { | |
| 229 get ['d']() { | |
| 230 assertUnreachable(); | |
| 231 } | |
| 232 get d() { | |
| 233 return 'D'; | |
| 234 } | |
| 235 } | |
| 236 assertEquals('D', new C4().d); | |
| 237 })(); | |
| 238 | |
| 239 | |
| 240 (function TestSetter() { | |
| 241 var calls = 0; | |
| 242 class C { | |
| 243 set ['a'](_) { | |
| 244 calls++; | |
| 245 } | |
| 246 } | |
| 247 new C().a = 'A'; | |
| 248 assertEquals(1, calls); | |
| 249 | |
| 250 calls = 0; | |
| 251 class C2 { | |
| 252 set b(_) { | |
| 253 assertUnreachable(); | |
| 254 } | |
| 255 set ['b'](_) { | |
| 256 calls++; | |
| 257 } | |
| 258 } | |
| 259 new C2().b = 'B'; | |
| 260 assertEquals(1, calls); | |
| 261 | |
| 262 calls = 0; | |
| 263 class C3 { | |
| 264 set c(_) { | |
| 265 assertUnreachable() | |
| 266 } | |
| 267 set ['c'](_) { | |
| 268 assertUnreachable() | |
| 269 } | |
| 270 set ['c'](_) { | |
| 271 calls++ | |
| 272 } | |
| 273 } | |
| 274 new C3().c = 'C'; | |
| 275 assertEquals(1, calls); | |
| 276 | |
| 277 calls = 0; | |
| 278 class C4 { | |
| 279 set ['d'](_) { | |
| 280 assertUnreachable() | |
| 281 } | |
| 282 set d(_) { | |
| 283 calls++ | |
| 284 } | |
| 285 } | |
| 286 new C4().d = 'D'; | |
| 287 assertEquals(1, calls); | |
| 288 })(); | |
| 289 | |
| 290 | |
| 291 (function TestPrototype() { | |
| 292 // Normally a static prototype property is not allowed. | |
| 293 class C { | |
| 294 static ['prototype']() { | |
| 295 return 1; | |
| 296 } | |
| 297 } | |
| 298 assertEquals(1, C.prototype()); | |
| 299 | |
| 300 class C2 { | |
| 301 static get ['prototype']() { | |
| 302 return 2; | |
| 303 } | |
| 304 } | |
| 305 assertEquals(2, C2.prototype); | |
| 306 | |
| 307 var calls = 0; | |
| 308 class C3 { | |
| 309 static set ['prototype'](x) { | |
| 310 assertEquals(3, x); | |
| 311 calls++; | |
| 312 } | |
| 313 } | |
| 314 C3.prototype = 3; | |
| 315 assertEquals(1, calls); | |
| 316 | |
| 317 class C4 { | |
| 318 static *['prototype']() { | |
| 319 yield 1; | |
| 320 yield 2; | |
| 321 } | |
| 322 } | |
| 323 var iter = C4.prototype(); | |
| 324 assertIteratorResult(1, false, iter.next()); | |
| 325 assertIteratorResult(2, false, iter.next()); | |
| 326 assertIteratorResult(undefined, true, iter.next()); | |
| 327 })(); | |
| 328 | |
| 329 | |
| 330 (function TestConstructor() { | |
| 331 // Normally a constructor property is not allowed. | |
| 332 class C { | |
| 333 ['constructor']() { | |
| 334 return 1; | |
| 335 } | |
| 336 } | |
| 337 assertTrue(C !== C.prototype.constructor); | |
| 338 assertEquals(1, new C().constructor()); | |
| 339 | |
| 340 class C2 { | |
| 341 get ['constructor']() { | |
| 342 return 2; | |
| 343 } | |
| 344 } | |
| 345 assertEquals(2, new C2().constructor); | |
| 346 | |
| 347 var calls = 0; | |
| 348 class C3 { | |
| 349 set ['constructor'](x) { | |
| 350 assertEquals(3, x); | |
| 351 calls++; | |
| 352 } | |
| 353 } | |
| 354 new C3().constructor = 3; | |
| 355 assertEquals(1, calls); | |
| 356 | |
| 357 class C4 { | |
| 358 *['constructor']() { | |
| 359 yield 1; | |
| 360 yield 2; | |
| 361 } | |
| 362 } | |
| 363 var iter = new C4().constructor(); | |
| 364 assertIteratorResult(1, false, iter.next()); | |
| 365 assertIteratorResult(2, false, iter.next()); | |
| 366 assertIteratorResult(undefined, true, iter.next()); | |
| 367 })(); | |
| OLD | NEW |