weakref-deref-keeps-alive.js 1.02 KB
Newer Older
1 2 3 4
// 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.

5
// Flags: --expose-gc --noincremental-marking
6 7 8 9 10

let wr;
let wr_control; // control WeakRef for testing what happens without deref
(function() {
  let o1 = {};
11
  wr = new WeakRef(o1);
12
  let o2 = {};
13
  wr_control = new WeakRef(o2);
14 15 16 17 18 19
})();

let strong = {a: wr.deref(), b: wr_control.deref()};

gc();

20 21 22 23 24 25 26
// Next task.
setTimeout(function() {
  // Call deref inside a closure, trying to avoid accidentally storing a strong
  // reference into the object in the stack frame.
  (function() {
    wr.deref();
  })();
27

28
  strong = null;
29

30 31
  // This GC will clear wr_control.
  gc();
32

33 34 35 36 37
  (function() {
    assertNotEquals(undefined, wr.deref());
    // Now the control WeakRef got cleared, since nothing was keeping it alive.
    assertEquals(undefined, wr_control.deref());
  })();
38

39 40 41
  // Next task.
  setTimeout(function() {
    gc();
42

43 44 45
    assertEquals(undefined, wr.deref());
  }, 0);
}, 0);