5 <title>Test WeakRef works when target is in different compartment in the browser
</title>
6 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
7 <script type=
"application/javascript">
9 SimpleTest.waitForExplicitFinish();
11 let Cu = SpecialPowers.Cu;
12 let isSameCompartment = Cu.getJSTestingFunctions().isSameCompartment;
14 // Open a new window, which will be from different compartment.
15 let win = window.open();
16 is(isSameCompartment(win, window), false,
17 "Test for opeing a window from a different compartment.");
23 // WeakRef and target are both from different compartment.
24 wr1 = new win.WeakRef(new win.Object());
26 // WeakRef is same compartment, but target isn't.
27 wr2 = new WeakRef(new win.Object());
29 // WeakRef is in different compartment, but target is.
30 wr3 = new win.WeakRef(obj);
35 // WeakRef should keep the target in the current task.
36 isnot(wr1.deref(), undefined,
"wr1.deref() should return its target.");
37 isnot(wr2.deref(), undefined,
"wr2.deref() should return its target.");
38 isnot(wr3.deref(), undefined,
"we3.deref() should return its target.");
40 // Weakref should keep the target until the end of current Job, that
41 // includes microtask(Promise).
42 Promise.resolve().then(() =
> {
43 isnot(wr1.deref(), undefined,
44 "wr1.deref() should return its target in promise");
45 isnot(wr2.deref(), undefined,
46 "wr2.deref() should return its target in promise");
47 isnot(wr3.deref(), undefined,
48 "wr3.deref() should return its target in promise");
51 // setTimeout will launch a new job and call ClearKeptObjects().
53 // Call gc() forcibly to clear the target of wr.
54 SpecialPowers.DOMWindowUtils.garbageCollect();
56 is(wr1.deref(), undefined,
"wr1.deref() should return undefined in the new job.");
57 is(wr2.deref(), undefined,
"wr2.deref() should return undefined in the new job.");
58 is(wr3.deref(), undefined,
"wr3.deref() should return undefined in the new job.");
67 <body onload=
"go()"></body>