PR-URL: https://github.com/nodejs/node/pull/47251 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Richard Lau <rlau@redhat.com>
40 lines
1.3 KiB
JavaScript
40 lines
1.3 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
|
|
|
|
(async function () {
|
|
|
|
let r = Realm.create();
|
|
|
|
let cleanup = Realm.eval(r, "var stored_global; function cleanup() { stored_global = globalThis; } cleanup");
|
|
let realm_global_this = Realm.eval(r, "globalThis");
|
|
|
|
let fg = new FinalizationRegistry(cleanup);
|
|
|
|
// Create an object and register it in the FinalizationRegistry. The object needs
|
|
// to be inside a closure so that we can reliably kill them!
|
|
(function () {
|
|
let object = {};
|
|
fg.register(object, {});
|
|
// Object goes out of scope.
|
|
})();
|
|
|
|
// We need to invoke GC asynchronously and wait for it to finish, so that
|
|
// it doesn't need to scan the stack. Otherwise, the objects may not be
|
|
// reclaimed because of conservative stack scanning and the test may not
|
|
// work as intended.
|
|
await gc({ type: 'major', execution: 'async' });
|
|
|
|
// Assert that the cleanup function was called in its Realm.
|
|
let timeout_func = function () {
|
|
let stored_global = Realm.eval(r, "stored_global;");
|
|
assertNotEquals(stored_global, globalThis);
|
|
assertEquals(stored_global, realm_global_this);
|
|
}
|
|
|
|
setTimeout(timeout_func, 0);
|
|
|
|
})();
|