undefined-holdings.js 1.04 KB
Newer Older
1
// Copyright 2019 the V8 project authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
// Flags: --harmony-weak-refs --expose-gc --noincremental-marking
6 7

let cleanup_call_count = 0;
8
let cleanup_holdings_count = 0;
9
let cleanup = function(iter) {
10 11 12
  for (holdings of iter) {
    assertEquals(holdings, undefined);
    ++cleanup_holdings_count;
13 14 15 16
  }
  ++cleanup_call_count;
}

17
let fg = new FinalizationRegistry(cleanup);
18

19
// Create an object and register it in the FinalizationRegistry. The object needs to be inside
20 21 22 23
// a closure so that we can reliably kill them!

(function() {
  let object = {};
24
  fg.register(object);
25 26 27 28

  // object goes out of scope.
})();

29
// This GC will reclaim the target object and schedule cleanup.
30 31 32
gc();
assertEquals(0, cleanup_call_count);

33
// Assert that the cleanup function was called and iterated the holdings.
34 35
let timeout_func = function() {
  assertEquals(1, cleanup_call_count);
36
  assertEquals(1, cleanup_holdings_count);
37 38 39
}

setTimeout(timeout_func, 0);