summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/harmony/weakrefs/finalizationregistry-independent-lifetime-multiple.js
blob: 02c4e196df37845360a0673ca52a370b6b68a1f7 (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
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --expose-gc --noincremental-marking --no-concurrent-inlining

(async function () {

  let cleanup_called = false;
  function cleanup(holdings) {
    cleanup_called = true;
  };

  let cleanup_called_2 = false;
  function cleanup2(holdings) {
    cleanup_called_2 = true;
  };

  const fg = new FinalizationRegistry(cleanup);

  let task_1_gc = (async function () {
    const fg2 = new FinalizationRegistry(cleanup2);

    (function () {
      fg.register({}, "holdings1");
      fg2.register({}, "holdings2");
    })();

    // Schedule fg and fg2 for cleanup.
    await gc({ type: 'major', execution: 'async' });
    assertFalse(cleanup_called);
    assertFalse(cleanup_called_2);
   })();

  // Schedule a task to collect fg2, but fg is still alive.
  let task_2_gc = (async function () {
    await gc({ type: 'major', execution: 'async' });
    assertFalse(cleanup_called);
    assertFalse(cleanup_called_2);
  })();

  // Wait for the two GC tasks to be executed.
  await task_1_gc;
  await task_2_gc;

  // Check that only the cleanup for fg will be called.
  setTimeout(function() {
    assertTrue(cleanup_called);
    assertFalse(cleanup_called_2);
  }, 0);

})();