Bug 1915045 Ensure decode tasks are scheduled on BufferingState::Enter() r=media...
[gecko.git] / js / xpconnect / tests / mochitest / test_weakmaps.html
blob5e00106feddebdeb9f59045678343ea8919627dc
1 <!DOCTYPE HTML>
2 <html>
3 <!--
4 https://bugzilla.mozilla.org/show_bug.cgi?id=668855
5 -->
6 <head>
7 <meta charset="utf-8">
8 <title>Test Cross-Compartment DOM WeakMaps</title>
9 <script src="/tests/SimpleTest/SimpleTest.js"></script>
10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
11 <script type="application/javascript">
12 /** Test for Bug 668855 **/
14 SimpleTest.waitForExplicitFinish();
16 // We wait to run this until the load event because it needs to access an element.
17 function go() {
19 /* Create a weak reference, with a single-element weak map. */
20 let make_weak_ref = function (obj) {
21 let m = new WeakMap;
22 m.set(obj, {});
23 return m;
26 /* Check to see if a weak reference is dead. */
27 let weak_ref_dead = function (r) {
28 return SpecialPowers.nondeterministicGetWeakMapKeys(r).length == 0;
31 /* Deterministically grab an arbitrary DOM element. */
32 let get_live_dom = function () {
33 let elems = document.getElementsByTagName("a");
34 return elems[0];
38 /* Test case from bug 653248, adapted into a standard test.
40 This is a dead cycle involving a DOM edge, so the cycle collector can free it. Keys and
41 values reachable only from XPConnect must be marked gray for this to work, and the cycle collector
42 must know the proper structure of the heap.
45 let make_gray_loop = function () {
46 let map = new WeakMap;
47 let div = document.createElement("div");
48 let key = {};
49 let obj = {m:map, k:key};
50 div.addEventListener("foo", function() {
51 // The code below doesn't matter (it won't run). Just pull a
52 // reference to obj.
53 obj.k = 1;
54 obj.m = "bar";
55 });
56 //div.entrain = {m:map, k:key}; This is not sufficient to cause a leak in Fx9
57 map.set(key, div);
58 return make_weak_ref(map);
61 let weakref = make_gray_loop();
64 /* Combinations of live and dead gray maps/keys. */
65 let basic_weak_ref = null;
66 let basic_map_weak_ref = null;
67 let black_map = new WeakMap;
68 let black_key = {};
70 let basic_unit_tests = function () {
71 let live_dom = get_live_dom();
72 let dead_dom = document.createElement("div");
73 let live_map = new WeakMap;
74 let dead_map = new WeakMap;
75 let live_key = {};
76 let dead_key = {};
78 // put the live/dead maps/keys into the appropriate DOM elements
79 live_dom.basic_unit_tests = {m:live_map, k:live_key};
81 let obj = {m:dead_map, k:dead_key};
82 // dead_dom.hook = {m:dead_map, k:dead_key};
83 dead_dom.addEventListener("foo", function() {
84 // The code below doesn't matter (it won't run). Just pull a
85 // reference to obj.
86 obj.m = 1;
87 obj.k = "2";
88 });
90 // Create a dead value, and a weak ref to it.
91 // The loop keeps dead_dom alive unless the CC is smart enough to kill it.
92 let dead_val = {loop:dead_dom};
93 basic_weak_ref = make_weak_ref(dead_val);
94 basic_map_weak_ref = make_weak_ref(dead_map);
96 // set up the actual entries. most will die.
97 live_map.set(live_key, {my_key:'live_live'});
98 live_map.set(dead_key, dead_val);
99 live_map.set(black_key, {my_key:'live_black'});
101 dead_map.set(live_key, dead_val);
102 dead_map.set(dead_key, dead_val);
103 dead_map.set(black_key, dead_val);
105 black_map.set(live_key, {my_key:'black_live'});
106 black_map.set(dead_key, dead_val);
107 black_map.set(black_key, {my_key:'black_black'});
111 basic_unit_tests();
114 let check_basic_unit = function () {
115 let live_dom = get_live_dom();
116 let live_map = live_dom.basic_unit_tests.m;
117 let live_key = live_dom.basic_unit_tests.k;
119 // check the dead elements
120 ok(weak_ref_dead(basic_weak_ref), "Dead value was kept alive.");
121 ok(weak_ref_dead(basic_map_weak_ref), "Dead map was kept alive.");
123 // check the live gray map
124 is(live_map.get(live_key).my_key, 'live_live',
125 "Live key should have the same value in live map.");
126 is(live_map.get(black_key).my_key, 'live_black',
127 "Black key should have the same value in live map.");
128 is(SpecialPowers.nondeterministicGetWeakMapKeys(live_map).length, 2,
129 "Live map should have two entries.");
131 // check the live black map
132 is(black_map.get(live_key).my_key, 'black_live',
133 "Live key should have the same value in black map.");
134 is(black_map.get(black_key).my_key, 'black_black',
135 "Black key should have the same value in black map.");
136 is(SpecialPowers.nondeterministicGetWeakMapKeys(black_map).length, 2,
137 "Black map should have two entries.");
142 /* live gray chained weak map entries, involving the cycle collector. */
143 let chainm = new WeakMap;
144 let num_chains = 5;
146 let nested_cc_maps = function () {
147 let dom = get_live_dom();
148 for(let i = 0; i < num_chains; i++) {
149 let k = {count:i};
150 dom.key = k;
151 dom0 = document.createElement("div");
152 chainm.set(k, {d:dom0});
153 dom = document.createElement("div");
154 dom0.appendChild(dom);
158 let check_nested_cc_maps = function () {
159 let dom = get_live_dom();
160 let all_ok = true;
161 for(let i = 0; i < num_chains; i++) {
162 let k = dom.key;
163 all_ok = all_ok && k.count == i;
164 dom = chainm.get(k).d.firstChild;
166 ok(all_ok, "Count was invalid on a key in chained weak map entries.");
169 nested_cc_maps();
172 /* black weak map, chained garbage cycle involving DOM */
173 let garbage_map = new WeakMap;
175 let chained_garbage_maps = function () {
176 let dom0 = document.createElement("div");
177 let dom = dom0;
178 for(let i = 0; i < num_chains; i++) {
179 let k = {};
180 dom.key = k;
181 let new_dom = document.createElement("div");
182 garbage_map.set(k, {val_child:new_dom});
183 dom = document.createElement("div");
184 new_dom.appendChild(dom);
186 // tie the knot
187 dom.appendChild(dom0);
190 chained_garbage_maps();
193 /* black weak map, chained garbage cycle involving DOM, XPCWN keys */
194 let wn_garbage_map = new WeakMap;
196 let wn_chained_garbage_maps = function () {
197 let dom0 = document.createElement("div");
198 let dom = dom0;
199 for(let i = 0; i < num_chains; i++) {
200 let new_dom = document.createElement("div");
201 wn_garbage_map.set(dom, {wn_val_child:new_dom});
202 dom = document.createElement("div");
203 new_dom.appendChild(dom);
205 // tie the knot
206 dom.appendChild(dom0);
209 wn_chained_garbage_maps();
212 /* The cycle collector shouldn't remove a live wrapped native key. */
214 let wn_live_map = new WeakMap;
216 let make_live_map = function () {
217 let live = get_live_dom();
218 wn_live_map.set(live, {});
219 ok(wn_live_map.has(get_live_dom()), "Live map should have live DOM node before GC.");
222 make_live_map();
224 // We're out of ideas for unpreservable natives, now that just about
225 // everything is on webidl, so just don't test those.
227 /* set up for running precise GC/CC then checking the results */
229 SpecialPowers.exactGC(function () {
230 SpecialPowers.forceCC();
231 SpecialPowers.forceGC();
232 SpecialPowers.forceGC();
234 ok(weak_ref_dead(weakref), "Garbage gray cycle should be collected.");
236 check_nested_cc_maps();
238 is(SpecialPowers.nondeterministicGetWeakMapKeys(garbage_map).length, 0, "Chained garbage weak map entries should not leak.");
240 check_basic_unit();
242 // fixed by Bug 680937
243 is(SpecialPowers.nondeterministicGetWeakMapKeys(wn_garbage_map).length, 0,
244 "Chained garbage WN weak map entries should not leak.");
246 // fixed by Bug 680937
247 is(SpecialPowers.nondeterministicGetWeakMapKeys(wn_live_map).length, 1,
248 "Live weak map wrapped native key should not be removed.");
250 ok(wn_live_map.has(get_live_dom()), "Live map should have live dom.");
252 SimpleTest.finish();
256 </script>
257 </head>
258 <div></div>
259 <div id="mydivname"></div>
260 <body onload="go()";>
261 <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=668855" target="_blank">Mozilla Bug 668855</a>
262 <p id="display"></p>
263 </body>
264 </html>