OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2012 the V8 project authors. All rights reserved. | |
Michael Starzinger
2013/04/12 09:12:32
Should say 2013 here.
marja
2013/04/12 09:33:04
Done.
| |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | |
28 #include "global-handles.h" | |
29 | |
30 #include "cctest.h" | |
31 | |
32 using namespace v8::internal; | |
33 using v8::UniqueId; | |
34 | |
35 static v8::Persistent<v8::Context> env; | |
36 | |
37 static void InitializeVM() { | |
Michael Starzinger
2013/04/12 09:12:32
Could we switch this to use CcTest::InitializeVM a
marja
2013/04/12 09:33:04
Done.
| |
38 if (env.IsEmpty()) env = v8::Context::New(); | |
39 env->Enter(); | |
40 } | |
41 | |
42 static int NumberOfWeakCalls = 0; | |
43 static void WeakPointerCallback(v8::Isolate* isolate, | |
44 v8::Persistent<v8::Value> handle, | |
45 void* id) { | |
46 ASSERT(id == reinterpret_cast<void*>(1234)); | |
47 NumberOfWeakCalls++; | |
48 handle.Dispose(isolate); | |
49 } | |
50 | |
51 static List<Object*> skippable_objects; | |
52 static List<Object*> can_skip_called_objects; | |
53 | |
54 static bool CanSkipCallback(Heap* heap, Object** pointer) { | |
55 can_skip_called_objects.Add(*pointer); | |
56 return skippable_objects.Contains(*pointer); | |
57 } | |
58 | |
59 static void ResetCanSkipData() { | |
60 skippable_objects.Clear(); | |
61 can_skip_called_objects.Clear(); | |
62 } | |
63 | |
64 class TestRetainedObjectInfo : public v8::RetainedObjectInfo { | |
65 public: | |
66 TestRetainedObjectInfo() : has_been_disposed_(false) {} | |
67 | |
68 bool has_been_disposed() { return has_been_disposed_; } | |
69 | |
70 virtual void Dispose() { | |
71 ASSERT(!has_been_disposed_); | |
72 has_been_disposed_ = true; | |
73 } | |
74 | |
75 virtual bool IsEquivalent(v8::RetainedObjectInfo* other) { | |
76 return other == this; | |
77 } | |
78 | |
79 virtual intptr_t GetHash() { return 0; } | |
80 | |
81 virtual const char* GetLabel() { return "whatever"; } | |
82 | |
83 private: | |
84 bool has_been_disposed_; | |
85 }; | |
86 | |
87 class TestObjectVisitor : public ObjectVisitor { | |
88 public: | |
89 virtual void VisitPointers(Object** start, Object** end) { | |
90 for (Object** o = start; o != end; ++o) | |
91 visited.Add(*o); | |
92 } | |
93 | |
94 List<Object*> visited; | |
95 }; | |
96 | |
97 TEST(IterateObjectGroupsOldApi) { | |
98 InitializeVM(); | |
99 GlobalHandles* global_handles = Isolate::Current()->global_handles(); | |
100 | |
101 v8::HandleScope handle_scope(env->GetIsolate()); | |
102 | |
103 Handle<Object> g1s1 = | |
104 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); | |
105 Handle<Object> g1s2 = | |
106 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); | |
107 global_handles->MakeWeak(g1s1.location(), | |
108 reinterpret_cast<void*>(1234), | |
109 NULL, | |
110 &WeakPointerCallback); | |
111 global_handles->MakeWeak(g1s2.location(), | |
112 reinterpret_cast<void*>(1234), | |
113 NULL, | |
114 &WeakPointerCallback); | |
115 | |
116 Handle<Object> g2s1 = | |
117 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); | |
118 Handle<Object> g2s2 = | |
119 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); | |
Michael Starzinger
2013/04/12 09:12:32
nit: Indentation is off.
marja
2013/04/12 09:33:04
Done.
| |
120 global_handles->MakeWeak(g2s1.location(), | |
121 reinterpret_cast<void*>(1234), | |
122 NULL, | |
123 &WeakPointerCallback); | |
124 global_handles->MakeWeak(g2s2.location(), | |
125 reinterpret_cast<void*>(1234), | |
126 NULL, | |
127 &WeakPointerCallback); | |
128 | |
129 TestRetainedObjectInfo info1; | |
130 TestRetainedObjectInfo info2; | |
131 { | |
132 Object** g1_objects[] = { g1s1.location(), g1s2.location() }; | |
133 Object** g2_objects[] = { g2s1.location(), g2s2.location() }; | |
134 | |
135 global_handles->AddObjectGroup(g1_objects, 2, &info1); | |
136 global_handles->AddObjectGroup(g2_objects, 2, &info2); | |
137 } | |
138 | |
139 // Iterate the object groups. First skip all. | |
140 { | |
141 ResetCanSkipData(); | |
142 skippable_objects.Add(*g1s1.location()); | |
143 skippable_objects.Add(*g1s2.location()); | |
144 skippable_objects.Add(*g2s1.location()); | |
145 skippable_objects.Add(*g2s2.location()); | |
146 TestObjectVisitor visitor; | |
147 global_handles->IterateObjectGroups(&visitor, &CanSkipCallback); | |
148 | |
149 // CanSkipCallback was called for all objects. | |
150 ASSERT(can_skip_called_objects.length() == 4); | |
151 ASSERT(can_skip_called_objects.Contains(*g1s1.location())); | |
152 ASSERT(can_skip_called_objects.Contains(*g1s2.location())); | |
153 ASSERT(can_skip_called_objects.Contains(*g2s1.location())); | |
154 ASSERT(can_skip_called_objects.Contains(*g2s2.location())); | |
155 | |
156 // Nothing was visited. | |
157 ASSERT(visitor.visited.length() == 0); | |
158 ASSERT(!info1.has_been_disposed()); | |
159 ASSERT(!info2.has_been_disposed()); | |
160 } | |
161 | |
162 // Iterate again, now only skip the second object group. | |
163 { | |
164 ResetCanSkipData(); | |
165 // The first grough should still be visited, since only one object is | |
166 // skipped. | |
167 skippable_objects.Add(*g1s1.location()); | |
168 skippable_objects.Add(*g2s1.location()); | |
169 skippable_objects.Add(*g2s2.location()); | |
170 TestObjectVisitor visitor; | |
171 global_handles->IterateObjectGroups(&visitor, &CanSkipCallback); | |
172 | |
173 // CanSkipCallback was called for all objects. | |
174 ASSERT(can_skip_called_objects.length() == 3 || | |
175 can_skip_called_objects.length() == 4); | |
176 ASSERT(can_skip_called_objects.Contains(*g1s2.location())); | |
177 ASSERT(can_skip_called_objects.Contains(*g2s1.location())); | |
178 ASSERT(can_skip_called_objects.Contains(*g2s2.location())); | |
179 | |
180 // The first group was visited. | |
181 ASSERT(visitor.visited.length() == 2); | |
182 ASSERT(visitor.visited.Contains(*g1s1.location())); | |
183 ASSERT(visitor.visited.Contains(*g1s2.location())); | |
184 ASSERT(info1.has_been_disposed()); | |
185 ASSERT(!info2.has_been_disposed()); | |
186 } | |
187 | |
188 // Iterate again, don't skip anything. | |
189 { | |
190 ResetCanSkipData(); | |
191 TestObjectVisitor visitor; | |
192 global_handles->IterateObjectGroups(&visitor, &CanSkipCallback); | |
193 | |
194 // CanSkipCallback was called for all objects. | |
195 fprintf(stderr, "can skip len %d\n", can_skip_called_objects.length()); | |
196 ASSERT(can_skip_called_objects.length() == 1); | |
197 ASSERT(can_skip_called_objects.Contains(*g2s1.location()) || | |
198 can_skip_called_objects.Contains(*g2s2.location())); | |
199 | |
200 // The second group was visited. | |
201 ASSERT(visitor.visited.length() == 2); | |
202 ASSERT(visitor.visited.Contains(*g2s1.location())); | |
203 ASSERT(visitor.visited.Contains(*g2s2.location())); | |
204 ASSERT(info2.has_been_disposed()); | |
205 } | |
206 } | |
207 | |
208 TEST(IterateObjectGroups) { | |
209 InitializeVM(); | |
210 GlobalHandles* global_handles = Isolate::Current()->global_handles(); | |
211 | |
212 v8::HandleScope handle_scope(env->GetIsolate()); | |
213 | |
214 Handle<Object> g1s1 = | |
215 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); | |
216 Handle<Object> g1s2 = | |
217 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); | |
218 global_handles->MakeWeak(g1s1.location(), | |
219 reinterpret_cast<void*>(1234), | |
220 NULL, | |
221 &WeakPointerCallback); | |
222 global_handles->MakeWeak(g1s2.location(), | |
223 reinterpret_cast<void*>(1234), | |
224 NULL, | |
225 &WeakPointerCallback); | |
226 | |
227 Handle<Object> g2s1 = | |
228 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); | |
229 Handle<Object> g2s2 = | |
230 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); | |
231 global_handles->MakeWeak(g2s1.location(), | |
232 reinterpret_cast<void*>(1234), | |
233 NULL, | |
234 &WeakPointerCallback); | |
235 global_handles->MakeWeak(g2s2.location(), | |
236 reinterpret_cast<void*>(1234), | |
237 NULL, | |
238 &WeakPointerCallback); | |
239 | |
240 TestRetainedObjectInfo info1; | |
241 TestRetainedObjectInfo info2; | |
242 { | |
243 global_handles->SetObjectGroupId(g2s1.location(), UniqueId(2)); | |
244 global_handles->SetObjectGroupId(g2s2.location(), UniqueId(2)); | |
245 global_handles->SetRetainedObjectInfo(UniqueId(2), &info2); | |
246 global_handles->SetObjectGroupId(g1s1.location(), UniqueId(1)); | |
247 global_handles->SetObjectGroupId(g1s2.location(), UniqueId(1)); | |
248 global_handles->SetRetainedObjectInfo(UniqueId(1), &info1); | |
249 } | |
250 | |
251 // Iterate the object groups. First skip all. | |
252 { | |
253 ResetCanSkipData(); | |
254 skippable_objects.Add(*g1s1.location()); | |
255 skippable_objects.Add(*g1s2.location()); | |
256 skippable_objects.Add(*g2s1.location()); | |
257 skippable_objects.Add(*g2s2.location()); | |
258 TestObjectVisitor visitor; | |
259 global_handles->IterateObjectGroups(&visitor, &CanSkipCallback); | |
260 | |
261 // CanSkipCallback was called for all objects. | |
262 ASSERT(can_skip_called_objects.length() == 4); | |
263 ASSERT(can_skip_called_objects.Contains(*g1s1.location())); | |
264 ASSERT(can_skip_called_objects.Contains(*g1s2.location())); | |
265 ASSERT(can_skip_called_objects.Contains(*g2s1.location())); | |
266 ASSERT(can_skip_called_objects.Contains(*g2s2.location())); | |
267 | |
268 // Nothing was visited. | |
269 ASSERT(visitor.visited.length() == 0); | |
270 ASSERT(!info1.has_been_disposed()); | |
271 ASSERT(!info2.has_been_disposed()); | |
272 } | |
273 | |
274 // Iterate again, now only skip the second object group. | |
275 { | |
276 ResetCanSkipData(); | |
277 // The first grough should still be visited, since only one object is | |
278 // skipped. | |
279 skippable_objects.Add(*g1s1.location()); | |
280 skippable_objects.Add(*g2s1.location()); | |
281 skippable_objects.Add(*g2s2.location()); | |
282 TestObjectVisitor visitor; | |
283 global_handles->IterateObjectGroups(&visitor, &CanSkipCallback); | |
284 | |
285 // CanSkipCallback was called for all objects. | |
286 ASSERT(can_skip_called_objects.length() == 3 || | |
287 can_skip_called_objects.length() == 4); | |
288 ASSERT(can_skip_called_objects.Contains(*g1s2.location())); | |
289 ASSERT(can_skip_called_objects.Contains(*g2s1.location())); | |
290 ASSERT(can_skip_called_objects.Contains(*g2s2.location())); | |
291 | |
292 // The first group was visited. | |
293 ASSERT(visitor.visited.length() == 2); | |
294 ASSERT(visitor.visited.Contains(*g1s1.location())); | |
295 ASSERT(visitor.visited.Contains(*g1s2.location())); | |
296 ASSERT(info1.has_been_disposed()); | |
297 ASSERT(!info2.has_been_disposed()); | |
298 } | |
299 | |
300 // Iterate again, don't skip anything. | |
301 { | |
302 ResetCanSkipData(); | |
303 TestObjectVisitor visitor; | |
304 global_handles->IterateObjectGroups(&visitor, &CanSkipCallback); | |
305 | |
306 // CanSkipCallback was called for all objects. | |
307 fprintf(stderr, "can skip len %d\n", can_skip_called_objects.length()); | |
308 ASSERT(can_skip_called_objects.length() == 1); | |
309 ASSERT(can_skip_called_objects.Contains(*g2s1.location()) || | |
310 can_skip_called_objects.Contains(*g2s2.location())); | |
311 | |
312 // The second group was visited. | |
313 ASSERT(visitor.visited.length() == 2); | |
314 ASSERT(visitor.visited.Contains(*g2s1.location())); | |
315 ASSERT(visitor.visited.Contains(*g2s2.location())); | |
316 ASSERT(info2.has_been_disposed()); | |
317 } | |
318 } | |
OLD | NEW |