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.
7 // Flags: --harmony-computed-property-names --harmony-classes
15 (function TestClassMethodString() {
18 ['b']() { return 'B'; }
20 [ID('d')]() { return 'D'; }
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([], Object.keys(C.prototype));
27 assertArrayEquals(['constructor', 'a', 'b', 'c', 'd'],
28 Object.getOwnPropertyNames(C.prototype));
32 (function TestClassMethodNumber() {
37 [ID(2)]() { return 'D'; }
39 assertEquals('A', new C().a());
40 assertEquals('B', new C()[1]());
41 assertEquals('C', new C().c());
42 assertEquals('D', new C()[2]());
43 // Array indexes first.
44 assertArrayEquals([], Object.keys(C.prototype));
45 assertArrayEquals(['1', '2', 'constructor', 'a', 'c'],
46 Object.getOwnPropertyNames(C.prototype));
50 (function TestClassMethodSymbol() {
55 [sym1]() { return 'B'; }
57 [ID(sym2)]() { return 'D'; }
59 assertEquals('A', new C().a());
60 assertEquals('B', new C()[sym1]());
61 assertEquals('C', new C().c());
62 assertEquals('D', new C()[sym2]());
63 assertArrayEquals([], Object.keys(C.prototype));
64 assertArrayEquals(['constructor', 'a', 'c'],
65 Object.getOwnPropertyNames(C.prototype));
66 assertArrayEquals([sym1, sym2], Object.getOwnPropertySymbols(C.prototype));
71 (function TestStaticClassMethodString() {
73 static a() { return 'A'}
74 static ['b']() { return 'B'; }
75 static c() { return 'C'; }
76 static ['d']() { return 'D'; }
78 assertEquals('A', C.a());
79 assertEquals('B', C.b());
80 assertEquals('C', C.c());
81 assertEquals('D', C.d());
82 assertArrayEquals([], Object.keys(C));
83 // TODO(arv): It is not clear that we are adding the "standard" properties
84 // in the right order. As far as I can tell the spec adds them in alphabetical
86 assertArrayEquals(['length', 'name', 'arguments', 'caller', 'prototype',
88 Object.getOwnPropertyNames(C));
92 (function TestStaticClassMethodNumber() {
94 static a() { return 'A'; }
95 static [1]() { return 'B'; }
96 static c() { return 'C'; }
97 static [2]() { return 'D'; }
99 assertEquals('A', C.a());
100 assertEquals('B', C[1]());
101 assertEquals('C', C.c());
102 assertEquals('D', C[2]());
103 // Array indexes first.
104 assertArrayEquals([], Object.keys(C));
105 assertArrayEquals(['1', '2', 'length', 'name', 'arguments', 'caller',
106 'prototype', 'a', 'c'], Object.getOwnPropertyNames(C));
110 (function TestStaticClassMethodSymbol() {
114 static a() { return 'A'; }
115 static [sym1]() { return 'B'; }
116 static c() { return 'C'; }
117 static [sym2]() { return 'D'; }
119 assertEquals('A', C.a());
120 assertEquals('B', C[sym1]());
121 assertEquals('C', C.c());
122 assertEquals('D', C[sym2]());
123 assertArrayEquals([], Object.keys(C));
124 assertArrayEquals(['length', 'name', 'arguments', 'caller', 'prototype',
126 Object.getOwnPropertyNames(C));
127 assertArrayEquals([sym1, sym2], Object.getOwnPropertySymbols(C));
132 function assertIteratorResult(value, done, result) {
133 assertEquals({ value: value, done: done}, result);
137 (function TestGeneratorComputedName() {
144 var iter = new C().a();
145 assertIteratorResult(1, false, iter.next());
146 assertIteratorResult(2, false, iter.next());
147 assertIteratorResult(undefined, true, iter.next());
148 assertArrayEquals([], Object.keys(C.prototype));
149 assertArrayEquals(['constructor', 'a'],
150 Object.getOwnPropertyNames(C.prototype));
154 (function TestToNameSideEffects() {
157 toString: function() {
158 assertEquals(0, counter++);
163 toString: function() {
164 assertEquals(1, counter++);
170 [key1]() { return 'B'; }
172 [key2]() { return 'D'; }
174 assertEquals(2, counter);
175 assertEquals('A', new C().a());
176 assertEquals('B', new C().b());
177 assertEquals('C', new C().c());
178 assertEquals('D', new C().d());
179 assertArrayEquals([], Object.keys(C.prototype));
180 assertArrayEquals(['constructor', 'a', 'b', 'c', 'd'],
181 Object.getOwnPropertyNames(C.prototype));
185 (function TestToNameSideEffectsNumbers() {
188 valueOf: function() {
189 assertEquals(0, counter++);
195 valueOf: function() {
196 assertEquals(1, counter++);
204 [key1]() { return 'B'; }
206 [key2]() { return 'D'; }
208 assertEquals(2, counter);
209 assertEquals('A', new C().a());
210 assertEquals('B', new C()[1]());
211 assertEquals('C', new C().c());
212 assertEquals('D', new C()[2]());
213 // Array indexes first.
214 assertArrayEquals([], Object.keys(C.prototype));
215 assertArrayEquals(['1', '2', 'constructor', 'a', 'c'],
216 Object.getOwnPropertyNames(C.prototype));
220 (function TestGetter() {
226 assertEquals('A', new C().a);
236 assertEquals('B', new C2().b);
249 assertEquals('C', new C3().c);
259 assertEquals('D', new C4().d);
263 (function TestSetter() {
271 assertEquals(1, calls);
283 assertEquals(1, calls);
298 assertEquals(1, calls);
310 assertEquals(1, calls);
314 (function TestPrototype() {
315 assertThrows(function() {
317 static ['prototype']() {
323 assertThrows(function() {
325 static get ['prototype']() {
331 assertThrows(function() {
333 static set ['prototype'](x) {
339 assertThrows(function() {
341 static *['prototype']() {
350 (function TestPrototypeConcat() {
351 assertThrows(function() {
353 static ['pro' + 'tot' + 'ype']() {
359 assertThrows(function() {
361 static get ['pro' + 'tot' + 'ype']() {
367 assertThrows(function() {
369 static set ['pro' + 'tot' + 'ype'](x) {
375 assertThrows(function() {
377 static *['pro' + 'tot' + 'ype']() {
386 (function TestConstructor() {
387 // Normally a constructor property is not allowed.
393 assertTrue(C !== C.prototype.constructor);
394 assertEquals(1, new C().constructor());
397 get ['constructor']() {
401 assertEquals(2, new C2().constructor);
405 set ['constructor'](x) {
410 new C3().constructor = 3;
411 assertEquals(1, calls);
419 var iter = new C4().constructor();
420 assertIteratorResult(1, false, iter.next());
421 assertIteratorResult(2, false, iter.next());
422 assertIteratorResult(undefined, true, iter.next());
426 (function TestExceptionInName() {
427 function MyError() {};
428 function throwMyError() {
431 assertThrows(function() {
433 [throwMyError()]() {}
436 assertThrows(function() {
438 get [throwMyError()]() { return 42; }
441 assertThrows(function() {
443 set [throwMyError()](_) { }
449 (function TestTdzName() {
450 assertThrows(function() {
455 assertThrows(function() {
457 get [C]() { return 42; }
460 assertThrows(function() {