OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 14 matching lines...) Expand all Loading... |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // Flags: --harmony-weakmaps --expose-gc | 28 // Flags: --harmony-weakmaps --expose-gc |
29 | 29 |
30 | 30 |
31 // Test valid getter and setter calls | 31 // Test valid getter and setter calls |
32 var m = new WeakMap; | 32 var m = new WeakMap; |
33 assertDoesNotThrow(function () { m.get(new Object) }); | 33 assertDoesNotThrow(function () { m.get(new Object) }); |
34 assertDoesNotThrow(function () { m.set(new Object) }); | 34 assertDoesNotThrow(function () { m.set(new Object) }); |
| 35 assertDoesNotThrow(function () { m.has(new Object) }); |
| 36 assertDoesNotThrow(function () { m.delete(new Object) }); |
35 | 37 |
36 | 38 |
37 // Test invalid getter and setter calls | 39 // Test invalid getter and setter calls |
38 var m = new WeakMap; | 40 var m = new WeakMap; |
39 assertThrows(function () { m.get(undefined) }, TypeError); | 41 assertThrows(function () { m.get(undefined) }, TypeError); |
40 assertThrows(function () { m.set(undefined, 0) }, TypeError); | 42 assertThrows(function () { m.set(undefined, 0) }, TypeError); |
41 assertThrows(function () { m.get(0) }, TypeError); | 43 assertThrows(function () { m.get(0) }, TypeError); |
42 assertThrows(function () { m.set(0, 0) }, TypeError); | 44 assertThrows(function () { m.set(0, 0) }, TypeError); |
43 assertThrows(function () { m.get('a-key') }, TypeError); | 45 assertThrows(function () { m.get('a-key') }, TypeError); |
44 assertThrows(function () { m.set('a-key', 0) }, TypeError); | 46 assertThrows(function () { m.set('a-key', 0) }, TypeError); |
45 | 47 |
46 | 48 |
47 // Test expected mapping behavior | 49 // Test expected mapping behavior |
48 var m = new WeakMap; | 50 var m = new WeakMap; |
49 function TestMapping(map, key, value) { | 51 function TestMapping(map, key, value) { |
50 map.set(key, value); | 52 map.set(key, value); |
51 assertSame(value, map.get(key)); | 53 assertSame(value, map.get(key)); |
52 } | 54 } |
53 TestMapping(m, new Object, 23); | 55 TestMapping(m, new Object, 23); |
54 TestMapping(m, new Object, 'the-value'); | 56 TestMapping(m, new Object, 'the-value'); |
55 TestMapping(m, new Object, new Object); | 57 TestMapping(m, new Object, new Object); |
56 | 58 |
57 | 59 |
| 60 // Test expected querying behavior |
| 61 var m = new WeakMap; |
| 62 var key = new Object; |
| 63 TestMapping(m, key, 'to-be-present'); |
| 64 assertTrue(m.has(key)); |
| 65 assertFalse(m.has(new Object)); |
| 66 TestMapping(m, key, undefined); |
| 67 assertFalse(m.has(key)); |
| 68 assertFalse(m.has(new Object)); |
| 69 |
| 70 |
| 71 // Test expected deletion behavior |
| 72 var m = new WeakMap; |
| 73 var key = new Object; |
| 74 TestMapping(m, key, 'to-be-deleted'); |
| 75 assertTrue(m.delete(key)); |
| 76 assertFalse(m.delete(key)); |
| 77 assertFalse(m.delete(new Object)); |
| 78 assertSame(m.get(key), undefined); |
| 79 |
| 80 |
58 // Test GC of map with entry | 81 // Test GC of map with entry |
59 var m = new WeakMap; | 82 var m = new WeakMap; |
60 var key = new Object; | 83 var key = new Object; |
61 m.set(key, 'not-collected'); | 84 m.set(key, 'not-collected'); |
62 gc(); | 85 gc(); |
63 assertSame('not-collected', m.get(key)); | 86 assertSame('not-collected', m.get(key)); |
64 | 87 |
65 | 88 |
66 // Test GC of map with chained entries | 89 // Test GC of map with chained entries |
67 var m = new WeakMap; | 90 var m = new WeakMap; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 // Test direct constructor call | 128 // Test direct constructor call |
106 var m = WeakMap(); | 129 var m = WeakMap(); |
107 assertTrue(m instanceof WeakMap); | 130 assertTrue(m instanceof WeakMap); |
108 | 131 |
109 | 132 |
110 // Test some common JavaScript idioms | 133 // Test some common JavaScript idioms |
111 var m = new WeakMap; | 134 var m = new WeakMap; |
112 assertTrue(m instanceof WeakMap); | 135 assertTrue(m instanceof WeakMap); |
113 assertTrue(WeakMap.prototype.set instanceof Function) | 136 assertTrue(WeakMap.prototype.set instanceof Function) |
114 assertTrue(WeakMap.prototype.get instanceof Function) | 137 assertTrue(WeakMap.prototype.get instanceof Function) |
| 138 assertTrue(WeakMap.prototype.has instanceof Function) |
| 139 assertTrue(WeakMap.prototype.delete instanceof Function) |
115 | 140 |
116 | 141 |
117 // Stress Test | 142 // Stress Test |
118 // There is a proposed stress-test available at the es-discuss mailing list | 143 // There is a proposed stress-test available at the es-discuss mailing list |
119 // which cannot be reasonably automated. Check it out by hand if you like: | 144 // which cannot be reasonably automated. Check it out by hand if you like: |
120 // https://mail.mozilla.org/pipermail/es-discuss/2011-May/014096.html | 145 // https://mail.mozilla.org/pipermail/es-discuss/2011-May/014096.html |
OLD | NEW |