OLD | NEW |
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 24 matching lines...) Expand all Loading... |
35 | 35 |
36 using namespace v8::internal; | 36 using namespace v8::internal; |
37 | 37 |
38 | 38 |
39 static Isolate* GetIsolateFrom(LocalContext* context) { | 39 static Isolate* GetIsolateFrom(LocalContext* context) { |
40 return reinterpret_cast<Isolate*>((*context)->GetIsolate()); | 40 return reinterpret_cast<Isolate*>((*context)->GetIsolate()); |
41 } | 41 } |
42 | 42 |
43 | 43 |
44 static Handle<JSWeakMap> AllocateJSWeakMap(Isolate* isolate) { | 44 static Handle<JSWeakMap> AllocateJSWeakMap(Isolate* isolate) { |
45 Factory* factory = isolate->factory(); | 45 Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap(); |
46 Handle<Map> map = factory->NewMap(JS_WEAK_MAP_TYPE, JSWeakMap::kSize); | |
47 Handle<JSObject> weakmap_obj = factory->NewJSObjectFromMap(map); | |
48 Handle<JSWeakMap> weakmap(JSWeakMap::cast(*weakmap_obj)); | |
49 // 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. |
50 { | 47 { |
51 HandleScope scope(isolate); | 48 HandleScope scope(isolate); |
52 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 1); | 49 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 1); |
53 weakmap->set_table(*table); | 50 weakmap->set_table(*table); |
54 } | 51 } |
55 return weakmap; | 52 return weakmap; |
56 } | 53 } |
57 | 54 |
58 static void PutIntoWeakMap(Handle<JSWeakMap> weakmap, | |
59 Handle<JSObject> key, | |
60 Handle<Object> value) { | |
61 Handle<ObjectHashTable> table = ObjectHashTable::Put( | |
62 Handle<ObjectHashTable>(ObjectHashTable::cast(weakmap->table())), | |
63 Handle<JSObject>(JSObject::cast(*key)), | |
64 value); | |
65 weakmap->set_table(*table); | |
66 } | |
67 | |
68 static int NumberOfWeakCalls = 0; | 55 static int NumberOfWeakCalls = 0; |
69 static void WeakPointerCallback( | 56 static void WeakPointerCallback( |
70 const v8::WeakCallbackData<v8::Value, void>& data) { | 57 const v8::WeakCallbackData<v8::Value, void>& data) { |
71 std::pair<v8::Persistent<v8::Value>*, int>* p = | 58 std::pair<v8::Persistent<v8::Value>*, int>* p = |
72 reinterpret_cast<std::pair<v8::Persistent<v8::Value>*, int>*>( | 59 reinterpret_cast<std::pair<v8::Persistent<v8::Value>*, int>*>( |
73 data.GetParameter()); | 60 data.GetParameter()); |
74 DCHECK_EQ(1234, p->second); | 61 DCHECK_EQ(1234, p->second); |
75 NumberOfWeakCalls++; | 62 NumberOfWeakCalls++; |
76 p->first->Reset(); | 63 p->first->Reset(); |
77 } | 64 } |
(...skipping 17 matching lines...) Expand all Loading... |
95 Handle<JSObject> object = factory->NewJSObjectFromMap(map); | 82 Handle<JSObject> object = factory->NewJSObjectFromMap(map); |
96 key = global_handles->Create(*object); | 83 key = global_handles->Create(*object); |
97 } | 84 } |
98 CHECK(!global_handles->IsWeak(key.location())); | 85 CHECK(!global_handles->IsWeak(key.location())); |
99 | 86 |
100 // Put two chained entries into weak map. | 87 // Put two chained entries into weak map. |
101 { | 88 { |
102 HandleScope scope(isolate); | 89 HandleScope scope(isolate); |
103 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 90 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
104 Handle<JSObject> object = factory->NewJSObjectFromMap(map); | 91 Handle<JSObject> object = factory->NewJSObjectFromMap(map); |
105 PutIntoWeakMap(weakmap, Handle<JSObject>(JSObject::cast(*key)), object); | 92 Handle<Smi> smi(Smi::FromInt(23), isolate); |
106 PutIntoWeakMap(weakmap, object, Handle<Smi>(Smi::FromInt(23), isolate)); | 93 Runtime::WeakCollectionSet(weakmap, key, object); |
| 94 Runtime::WeakCollectionSet(weakmap, object, smi); |
107 } | 95 } |
108 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 96 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
109 | 97 |
110 // Force a full GC. | 98 // Force a full GC. |
111 heap->CollectAllGarbage(false); | 99 heap->CollectAllGarbage(false); |
112 CHECK_EQ(0, NumberOfWeakCalls); | 100 CHECK_EQ(0, NumberOfWeakCalls); |
113 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 101 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
114 CHECK_EQ( | 102 CHECK_EQ( |
115 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 103 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
116 | 104 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 | 138 |
151 // Check initial capacity. | 139 // Check initial capacity. |
152 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); | 140 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); |
153 | 141 |
154 // Fill up weak map to trigger capacity change. | 142 // Fill up weak map to trigger capacity change. |
155 { | 143 { |
156 HandleScope scope(isolate); | 144 HandleScope scope(isolate); |
157 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 145 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
158 for (int i = 0; i < 32; i++) { | 146 for (int i = 0; i < 32; i++) { |
159 Handle<JSObject> object = factory->NewJSObjectFromMap(map); | 147 Handle<JSObject> object = factory->NewJSObjectFromMap(map); |
160 PutIntoWeakMap(weakmap, object, Handle<Smi>(Smi::FromInt(i), isolate)); | 148 Handle<Smi> smi(Smi::FromInt(i), isolate); |
| 149 Runtime::WeakCollectionSet(weakmap, object, smi); |
161 } | 150 } |
162 } | 151 } |
163 | 152 |
164 // Check increased capacity. | 153 // Check increased capacity. |
165 CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity()); | 154 CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity()); |
166 | 155 |
167 // Force a full GC. | 156 // Force a full GC. |
168 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 157 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
169 CHECK_EQ( | 158 CHECK_EQ( |
170 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 159 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
(...skipping 26 matching lines...) Expand all Loading... |
197 Page* first_page = heap->old_pointer_space()->anchor()->next_page(); | 186 Page* first_page = heap->old_pointer_space()->anchor()->next_page(); |
198 factory->NewFixedArray(900 * KB / kPointerSize, TENURED); | 187 factory->NewFixedArray(900 * KB / kPointerSize, TENURED); |
199 | 188 |
200 // Fill up weak map with values on an evacuation candidate. | 189 // Fill up weak map with values on an evacuation candidate. |
201 { | 190 { |
202 HandleScope scope(isolate); | 191 HandleScope scope(isolate); |
203 for (int i = 0; i < 32; i++) { | 192 for (int i = 0; i < 32; i++) { |
204 Handle<JSObject> object = factory->NewJSObject(function, TENURED); | 193 Handle<JSObject> object = factory->NewJSObject(function, TENURED); |
205 CHECK(!heap->InNewSpace(object->address())); | 194 CHECK(!heap->InNewSpace(object->address())); |
206 CHECK(!first_page->Contains(object->address())); | 195 CHECK(!first_page->Contains(object->address())); |
207 PutIntoWeakMap(weakmap, key, object); | 196 Runtime::WeakCollectionSet(weakmap, key, object); |
208 } | 197 } |
209 } | 198 } |
210 | 199 |
211 // Force compacting garbage collection. | 200 // Force compacting garbage collection. |
212 CHECK(FLAG_always_compact); | 201 CHECK(FLAG_always_compact); |
213 heap->CollectAllGarbage(Heap::kNoGCFlags); | 202 heap->CollectAllGarbage(Heap::kNoGCFlags); |
214 } | 203 } |
215 | 204 |
216 | 205 |
217 // 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 Loading... |
237 | 226 |
238 // Fill up weak map with keys on an evacuation candidate. | 227 // Fill up weak map with keys on an evacuation candidate. |
239 Handle<JSObject> keys[32]; | 228 Handle<JSObject> keys[32]; |
240 for (int i = 0; i < 32; i++) { | 229 for (int i = 0; i < 32; i++) { |
241 keys[i] = factory->NewJSObject(function, TENURED); | 230 keys[i] = factory->NewJSObject(function, TENURED); |
242 CHECK(!heap->InNewSpace(keys[i]->address())); | 231 CHECK(!heap->InNewSpace(keys[i]->address())); |
243 CHECK(!first_page->Contains(keys[i]->address())); | 232 CHECK(!first_page->Contains(keys[i]->address())); |
244 } | 233 } |
245 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); | 234 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); |
246 for (int i = 0; i < 32; i++) { | 235 for (int i = 0; i < 32; i++) { |
247 PutIntoWeakMap(weakmap, | 236 Handle<Smi> smi(Smi::FromInt(i), isolate); |
248 keys[i], | 237 Runtime::WeakCollectionSet(weakmap, keys[i], smi); |
249 Handle<Smi>(Smi::FromInt(i), isolate)); | |
250 } | 238 } |
251 | 239 |
252 // Force compacting garbage collection. The subsequent collections are used | 240 // Force compacting garbage collection. The subsequent collections are used |
253 // to verify that key references were actually updated. | 241 // to verify that key references were actually updated. |
254 CHECK(FLAG_always_compact); | 242 CHECK(FLAG_always_compact); |
255 heap->CollectAllGarbage(Heap::kNoGCFlags); | 243 heap->CollectAllGarbage(Heap::kNoGCFlags); |
256 heap->CollectAllGarbage(Heap::kNoGCFlags); | 244 heap->CollectAllGarbage(Heap::kNoGCFlags); |
257 heap->CollectAllGarbage(Heap::kNoGCFlags); | 245 heap->CollectAllGarbage(Heap::kNoGCFlags); |
258 } | 246 } |
259 | 247 |
260 | 248 |
261 TEST(Regress399527) { | 249 TEST(Regress399527) { |
262 CcTest::InitializeVM(); | 250 CcTest::InitializeVM(); |
263 v8::HandleScope scope(CcTest::isolate()); | 251 v8::HandleScope scope(CcTest::isolate()); |
264 Isolate* isolate = CcTest::i_isolate(); | 252 Isolate* isolate = CcTest::i_isolate(); |
265 Heap* heap = isolate->heap(); | 253 Heap* heap = isolate->heap(); |
266 { | 254 { |
267 HandleScope scope(isolate); | 255 HandleScope scope(isolate); |
268 AllocateJSWeakMap(isolate); | 256 AllocateJSWeakMap(isolate); |
269 SimulateIncrementalMarking(heap); | 257 SimulateIncrementalMarking(heap); |
270 } | 258 } |
271 // 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 |
272 // the object unreachable. Aborting incremental marking will clear all the | 260 // the object unreachable. Aborting incremental marking will clear all the |
273 // marking bits which makes the weak map garbage. | 261 // marking bits which makes the weak map garbage. |
274 heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); | 262 heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
275 } | 263 } |
OLD | NEW |