summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/harmony/weakrefs/finalizationregistry-independent-lifetime.js
blob: a1744ead9a3aa16a48d28bd89c700a2c128cbe40 (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
// Copyright 2020 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-recompilation

(async function () {

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

  let task_1_gc = (async function () {
    const fg = new FinalizationRegistry(cleanup);

    (function () {
      let x = {};
      fg.register(x, "holdings");
      x = null;
    })();

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

  // Schedule a task to collect fg, which should result in cleanup not called.
  let task_2_gc = (async function () {
    await gc({ type: 'major', execution: 'async' });
    assertFalse(cleanup_called);
  })();

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

  // Check that the cleanup will not be called.
  setTimeout(function () { assertFalse(cleanup_called); }, 0);

})();