v8/test/mjsunit/harmony/weakrefs/cleanup-from-different-realm.js
Shu-yu Guo ff89c6bc6f [weakrefs] Rename FinalizationGroup to FinalizationRegistry for JS
The deprecated legacy FinalizationGroup APIs are left unchanged for
compat.

Bug: v8:8179
Change-Id: I9bdcaa92360db318c96fc8524c04163ece25118e
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2071236
Commit-Queue: Shu-yu Guo <syg@chromium.org>
Reviewed-by: Adam Klein <adamk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66437}
2020-02-25 22:53:20 +00:00

35 lines
1012 B
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 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 a register it in the FinalizationRegistry. The object needs
// to be inside a closure so that we can reliably kill them!
let weak_cell;
(function() {
let object = {};
fg.register(object, {});
// object goes out of scope.
})();
gc();
// 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);