cleanup-from-different-realm.js 1006 Bytes
Newer Older
1 2 3 4
// 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.

5
// Flags: --harmony-weak-refs --expose-gc --noincremental-marking
6 7 8 9 10 11

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");

12
let fg = new FinalizationGroup(cleanup);
13

14 15
// Create an object and a register it in the FinalizationGroup. The object needs
// to be inside a closure so that we can reliably kill them!
16 17 18 19
let weak_cell;

(function() {
  let object = {};
20
  fg.register(object, {});
21 22 23 24 25 26 27 28 29 30 31 32 33 34

  // 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);