1/* Copyright JS Foundation and other contributors, http://js.foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16var m1 = new WeakSet(); 17 18var k1 = {}; 19assert (m1.add (k1) instanceof WeakSet); 20assert (m1.has (k1) === true); 21 22k1 = {}; 23gc (); /* This will remove the entry, but there is no way to validate it from js. */ 24 25assert (m1.has (k1) === false); 26assert (m1.delete(k1) === false); 27assert (m1.add (k1) instanceof WeakSet); 28assert (m1.has (k1) === true); 29 30assert (m1.delete ("str") === false); 31assert (m1.has (42) === false); 32 33try { 34 m1.add (42); 35 assert (false); 36} catch (e) { 37 assert (e instanceof TypeError); 38} 39 40gc (); 41assert (m1.delete(k1) === true); 42gc (); 43assert (m1.has (k1) === false); 44k1 = {}; 45gc (); 46 47Object.freeze(k1); 48assert (m1.add (k1) instanceof WeakSet); 49assert (m1.has (k1) === true); 50k1 = {}; 51gc (); 52 53var m2 = new WeakSet(); 54 55k1 = {}; 56gc (); 57var k2 = {}; 58 59m1.add (k1); 60m1.add (k2); 61m2.add (k1); 62m2.add (k2); 63 64m2.delete (k1); 65gc (); 66k1 = {}; 67k2 = {}; 68gc (); 69 70new WeakSet().add(new WeakSet().add(new WeakSet().add(new WeakSet().add(new WeakSet().add(new WeakSet().add(new WeakSet())))))); 71gc(); 72 73try { 74 WeakSet(); 75 assert (false); 76} catch (e) { 77 assert (e instanceof TypeError); 78} 79 80try { 81 WeakSet.prototype.get.call({}); 82 assert (false); 83} catch (e) { 84 assert (e instanceof TypeError); 85} 86 87assert (new WeakSet(null) instanceof WeakSet); 88 89k1 = {}; 90k2 = {}; 91m1 = new WeakSet([k1, k2]); 92assert (m1.has (k1)); 93assert (m1.has (k2)); 94gc(); 95m1 = undefined; 96gc(); 97 98m1 = new WeakSet(); 99k1 = {}; 100m1.add (k1); 101m1.add (k1); 102m1.add (k1); 103assert (m1.has (k1)); 104k1 = {}; 105gc(); 106 107m1 = new WeakSet(); 108m1.add (k1); 109m1.add (k1); 110m1.add (k1); 111assert (m1.has (k1)); 112m1 = new WeakSet(); 113gc(); 114 115try { 116 new WeakSet([1,2,3,4]); 117 assert (false); 118} catch (e) { 119 assert (e instanceof TypeError); 120} 121 122function createIterable(arr, methods = {}) { 123 let iterable = function *() { 124 let idx = 0; 125 while (idx < arr.length) { 126 yield arr[idx]; 127 idx++; 128 } 129 }(); 130 iterable['return'] = methods['return']; 131 iterable['throw'] = methods['throw']; 132 133 return iterable; 134}; 135 136var closed = false; 137var iter = createIterable([1, 2, 3], { 138 'return': function(){ closed = true; return {}; } 139}); 140try { 141 new WeakMap(iter); 142} catch(e){} 143 144assert(closed === true); 145 146m1.add([]); 147 148assert (WeakSet.prototype.toString() === "[object WeakSet]"); 149 150WeakSet.prototype.add = function () { throw "abrupt add" }; 151 152try { 153 new WeakSet([{}]); 154 assert (false); 155} catch (e) { 156 assert (e === "abrupt add"); 157} 158 159Object.defineProperty(WeakSet.prototype, 'add', { 160 get: () => { throw "abrupt add getter" } 161}); 162 163try { 164 new WeakSet([]); 165 assert (false); 166} catch (e) { 167 assert (e === "abrupt add getter"); 168} 169