summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/harmony/weakrefs/finalizationregistry-scheduled-for-cleanup-multiple-times.js
blob: 3262442b2fafd46821e7d9b1e84049188ae2a9b6 (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
67
// Copyright 2018 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
// Flags: --no-stress-flush-code

let cleanup0_call_count = 0;
let cleanup0_holdings_count = 0;

let cleanup1_call_count = 0;
let cleanup1_holdings_count = 0;

let cleanup0 = function(holdings) {
  ++cleanup0_holdings_count;
  ++cleanup0_call_count;
}

let cleanup1 = function(holdings) {
  ++cleanup1_holdings_count;
  ++cleanup1_call_count;
}

let fg0 = new FinalizationRegistry(cleanup0);
let fg1 = new FinalizationRegistry(cleanup1);

// Register 1 weak reference for each FinalizationRegistry and kill the objects they point to.
(function() {
  // The objects need to be inside a closure so that we can reliably kill them.
  let objects = [];
  objects[0] = {};
  objects[1] = {};

  fg0.register(objects[0], "holdings0-0");
  fg1.register(objects[1], "holdings1-0");

  // Drop the references to the objects.
  objects = [];
})();

// Will schedule both fg0 and fg1 for cleanup.
gc();

// Before the cleanup task has a chance to run, do the same thing again, so both
// FinalizationRegistries are (again) scheduled for cleanup. This has to be a IIFE function
// (so that we can reliably kill the objects) so we cannot use the same function
// as before.
(function() {
  let objects = [];
  objects[0] = {};
  objects[1] = {};
  fg0.register(objects[0], "holdings0-1");
  fg1.register(objects[1], "holdings1-1");
  objects = [];
})();

gc();

let timeout_func = function() {
  assertEquals(2, cleanup0_call_count);
  assertEquals(2, cleanup0_holdings_count);
  assertEquals(2, cleanup1_call_count);
  assertEquals(2, cleanup1_holdings_count);
}

// Give the cleanup task a chance to run.
setTimeout(timeout_func, 0);