tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

symbols-as-weakmap-keys.js (2965B)


      1 // |jit-test| --enable-symbols-as-weakmap-keys
      2 
      3 // Test weak maps with symbols keys where the symbols are referenced in
      4 // different zones. Currently we require all participating zones plus the
      5 // atoms zone to be collected to collect such weakmap entries.
      6 
      7 gczeal(0);
      8 gczeal('CheckHeapAfterGC');
      9 
     10 function makeSymbol(name) {
     11  return Symbol(name);
     12 }
     13 
     14 function mapSize(wm) {
     15  return nondeterministicGetWeakMapSize(wm);
     16 }
     17 
     18 // Test weak map entry with a symbol key is removed when the symbol dies.
     19 printErr("Test 1");
     20 let wm = new WeakMap;
     21 assertEq(mapSize(wm), 0);
     22 let k = makeSymbol('test1');
     23 wm.set(k, {});
     24 assertEq(mapSize(wm), 1);
     25 gc();
     26 assertEq(mapSize(wm), 1);
     27 k = undefined;
     28 gc();
     29 assertEq(mapSize(wm), 0);
     30 wm = undefined;
     31 gc();
     32 
     33 // Repeat the test with the key held alive from a different zone.
     34 printErr("Test 2");
     35 let keyZone = newGlobal({newCompartment: true});
     36 keyZone.key = makeSymbol('test2');
     37 let mapZone = newGlobal({newCompartment: true});
     38 mapZone.map = undefined;
     39 mapZone.eval(`map = new WeakMap;`);
     40 mapZone.map.set(keyZone.key, {});
     41 assertEq(mapSize(mapZone.map), 1);
     42 gc();
     43 assertEq(mapSize(mapZone.map), 1);
     44 keyZone.key = undefined;
     45 gc();
     46 assertEq(mapSize(mapZone.map), 0);
     47 keyZone = undefined;
     48 mapZone = undefined;
     49 gc();
     50 
     51 // Repeat the previous test with per zone GC.
     52 printErr("Test 3");
     53 keyZone = newGlobal({newCompartment: true});
     54 mapZone = newGlobal({newCompartment: true});
     55 keyZone.eval('var key = Symbol("test2");');
     56 mapZone.eval(`var map = new WeakMap;`);
     57 mapZone.keyZone = keyZone;
     58 mapZone.eval('map.set(keyZone.key, {});');
     59 mapZone.keyZone = undefined;
     60 gc();
     61 assertEq(mapSize(mapZone.map), 1);
     62 
     63 printErr("  3.1 Setup");
     64 keyZone.key = undefined;
     65 
     66 printErr("  3.2 Collect only main zone");
     67 gc(this);
     68 assertEq(mapSize(mapZone.map), 1);
     69 
     70 printErr("  3.3 Collect only map zone");
     71 gc(mapZone);
     72 assertEq(mapSize(mapZone.map), 1);
     73 
     74 printErr("  3.4 Collect only key zone");
     75 gc(keyZone);
     76 // Bug 1410123: Perhaps we could stop marking the key atom in the key zone at
     77 // this point if we refine the atom bitmaps without requiring the atoms zone
     78 // to be collected.
     79 assertEq(mapSize(mapZone.map), 1);
     80 
     81 printErr("  3.5 Collect map zone and atoms zone (entry not removed)");
     82 schedulezone(mapZone);
     83 schedulezone('atoms');
     84 gc('zone');
     85 assertEq(mapSize(mapZone.map), 1);
     86 
     87 printErr("  3.6 Collect key zone and atoms zone (key no longer referenced)");
     88 schedulezone(keyZone);
     89 schedulezone('atoms');
     90 gc('zone');
     91 assertEq(mapSize(mapZone.map), 1);
     92 
     93 printErr("  3.7 Collect only atoms zone (nothing collected)");
     94 schedulezone('atoms');
     95 gc('zone');
     96 assertEq(mapSize(mapZone.map), 1);
     97 
     98 printErr("  3.8 Collect only map zone (nothing collected)");
     99 gc(mapZone);
    100 assertEq(mapSize(mapZone.map), 1);
    101 
    102 printErr("  3.9 Collect map zone and atoms zone (entry collected)");
    103 schedulezone(mapZone);
    104 schedulezone('atoms');
    105 gc('zone');
    106 assertEq(mapSize(mapZone.map), 0);
    107 
    108 printErr("  3.10 Collect all zones (nothing more collected)");
    109 gc();
    110 assertEq(mapSize(mapZone.map), 0);