v8/test/mjsunit/harmony/weakrefs/cleanup-doesnt-iterate-all-holdings.js
Sathya Gunasekaran 743ce7726d [WeakRefs] Make cleanup callback run as a task
Previously, this was run as a microtask and this CL changes it to run
as a separate task as mandated by the current WeakRef spec.

This CL also introduces a FinalizationGroup type to the V8 API
representing the JSFinalizationGroup. This has a `Cleanup`
function that runs the cleanup callback associated with it.

SetHostCleanupFinalizationGroupCallback is added to set
the embedder defined HostCleanupFinalizationGroupCallback.

ClearKeptObject is exposed on the v8::Isolate to reset the strongly
held set of objects.

The general workflow is the following:

(a) When the GC notices that a given finalization group has dirty
    cells, it calls HostCleanupFinalizationGroupCallback with the given
    finalization group.

(b) As part of HostCleanupFinalizationGroupCallback, the embedder
    enqueues a task that at some point later calls
    FinalizationGroup::Cleanup.

(c) At some point in the future, FinalizationGroup::Cleanup is called,
    which runs the cleanup callback of the finalization group.

This patch also includes d8 changes to use these new APIs. Currently,
d8 cycles through the enqueued finalization groups after a synchronous
turn (and it's microtask checkpoint) and runs the cleanup callbacks.

Change-Id: I06eb4da2c103b2792a9c62bc4b98fd4e5c4892fc
Bug: v8:8179
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1655655
Commit-Queue: Sathya Gunasekaran <gsathya@chromium.org>
Reviewed-by: Ulan Degenbaev <ulan@chromium.org>
Reviewed-by: Hannes Payer <hpayer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62984}
2019-07-30 12:19:39 +00:00

94 lines
2.6 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: --harmony-weak-refs --expose-gc --noincremental-marking
let cleanup_call_count = 0;
let cleanup = function(iter) {
print("in cleanup");
if (cleanup_call_count == 0) {
// 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) {
break;
}
}
assertEquals(holdings_list.length, 2);
assertTrue(holdings_list[0] < 3);
assertTrue(holdings_list[1] < 3);
// 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 {
// Second call: iterate one leftover holdings and one holdings.
assertEquals(1, cleanup_call_count);
let holdings_list = [];
for (holdings of iter) {
holdings_list.push(holdings);
}
assertEquals(holdings_list.length, 2);
assertTrue((holdings_list[0] < 3 && holdings_list[1] == 100) ||
(holdings_list[1] < 3 && holdings_list[0] == 100));
// 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++;
}
}
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!
(function() {
let objects = [];
for (let i = 0; i < 3; ++i) {
objects[i] = {a: i};
fg.register(objects[i], i);
}
gc();
assertEquals(0, cleanup_call_count);
// Drop the references to the objects.
objects = [];
})();
// This GC will reclaim the targets.
gc();
assertEquals(0, cleanup_call_count);
let timeout_func_1 = function() {
assertEquals(1, cleanup_call_count);
// Assert that the cleanup function won't be called unless new targets appear.
setTimeout(timeout_func_2, 0);
}
setTimeout(timeout_func_1, 0);
let timeout_func_2 = function() {
assertEquals(1, cleanup_call_count);
// Create a new object and register it.
(function() {
let obj = {};
let wc = fg.register(obj, 100);
obj = null;
})();
// This GC will reclaim the targets.
gc();
assertEquals(1, cleanup_call_count);
setTimeout(timeout_func_3, 0);
}
let timeout_func_3 = function() {
assertEquals(2, cleanup_call_count);
}