summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/mochitest/test_finalizationRegistry.html
blob: 9d9a798cdfb9069b48bb9af984dae0d9f2a35faa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>Test FinalizationRegistry works in the browser</title>
    <script src="/tests/SimpleTest/SimpleTest.js"></script>
    <script type="application/javascript">
      let registry1, holdings1;
      let registry2, holdings2;
      let registry3, holdings3;
      let registry4, holdings4;
      let registry5, holdings5;
      let registry6, holdings6;
      let registry7, holdings7;
      let registry8, holdings8;
      let registry9, holdings9;

      let object4 = {};

      function go() {
        SimpleTest.waitForExplicitFinish();

        // Registry with no registered objects.
        holdings1 = [];
        registry1 = new FinalizationRegistry(v => { holdings1.push(v); });

        // Registry with three registered objects.
        holdings2 = [];
        registry2 = new FinalizationRegistry(v => { holdings2.push(v); });
        registry2.register({}, 1);
        registry2.register({}, 2);
        registry2.register({}, 3);

        // Registry with registered object that is then unregistered.
        holdings3 = [];
        registry3 = new FinalizationRegistry(v => { holdings3.push(v); });
        let token3 = {}
        registry3.register({}, 1, token3);
        registry3.unregister(token3);

        // Registry with registered object that doesn't die.
        holdings4 = [];
        registry4 = new FinalizationRegistry(v => { holdings4.push(v); });
        registry4.register(object4, 1);

        // Registry observing cyclic JS data structure.
        holdings5 = [];
        registry5 = new FinalizationRegistry(v => { holdings5.push(v); });
        registry5.register(makeJSCycle(4), 5);

        // Registry observing DOM object without preserved wrappers.
        holdings6 = [];
        registry6 = new FinalizationRegistry(v => { holdings6.push(v); });
        registry6.register(document.createElement("div"), 6);

        // Registry observing DOM object with preserved wrappers.
        holdings7 = [];
        registry7 = new FinalizationRegistry(v => { holdings7.push(v); });
        let object = document.createElement("div");
        object.someProperty = true;
        registry7.register(object, 7);
        object = null;

        // Registry observing reachable DOM object without preserved wrappers.
        holdings8 = [];
        registry8 = new FinalizationRegistry(v => { holdings8.push(v); });
        document.body.appendChild(document.createElement("div"));
        registry8.register(document.body.lastChild, 8);

        // Registry observing cyclic DOM/JS data structure.
        holdings9 = [];
        registry9 = new FinalizationRegistry(v => { holdings9.push(v); });
        registry9.register(makeDOMCycle(4), 9);

        // Need to run full GC/CC/GC cycle to collect cyclic garbage through DOM
        // and JS heaps.
        SpecialPowers.DOMWindowUtils.garbageCollect();
        SpecialPowers.DOMWindowUtils.cycleCollect();
        SpecialPowers.DOMWindowUtils.garbageCollect();

        // Microtasks are run before cleanup callbacks.
        Promise.resolve().then(() => {
          is(holdings1.length, 0);
          is(holdings2.length, 0);
          is(holdings3.length, 0);
          is(holdings4.length, 0);
          is(holdings5.length, 0);
          is(holdings6.length, 0);
          is(holdings7.length, 0);
          is(holdings8.length, 0);
          is(holdings9.length, 0);
        });

        // setTimeout queues a task which will run after cleanup callbacks.
        setTimeout(task2, 0);
      }

      function task2() {
        is(holdings1.length, 0);

        let result = holdings2.sort((a, b) => a - b);
        is(result.length, 3);
        is(result[0], 1);
        is(result[1], 2);
        is(result[2], 3);

        is(holdings3.length, 0);
        is(holdings4.length, 0);

        is(holdings5.length, 1);
        is(holdings5[0], 5);

        is(holdings6.length, 1);
        is(holdings6[0], 6);

        is(holdings7.length, 1);
        is(holdings7[0], 7);

        is(holdings8.length, 0);

        is(holdings9.length, 1);
        is(holdings9[0], 9);

        document.body.removeChild(document.body.lastChild);

        SpecialPowers.DOMWindowUtils.garbageCollect();
        SpecialPowers.DOMWindowUtils.cycleCollect();
        SpecialPowers.DOMWindowUtils.garbageCollect();

        setTimeout(task3, 0);
      }

      function task3()  {
        is(holdings8.length, 1);
        is(holdings8[0], 8);

        SimpleTest.finish();
      }

      function makeJSCycle(size) {
        let first = {};
        let current = first;
        for (let i = 0; i < size; i++) {
          current.next = {};
          current = current.next;
        }
        current.next = first;
        return first;
      }

      function makeDOMCycle(size) {
        let first = {};
        let current = first;
        for (let i = 0; i < size; i++) {
          if (i % 2 === 0) {
            current.next = document.createElement("div");
          } else {
            current.next = {};
          }
          current = current.next;
        }
        current.next = first;
        return first;
      }
    </script>
  </head>
  <body onload="go()"></body>
</html>