| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 27 |
| 28 // Flags: --harmony-weakmaps --expose-gc |
| 29 |
| 30 |
| 31 // Test valid getter and setter calls |
| 32 var m = new WeakMap; |
| 33 assertDoesNotThrow(function () { m.get(new Object) }); |
| 34 assertDoesNotThrow(function () { m.set(new Object) }); |
| 35 assertDoesNotThrow(function () { m.has(new Object) }); |
| 36 assertDoesNotThrow(function () { m.delete(new Object) }); |
| 37 |
| 38 |
| 39 // Test invalid getter and setter calls |
| 40 var m = new WeakMap; |
| 41 assertThrows(function () { m.get(undefined) }, TypeError); |
| 42 assertThrows(function () { m.set(undefined, 0) }, TypeError); |
| 43 assertThrows(function () { m.get(0) }, TypeError); |
| 44 assertThrows(function () { m.set(0, 0) }, TypeError); |
| 45 assertThrows(function () { m.get('a-key') }, TypeError); |
| 46 assertThrows(function () { m.set('a-key', 0) }, TypeError); |
| 47 |
| 48 |
| 49 // Test expected mapping behavior |
| 50 var m = new WeakMap; |
| 51 function TestMapping(map, key, value) { |
| 52 map.set(key, value); |
| 53 assertSame(value, map.get(key)); |
| 54 } |
| 55 TestMapping(m, new Object, 23); |
| 56 TestMapping(m, new Object, 'the-value'); |
| 57 TestMapping(m, new Object, new Object); |
| 58 |
| 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 |
| 81 // Test GC of map with entry |
| 82 var m = new WeakMap; |
| 83 var key = new Object; |
| 84 m.set(key, 'not-collected'); |
| 85 gc(); |
| 86 assertSame('not-collected', m.get(key)); |
| 87 |
| 88 |
| 89 // Test GC of map with chained entries |
| 90 var m = new WeakMap; |
| 91 var head = new Object; |
| 92 for (key = head, i = 0; i < 10; i++, key = m.get(key)) { |
| 93 m.set(key, new Object); |
| 94 } |
| 95 gc(); |
| 96 var count = 0; |
| 97 for (key = head; key != undefined; key = m.get(key)) { |
| 98 count++; |
| 99 } |
| 100 assertEquals(11, count); |
| 101 |
| 102 |
| 103 // Test property attribute [[Enumerable]] |
| 104 var m = new WeakMap; |
| 105 function props(x) { |
| 106 var array = []; |
| 107 for (var p in x) array.push(p); |
| 108 return array.sort(); |
| 109 } |
| 110 assertArrayEquals([], props(WeakMap)); |
| 111 assertArrayEquals([], props(WeakMap.prototype)); |
| 112 assertArrayEquals([], props(m)); |
| 113 |
| 114 |
| 115 // Test arbitrary properties on weak maps |
| 116 var m = new WeakMap; |
| 117 function TestProperty(map, property, value) { |
| 118 map[property] = value; |
| 119 assertEquals(value, map[property]); |
| 120 } |
| 121 for (i = 0; i < 20; i++) { |
| 122 TestProperty(m, i, 'val' + i); |
| 123 TestProperty(m, 'foo' + i, 'bar' + i); |
| 124 } |
| 125 TestMapping(m, new Object, 'foobar'); |
| 126 |
| 127 |
| 128 // Test direct constructor call |
| 129 var m = WeakMap(); |
| 130 assertTrue(m instanceof WeakMap); |
| 131 |
| 132 |
| 133 // Test some common JavaScript idioms |
| 134 var m = new WeakMap; |
| 135 assertTrue(m instanceof WeakMap); |
| 136 assertTrue(WeakMap.prototype.set instanceof Function) |
| 137 assertTrue(WeakMap.prototype.get instanceof Function) |
| 138 assertTrue(WeakMap.prototype.has instanceof Function) |
| 139 assertTrue(WeakMap.prototype.delete instanceof Function) |
| 140 |
| 141 |
| 142 // Stress Test |
| 143 // There is a proposed stress-test available at the es-discuss mailing list |
| 144 // which cannot be reasonably automated. Check it out by hand if you like: |
| 145 // https://mail.mozilla.org/pipermail/es-discuss/2011-May/014096.html |
| OLD | NEW |