Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: test/cctest/test-weakmaps.cc

Issue 903463002: Simplify WeakMap and WeakSet tests slightly. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/cctest/test-weaksets.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap(); 45 Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
46 // Do not leak handles for the hash table, it would make entries strong. 46 // Do not leak handles for the hash table, it would make entries strong.
47 { 47 {
48 HandleScope scope(isolate); 48 HandleScope scope(isolate);
49 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 1); 49 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 1);
50 weakmap->set_table(*table); 50 weakmap->set_table(*table);
51 } 51 }
52 return weakmap; 52 return weakmap;
53 } 53 }
54 54
55 static void PutIntoWeakMap(Handle<JSWeakMap> weakmap,
56 Handle<JSObject> key,
57 Handle<Object> value) {
58 Runtime::WeakCollectionSet(weakmap, key, value);
59 }
60
61 static int NumberOfWeakCalls = 0; 55 static int NumberOfWeakCalls = 0;
62 static void WeakPointerCallback( 56 static void WeakPointerCallback(
63 const v8::WeakCallbackData<v8::Value, void>& data) { 57 const v8::WeakCallbackData<v8::Value, void>& data) {
64 std::pair<v8::Persistent<v8::Value>*, int>* p = 58 std::pair<v8::Persistent<v8::Value>*, int>* p =
65 reinterpret_cast<std::pair<v8::Persistent<v8::Value>*, int>*>( 59 reinterpret_cast<std::pair<v8::Persistent<v8::Value>*, int>*>(
66 data.GetParameter()); 60 data.GetParameter());
67 DCHECK_EQ(1234, p->second); 61 DCHECK_EQ(1234, p->second);
68 NumberOfWeakCalls++; 62 NumberOfWeakCalls++;
69 p->first->Reset(); 63 p->first->Reset();
70 } 64 }
(...skipping 17 matching lines...) Expand all
88 Handle<JSObject> object = factory->NewJSObjectFromMap(map); 82 Handle<JSObject> object = factory->NewJSObjectFromMap(map);
89 key = global_handles->Create(*object); 83 key = global_handles->Create(*object);
90 } 84 }
91 CHECK(!global_handles->IsWeak(key.location())); 85 CHECK(!global_handles->IsWeak(key.location()));
92 86
93 // Put two chained entries into weak map. 87 // Put two chained entries into weak map.
94 { 88 {
95 HandleScope scope(isolate); 89 HandleScope scope(isolate);
96 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); 90 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
97 Handle<JSObject> object = factory->NewJSObjectFromMap(map); 91 Handle<JSObject> object = factory->NewJSObjectFromMap(map);
98 PutIntoWeakMap(weakmap, Handle<JSObject>(JSObject::cast(*key)), object); 92 Handle<Smi> smi(Smi::FromInt(23), isolate);
99 PutIntoWeakMap(weakmap, object, Handle<Smi>(Smi::FromInt(23), isolate)); 93 Runtime::WeakCollectionSet(weakmap, key, object);
94 Runtime::WeakCollectionSet(weakmap, object, smi);
100 } 95 }
101 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); 96 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
102 97
103 // Force a full GC. 98 // Force a full GC.
104 heap->CollectAllGarbage(false); 99 heap->CollectAllGarbage(false);
105 CHECK_EQ(0, NumberOfWeakCalls); 100 CHECK_EQ(0, NumberOfWeakCalls);
106 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); 101 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
107 CHECK_EQ( 102 CHECK_EQ(
108 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); 103 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
109 104
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 138
144 // Check initial capacity. 139 // Check initial capacity.
145 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); 140 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity());
146 141
147 // Fill up weak map to trigger capacity change. 142 // Fill up weak map to trigger capacity change.
148 { 143 {
149 HandleScope scope(isolate); 144 HandleScope scope(isolate);
150 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); 145 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
151 for (int i = 0; i < 32; i++) { 146 for (int i = 0; i < 32; i++) {
152 Handle<JSObject> object = factory->NewJSObjectFromMap(map); 147 Handle<JSObject> object = factory->NewJSObjectFromMap(map);
153 PutIntoWeakMap(weakmap, object, Handle<Smi>(Smi::FromInt(i), isolate)); 148 Handle<Smi> smi(Smi::FromInt(i), isolate);
149 Runtime::WeakCollectionSet(weakmap, object, smi);
154 } 150 }
155 } 151 }
156 152
157 // Check increased capacity. 153 // Check increased capacity.
158 CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity()); 154 CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity());
159 155
160 // Force a full GC. 156 // Force a full GC.
161 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); 157 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
162 CHECK_EQ( 158 CHECK_EQ(
163 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); 159 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
(...skipping 26 matching lines...) Expand all
190 Page* first_page = heap->old_pointer_space()->anchor()->next_page(); 186 Page* first_page = heap->old_pointer_space()->anchor()->next_page();
191 factory->NewFixedArray(900 * KB / kPointerSize, TENURED); 187 factory->NewFixedArray(900 * KB / kPointerSize, TENURED);
192 188
193 // Fill up weak map with values on an evacuation candidate. 189 // Fill up weak map with values on an evacuation candidate.
194 { 190 {
195 HandleScope scope(isolate); 191 HandleScope scope(isolate);
196 for (int i = 0; i < 32; i++) { 192 for (int i = 0; i < 32; i++) {
197 Handle<JSObject> object = factory->NewJSObject(function, TENURED); 193 Handle<JSObject> object = factory->NewJSObject(function, TENURED);
198 CHECK(!heap->InNewSpace(object->address())); 194 CHECK(!heap->InNewSpace(object->address()));
199 CHECK(!first_page->Contains(object->address())); 195 CHECK(!first_page->Contains(object->address()));
200 PutIntoWeakMap(weakmap, key, object); 196 Runtime::WeakCollectionSet(weakmap, key, object);
201 } 197 }
202 } 198 }
203 199
204 // Force compacting garbage collection. 200 // Force compacting garbage collection.
205 CHECK(FLAG_always_compact); 201 CHECK(FLAG_always_compact);
206 heap->CollectAllGarbage(Heap::kNoGCFlags); 202 heap->CollectAllGarbage(Heap::kNoGCFlags);
207 } 203 }
208 204
209 205
210 // Test that weak map keys on an evacuation candidate which are reachable by 206 // Test that weak map keys on an evacuation candidate which are reachable by
(...skipping 19 matching lines...) Expand all
230 226
231 // Fill up weak map with keys on an evacuation candidate. 227 // Fill up weak map with keys on an evacuation candidate.
232 Handle<JSObject> keys[32]; 228 Handle<JSObject> keys[32];
233 for (int i = 0; i < 32; i++) { 229 for (int i = 0; i < 32; i++) {
234 keys[i] = factory->NewJSObject(function, TENURED); 230 keys[i] = factory->NewJSObject(function, TENURED);
235 CHECK(!heap->InNewSpace(keys[i]->address())); 231 CHECK(!heap->InNewSpace(keys[i]->address()));
236 CHECK(!first_page->Contains(keys[i]->address())); 232 CHECK(!first_page->Contains(keys[i]->address()));
237 } 233 }
238 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); 234 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate);
239 for (int i = 0; i < 32; i++) { 235 for (int i = 0; i < 32; i++) {
240 PutIntoWeakMap(weakmap, 236 Handle<Smi> smi(Smi::FromInt(i), isolate);
241 keys[i], 237 Runtime::WeakCollectionSet(weakmap, keys[i], smi);
242 Handle<Smi>(Smi::FromInt(i), isolate));
243 } 238 }
244 239
245 // Force compacting garbage collection. The subsequent collections are used 240 // Force compacting garbage collection. The subsequent collections are used
246 // to verify that key references were actually updated. 241 // to verify that key references were actually updated.
247 CHECK(FLAG_always_compact); 242 CHECK(FLAG_always_compact);
248 heap->CollectAllGarbage(Heap::kNoGCFlags); 243 heap->CollectAllGarbage(Heap::kNoGCFlags);
249 heap->CollectAllGarbage(Heap::kNoGCFlags); 244 heap->CollectAllGarbage(Heap::kNoGCFlags);
250 heap->CollectAllGarbage(Heap::kNoGCFlags); 245 heap->CollectAllGarbage(Heap::kNoGCFlags);
251 } 246 }
252 247
253 248
254 TEST(Regress399527) { 249 TEST(Regress399527) {
255 CcTest::InitializeVM(); 250 CcTest::InitializeVM();
256 v8::HandleScope scope(CcTest::isolate()); 251 v8::HandleScope scope(CcTest::isolate());
257 Isolate* isolate = CcTest::i_isolate(); 252 Isolate* isolate = CcTest::i_isolate();
258 Heap* heap = isolate->heap(); 253 Heap* heap = isolate->heap();
259 { 254 {
260 HandleScope scope(isolate); 255 HandleScope scope(isolate);
261 AllocateJSWeakMap(isolate); 256 AllocateJSWeakMap(isolate);
262 SimulateIncrementalMarking(heap); 257 SimulateIncrementalMarking(heap);
263 } 258 }
264 // The weak map is marked black here but leaving the handle scope will make 259 // The weak map is marked black here but leaving the handle scope will make
265 // the object unreachable. Aborting incremental marking will clear all the 260 // the object unreachable. Aborting incremental marking will clear all the
266 // marking bits which makes the weak map garbage. 261 // marking bits which makes the weak map garbage.
267 heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); 262 heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask);
268 } 263 }
OLDNEW
« no previous file with comments | « no previous file | test/cctest/test-weaksets.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698