blob: cd3c490370b3160857ab22cc577b35778b870697 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
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 <stdlib.h>
29
30#include "v8.h"
31
32#include "global-handles.h"
33#include "snapshot.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "cctest.h"
35
36using namespace v8::internal;
37
38static v8::Persistent<v8::Context> env;
39
40static void InitializeVM() {
41 if (env.IsEmpty()) env = v8::Context::New();
42 v8::HandleScope scope;
43 env->Enter();
44}
45
46
Ben Murdoch592a9fc2012-03-05 11:04:45 +000047TEST(MarkingDeque) {
Steve Blocka7e24c12009-10-30 11:49:00 +000048 int mem_size = 20 * kPointerSize;
49 byte* mem = NewArray<byte>(20*kPointerSize);
50 Address low = reinterpret_cast<Address>(mem);
51 Address high = low + mem_size;
Ben Murdoch592a9fc2012-03-05 11:04:45 +000052 MarkingDeque s;
Steve Blocka7e24c12009-10-30 11:49:00 +000053 s.Initialize(low, high);
54
55 Address address = NULL;
Ben Murdoch592a9fc2012-03-05 11:04:45 +000056 while (!s.IsFull()) {
57 s.PushBlack(HeapObject::FromAddress(address));
Steve Blocka7e24c12009-10-30 11:49:00 +000058 address += kPointerSize;
59 }
60
Ben Murdoch592a9fc2012-03-05 11:04:45 +000061 while (!s.IsEmpty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +000062 Address value = s.Pop()->address();
63 address -= kPointerSize;
64 CHECK_EQ(address, value);
65 }
66
67 CHECK_EQ(NULL, address);
68 DeleteArray(mem);
69}
70
71
72TEST(Promotion) {
Ben Murdochb0fe1622011-05-05 13:52:32 +010073 // This test requires compaction. If compaction is turned off, we
74 // skip the entire test.
75 if (FLAG_never_compact) return;
76
Steve Blocka7e24c12009-10-30 11:49:00 +000077 // Ensure that we get a compacting collection so that objects are promoted
78 // from new space.
79 FLAG_gc_global = true;
80 FLAG_always_compact = true;
Ben Murdoch592a9fc2012-03-05 11:04:45 +000081 HEAP->ConfigureHeap(2*256*KB, 8*MB, 8*MB);
Steve Blocka7e24c12009-10-30 11:49:00 +000082
83 InitializeVM();
84
85 v8::HandleScope sc;
86
87 // Allocate a fixed array in the new space.
88 int array_size =
Ben Murdoch592a9fc2012-03-05 11:04:45 +000089 (Page::kMaxNonCodeHeapObjectSize - FixedArray::kHeaderSize) /
Steve Blocka7e24c12009-10-30 11:49:00 +000090 (kPointerSize * 4);
Steve Block44f0eee2011-05-26 01:26:41 +010091 Object* obj = HEAP->AllocateFixedArray(array_size)->ToObjectChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +000092
93 Handle<FixedArray> array(FixedArray::cast(obj));
94
95 // Array should be in the new space.
Steve Block44f0eee2011-05-26 01:26:41 +010096 CHECK(HEAP->InSpace(*array, NEW_SPACE));
Steve Blocka7e24c12009-10-30 11:49:00 +000097
98 // Call the m-c collector, so array becomes an old object.
Steve Block44f0eee2011-05-26 01:26:41 +010099 HEAP->CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000100
101 // Array now sits in the old space
Steve Block44f0eee2011-05-26 01:26:41 +0100102 CHECK(HEAP->InSpace(*array, OLD_POINTER_SPACE));
Steve Blocka7e24c12009-10-30 11:49:00 +0000103}
104
105
106TEST(NoPromotion) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000107 HEAP->ConfigureHeap(2*256*KB, 8*MB, 8*MB);
Steve Blocka7e24c12009-10-30 11:49:00 +0000108
109 // Test the situation that some objects in new space are promoted to
110 // the old space
111 InitializeVM();
112
113 v8::HandleScope sc;
114
115 // Do a mark compact GC to shrink the heap.
Steve Block44f0eee2011-05-26 01:26:41 +0100116 HEAP->CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000117
118 // Allocate a big Fixed array in the new space.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000119 int max_size =
120 Min(Page::kMaxNonCodeHeapObjectSize, HEAP->MaxObjectSizeInNewSpace());
121
122 int length = (max_size - FixedArray::kHeaderSize) / (2*kPointerSize);
123 Object* obj = i::Isolate::Current()->heap()->AllocateFixedArray(length)->
124 ToObjectChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +0000125
126 Handle<FixedArray> array(FixedArray::cast(obj));
127
128 // Array still stays in the new space.
Steve Block44f0eee2011-05-26 01:26:41 +0100129 CHECK(HEAP->InSpace(*array, NEW_SPACE));
Steve Blocka7e24c12009-10-30 11:49:00 +0000130
131 // Allocate objects in the old space until out of memory.
132 FixedArray* host = *array;
133 while (true) {
John Reck59135872010-11-02 12:39:01 -0700134 Object* obj;
Steve Block44f0eee2011-05-26 01:26:41 +0100135 { MaybeObject* maybe_obj = HEAP->AllocateFixedArray(100, TENURED);
John Reck59135872010-11-02 12:39:01 -0700136 if (!maybe_obj->ToObject(&obj)) break;
137 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000138
139 host->set(0, obj);
140 host = FixedArray::cast(obj);
141 }
142
143 // Call mark compact GC, and it should pass.
Steve Block44f0eee2011-05-26 01:26:41 +0100144 HEAP->CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000145
146 // array should not be promoted because the old space is full.
Steve Block44f0eee2011-05-26 01:26:41 +0100147 CHECK(HEAP->InSpace(*array, NEW_SPACE));
Steve Blocka7e24c12009-10-30 11:49:00 +0000148}
149
150
151TEST(MarkCompactCollector) {
152 InitializeVM();
153
154 v8::HandleScope sc;
155 // call mark-compact when heap is empty
Steve Block44f0eee2011-05-26 01:26:41 +0100156 HEAP->CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000157
158 // keep allocating garbage in new space until it fails
159 const int ARRAY_SIZE = 100;
160 Object* array;
John Reck59135872010-11-02 12:39:01 -0700161 MaybeObject* maybe_array;
Steve Blocka7e24c12009-10-30 11:49:00 +0000162 do {
Steve Block44f0eee2011-05-26 01:26:41 +0100163 maybe_array = HEAP->AllocateFixedArray(ARRAY_SIZE);
John Reck59135872010-11-02 12:39:01 -0700164 } while (maybe_array->ToObject(&array));
Steve Block44f0eee2011-05-26 01:26:41 +0100165 HEAP->CollectGarbage(NEW_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000166
Steve Block44f0eee2011-05-26 01:26:41 +0100167 array = HEAP->AllocateFixedArray(ARRAY_SIZE)->ToObjectChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +0000168
169 // keep allocating maps until it fails
170 Object* mapp;
John Reck59135872010-11-02 12:39:01 -0700171 MaybeObject* maybe_mapp;
Steve Blocka7e24c12009-10-30 11:49:00 +0000172 do {
Steve Block44f0eee2011-05-26 01:26:41 +0100173 maybe_mapp = HEAP->AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
John Reck59135872010-11-02 12:39:01 -0700174 } while (maybe_mapp->ToObject(&mapp));
Steve Block44f0eee2011-05-26 01:26:41 +0100175 HEAP->CollectGarbage(MAP_SPACE);
176 mapp = HEAP->AllocateMap(JS_OBJECT_TYPE,
John Reck59135872010-11-02 12:39:01 -0700177 JSObject::kHeaderSize)->ToObjectChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +0000178
179 // allocate a garbage
John Reck59135872010-11-02 12:39:01 -0700180 String* func_name =
Steve Block44f0eee2011-05-26 01:26:41 +0100181 String::cast(HEAP->LookupAsciiSymbol("theFunction")->ToObjectChecked());
John Reck59135872010-11-02 12:39:01 -0700182 SharedFunctionInfo* function_share = SharedFunctionInfo::cast(
Steve Block44f0eee2011-05-26 01:26:41 +0100183 HEAP->AllocateSharedFunctionInfo(func_name)->ToObjectChecked());
John Reck59135872010-11-02 12:39:01 -0700184 JSFunction* function = JSFunction::cast(
Steve Block44f0eee2011-05-26 01:26:41 +0100185 HEAP->AllocateFunction(*Isolate::Current()->function_map(),
John Reck59135872010-11-02 12:39:01 -0700186 function_share,
Steve Block44f0eee2011-05-26 01:26:41 +0100187 HEAP->undefined_value())->ToObjectChecked());
Steve Blocka7e24c12009-10-30 11:49:00 +0000188 Map* initial_map =
Steve Block44f0eee2011-05-26 01:26:41 +0100189 Map::cast(HEAP->AllocateMap(JS_OBJECT_TYPE,
John Reck59135872010-11-02 12:39:01 -0700190 JSObject::kHeaderSize)->ToObjectChecked());
Steve Blocka7e24c12009-10-30 11:49:00 +0000191 function->set_initial_map(initial_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100192 Isolate::Current()->context()->global()->SetProperty(
193 func_name, function, NONE, kNonStrictMode)->ToObjectChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +0000194
Steve Block44f0eee2011-05-26 01:26:41 +0100195 JSObject* obj = JSObject::cast(
196 HEAP->AllocateJSObject(function)->ToObjectChecked());
197 HEAP->CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000198
John Reck59135872010-11-02 12:39:01 -0700199 func_name =
Steve Block44f0eee2011-05-26 01:26:41 +0100200 String::cast(HEAP->LookupAsciiSymbol("theFunction")->ToObjectChecked());
201 CHECK(Isolate::Current()->context()->global()->HasLocalProperty(func_name));
202 Object* func_value = Isolate::Current()->context()->global()->
203 GetProperty(func_name)->ToObjectChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +0000204 CHECK(func_value->IsJSFunction());
205 function = JSFunction::cast(func_value);
206
Steve Block44f0eee2011-05-26 01:26:41 +0100207 obj = JSObject::cast(HEAP->AllocateJSObject(function)->ToObjectChecked());
John Reck59135872010-11-02 12:39:01 -0700208 String* obj_name =
Steve Block44f0eee2011-05-26 01:26:41 +0100209 String::cast(HEAP->LookupAsciiSymbol("theObject")->ToObjectChecked());
210 Isolate::Current()->context()->global()->SetProperty(
211 obj_name, obj, NONE, kNonStrictMode)->ToObjectChecked();
John Reck59135872010-11-02 12:39:01 -0700212 String* prop_name =
Steve Block44f0eee2011-05-26 01:26:41 +0100213 String::cast(HEAP->LookupAsciiSymbol("theSlot")->ToObjectChecked());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100214 obj->SetProperty(prop_name,
215 Smi::FromInt(23),
216 NONE,
217 kNonStrictMode)->ToObjectChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +0000218
Steve Block44f0eee2011-05-26 01:26:41 +0100219 HEAP->CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000220
John Reck59135872010-11-02 12:39:01 -0700221 obj_name =
Steve Block44f0eee2011-05-26 01:26:41 +0100222 String::cast(HEAP->LookupAsciiSymbol("theObject")->ToObjectChecked());
223 CHECK(Isolate::Current()->context()->global()->HasLocalProperty(obj_name));
224 CHECK(Isolate::Current()->context()->global()->
225 GetProperty(obj_name)->ToObjectChecked()->IsJSObject());
226 obj = JSObject::cast(Isolate::Current()->context()->global()->
227 GetProperty(obj_name)->ToObjectChecked());
John Reck59135872010-11-02 12:39:01 -0700228 prop_name =
Steve Block44f0eee2011-05-26 01:26:41 +0100229 String::cast(HEAP->LookupAsciiSymbol("theSlot")->ToObjectChecked());
230 CHECK(obj->GetProperty(prop_name) == Smi::FromInt(23));
Steve Blocka7e24c12009-10-30 11:49:00 +0000231}
232
233
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000234// TODO(1600): compaction of map space is temporary removed from GC.
235#if 0
Leon Clarked91b9f72010-01-27 17:25:45 +0000236static Handle<Map> CreateMap() {
Steve Block44f0eee2011-05-26 01:26:41 +0100237 return FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
Leon Clarked91b9f72010-01-27 17:25:45 +0000238}
239
240
241TEST(MapCompact) {
242 FLAG_max_map_space_pages = 16;
243 InitializeVM();
244
245 {
246 v8::HandleScope sc;
247 // keep allocating maps while pointers are still encodable and thus
248 // mark compact is permitted.
Steve Block44f0eee2011-05-26 01:26:41 +0100249 Handle<JSObject> root = FACTORY->NewJSObjectFromMap(CreateMap());
Leon Clarked91b9f72010-01-27 17:25:45 +0000250 do {
251 Handle<Map> map = CreateMap();
252 map->set_prototype(*root);
Steve Block44f0eee2011-05-26 01:26:41 +0100253 root = FACTORY->NewJSObjectFromMap(map);
254 } while (HEAP->map_space()->MapPointersEncodable());
Leon Clarked91b9f72010-01-27 17:25:45 +0000255 }
256 // Now, as we don't have any handles to just allocated maps, we should
257 // be able to trigger map compaction.
258 // To give an additional chance to fail, try to force compaction which
259 // should be impossible right now.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000260 HEAP->CollectAllGarbage(Heap::kForceCompactionMask);
Leon Clarked91b9f72010-01-27 17:25:45 +0000261 // And now map pointers should be encodable again.
Steve Block44f0eee2011-05-26 01:26:41 +0100262 CHECK(HEAP->map_space()->MapPointersEncodable());
Leon Clarked91b9f72010-01-27 17:25:45 +0000263}
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000264#endif
Leon Clarked91b9f72010-01-27 17:25:45 +0000265
Steve Blocka7e24c12009-10-30 11:49:00 +0000266static int gc_starts = 0;
267static int gc_ends = 0;
268
269static void GCPrologueCallbackFunc() {
270 CHECK(gc_starts == gc_ends);
271 gc_starts++;
272}
273
274
275static void GCEpilogueCallbackFunc() {
276 CHECK(gc_starts == gc_ends + 1);
277 gc_ends++;
278}
279
280
281TEST(GCCallback) {
282 InitializeVM();
283
Steve Block44f0eee2011-05-26 01:26:41 +0100284 HEAP->SetGlobalGCPrologueCallback(&GCPrologueCallbackFunc);
285 HEAP->SetGlobalGCEpilogueCallback(&GCEpilogueCallbackFunc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000286
287 // Scavenge does not call GC callback functions.
Steve Block44f0eee2011-05-26 01:26:41 +0100288 HEAP->PerformScavenge();
Steve Blocka7e24c12009-10-30 11:49:00 +0000289
290 CHECK_EQ(0, gc_starts);
291 CHECK_EQ(gc_ends, gc_starts);
292
Steve Block44f0eee2011-05-26 01:26:41 +0100293 HEAP->CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000294 CHECK_EQ(1, gc_starts);
295 CHECK_EQ(gc_ends, gc_starts);
296}
297
298
299static int NumberOfWeakCalls = 0;
300static void WeakPointerCallback(v8::Persistent<v8::Value> handle, void* id) {
Steve Block44f0eee2011-05-26 01:26:41 +0100301 ASSERT(id == reinterpret_cast<void*>(1234));
Steve Blocka7e24c12009-10-30 11:49:00 +0000302 NumberOfWeakCalls++;
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100303 handle.Dispose();
Steve Blocka7e24c12009-10-30 11:49:00 +0000304}
305
306TEST(ObjectGroups) {
307 InitializeVM();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100308 GlobalHandles* global_handles = Isolate::Current()->global_handles();
Steve Blocka7e24c12009-10-30 11:49:00 +0000309
310 NumberOfWeakCalls = 0;
311 v8::HandleScope handle_scope;
312
313 Handle<Object> g1s1 =
Steve Block44f0eee2011-05-26 01:26:41 +0100314 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
Steve Blocka7e24c12009-10-30 11:49:00 +0000315 Handle<Object> g1s2 =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100316 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
Steve Block44f0eee2011-05-26 01:26:41 +0100317 Handle<Object> g1c1 =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100318 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
Steve Block44f0eee2011-05-26 01:26:41 +0100319 global_handles->MakeWeak(g1s1.location(),
320 reinterpret_cast<void*>(1234),
321 &WeakPointerCallback);
322 global_handles->MakeWeak(g1s2.location(),
323 reinterpret_cast<void*>(1234),
324 &WeakPointerCallback);
325 global_handles->MakeWeak(g1c1.location(),
326 reinterpret_cast<void*>(1234),
327 &WeakPointerCallback);
Steve Blocka7e24c12009-10-30 11:49:00 +0000328
329 Handle<Object> g2s1 =
Steve Block44f0eee2011-05-26 01:26:41 +0100330 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
Steve Blocka7e24c12009-10-30 11:49:00 +0000331 Handle<Object> g2s2 =
Steve Block44f0eee2011-05-26 01:26:41 +0100332 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
333 Handle<Object> g2c1 =
334 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
335 global_handles->MakeWeak(g2s1.location(),
336 reinterpret_cast<void*>(1234),
337 &WeakPointerCallback);
338 global_handles->MakeWeak(g2s2.location(),
339 reinterpret_cast<void*>(1234),
340 &WeakPointerCallback);
341 global_handles->MakeWeak(g2c1.location(),
342 reinterpret_cast<void*>(1234),
343 &WeakPointerCallback);
Steve Blocka7e24c12009-10-30 11:49:00 +0000344
Steve Block44f0eee2011-05-26 01:26:41 +0100345 Handle<Object> root = global_handles->Create(*g1s1); // make a root.
Steve Blocka7e24c12009-10-30 11:49:00 +0000346
347 // Connect group 1 and 2, make a cycle.
348 Handle<FixedArray>::cast(g1s2)->set(0, *g2s2);
349 Handle<FixedArray>::cast(g2s1)->set(0, *g1s1);
350
351 {
352 Object** g1_objects[] = { g1s1.location(), g1s2.location() };
Steve Block44f0eee2011-05-26 01:26:41 +0100353 Object** g1_children[] = { g1c1.location() };
Steve Blocka7e24c12009-10-30 11:49:00 +0000354 Object** g2_objects[] = { g2s1.location(), g2s2.location() };
Steve Block44f0eee2011-05-26 01:26:41 +0100355 Object** g2_children[] = { g2c1.location() };
356 global_handles->AddObjectGroup(g1_objects, 2, NULL);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100357 global_handles->AddImplicitReferences(
358 Handle<HeapObject>::cast(g1s1).location(), g1_children, 1);
Steve Block44f0eee2011-05-26 01:26:41 +0100359 global_handles->AddObjectGroup(g2_objects, 2, NULL);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100360 global_handles->AddImplicitReferences(
361 Handle<HeapObject>::cast(g2s2).location(), g2_children, 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000362 }
363 // Do a full GC
Steve Block44f0eee2011-05-26 01:26:41 +0100364 HEAP->CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000365
366 // All object should be alive.
367 CHECK_EQ(0, NumberOfWeakCalls);
368
369 // Weaken the root.
Steve Block44f0eee2011-05-26 01:26:41 +0100370 global_handles->MakeWeak(root.location(),
371 reinterpret_cast<void*>(1234),
372 &WeakPointerCallback);
373 // But make children strong roots---all the objects (except for children)
374 // should be collectable now.
375 global_handles->ClearWeakness(g1c1.location());
376 global_handles->ClearWeakness(g2c1.location());
Steve Blocka7e24c12009-10-30 11:49:00 +0000377
378 // Groups are deleted, rebuild groups.
379 {
380 Object** g1_objects[] = { g1s1.location(), g1s2.location() };
Steve Block44f0eee2011-05-26 01:26:41 +0100381 Object** g1_children[] = { g1c1.location() };
Steve Blocka7e24c12009-10-30 11:49:00 +0000382 Object** g2_objects[] = { g2s1.location(), g2s2.location() };
Steve Block44f0eee2011-05-26 01:26:41 +0100383 Object** g2_children[] = { g2c1.location() };
384 global_handles->AddObjectGroup(g1_objects, 2, NULL);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100385 global_handles->AddImplicitReferences(
386 Handle<HeapObject>::cast(g1s1).location(), g1_children, 1);
Steve Block44f0eee2011-05-26 01:26:41 +0100387 global_handles->AddObjectGroup(g2_objects, 2, NULL);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100388 global_handles->AddImplicitReferences(
389 Handle<HeapObject>::cast(g2s2).location(), g2_children, 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000390 }
391
Steve Block44f0eee2011-05-26 01:26:41 +0100392 HEAP->CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000393
394 // All objects should be gone. 5 global handles in total.
395 CHECK_EQ(5, NumberOfWeakCalls);
Steve Block44f0eee2011-05-26 01:26:41 +0100396
397 // And now make children weak again and collect them.
398 global_handles->MakeWeak(g1c1.location(),
399 reinterpret_cast<void*>(1234),
400 &WeakPointerCallback);
401 global_handles->MakeWeak(g2c1.location(),
402 reinterpret_cast<void*>(1234),
403 &WeakPointerCallback);
404
405 HEAP->CollectGarbage(OLD_POINTER_SPACE);
406 CHECK_EQ(7, NumberOfWeakCalls);
Steve Blocka7e24c12009-10-30 11:49:00 +0000407}
Ben Murdoch8b112d22011-06-08 16:22:53 +0100408
409
410class TestRetainedObjectInfo : public v8::RetainedObjectInfo {
411 public:
412 TestRetainedObjectInfo() : has_been_disposed_(false) {}
413
414 bool has_been_disposed() { return has_been_disposed_; }
415
416 virtual void Dispose() {
417 ASSERT(!has_been_disposed_);
418 has_been_disposed_ = true;
419 }
420
421 virtual bool IsEquivalent(v8::RetainedObjectInfo* other) {
422 return other == this;
423 }
424
425 virtual intptr_t GetHash() { return 0; }
426
427 virtual const char* GetLabel() { return "whatever"; }
428
429 private:
430 bool has_been_disposed_;
431};
432
433
434TEST(EmptyObjectGroups) {
435 InitializeVM();
436 GlobalHandles* global_handles = Isolate::Current()->global_handles();
437
438 v8::HandleScope handle_scope;
439
440 Handle<Object> object =
441 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
442
443 TestRetainedObjectInfo info;
444 global_handles->AddObjectGroup(NULL, 0, &info);
445 ASSERT(info.has_been_disposed());
446
447 global_handles->AddImplicitReferences(
448 Handle<HeapObject>::cast(object).location(), NULL, 0);
449}