5 <title>Test FinalizationRegistry works in the browser
</title>
6 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
7 <script type=
"application/javascript">
8 let registry1, holdings1;
9 let registry2, holdings2;
10 let registry3, holdings3;
11 let registry4, holdings4;
12 let registry5, holdings5;
13 let registry6, holdings6;
14 let registry7, holdings7;
15 let registry8, holdings8;
16 let registry9, holdings9;
21 SimpleTest.waitForExplicitFinish();
23 // Registry with no registered objects.
25 registry1 = new FinalizationRegistry(v =
> { holdings1.push(v); });
27 // Registry with three registered objects.
29 registry2 = new FinalizationRegistry(v =
> { holdings2.push(v); });
30 registry2.register({},
1);
31 registry2.register({},
2);
32 registry2.register({},
3);
34 // Registry with registered object that is then unregistered.
36 registry3 = new FinalizationRegistry(v =
> { holdings3.push(v); });
38 registry3.register({},
1, token3);
39 registry3.unregister(token3);
41 // Registry with registered object that doesn't die.
43 registry4 = new FinalizationRegistry(v =
> { holdings4.push(v); });
44 registry4.register(object4,
1);
46 // Registry observing cyclic JS data structure.
48 registry5 = new FinalizationRegistry(v =
> { holdings5.push(v); });
49 registry5.register(makeJSCycle(
4),
5);
51 // Registry observing DOM object without preserved wrappers.
53 registry6 = new FinalizationRegistry(v =
> { holdings6.push(v); });
54 registry6.register(document.createElement(
"div"),
6);
56 // Registry observing DOM object with preserved wrappers.
58 registry7 = new FinalizationRegistry(v =
> { holdings7.push(v); });
59 let object = document.createElement(
"div");
60 object.someProperty = true;
61 registry7.register(object,
7);
64 // Registry observing reachable DOM object without preserved wrappers.
66 registry8 = new FinalizationRegistry(v =
> { holdings8.push(v); });
67 document.body.appendChild(document.createElement(
"div"));
68 registry8.register(document.body.lastChild,
8);
70 // Registry observing cyclic DOM/JS data structure.
72 registry9 = new FinalizationRegistry(v =
> { holdings9.push(v); });
73 registry9.register(makeDOMCycle(
4),
9);
75 // Need to run full GC/CC/GC cycle to collect cyclic garbage through DOM
77 SpecialPowers.DOMWindowUtils.garbageCollect();
78 SpecialPowers.DOMWindowUtils.cycleCollect();
79 SpecialPowers.DOMWindowUtils.garbageCollect();
81 // Microtasks are run before cleanup callbacks.
82 Promise.resolve().then(() =
> {
83 is(holdings1.length,
0);
84 is(holdings2.length,
0);
85 is(holdings3.length,
0);
86 is(holdings4.length,
0);
87 is(holdings5.length,
0);
88 is(holdings6.length,
0);
89 is(holdings7.length,
0);
90 is(holdings8.length,
0);
91 is(holdings9.length,
0);
94 // setTimeout queues a task which will run after cleanup callbacks.
99 is(holdings1.length,
0);
101 let result = holdings2.sort((a, b) =
> a - b);
102 is(result.length,
3);
107 is(holdings3.length,
0);
108 is(holdings4.length,
0);
110 is(holdings5.length,
1);
113 is(holdings6.length,
1);
116 is(holdings7.length,
1);
119 is(holdings8.length,
0);
121 is(holdings9.length,
1);
124 document.body.removeChild(document.body.lastChild);
126 SpecialPowers.DOMWindowUtils.garbageCollect();
127 SpecialPowers.DOMWindowUtils.cycleCollect();
128 SpecialPowers.DOMWindowUtils.garbageCollect();
130 setTimeout(task3,
0);
134 is(holdings8.length,
1);
140 function makeJSCycle(size) {
143 for (let i =
0; i < size; i++) {
145 current = current.next;
147 current.next = first;
151 function makeDOMCycle(size) {
154 for (let i =
0; i < size; i++) {
156 current.next = document.createElement(
"div");
160 current = current.next;
162 current.next = first;
167 <body onload=
"go()"></body>