2018-11-05 14:21:02 +00:00
|
|
|
// 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.
|
|
|
|
|
2019-07-30 10:10:18 +00:00
|
|
|
// Flags: --harmony-weak-refs --expose-gc --noincremental-marking
|
2018-11-05 14:21:02 +00:00
|
|
|
|
|
|
|
let wr;
|
|
|
|
let wr_control; // control WeakRef for testing what happens without deref
|
|
|
|
(function() {
|
|
|
|
let o1 = {};
|
2018-12-17 08:15:02 +00:00
|
|
|
wr = new WeakRef(o1);
|
2018-11-05 14:21:02 +00:00
|
|
|
let o2 = {};
|
2018-12-17 08:15:02 +00:00
|
|
|
wr_control = new WeakRef(o2);
|
2018-11-05 14:21:02 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
let strong = {a: wr.deref(), b: wr_control.deref()};
|
|
|
|
|
|
|
|
gc();
|
|
|
|
|
2019-07-30 10:10:18 +00:00
|
|
|
// 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();
|
|
|
|
})();
|
2018-11-05 14:21:02 +00:00
|
|
|
|
2019-07-30 10:10:18 +00:00
|
|
|
strong = null;
|
2018-11-05 14:21:02 +00:00
|
|
|
|
2019-07-30 10:10:18 +00:00
|
|
|
// This GC will clear wr_control.
|
|
|
|
gc();
|
2018-11-05 14:21:02 +00:00
|
|
|
|
2019-07-30 10:10:18 +00:00
|
|
|
(function() {
|
|
|
|
assertNotEquals(undefined, wr.deref());
|
|
|
|
// Now the control WeakRef got cleared, since nothing was keeping it alive.
|
|
|
|
assertEquals(undefined, wr_control.deref());
|
|
|
|
})();
|
2018-11-05 14:21:02 +00:00
|
|
|
|
2019-07-30 10:10:18 +00:00
|
|
|
// Next task.
|
|
|
|
setTimeout(function() {
|
|
|
|
gc();
|
2018-11-05 14:21:02 +00:00
|
|
|
|
2019-07-30 10:10:18 +00:00
|
|
|
assertEquals(undefined, wr.deref());
|
|
|
|
}, 0);
|
|
|
|
}, 0);
|