summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/js/builtins/weakrefs/reentrancy.optional.any.js
blob: fa7b7d55c468997adaf831163769897057a4b907 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// META: script=/common/gc.js
// META: script=resources/maybe-garbage-collect.js
// ├──> maybeGarbageCollectAndCleanupAsync
// └──> resolveGarbageCollection
/*---
esid: sec-properties-of-the-finalization-registry-constructor
---*/

let called = 0;
let endOfCall = 0;
let finalizationRegistry = new FinalizationRegistry(function() {});

function callback(holding) {
  called += 1;

  if (called === 1) {
    // Atempt to re-enter the callback.
    let nestedCallbackRan = false;
    finalizationRegistry.cleanupSome(() => { nestedCallbackRan = true });
    assert_equals(nestedCallbackRan, true);
  }

  endOfCall += 1;
}

function emptyCells() {
  let o1 = {};
  let o2 = {};
  // Register more than one objects to test reentrancy.
  finalizationRegistry.register(o1, 'holdings 1');
  finalizationRegistry.register(o2, 'holdings 2');

  let prom = maybeGarbageCollectAndCleanupAsync(o1);
  o1 = null;

  return prom;
}

promise_test(() => {
  return (async () => {
    assert_implements(
      typeof FinalizationRegistry.prototype.cleanupSome === 'function',
      'FinalizationRegistry.prototype.cleanupSome is not implemented.'
    );
    await emptyCells();
    finalizationRegistry.cleanupSome(callback);

    assert_equals(called, 1, 'callback was called');
    assert_equals(endOfCall, 1, 'callback finished');
  })().catch(resolveGarbageCollection);
}, 'cleanupCallback has only one optional chance to be called for a GC that cleans up a registered target.');