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 // Flags: --harmony-computed-property-names | |
6 | |
7 | |
8 function ID(x) { | |
9 return x; | |
10 } | |
11 | |
12 | |
13 | |
14 (function TestBasicsString() { | |
15 var object = { | |
16 a: 'A', | |
17 ['b']: 'B', | |
18 c: 'C', | |
19 [ID('d')]: 'D', | |
20 }; | |
21 assertEquals('A', object.a); | |
22 assertEquals('B', object.b); | |
23 assertEquals('C', object.c); | |
24 assertEquals('D', object.d); | |
25 assertArrayEquals(['a', 'b', 'c', 'd'], Object.keys(object)); | |
26 })(); | |
27 | |
28 | |
29 (function TestBasicsNumber() { | |
30 var object = { | |
31 a: 'A', | |
32 [1]: 'B', | |
33 c: 'C', | |
34 [ID(2)]: 'D', | |
35 }; | |
36 assertEquals('A', object.a); | |
37 assertEquals('B', object[1]); | |
38 assertEquals('C', object.c); | |
39 assertEquals('D', object[2]); | |
40 // Array indexes first. | |
41 assertArrayEquals(['1', '2', 'a', 'c'], Object.keys(object)); | |
42 })(); | |
43 | |
44 | |
45 (function TestBasicsSymbol() { | |
46 var sym1 = Symbol(); | |
47 var sym2 = Symbol(); | |
48 var object = { | |
49 a: 'A', | |
50 [sym1]: 'B', | |
51 c: 'C', | |
52 [ID(sym2)]: 'D', | |
53 }; | |
54 assertEquals('A', object.a); | |
55 assertEquals('B', object[sym1]); | |
56 assertEquals('C', object.c); | |
57 assertEquals('D', object[sym2]); | |
58 assertArrayEquals(['a', 'c'], Object.keys(object)); | |
59 assertArrayEquals([sym1, sym2], Object.getOwnPropertySymbols(object)); | |
60 })(); | |
61 | |
62 | |
63 (function TestToNameSideEffects() { | |
64 var counter = 0; | |
65 var key1 = { | |
66 toString: function() { | |
67 assertEquals(0, counter++); | |
68 return 'b'; | |
69 } | |
70 }; | |
71 var key2 = { | |
72 toString: function() { | |
73 assertEquals(1, counter++); | |
74 return 'd'; | |
75 } | |
76 }; | |
77 var object = { | |
78 a: 'A', | |
79 [key1]: 'B', | |
80 c: 'C', | |
81 [key2]: 'D', | |
82 }; | |
83 assertEquals(2, counter); | |
84 assertEquals('A', object.a); | |
85 assertEquals('B', object.b); | |
86 assertEquals('C', object.c); | |
87 assertEquals('D', object.d); | |
88 assertArrayEquals(['a', 'b', 'c', 'd'], Object.keys(object)); | |
89 })(); | |
90 | |
91 | |
92 (function TestToNameSideEffectsNumbers() { | |
93 var counter = 0; | |
94 var key1 = { | |
95 valueOf: function() { | |
96 assertEquals(0, counter++); | |
97 return 1; | |
98 }, | |
99 toString: null | |
100 }; | |
101 var key2 = { | |
102 valueOf: function() { | |
103 assertEquals(1, counter++); | |
104 return 2; | |
105 }, | |
106 toString: null | |
107 }; | |
108 | |
109 var object = { | |
110 a: 'A', | |
111 [key1]: 'B', | |
112 c: 'C', | |
113 [key2]: 'D', | |
114 }; | |
115 assertEquals(2, counter); | |
116 assertEquals('A', object.a); | |
117 assertEquals('B', object[1]); | |
118 assertEquals('C', object.c); | |
119 assertEquals('D', object[2]); | |
120 // Array indexes first. | |
121 assertArrayEquals(['1', '2', 'a', 'c'], Object.keys(object)); | |
122 })(); | |
123 | |
124 | |
125 (function TestDoubleName() { | |
126 var object = { | |
127 [1.2]: 'A', | |
128 [1e55]: 'B', | |
129 [0.000001]: 'C', | |
130 [-0]: 'D', | |
131 [Infinity]: 'E', | |
132 [-Infinity]: 'F', | |
133 [NaN]: 'G', | |
134 }; | |
135 assertEquals('A', object['1.2']); | |
136 assertEquals('B', object['1e+55']); | |
137 assertEquals('C', object['0.000001']); | |
138 assertEquals('D', object[0]); | |
139 assertEquals('E', object[Infinity]); | |
140 assertEquals('F', object[-Infinity]); | |
141 assertEquals('G', object[NaN]); | |
142 })(); | |
143 | |
144 | |
145 (function TestGetter() { | |
146 var object = { | |
147 get ['a']() { | |
148 return 'A'; | |
149 } | |
150 }; | |
151 assertEquals('A', object.a); | |
152 | |
153 object = { | |
154 get b() { | |
155 assertUnreachable(); | |
156 }, | |
157 get ['b']() { | |
158 return 'B'; | |
159 } | |
160 }; | |
161 assertEquals('B', object.b); | |
162 | |
163 object = { | |
164 get c() { | |
165 assertUnreachable(); | |
166 }, | |
167 get ['c']() { | |
168 assertUnreachable(); | |
169 }, | |
170 get ['c']() { | |
171 return 'C'; | |
172 } | |
173 }; | |
174 assertEquals('C', object.c); | |
175 | |
176 object = { | |
177 get ['d']() { | |
178 assertUnreachable(); | |
179 }, | |
180 get d() { | |
181 return 'D'; | |
182 } | |
183 }; | |
184 assertEquals('D', object.d); | |
185 })(); | |
186 | |
187 | |
188 (function TestSetter() { | |
189 var calls = 0; | |
190 var object = { | |
191 set ['a'](_) { | |
192 calls++; | |
193 } | |
194 }; | |
195 object.a = 'A'; | |
196 assertEquals(1, calls); | |
197 | |
198 calls = 0; | |
199 object = { | |
200 set b(_) { | |
201 assertUnreachable(); | |
202 }, | |
203 set ['b'](_) { | |
204 calls++; | |
205 } | |
206 }; | |
207 object.b = 'B'; | |
208 assertEquals(1, calls); | |
209 | |
210 calls = 0; | |
211 object = { | |
212 set c(_) { | |
213 assertUnreachable() | |
214 }, | |
215 set ['c'](_) { | |
216 assertUnreachable() | |
217 }, | |
218 set ['c'](_) { | |
219 calls++ | |
220 } | |
221 }; | |
222 object.c = 'C'; | |
223 assertEquals(1, calls); | |
224 | |
225 calls = 0; | |
226 object = { | |
227 set ['d'](_) { | |
228 assertUnreachable() | |
229 }, | |
230 set d(_) { | |
231 calls++ | |
232 } | |
233 }; | |
234 object.d = 'D'; | |
235 assertEquals(1, calls); | |
236 })(); | |
237 | |
238 | |
239 (function TestDuplicateKeys() { | |
240 'use strict'; | |
241 // ES5 does not allow duplicate keys. | |
242 // ES6 does but we haven't changed our code yet. | |
243 | |
244 var object = { | |
245 a: 1, | |
246 ['a']: 2, | |
247 }; | |
248 assertEquals(2, object.a); | |
249 })(); | |
250 | |
251 | |
252 (function TestProto() { | |
253 var proto = {}; | |
254 var object = { | |
255 __proto__: proto | |
256 }; | |
257 assertEquals(proto, Object.getPrototypeOf(object)); | |
258 | |
259 object = { | |
260 '__proto__': proto | |
261 }; | |
262 assertEquals(proto, Object.getPrototypeOf(object)); | |
263 | |
264 var object = { | |
265 ['__proto__']: proto | |
266 }; | |
267 assertEquals(Object.prototype, Object.getPrototypeOf(object)); | |
268 assertEquals(proto, object.__proto__); | |
269 assertTrue(object.hasOwnProperty('__proto__')); | |
270 })(); | |
OLD | NEW |