summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/harmony/weakrefs/two-weakrefs.js
blob: a23c1e8cf2c58bc5a36c78ee3c8e76b2fd718a3b (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
// 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

let o1 = {};
let o2 = {};
let wr1;
let wr2;
(function() {
  wr1 = new WeakRef(o1);
  wr2 = new WeakRef(o2);
})();

// Since the WeakRefs were created during this turn, they're not cleared by GC.
// Here and below, we invoke GC synchronously and, with conservative stack
// scanning, there is a chance that the object is not reclaimed now. In any
// case, the WeakRef should not be cleared.
gc();

(function() {
  assertNotEquals(undefined, wr1.deref());
  assertNotEquals(undefined, wr2.deref());
})();

// New task
setTimeout(function() {
  (function () { wr1.deref(); })();
  o1 = null;
  gc(); // deref makes sure we don't clean up wr1
  (function () { assertNotEquals(undefined, wr1.deref()); })();

  // New task
  setTimeout(function() {
    (function () { wr2.deref(); })();
    o2 = null;
    gc(); // deref makes sure we don't clean up wr2
    (function () { assertNotEquals(undefined, wr2.deref()); })();

    // New task
    (async function () {
      // Trigger GC again to make sure the two WeakRefs are cleared.
      // We need to invoke GC asynchronously and wait for it to finish, so that
      // it doesn't need to scan the stack. Otherwise, the objects may not be
      // reclaimed because of conservative stack scanning and the test may not
      // work as intended.
      await gc({ type: 'major', execution: 'async' });

      assertEquals(undefined, wr1.deref());
      assertEquals(undefined, wr2.deref());
    })();
  }, 0);
}, 0);