2018-10-19 12:58:25 +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 --allow-natives-syntax
|
2018-10-19 12:58:25 +00:00
|
|
|
|
|
|
|
// This test asserts that the cleanup function call, scheduled by GC, is a
|
|
|
|
// microtask and not a normal task.
|
|
|
|
|
|
|
|
// Inside a microtask, cause GC (which should schedule the cleanup as
|
|
|
|
// microtask). lso schedule another microtask. Assert that the cleanup
|
|
|
|
// function ran before the other microtask.
|
|
|
|
|
2019-07-30 10:10:18 +00:00
|
|
|
let cleanedUp = false;
|
|
|
|
|
2018-10-19 12:58:25 +00:00
|
|
|
function scheduleMicrotask(func) {
|
|
|
|
Promise.resolve().then(func);
|
|
|
|
}
|
|
|
|
|
|
|
|
let log = [];
|
|
|
|
|
2020-04-07 22:07:13 +00:00
|
|
|
let cleanup = (holdings) => {
|
2019-07-30 10:10:18 +00:00
|
|
|
cleanedUp = true;
|
2018-10-19 12:58:25 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 01:19:48 +00:00
|
|
|
let fg = new FinalizationRegistry(cleanup);
|
2018-10-19 12:58:25 +00:00
|
|
|
let o = null;
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
// Use a closure here to avoid other references to o which might keep it alive
|
|
|
|
// (e.g., stack frames pointing to it).
|
|
|
|
o = {};
|
2019-01-30 12:06:32 +00:00
|
|
|
fg.register(o, {});
|
2018-10-19 12:58:25 +00:00
|
|
|
})();
|
|
|
|
|
2019-07-30 10:10:18 +00:00
|
|
|
let microtask = function() {
|
2018-10-19 12:58:25 +00:00
|
|
|
log.push("first_microtask");
|
|
|
|
|
2019-07-30 10:10:18 +00:00
|
|
|
// cause GC during a microtask
|
2018-10-19 12:58:25 +00:00
|
|
|
o = null;
|
|
|
|
gc();
|
|
|
|
}
|
|
|
|
|
2019-07-30 10:10:18 +00:00
|
|
|
assertFalse(cleanedUp);
|
|
|
|
|
|
|
|
// enqueue microtask that triggers GC
|
|
|
|
Promise.resolve().then(microtask);
|
|
|
|
|
|
|
|
// but cleanup callback hasn't been called yet, as we're still in
|
|
|
|
// synchronous execution
|
|
|
|
assertFalse(cleanedUp);
|
|
|
|
|
|
|
|
// flush the microtask queue to run the microtask that triggers GC
|
|
|
|
%PerformMicrotaskCheckpoint();
|
|
|
|
|
|
|
|
// still no cleanup callback, because it runs after as a separate task
|
|
|
|
assertFalse(cleanedUp);
|
2018-10-19 12:58:25 +00:00
|
|
|
|
|
|
|
setTimeout(() => {
|
2019-07-30 10:10:18 +00:00
|
|
|
assertTrue(cleanedUp);
|
2018-10-19 12:58:25 +00:00
|
|
|
}, 0);
|