OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 17 matching lines...) Expand all Loading... |
28 // Flags: --harmony-collections --expose-gc --allow-natives-syntax | 28 // Flags: --harmony-collections --expose-gc --allow-natives-syntax |
29 | 29 |
30 | 30 |
31 // Test valid getter and setter calls on Sets. | 31 // Test valid getter and setter calls on Sets. |
32 function TestValidSetCalls(m) { | 32 function TestValidSetCalls(m) { |
33 assertDoesNotThrow(function () { m.add(new Object) }); | 33 assertDoesNotThrow(function () { m.add(new Object) }); |
34 assertDoesNotThrow(function () { m.has(new Object) }); | 34 assertDoesNotThrow(function () { m.has(new Object) }); |
35 assertDoesNotThrow(function () { m.delete(new Object) }); | 35 assertDoesNotThrow(function () { m.delete(new Object) }); |
36 } | 36 } |
37 TestValidSetCalls(new Set); | 37 TestValidSetCalls(new Set); |
| 38 TestValidSetCalls(new WeakSet); |
38 | 39 |
39 | 40 |
40 // Test valid getter and setter calls on Maps and WeakMaps | 41 // Test valid getter and setter calls on Maps and WeakMaps |
41 function TestValidMapCalls(m) { | 42 function TestValidMapCalls(m) { |
42 assertDoesNotThrow(function () { m.get(new Object) }); | 43 assertDoesNotThrow(function () { m.get(new Object) }); |
43 assertDoesNotThrow(function () { m.set(new Object) }); | 44 assertDoesNotThrow(function () { m.set(new Object) }); |
44 assertDoesNotThrow(function () { m.has(new Object) }); | 45 assertDoesNotThrow(function () { m.has(new Object) }); |
45 assertDoesNotThrow(function () { m.delete(new Object) }); | 46 assertDoesNotThrow(function () { m.delete(new Object) }); |
46 } | 47 } |
47 TestValidMapCalls(new Map); | 48 TestValidMapCalls(new Map); |
(...skipping 30 matching lines...) Expand all Loading... |
78 TestSet(set, i); | 79 TestSet(set, i); |
79 TestSet(set, i / 100); | 80 TestSet(set, i / 100); |
80 TestSet(set, 'key-' + i); | 81 TestSet(set, 'key-' + i); |
81 } | 82 } |
82 var keys = [ +0, -0, +Infinity, -Infinity, true, false, null, undefined ]; | 83 var keys = [ +0, -0, +Infinity, -Infinity, true, false, null, undefined ]; |
83 for (var i = 0; i < keys.length; i++) { | 84 for (var i = 0; i < keys.length; i++) { |
84 TestSet(set, keys[i]); | 85 TestSet(set, keys[i]); |
85 } | 86 } |
86 } | 87 } |
87 TestSetBehavior(new Set); | 88 TestSetBehavior(new Set); |
| 89 TestSet(new WeakSet, new Object); |
88 | 90 |
89 | 91 |
90 // Test expected mapping behavior for Maps and WeakMaps | 92 // Test expected mapping behavior for Maps and WeakMaps |
91 function TestMapping(map, key, value) { | 93 function TestMapping(map, key, value) { |
92 assertSame(undefined, map.set(key, value)); | 94 assertSame(undefined, map.set(key, value)); |
93 assertSame(value, map.get(key)); | 95 assertSame(value, map.get(key)); |
94 } | 96 } |
95 function TestMapBehavior1(m) { | 97 function TestMapBehavior1(m) { |
96 TestMapping(m, new Object, 23); | 98 TestMapping(m, new Object, 23); |
97 TestMapping(m, new Object, 'the-value'); | 99 TestMapping(m, new Object, 'the-value'); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 for (var p in x) array.push(p); | 180 for (var p in x) array.push(p); |
179 return array.sort(); | 181 return array.sort(); |
180 } | 182 } |
181 assertArrayEquals([], props(func)); | 183 assertArrayEquals([], props(func)); |
182 assertArrayEquals([], props(func.prototype)); | 184 assertArrayEquals([], props(func.prototype)); |
183 assertArrayEquals([], props(new func())); | 185 assertArrayEquals([], props(new func())); |
184 } | 186 } |
185 TestEnumerable(Set); | 187 TestEnumerable(Set); |
186 TestEnumerable(Map); | 188 TestEnumerable(Map); |
187 TestEnumerable(WeakMap); | 189 TestEnumerable(WeakMap); |
| 190 TestEnumerable(WeakSet); |
188 | 191 |
189 | 192 |
190 // Test arbitrary properties on Maps and WeakMaps | 193 // Test arbitrary properties on Maps and WeakMaps |
191 function TestArbitrary(m) { | 194 function TestArbitrary(m) { |
192 function TestProperty(map, property, value) { | 195 function TestProperty(map, property, value) { |
193 map[property] = value; | 196 map[property] = value; |
194 assertEquals(value, map[property]); | 197 assertEquals(value, map[property]); |
195 } | 198 } |
196 for (var i = 0; i < 20; i++) { | 199 for (var i = 0; i < 20; i++) { |
197 TestProperty(m, i, 'val' + i); | 200 TestProperty(m, i, 'val' + i); |
198 TestProperty(m, 'foo' + i, 'bar' + i); | 201 TestProperty(m, 'foo' + i, 'bar' + i); |
199 } | 202 } |
200 TestMapping(m, new Object, 'foobar'); | 203 TestMapping(m, new Object, 'foobar'); |
201 } | 204 } |
202 TestArbitrary(new Map); | 205 TestArbitrary(new Map); |
203 TestArbitrary(new WeakMap); | 206 TestArbitrary(new WeakMap); |
204 | 207 |
205 | 208 |
206 // Test direct constructor call | 209 // Test direct constructor call |
207 assertTrue(Set() instanceof Set); | 210 assertTrue(Set() instanceof Set); |
208 assertTrue(Map() instanceof Map); | 211 assertTrue(Map() instanceof Map); |
209 assertTrue(WeakMap() instanceof WeakMap); | 212 assertTrue(WeakMap() instanceof WeakMap); |
| 213 assertTrue(WeakSet() instanceof WeakSet); |
210 | 214 |
211 | 215 |
212 // Test whether NaN values as keys are treated correctly. | 216 // Test whether NaN values as keys are treated correctly. |
213 var s = new Set; | 217 var s = new Set; |
214 assertFalse(s.has(NaN)); | 218 assertFalse(s.has(NaN)); |
215 assertFalse(s.has(NaN + 1)); | 219 assertFalse(s.has(NaN + 1)); |
216 assertFalse(s.has(23)); | 220 assertFalse(s.has(23)); |
217 s.add(NaN); | 221 s.add(NaN); |
218 assertTrue(s.has(NaN)); | 222 assertTrue(s.has(NaN)); |
219 assertTrue(s.has(NaN + 1)); | 223 assertTrue(s.has(NaN + 1)); |
220 assertFalse(s.has(23)); | 224 assertFalse(s.has(23)); |
221 var m = new Map; | 225 var m = new Map; |
222 assertFalse(m.has(NaN)); | 226 assertFalse(m.has(NaN)); |
223 assertFalse(m.has(NaN + 1)); | 227 assertFalse(m.has(NaN + 1)); |
224 assertFalse(m.has(23)); | 228 assertFalse(m.has(23)); |
225 m.set(NaN, 'a-value'); | 229 m.set(NaN, 'a-value'); |
226 assertTrue(m.has(NaN)); | 230 assertTrue(m.has(NaN)); |
227 assertTrue(m.has(NaN + 1)); | 231 assertTrue(m.has(NaN + 1)); |
228 assertFalse(m.has(23)); | 232 assertFalse(m.has(23)); |
229 | 233 |
230 | 234 |
231 // Test some common JavaScript idioms for Sets | 235 // Test some common JavaScript idioms for Sets |
232 var s = new Set; | 236 var s = new Set; |
233 assertTrue(s instanceof Set); | 237 assertTrue(s instanceof Set); |
234 assertTrue(Set.prototype.add instanceof Function) | 238 assertTrue(Set.prototype.add instanceof Function) |
235 assertTrue(Set.prototype.has instanceof Function) | 239 assertTrue(Set.prototype.has instanceof Function) |
236 assertTrue(Set.prototype.delete instanceof Function) | 240 assertTrue(Set.prototype.delete instanceof Function) |
| 241 assertTrue(Set.prototype.clear instanceof Function) |
237 | 242 |
238 | 243 |
239 // Test some common JavaScript idioms for Maps | 244 // Test some common JavaScript idioms for Maps |
240 var m = new Map; | 245 var m = new Map; |
241 assertTrue(m instanceof Map); | 246 assertTrue(m instanceof Map); |
242 assertTrue(Map.prototype.set instanceof Function) | 247 assertTrue(Map.prototype.set instanceof Function) |
243 assertTrue(Map.prototype.get instanceof Function) | 248 assertTrue(Map.prototype.get instanceof Function) |
244 assertTrue(Map.prototype.has instanceof Function) | 249 assertTrue(Map.prototype.has instanceof Function) |
245 assertTrue(Map.prototype.delete instanceof Function) | 250 assertTrue(Map.prototype.delete instanceof Function) |
| 251 assertTrue(Map.prototype.clear instanceof Function) |
246 | 252 |
247 | 253 |
248 // Test some common JavaScript idioms for WeakMaps | 254 // Test some common JavaScript idioms for WeakMaps |
249 var m = new WeakMap; | 255 var m = new WeakMap; |
250 assertTrue(m instanceof WeakMap); | 256 assertTrue(m instanceof WeakMap); |
251 assertTrue(WeakMap.prototype.set instanceof Function) | 257 assertTrue(WeakMap.prototype.set instanceof Function) |
252 assertTrue(WeakMap.prototype.get instanceof Function) | 258 assertTrue(WeakMap.prototype.get instanceof Function) |
253 assertTrue(WeakMap.prototype.has instanceof Function) | 259 assertTrue(WeakMap.prototype.has instanceof Function) |
254 assertTrue(WeakMap.prototype.delete instanceof Function) | 260 assertTrue(WeakMap.prototype.delete instanceof Function) |
| 261 assertTrue(WeakMap.prototype.clear instanceof Function) |
255 | 262 |
256 | 263 |
257 // Test class of the Set, Map and WeakMap instance and prototype. | 264 // Test some common JavaScript idioms for WeakSets |
| 265 var s = new WeakSet; |
| 266 assertTrue(s instanceof WeakSet); |
| 267 assertTrue(WeakSet.prototype.add instanceof Function) |
| 268 assertTrue(WeakSet.prototype.has instanceof Function) |
| 269 assertTrue(WeakSet.prototype.delete instanceof Function) |
| 270 assertTrue(WeakSet.prototype.clear instanceof Function) |
| 271 |
| 272 |
| 273 // Test class of instance and prototype. |
258 assertEquals("Set", %_ClassOf(new Set)) | 274 assertEquals("Set", %_ClassOf(new Set)) |
259 assertEquals("Object", %_ClassOf(Set.prototype)) | 275 assertEquals("Object", %_ClassOf(Set.prototype)) |
260 assertEquals("Map", %_ClassOf(new Map)) | 276 assertEquals("Map", %_ClassOf(new Map)) |
261 assertEquals("Object", %_ClassOf(Map.prototype)) | 277 assertEquals("Object", %_ClassOf(Map.prototype)) |
262 assertEquals("WeakMap", %_ClassOf(new WeakMap)) | 278 assertEquals("WeakMap", %_ClassOf(new WeakMap)) |
263 assertEquals("Object", %_ClassOf(WeakMap.prototype)) | 279 assertEquals("Object", %_ClassOf(WeakMap.prototype)) |
| 280 assertEquals("WeakSet", %_ClassOf(new WeakSet)) |
| 281 assertEquals("Object", %_ClassOf(WeakMap.prototype)) |
264 | 282 |
265 | 283 |
266 // Test constructor property of the Set, Map and WeakMap prototype. | 284 // Test constructor property of the Set, Map, WeakMap and WeakSet prototype. |
267 function TestConstructor(C) { | 285 function TestConstructor(C) { |
268 assertFalse(C === Object.prototype.constructor); | 286 assertFalse(C === Object.prototype.constructor); |
269 assertSame(C, C.prototype.constructor); | 287 assertSame(C, C.prototype.constructor); |
270 assertSame(C, C().__proto__.constructor); | 288 assertSame(C, C().__proto__.constructor); |
271 assertSame(C, (new C).__proto__.constructor); | 289 assertSame(C, (new C).__proto__.constructor); |
272 } | 290 } |
273 TestConstructor(Set); | 291 TestConstructor(Set); |
274 TestConstructor(Map); | 292 TestConstructor(Map); |
275 TestConstructor(WeakMap); | 293 TestConstructor(WeakMap); |
| 294 TestConstructor(WeakSet); |
276 | 295 |
277 | 296 |
278 // Regression test for WeakMap prototype. | 297 // Regression test for WeakMap prototype. |
279 assertTrue(WeakMap.prototype.constructor === WeakMap) | 298 assertTrue(WeakMap.prototype.constructor === WeakMap) |
280 assertTrue(Object.getPrototypeOf(WeakMap.prototype) === Object.prototype) | 299 assertTrue(Object.getPrototypeOf(WeakMap.prototype) === Object.prototype) |
281 | 300 |
282 | 301 |
283 // Regression test for issue 1617: The prototype of the WeakMap constructor | 302 // Regression test for issue 1617: The prototype of the WeakMap constructor |
284 // needs to be unique (i.e. different from the one of the Object constructor). | 303 // needs to be unique (i.e. different from the one of the Object constructor). |
285 assertFalse(WeakMap.prototype === Object.prototype); | 304 assertFalse(WeakMap.prototype === Object.prototype); |
(...skipping 11 matching lines...) Expand all Loading... |
297 assertEquals(10, o.myValue); | 316 assertEquals(10, o.myValue); |
298 | 317 |
299 | 318 |
300 // Regression test for issue 1884: Invoking any of the methods for Harmony | 319 // Regression test for issue 1884: Invoking any of the methods for Harmony |
301 // maps, sets, or weak maps, with a wrong type of receiver should be throwing | 320 // maps, sets, or weak maps, with a wrong type of receiver should be throwing |
302 // a proper TypeError. | 321 // a proper TypeError. |
303 var alwaysBogus = [ undefined, null, true, "x", 23, {} ]; | 322 var alwaysBogus = [ undefined, null, true, "x", 23, {} ]; |
304 var bogusReceiversTestSet = [ | 323 var bogusReceiversTestSet = [ |
305 { proto: Set.prototype, | 324 { proto: Set.prototype, |
306 funcs: [ 'add', 'has', 'delete' ], | 325 funcs: [ 'add', 'has', 'delete' ], |
307 receivers: alwaysBogus.concat([ new Map, new WeakMap ]), | 326 receivers: alwaysBogus.concat([ new Map, new WeakMap, new WeakSet ]), |
308 }, | 327 }, |
309 { proto: Map.prototype, | 328 { proto: Map.prototype, |
310 funcs: [ 'get', 'set', 'has', 'delete' ], | 329 funcs: [ 'get', 'set', 'has', 'delete' ], |
311 receivers: alwaysBogus.concat([ new Set, new WeakMap ]), | 330 receivers: alwaysBogus.concat([ new Set, new WeakMap, new WeakSet ]), |
312 }, | 331 }, |
313 { proto: WeakMap.prototype, | 332 { proto: WeakMap.prototype, |
314 funcs: [ 'get', 'set', 'has', 'delete' ], | 333 funcs: [ 'get', 'set', 'has', 'delete' ], |
315 receivers: alwaysBogus.concat([ new Set, new Map ]), | 334 receivers: alwaysBogus.concat([ new Set, new Map, new WeakSet ]), |
| 335 }, |
| 336 { proto: WeakSet.prototype, |
| 337 funcs: [ 'add', 'has', 'delete' ], |
| 338 receivers: alwaysBogus.concat([ new Set, new Map, new WeakMap ]), |
316 }, | 339 }, |
317 ]; | 340 ]; |
318 function TestBogusReceivers(testSet) { | 341 function TestBogusReceivers(testSet) { |
319 for (var i = 0; i < testSet.length; i++) { | 342 for (var i = 0; i < testSet.length; i++) { |
320 var proto = testSet[i].proto; | 343 var proto = testSet[i].proto; |
321 var funcs = testSet[i].funcs; | 344 var funcs = testSet[i].funcs; |
322 var receivers = testSet[i].receivers; | 345 var receivers = testSet[i].receivers; |
323 for (var j = 0; j < funcs.length; j++) { | 346 for (var j = 0; j < funcs.length; j++) { |
324 var func = proto[funcs[j]]; | 347 var func = proto[funcs[j]]; |
325 for (var k = 0; k < receivers.length; k++) { | 348 for (var k = 0; k < receivers.length; k++) { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 (function() { | 429 (function() { |
407 var k = new Object(); | 430 var k = new Object(); |
408 var w = new WeakMap(); | 431 var w = new WeakMap(); |
409 w.set(k, 23); | 432 w.set(k, 23); |
410 assertTrue(w.has(k)); | 433 assertTrue(w.has(k)); |
411 assertEquals(23, w.get(k)); | 434 assertEquals(23, w.get(k)); |
412 w.clear(); | 435 w.clear(); |
413 assertFalse(w.has(k)); | 436 assertFalse(w.has(k)); |
414 assertEquals(undefined, w.get(k)); | 437 assertEquals(undefined, w.get(k)); |
415 })(); | 438 })(); |
| 439 |
| 440 |
| 441 // Test WeakSet clear |
| 442 (function() { |
| 443 var k = new Object(); |
| 444 var w = new WeakSet(); |
| 445 w.add(k); |
| 446 assertTrue(w.has(k)); |
| 447 w.clear(); |
| 448 assertFalse(w.has(k)); |
| 449 })(); |
OLD | NEW |