| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 |
| 55 static int NumberOfWeakCalls = 0; | 61 static int NumberOfWeakCalls = 0; |
| 56 static void WeakPointerCallback( | 62 static void WeakPointerCallback( |
| 57 const v8::WeakCallbackData<v8::Value, void>& data) { | 63 const v8::WeakCallbackData<v8::Value, void>& data) { |
| 58 std::pair<v8::Persistent<v8::Value>*, int>* p = | 64 std::pair<v8::Persistent<v8::Value>*, int>* p = |
| 59 reinterpret_cast<std::pair<v8::Persistent<v8::Value>*, int>*>( | 65 reinterpret_cast<std::pair<v8::Persistent<v8::Value>*, int>*>( |
| 60 data.GetParameter()); | 66 data.GetParameter()); |
| 61 DCHECK_EQ(1234, p->second); | 67 DCHECK_EQ(1234, p->second); |
| 62 NumberOfWeakCalls++; | 68 NumberOfWeakCalls++; |
| 63 p->first->Reset(); | 69 p->first->Reset(); |
| 64 } | 70 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 82 Handle<JSObject> object = factory->NewJSObjectFromMap(map); | 88 Handle<JSObject> object = factory->NewJSObjectFromMap(map); |
| 83 key = global_handles->Create(*object); | 89 key = global_handles->Create(*object); |
| 84 } | 90 } |
| 85 CHECK(!global_handles->IsWeak(key.location())); | 91 CHECK(!global_handles->IsWeak(key.location())); |
| 86 | 92 |
| 87 // Put two chained entries into weak map. | 93 // Put two chained entries into weak map. |
| 88 { | 94 { |
| 89 HandleScope scope(isolate); | 95 HandleScope scope(isolate); |
| 90 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 96 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 91 Handle<JSObject> object = factory->NewJSObjectFromMap(map); | 97 Handle<JSObject> object = factory->NewJSObjectFromMap(map); |
| 92 Handle<Smi> smi(Smi::FromInt(23), isolate); | 98 PutIntoWeakMap(weakmap, Handle<JSObject>(JSObject::cast(*key)), object); |
| 93 Runtime::WeakCollectionSet(weakmap, key, object); | 99 PutIntoWeakMap(weakmap, object, Handle<Smi>(Smi::FromInt(23), isolate)); |
| 94 Runtime::WeakCollectionSet(weakmap, object, smi); | |
| 95 } | 100 } |
| 96 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 101 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 97 | 102 |
| 98 // Force a full GC. | 103 // Force a full GC. |
| 99 heap->CollectAllGarbage(false); | 104 heap->CollectAllGarbage(false); |
| 100 CHECK_EQ(0, NumberOfWeakCalls); | 105 CHECK_EQ(0, NumberOfWeakCalls); |
| 101 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 106 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 102 CHECK_EQ( | 107 CHECK_EQ( |
| 103 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 108 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
| 104 | 109 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 | 143 |
| 139 // Check initial capacity. | 144 // Check initial capacity. |
| 140 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); | 145 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); |
| 141 | 146 |
| 142 // Fill up weak map to trigger capacity change. | 147 // Fill up weak map to trigger capacity change. |
| 143 { | 148 { |
| 144 HandleScope scope(isolate); | 149 HandleScope scope(isolate); |
| 145 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 150 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 146 for (int i = 0; i < 32; i++) { | 151 for (int i = 0; i < 32; i++) { |
| 147 Handle<JSObject> object = factory->NewJSObjectFromMap(map); | 152 Handle<JSObject> object = factory->NewJSObjectFromMap(map); |
| 148 Handle<Smi> smi(Smi::FromInt(i), isolate); | 153 PutIntoWeakMap(weakmap, object, Handle<Smi>(Smi::FromInt(i), isolate)); |
| 149 Runtime::WeakCollectionSet(weakmap, object, smi); | |
| 150 } | 154 } |
| 151 } | 155 } |
| 152 | 156 |
| 153 // Check increased capacity. | 157 // Check increased capacity. |
| 154 CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity()); | 158 CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity()); |
| 155 | 159 |
| 156 // Force a full GC. | 160 // Force a full GC. |
| 157 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 161 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 158 CHECK_EQ( | 162 CHECK_EQ( |
| 159 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 163 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 186 Page* first_page = heap->old_pointer_space()->anchor()->next_page(); | 190 Page* first_page = heap->old_pointer_space()->anchor()->next_page(); |
| 187 factory->NewFixedArray(900 * KB / kPointerSize, TENURED); | 191 factory->NewFixedArray(900 * KB / kPointerSize, TENURED); |
| 188 | 192 |
| 189 // Fill up weak map with values on an evacuation candidate. | 193 // Fill up weak map with values on an evacuation candidate. |
| 190 { | 194 { |
| 191 HandleScope scope(isolate); | 195 HandleScope scope(isolate); |
| 192 for (int i = 0; i < 32; i++) { | 196 for (int i = 0; i < 32; i++) { |
| 193 Handle<JSObject> object = factory->NewJSObject(function, TENURED); | 197 Handle<JSObject> object = factory->NewJSObject(function, TENURED); |
| 194 CHECK(!heap->InNewSpace(object->address())); | 198 CHECK(!heap->InNewSpace(object->address())); |
| 195 CHECK(!first_page->Contains(object->address())); | 199 CHECK(!first_page->Contains(object->address())); |
| 196 Runtime::WeakCollectionSet(weakmap, key, object); | 200 PutIntoWeakMap(weakmap, key, object); |
| 197 } | 201 } |
| 198 } | 202 } |
| 199 | 203 |
| 200 // Force compacting garbage collection. | 204 // Force compacting garbage collection. |
| 201 CHECK(FLAG_always_compact); | 205 CHECK(FLAG_always_compact); |
| 202 heap->CollectAllGarbage(Heap::kNoGCFlags); | 206 heap->CollectAllGarbage(Heap::kNoGCFlags); |
| 203 } | 207 } |
| 204 | 208 |
| 205 | 209 |
| 206 // Test that weak map keys on an evacuation candidate which are reachable by | 210 // Test that weak map keys on an evacuation candidate which are reachable by |
| (...skipping 19 matching lines...) Expand all Loading... |
| 226 | 230 |
| 227 // Fill up weak map with keys on an evacuation candidate. | 231 // Fill up weak map with keys on an evacuation candidate. |
| 228 Handle<JSObject> keys[32]; | 232 Handle<JSObject> keys[32]; |
| 229 for (int i = 0; i < 32; i++) { | 233 for (int i = 0; i < 32; i++) { |
| 230 keys[i] = factory->NewJSObject(function, TENURED); | 234 keys[i] = factory->NewJSObject(function, TENURED); |
| 231 CHECK(!heap->InNewSpace(keys[i]->address())); | 235 CHECK(!heap->InNewSpace(keys[i]->address())); |
| 232 CHECK(!first_page->Contains(keys[i]->address())); | 236 CHECK(!first_page->Contains(keys[i]->address())); |
| 233 } | 237 } |
| 234 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); | 238 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); |
| 235 for (int i = 0; i < 32; i++) { | 239 for (int i = 0; i < 32; i++) { |
| 236 Handle<Smi> smi(Smi::FromInt(i), isolate); | 240 PutIntoWeakMap(weakmap, |
| 237 Runtime::WeakCollectionSet(weakmap, keys[i], smi); | 241 keys[i], |
| 242 Handle<Smi>(Smi::FromInt(i), isolate)); |
| 238 } | 243 } |
| 239 | 244 |
| 240 // Force compacting garbage collection. The subsequent collections are used | 245 // Force compacting garbage collection. The subsequent collections are used |
| 241 // to verify that key references were actually updated. | 246 // to verify that key references were actually updated. |
| 242 CHECK(FLAG_always_compact); | 247 CHECK(FLAG_always_compact); |
| 243 heap->CollectAllGarbage(Heap::kNoGCFlags); | 248 heap->CollectAllGarbage(Heap::kNoGCFlags); |
| 244 heap->CollectAllGarbage(Heap::kNoGCFlags); | 249 heap->CollectAllGarbage(Heap::kNoGCFlags); |
| 245 heap->CollectAllGarbage(Heap::kNoGCFlags); | 250 heap->CollectAllGarbage(Heap::kNoGCFlags); |
| 246 } | 251 } |
| 247 | 252 |
| 248 | 253 |
| 249 TEST(Regress399527) { | 254 TEST(Regress399527) { |
| 250 CcTest::InitializeVM(); | 255 CcTest::InitializeVM(); |
| 251 v8::HandleScope scope(CcTest::isolate()); | 256 v8::HandleScope scope(CcTest::isolate()); |
| 252 Isolate* isolate = CcTest::i_isolate(); | 257 Isolate* isolate = CcTest::i_isolate(); |
| 253 Heap* heap = isolate->heap(); | 258 Heap* heap = isolate->heap(); |
| 254 { | 259 { |
| 255 HandleScope scope(isolate); | 260 HandleScope scope(isolate); |
| 256 AllocateJSWeakMap(isolate); | 261 AllocateJSWeakMap(isolate); |
| 257 SimulateIncrementalMarking(heap); | 262 SimulateIncrementalMarking(heap); |
| 258 } | 263 } |
| 259 // The weak map is marked black here but leaving the handle scope will make | 264 // The weak map is marked black here but leaving the handle scope will make |
| 260 // the object unreachable. Aborting incremental marking will clear all the | 265 // the object unreachable. Aborting incremental marking will clear all the |
| 261 // marking bits which makes the weak map garbage. | 266 // marking bits which makes the weak map garbage. |
| 262 heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); | 267 heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
| 263 } | 268 } |
| OLD | NEW |