v8/test/mjsunit/harmony/weakrefs/weakref-deref-keeps-alive.js
Antoine du Hamel d59db06bf5 [weakrefs] Remove --no-harmony-weak-refs flag
Bug: v8:8179
Change-Id: I7f699073807d1874d0c10a4f1641de6bfb0efe6f
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2741582
Commit-Queue: Shu-yu Guo <syg@chromium.org>
Reviewed-by: Shu-yu Guo <syg@chromium.org>
Reviewed-by: Adam Klein <adamk@chromium.org>
Reviewed-by: Sathya Gunasekaran  <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#73871}
2021-04-08 23:10:53 +00:00

46 lines
1.0 KiB
JavaScript

// 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 wr;
let wr_control; // control WeakRef for testing what happens without deref
(function() {
let o1 = {};
wr = new WeakRef(o1);
let o2 = {};
wr_control = new WeakRef(o2);
})();
let strong = {a: wr.deref(), b: wr_control.deref()};
gc();
// 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();
})();
strong = null;
// This GC will clear wr_control.
gc();
(function() {
assertNotEquals(undefined, wr.deref());
// Now the control WeakRef got cleared, since nothing was keeping it alive.
assertEquals(undefined, wr_control.deref());
})();
// Next task.
setTimeout(function() {
gc();
assertEquals(undefined, wr.deref());
}, 0);
}, 0);