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 6function ID(x) { 7 return x; 8} 9 10 11(function TestMethodComputedNameString() { 12 var object = { 13 a() { return 'A'}, 14 ['b']() { return 'B'; }, 15 c() { return 'C'; }, 16 [ID('d')]() { return 'D'; }, 17 }; 18 assertEquals('A', object.a()); 19 assertEquals('B', object.b()); 20 assertEquals('C', object.c()); 21 assertEquals('D', object.d()); 22 assertArrayEquals(['a', 'b', 'c', 'd'], Object.keys(object)); 23})(); 24 25 26(function TestMethodComputedNameNumber() { 27 var object = { 28 a() { return 'A'; }, 29 [1]() { return 'B'; }, 30 c() { return 'C'; }, 31 [ID(2)]() { return 'D'; }, 32 }; 33 assertEquals('A', object.a()); 34 assertEquals('B', object[1]()); 35 assertEquals('C', object.c()); 36 assertEquals('D', object[2]()); 37 // Array indexes first. 38 assertArrayEquals(['1', '2', 'a', 'c'], Object.keys(object)); 39})(); 40 41 42(function TestMethodComputedNameSymbol() { 43 var sym1 = Symbol(); 44 var sym2 = Symbol(); 45 var object = { 46 a() { return 'A'; }, 47 [sym1]() { return 'B'; }, 48 c() { return 'C'; }, 49 [ID(sym2)]() { return 'D'; }, 50 }; 51 assertEquals('A', object.a()); 52 assertEquals('B', object[sym1]()); 53 assertEquals('C', object.c()); 54 assertEquals('D', object[sym2]()); 55 assertArrayEquals(['a', 'c'], Object.keys(object)); 56 assertArrayEquals([sym1, sym2], Object.getOwnPropertySymbols(object)); 57})(); 58 59 60function assertIteratorResult(value, done, result) { 61 assertEquals({ value: value, done: done}, result); 62} 63 64 65(function TestGeneratorComputedName() { 66 var object = { 67 *['a']() { 68 yield 1; 69 yield 2; 70 } 71 }; 72 var iter = object.a(); 73 assertIteratorResult(1, false, iter.next()); 74 assertIteratorResult(2, false, iter.next()); 75 assertIteratorResult(undefined, true, iter.next()); 76 assertArrayEquals(['a'], Object.keys(object)); 77})(); 78 79 80(function TestToNameSideEffects() { 81 var counter = 0; 82 var key1 = { 83 toString: function() { 84 assertEquals(0, counter++); 85 return 'b'; 86 } 87 }; 88 var key2 = { 89 toString: function() { 90 assertEquals(1, counter++); 91 return 'd'; 92 } 93 }; 94 var object = { 95 a() { return 'A'; }, 96 [key1]() { return 'B'; }, 97 c() { return 'C'; }, 98 [key2]() { return 'D'; }, 99 }; 100 assertEquals(2, counter); 101 assertEquals('A', object.a()); 102 assertEquals('B', object.b()); 103 assertEquals('C', object.c()); 104 assertEquals('D', object.d()); 105 assertArrayEquals(['a', 'b', 'c', 'd'], Object.keys(object)); 106})(); 107 108 109(function TestDuplicateKeys() { 110 'use strict'; 111 // ES5 does not allow duplicate keys. 112 // ES6 does but we haven't changed our code yet. 113 114 var object = { 115 a() { return 1; }, 116 ['a']() { return 2; }, 117 }; 118 assertEquals(2, object.a()); 119})(); 120