summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/js/builtins/weakrefs/holdings-multiple-values.optional.any.js
blob: 604174e46796b74a3e94bf5b9f5497958bc0e92a (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// META: script=/common/gc.js
// META: script=resources/maybe-garbage-collect.js
// ├──> maybeGarbageCollectAndCleanupAsync
// └──> resolveGarbageCollection
/*---
esid: sec-properties-of-the-finalization-registry-constructor
info: |
  FinalizationRegistry.prototype.cleanupSome ( [ callback ] )

  1. Let finalizationRegistry be the this value.
  ...
  5. Perform ! CleanupFinalizationRegistry(finalizationRegistry, callback).
  ...

  CleanupFinalizationRegistry ( finalizationRegistry [ , callback ] )

  ...
  3. While finalizationRegistry.[[Cells]] contains a Record cell such that cell.[[WeakRefTarget]] is ~empty~, then an implementation may perform the following steps,
    a. Choose any such cell.
    b. Remove cell from finalizationRegistry.[[Cells]].
    c. Perform ? Call(callback, undefined, << cell.[[HeldValue]] >>).

---*/

function check(value, expectedName) {
  let holdings = [];
  let called = 0;
  let finalizationRegistry = new FinalizationRegistry(function() {});

  function callback(holding) {
    called += 1;
    holdings.push(holding);
  }

  // This is internal to avoid conflicts
  function emptyCells(value) {
    let target = {};
    finalizationRegistry.register(target, value);

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

    return prom;
  }

  return emptyCells(value).then(function() {
    finalizationRegistry.cleanupSome(callback);
    assert_equals(called, 1, expectedName);
    assert_equals(holdings.length, 1, expectedName);
    assert_equals(holdings[0], value, expectedName);
  });
}

test(() =>
  assert_implements(
  typeof FinalizationRegistry.prototype.cleanupSome === 'function',
  'FinalizationRegistry.prototype.cleanupSome is not implemented.'
), 'Requires FinalizationRegistry.prototype.cleanupSome');
promise_test(() => check(undefined, 'undefined'), '`undefined` as registered holding value');
promise_test(() => check(null, 'null'), '`null` as registered holding value');
promise_test(() => check('', 'the empty string'), '`""` as registered holding value');
promise_test(() => check({}, 'object'), '`{}` as registered holding value');
promise_test(() => check(42, 'number'), '`42` as registered holding value');
promise_test(() => check(true, 'true'), '`true` as registered holding value');
promise_test(() => check(false, 'false'), '`false` as registered holding value');
promise_test(() => check(Symbol(1), 'symbol'), '`Symbol(1)` as registered holding value');