2018-10-18 11:30:16 +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.
|
|
|
|
|
2018-10-30 08:43:08 +00:00
|
|
|
// Flags: --harmony-weak-refs --expose-gc --noincremental-marking
|
2018-10-18 11:30:16 +00:00
|
|
|
|
|
|
|
let cleanup_call_count = 0;
|
|
|
|
let cleanup = function(iter) {
|
2019-07-30 10:10:18 +00:00
|
|
|
print("in cleanup");
|
2018-10-18 11:30:16 +00:00
|
|
|
if (cleanup_call_count == 0) {
|
2019-01-30 12:06:32 +00:00
|
|
|
// First call: iterate 2 of the 3 holdings
|
|
|
|
let holdings_list = [];
|
|
|
|
for (holdings of iter) {
|
|
|
|
holdings_list.push(holdings);
|
|
|
|
// Don't iterate the rest of the holdings
|
|
|
|
if (holdings_list.length == 2) {
|
2018-10-18 11:30:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-01-30 12:06:32 +00:00
|
|
|
assertEquals(holdings_list.length, 2);
|
|
|
|
assertTrue(holdings_list[0] < 3);
|
|
|
|
assertTrue(holdings_list[1] < 3);
|
2018-10-18 11:30:16 +00:00
|
|
|
// Update call count only after the asserts; this ensures that the test
|
|
|
|
// fails even if the exceptions inside the cleanup function are swallowed.
|
|
|
|
cleanup_call_count++;
|
|
|
|
} else {
|
2019-01-30 12:06:32 +00:00
|
|
|
// Second call: iterate one leftover holdings and one holdings.
|
2018-10-18 11:30:16 +00:00
|
|
|
assertEquals(1, cleanup_call_count);
|
2019-01-30 12:06:32 +00:00
|
|
|
let holdings_list = [];
|
|
|
|
for (holdings of iter) {
|
|
|
|
holdings_list.push(holdings);
|
2018-10-18 11:30:16 +00:00
|
|
|
}
|
2019-01-30 12:06:32 +00:00
|
|
|
assertEquals(holdings_list.length, 2);
|
|
|
|
assertTrue((holdings_list[0] < 3 && holdings_list[1] == 100) ||
|
|
|
|
(holdings_list[1] < 3 && holdings_list[0] == 100));
|
2018-10-18 11:30:16 +00:00
|
|
|
// Update call count only after the asserts; this ensures that the test
|
|
|
|
// fails even if the exceptions inside the cleanup function are swallowed.
|
|
|
|
cleanup_call_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 12:06:32 +00:00
|
|
|
let fg = new FinalizationGroup(cleanup);
|
|
|
|
// Create 3 objects and register them in the FinalizationGroup. The objects need
|
|
|
|
// to be inside a closure so that we can reliably kill them!
|
2018-10-18 11:30:16 +00:00
|
|
|
|
|
|
|
(function() {
|
|
|
|
let objects = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < 3; ++i) {
|
|
|
|
objects[i] = {a: i};
|
2019-01-30 12:06:32 +00:00
|
|
|
fg.register(objects[i], i);
|
2018-10-18 11:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gc();
|
|
|
|
assertEquals(0, cleanup_call_count);
|
|
|
|
|
|
|
|
// Drop the references to the objects.
|
|
|
|
objects = [];
|
|
|
|
})();
|
|
|
|
|
2019-01-30 12:06:32 +00:00
|
|
|
// This GC will reclaim the targets.
|
2018-10-18 11:30:16 +00:00
|
|
|
gc();
|
|
|
|
assertEquals(0, cleanup_call_count);
|
|
|
|
|
|
|
|
let timeout_func_1 = function() {
|
|
|
|
assertEquals(1, cleanup_call_count);
|
|
|
|
|
2019-01-30 12:06:32 +00:00
|
|
|
// Assert that the cleanup function won't be called unless new targets appear.
|
2018-10-18 11:30:16 +00:00
|
|
|
setTimeout(timeout_func_2, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(timeout_func_1, 0);
|
|
|
|
|
|
|
|
let timeout_func_2 = function() {
|
|
|
|
assertEquals(1, cleanup_call_count);
|
|
|
|
|
2019-01-30 12:06:32 +00:00
|
|
|
// Create a new object and register it.
|
2019-07-30 10:10:18 +00:00
|
|
|
(function() {
|
|
|
|
let obj = {};
|
|
|
|
let wc = fg.register(obj, 100);
|
|
|
|
obj = null;
|
|
|
|
})();
|
2018-10-18 11:30:16 +00:00
|
|
|
|
2019-07-30 10:10:18 +00:00
|
|
|
// This GC will reclaim the targets.
|
2018-10-18 11:30:16 +00:00
|
|
|
gc();
|
2019-07-30 10:10:18 +00:00
|
|
|
assertEquals(1, cleanup_call_count);
|
2018-10-18 11:30:16 +00:00
|
|
|
|
|
|
|
setTimeout(timeout_func_3, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
let timeout_func_3 = function() {
|
|
|
|
assertEquals(2, cleanup_call_count);
|
|
|
|
}
|