tor-browser

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

finalizationregistry-cleanupCallback-gets-a-microtask.optional.any.js (2111B)


      1 // META: script=/common/gc.js
      2 // META: script=resources/maybe-garbage-collect.js
      3 // ├──> maybeGarbageCollectAsync
      4 // └──> resolveGarbageCollection
      5 /*---
      6 esid: sec-finalization-registry-target
      7 info: |
      8  FinalizationRegistry ( cleanupCallback )
      9 
     10  Execution
     11  At any time, if a set of objects S is not live, an ECMAScript implementation may perform the
     12  following steps atomically:
     13 
     14  For each obj of S, do
     15    For each WeakRef ref such that ref.[[WeakRefTarget]] is obj, do
     16      Set ref.[[WeakRefTarget]] to empty.
     17    For each FinalizationRegistry fg such that fg.[[Cells]] contains cell, such that
     18    cell.[[WeakRefTarget]] is obj,
     19      Set cell.[[WeakRefTarget]] to empty.
     20      Optionally, perform ! HostCleanupFinalizationRegistry(fg).
     21 
     22  HostCleanupFinalizationRegistry(finalizationRegistry)
     23 
     24  HostCleanupFinalizationRegistry is an implementation-defined abstract operation that is expected
     25  to call CleanupFinalizationRegistry(finalizationRegistry) at some point in the future, if
     26  possible. The host's responsibility is to make this call at a time which does not interrupt
     27  synchronous ECMAScript code execution.
     28 ---*/
     29 
     30 let count = 1_000;
     31 let calls = 0;
     32 let registries = [];
     33 let callback = function() {
     34  calls++;
     35 };
     36 for (let i = 0; i < count; i++) {
     37  registries.push(
     38    new FinalizationRegistry(callback)
     39  );
     40 }
     41 setup({ allow_uncaught_exception: true });
     42 
     43 promise_test((test) => {
     44  assert_implements(
     45    typeof FinalizationRegistry.prototype.register === 'function',
     46    'FinalizationRegistry.prototype.register is not implemented.'
     47  );
     48  return (async () => {
     49 
     50    {
     51      let target = {};
     52      for (let registry of registries) {
     53        registry.register(target, 1);
     54      }
     55      target = null;
     56    }
     57 
     58    await maybeGarbageCollectAsync();
     59    await test.step_wait(() => calls === count, `Expected ${count} registry cleanups.`);
     60  })().catch(resolveGarbageCollection);
     61 }, 'HostCleanupFinalizationRegistry is an implementation-defined abstract operation that is expected to call CleanupFinalizationRegistry(finalizationRegistry) at some point in the future, if possible.');