2018-10-29 11:53:52 +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-29 11:53:52 +00:00
|
|
|
|
|
|
|
let cleanup_call_count = 0;
|
2019-01-30 12:06:32 +00:00
|
|
|
let cleanup_holdings_count = 0;
|
2018-10-29 11:53:52 +00:00
|
|
|
let cleanup = function(iter) {
|
2019-01-30 12:06:32 +00:00
|
|
|
for (holdings of iter) {
|
|
|
|
++cleanup_holdings_count;
|
2018-10-29 11:53:52 +00:00
|
|
|
}
|
|
|
|
++cleanup_call_count;
|
|
|
|
}
|
|
|
|
|
2019-01-30 12:06:32 +00:00
|
|
|
let fg1 = new FinalizationGroup(cleanup);
|
|
|
|
let fg2 = new FinalizationGroup(cleanup);
|
2018-10-29 11:53:52 +00:00
|
|
|
|
2019-01-30 12:06:32 +00:00
|
|
|
// Create two objects and register them in FinalizationGroups. The objects need
|
|
|
|
// to be inside a closure so that we can reliably kill them!
|
2018-10-29 11:53:52 +00:00
|
|
|
|
|
|
|
(function() {
|
|
|
|
let object1 = {};
|
2019-01-30 12:06:32 +00:00
|
|
|
fg1.register(object1, "holdings1");
|
2018-10-29 11:53:52 +00:00
|
|
|
|
|
|
|
let object2 = {};
|
2019-01-30 12:06:32 +00:00
|
|
|
fg2.register(object2, "holdings2");
|
2018-10-29 11:53:52 +00:00
|
|
|
|
|
|
|
// object1 and object2 go out of scope.
|
|
|
|
})();
|
|
|
|
|
|
|
|
// This GC will discover dirty WeakCells and schedule cleanup.
|
|
|
|
gc();
|
|
|
|
assertEquals(0, cleanup_call_count);
|
|
|
|
|
2019-01-30 12:06:32 +00:00
|
|
|
// Assert that the cleanup function was called and iterated the holdings.
|
2018-10-29 11:53:52 +00:00
|
|
|
let timeout_func = function() {
|
|
|
|
assertEquals(2, cleanup_call_count);
|
2019-01-30 12:06:32 +00:00
|
|
|
assertEquals(2, cleanup_holdings_count);
|
2018-10-29 11:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(timeout_func, 0);
|