summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/harmony/weakrefs/weakcell-and-weakref.js
blob: f6627be19e5d002c3f7b4f3ee06e8e735ec94699 (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
// 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: --harmony-weak-refs --expose-gc --noincremental-marking --allow-natives-syntax

let cleanup_called = false;
let cleanup = function(iter) {
  assertFalse(cleanup_called);
  let cells = [];
  for (wc of iter) {
    cells.push(wc);
  }
  assertEquals(1, cells.length);
  assertEquals(weak_cell, cells[0]);
  cleanup_called = true;
}

let wf = new WeakFactory(cleanup);
let weak_ref;
let weak_cell;
(function() {
  let o = {};
  weak_ref = new WeakRef(o);
  weak_cell = wf.makeCell(o);
})();

// Since the WeakRef was created during this turn, it is not cleared by GC. The
// WeakCell is not cleared either, since the WeakRef keeps the target object
// alive.
gc();
(function() {
  assertNotEquals(undefined, weak_ref.deref());
})();

%PerformMicrotaskCheckpoint();
// Next turn.

gc();

%PerformMicrotaskCheckpoint();
// Next turn.

assertTrue(cleanup_called);
assertEquals(undefined, weak_ref.deref());