Debian Codegraph @

js/src/tests/non262/extensions/collect-gray.js

raw
1 // |reftest| skip-if(!xulRuntime.shell)
2 // Any copyright is dedicated to the Public Domain.
3 // http://creativecommons.org/licenses/publicdomain/
4
5 //-----------------------------------------------------------------------------
6 var BUGNUMBER = 1337209;
7 var summary =
8 "Test gray marking";
9
10 print(BUGNUMBER + ": " + summary);
11
12 if (typeof gczeal !== 'undefined')
13 gczeal(0);
14
15 grayRoot().x = Object.create(null);
16 addMarkObservers([grayRoot(), grayRoot().x, this, Object.create(null)]);
17 gc();
18 let marks = getMarks();
19 assertEq(marks[0], 'gray', 'gray root');
20 assertEq(marks[1], 'gray', 'object reachable from gray root');
21 assertEq(marks[2], 'black', 'global');
22 assertEq(marks[3], 'dead', 'dead object should have been collected');
23
24 grayRoot().x = 7; // Overwrite the object
25 gc();
26 marks = getMarks();
27 assertEq(marks[0], 'gray', 'gray root');
28 assertEq(marks[1], 'dead', 'object no longer reachable from gray root');
29 assertEq(marks[2], 'black', 'global');
30 assertEq(marks[3], 'dead', 'dead object should have been collected');
31
32 var wm = new WeakMap();
33 var global = newGlobal({newCompartment: true});
34
35 var wrapper1 = global.eval("Object.create(null)");
36 wrapper1.name = "wrapper1";
37 var value1 = Object.create(null);
38 wm.set(wrapper1, value1);
39
40 var wrapper2 = global.eval("Object.create(null)");
41 wrapper2.name = "wrapper2";
42 var value2 = global.eval("Object.create(null)");
43 wm.set(wrapper2, value2);
44
45 grayRoot().root1 = wrapper1;
46 grayRoot().root2 = wrapper2;
47 clearMarkObservers();
48 addMarkObservers([wrapper1, value1, wrapper2, value2]);
49 wrapper1 = wrapper2 = null;
50 value1 = value2 = null;
51 gc();
52 marks = getMarks();
53 assertEq(marks[0], 'gray', 'gray key 1');
54 assertEq(marks[1], 'gray', 'black map, gray key => gray value');
55 assertEq(marks[2], 'gray', 'gray key 2');
56 assertEq(marks[3], 'gray', 'black map, gray key => gray value');
57
58 // Blacken one of the keys
59 wrapper1 = grayRoot().root1;
60 gc();
61 marks = getMarks();
62 assertEq(marks[0], 'black', 'black key 1');
63 assertEq(marks[1], 'black', 'black map, black key => black value');
64 assertEq(marks[2], 'gray', 'gray key 2');
65 assertEq(marks[3], 'gray', 'black map, gray key => gray value');
66
67 // Test edges from map&delegate => key and map&key => value.
68 //
69 // In general, when a&b => x, then if both a and b are black, then x must be
70 // black. If either is gray and the other is marked (gray or black), then x
71 // must be gray (unless otherwise reachable from black.) If neither a nor b is
72 // marked at all, then they will not keep x alive.
73
74 clearMarkObservers();
75
76 // Black map, gray delegate => gray key
77
78 // wm is in a variable, so is black.
79 wm = new WeakMap();
80
81 let key = Object.create(null);
82 // delegate unwraps key in the 'global' compartment
83 global.grayRoot().delegate = key;
84
85 // Create a value and map to it from a gray key, then make the value a gray
86 // root.
87 let value = Object.create(null);
88 wm.set(key, value);
89 grayRoot().value = value;
90
91 // We are interested in the mark bits of the map, key, and value, as well as
92 // the mark bits of the wrapped versions in the other compartment. Note that
93 // the other-compartment key is the known as the key's delegate with respect to
94 // the weakmap.
95 global.addMarkObservers([wm, key, value]);
96 addMarkObservers([wm, key, value]);
97
98 // Key is otherwise dead in main compartment.
99 key = null;
100 // Don't want value to be marked black.
101 value = null;
102
103 gc(); // Update mark bits.
104 let [
105 other_map_mark, other_key_mark, other_value_mark,
106 map_mark, key_mark, value_mark
107 ] = getMarks();
108 assertEq(other_map_mark, 'dead', 'nothing points to wm in other compartment');
109 assertEq(other_key_mark, 'gray', 'delegate should be gray');
110 assertEq(other_value_mark, 'dead', 'nothing points to value wrapper in other compartment');
111 assertEq(map_mark, 'black', 'map in var => black');
112 assertEq(key_mark, 'gray', 'black map, gray delegate => gray key');
113 assertEq(value_mark, 'gray', 'black map, gray delegate/key => gray value');
114
115 // Black map, black delegate => black key
116
117 // Blacken the delegate by pointing to it from the other global.
118 global.delegate = global.grayRoot().delegate;
119
120 gc();
121 [
122 other_map_mark, other_key_mark, other_value_mark,
123 map_mark, key_mark, value_mark
124 ] = getMarks();
125 assertEq(other_map_mark, 'dead', 'nothing points to wm in other compartment');
126 assertEq(other_key_mark, 'black', 'delegate held in global.delegate');
127 assertEq(other_value_mark, 'dead', 'nothing points to value wrapper in other compartment');
128 assertEq(map_mark, 'black', 'map in var => black');
129 assertEq(key_mark, 'black', 'black map, black delegate => black key');
130 assertEq(value_mark, 'black', 'black map, black key => black value');
131
132 // Gray map, black delegate => gray key. Unfortunately, there's no way to test
133 // this, because a WeakMap key's delegate is its wrapper, and there is a strong
134 // edge from wrappers to wrappees. The jsapi-test in testGCGrayMarking, inside
135 // TestWeakMaps, *does* test this case.
136
137 grayRoot().map = wm;
138 wm = null;
139
140 gc();
141 [
142 other_map_mark, other_key_mark, other_value_mark,
143 map_mark, key_mark, value_mark
144 ] = getMarks();
145 assertEq(other_map_mark, 'dead', 'nothing points to wm in other compartment');
146 assertEq(other_key_mark, 'black', 'delegate held in global.delegate');
147 assertEq(other_value_mark, 'dead', 'nothing points to value wrapper in other compartment');
148 assertEq(map_mark, 'gray', 'map is a gray root');
149 assertEq(key_mark, 'black', 'black delegate marks its key black');
150 assertEq(value_mark, 'gray', 'gray map, black key => gray value');
151
152 if (typeof reportCompare === "function")
153 reportCompare(true, true);