ccws-as-weakmap-keys.js (2044B)
1 // Test weak maps with keys in different zones, checking whether entries are 2 // collected when we expect. The existance of CCWs from uncollected zones 3 // keeps keys alive and prevents entries from being collected. 4 5 gczeal(0); 6 7 function mapSize(wm) { 8 // Note: Using nondeterministicGetWeakMapKeys will create CCWs to the keys 9 // if they are in a different compartment. 10 return nondeterministicGetWeakMapSize(wm); 11 } 12 13 let keyZone = newGlobal({newCompartment: true}); 14 let mapZone = newGlobal({newCompartment: true}); 15 16 printErr("Test 1: Full GC"); 17 18 keyZone.eval('var key = makeFinalizeObserver();'); 19 mapZone.eval('var map = new WeakMap;'); 20 mapZone.map.set(keyZone.key, {}); 21 let initialCount = finalizeCount(); 22 assertEq(mapSize(mapZone.map), 1); 23 gc(); 24 assertEq(finalizeCount(), initialCount); 25 assertEq(mapSize(mapZone.map), 1); 26 keyZone.key = undefined; 27 gc(); 28 assertEq(finalizeCount(), initialCount + 1); 29 assertEq(mapSize(mapZone.map), 0); 30 mapZone.map = undefined; 31 gc(); 32 33 printErr("Test 2: Zone GC"); 34 keyZone.eval('var key = makeFinalizeObserver();'); 35 mapZone.eval('var map = new WeakMap;'); 36 mapZone.keyZone = keyZone; 37 mapZone.eval('map.set(keyZone.key, {});'); 38 mapZone.keyZone = undefined; 39 assertEq(mapSize(mapZone.map), 1); 40 initialCount = finalizeCount(); 41 42 printErr(" 2.1 Setup"); 43 gc(); 44 keyZone.key = undefined; 45 assertEq(finalizeCount(), initialCount); 46 assertEq(mapSize(mapZone.map), 1); 47 48 printErr(" 2.2 Collect only main zone (key not collected)"); 49 gc(this); 50 assertEq(finalizeCount(), initialCount); 51 assertEq(mapSize(mapZone.map), 1); 52 53 printErr(" 2.3 Collect only map zone (key not collected)"); 54 gc(mapZone); 55 assertEq(finalizeCount(), initialCount); 56 assertEq(mapSize(mapZone.map), 1); 57 58 printErr(" 2.4 Collect only key zone (key not collected)"); 59 gc(keyZone); 60 assertEq(finalizeCount(), initialCount); 61 assertEq(mapSize(mapZone.map), 1); 62 63 printErr(" 2.5 Collect key zone and map zone (key collected)"); 64 schedulezone(keyZone); 65 schedulezone(mapZone); 66 gc('zone'); 67 assertEq(finalizeCount(), initialCount + 1); 68 assertEq(mapSize(mapZone.map), 0);