| 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 21 matching lines...) Expand all Loading... |
| 32 #include "cctest.h" | 32 #include "cctest.h" |
| 33 | 33 |
| 34 using namespace v8::internal; | 34 using namespace v8::internal; |
| 35 | 35 |
| 36 | 36 |
| 37 static Isolate* GetIsolateFrom(LocalContext* context) { | 37 static Isolate* GetIsolateFrom(LocalContext* context) { |
| 38 return reinterpret_cast<Isolate*>((*context)->GetIsolate()); | 38 return reinterpret_cast<Isolate*>((*context)->GetIsolate()); |
| 39 } | 39 } |
| 40 | 40 |
| 41 | 41 |
| 42 static Handle<JSWeakMap> AllocateJSWeakMap(Isolate* isolate) { | 42 static Handle<JSWeakSet> AllocateJSWeakSet(Isolate* isolate) { |
| 43 Factory* factory = isolate->factory(); | 43 Factory* factory = isolate->factory(); |
| 44 Heap* heap = isolate->heap(); | 44 Heap* heap = isolate->heap(); |
| 45 Handle<Map> map = factory->NewMap(JS_WEAK_MAP_TYPE, JSWeakMap::kSize); | 45 Handle<Map> map = factory->NewMap(JS_WEAK_SET_TYPE, JSWeakSet::kSize); |
| 46 Handle<JSObject> weakmap_obj = factory->NewJSObjectFromMap(map); | 46 Handle<JSObject> weakset_obj = factory->NewJSObjectFromMap(map); |
| 47 Handle<JSWeakMap> weakmap(JSWeakMap::cast(*weakmap_obj)); | 47 Handle<JSWeakSet> weakset(JSWeakSet::cast(*weakset_obj)); |
| 48 // Do not use handles for the hash table, it would make entries strong. | 48 // Do not use handles for the hash table, it would make entries strong. |
| 49 Object* table_obj = ObjectHashTable::Allocate(heap, 1)->ToObjectChecked(); | 49 Object* table_obj = ObjectHashTable::Allocate(heap, 1)->ToObjectChecked(); |
| 50 ObjectHashTable* table = ObjectHashTable::cast(table_obj); | 50 ObjectHashTable* table = ObjectHashTable::cast(table_obj); |
| 51 weakmap->set_table(table); | 51 weakset->set_table(table); |
| 52 weakmap->set_next(Smi::FromInt(0)); | 52 weakset->set_next(Smi::FromInt(0)); |
| 53 return weakmap; | 53 return weakset; |
| 54 } | 54 } |
| 55 | 55 |
| 56 static void PutIntoWeakMap(Handle<JSWeakMap> weakmap, | 56 static void PutIntoWeakSet(Handle<JSWeakSet> weakset, |
| 57 Handle<JSObject> key, | 57 Handle<JSObject> key, |
| 58 Handle<Object> value) { | 58 Handle<Object> value) { |
| 59 Handle<ObjectHashTable> table = PutIntoObjectHashTable( | 59 Handle<ObjectHashTable> table = PutIntoObjectHashTable( |
| 60 Handle<ObjectHashTable>(ObjectHashTable::cast(weakmap->table())), | 60 Handle<ObjectHashTable>(ObjectHashTable::cast(weakset->table())), |
| 61 Handle<JSObject>(JSObject::cast(*key)), | 61 Handle<JSObject>(JSObject::cast(*key)), |
| 62 value); | 62 value); |
| 63 weakmap->set_table(*table); | 63 weakset->set_table(*table); |
| 64 } | 64 } |
| 65 | 65 |
| 66 static int NumberOfWeakCalls = 0; | 66 static int NumberOfWeakCalls = 0; |
| 67 static void WeakPointerCallback(v8::Isolate* isolate, | 67 static void WeakPointerCallback(v8::Isolate* isolate, |
| 68 v8::Persistent<v8::Value>* handle, | 68 v8::Persistent<v8::Value>* handle, |
| 69 void* id) { | 69 void* id) { |
| 70 ASSERT(id == reinterpret_cast<void*>(1234)); | 70 ASSERT(id == reinterpret_cast<void*>(1234)); |
| 71 NumberOfWeakCalls++; | 71 NumberOfWeakCalls++; |
| 72 handle->Dispose(isolate); | 72 handle->Dispose(isolate); |
| 73 } | 73 } |
| 74 | 74 |
| 75 | 75 |
| 76 TEST(Weakness) { | 76 TEST(WeakSet_Weakness) { |
| 77 FLAG_incremental_marking = false; | 77 FLAG_incremental_marking = false; |
| 78 LocalContext context; | 78 LocalContext context; |
| 79 Isolate* isolate = GetIsolateFrom(&context); | 79 Isolate* isolate = GetIsolateFrom(&context); |
| 80 Factory* factory = isolate->factory(); | 80 Factory* factory = isolate->factory(); |
| 81 Heap* heap = isolate->heap(); | 81 Heap* heap = isolate->heap(); |
| 82 HandleScope scope(isolate); | 82 HandleScope scope(isolate); |
| 83 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); | 83 Handle<JSWeakSet> weakset = AllocateJSWeakSet(isolate); |
| 84 GlobalHandles* global_handles = isolate->global_handles(); | 84 GlobalHandles* global_handles = isolate->global_handles(); |
| 85 | 85 |
| 86 // Keep global reference to the key. | 86 // Keep global reference to the key. |
| 87 Handle<Object> key; | 87 Handle<Object> key; |
| 88 { | 88 { |
| 89 HandleScope scope(isolate); | 89 HandleScope scope(isolate); |
| 90 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 90 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 91 Handle<JSObject> object = factory->NewJSObjectFromMap(map); | 91 Handle<JSObject> object = factory->NewJSObjectFromMap(map); |
| 92 key = global_handles->Create(*object); | 92 key = global_handles->Create(*object); |
| 93 } | 93 } |
| 94 CHECK(!global_handles->IsWeak(key.location())); | 94 CHECK(!global_handles->IsWeak(key.location())); |
| 95 | 95 |
| 96 // Put entry into weak map. | 96 // Put entry into weak set. |
| 97 { | 97 { |
| 98 HandleScope scope(isolate); | 98 HandleScope scope(isolate); |
| 99 PutIntoWeakMap(weakmap, | 99 PutIntoWeakSet(weakset, |
| 100 Handle<JSObject>(JSObject::cast(*key)), | 100 Handle<JSObject>(JSObject::cast(*key)), |
| 101 Handle<Smi>(Smi::FromInt(23), isolate)); | 101 Handle<Smi>(Smi::FromInt(23), isolate)); |
| 102 } | 102 } |
| 103 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 103 CHECK_EQ(1, ObjectHashTable::cast(weakset->table())->NumberOfElements()); |
| 104 | 104 |
| 105 // Force a full GC. | 105 // Force a full GC. |
| 106 heap->CollectAllGarbage(false); | 106 heap->CollectAllGarbage(false); |
| 107 CHECK_EQ(0, NumberOfWeakCalls); | 107 CHECK_EQ(0, NumberOfWeakCalls); |
| 108 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 108 CHECK_EQ(1, ObjectHashTable::cast(weakset->table())->NumberOfElements()); |
| 109 CHECK_EQ( | 109 CHECK_EQ( |
| 110 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 110 0, ObjectHashTable::cast(weakset->table())->NumberOfDeletedElements()); |
| 111 | 111 |
| 112 // Make the global reference to the key weak. | 112 // Make the global reference to the key weak. |
| 113 { | 113 { |
| 114 HandleScope scope(isolate); | 114 HandleScope scope(isolate); |
| 115 global_handles->MakeWeak(key.location(), | 115 global_handles->MakeWeak(key.location(), |
| 116 reinterpret_cast<void*>(1234), | 116 reinterpret_cast<void*>(1234), |
| 117 &WeakPointerCallback); | 117 &WeakPointerCallback); |
| 118 } | 118 } |
| 119 CHECK(global_handles->IsWeak(key.location())); | 119 CHECK(global_handles->IsWeak(key.location())); |
| 120 | 120 |
| 121 // Force a full GC. | 121 // Force a full GC. |
| 122 // Perform two consecutive GCs because the first one will only clear | 122 // Perform two consecutive GCs because the first one will only clear |
| 123 // weak references whereas the second one will also clear weak maps. | 123 // weak references whereas the second one will also clear weak sets. |
| 124 heap->CollectAllGarbage(false); | 124 heap->CollectAllGarbage(false); |
| 125 CHECK_EQ(1, NumberOfWeakCalls); | 125 CHECK_EQ(1, NumberOfWeakCalls); |
| 126 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 126 CHECK_EQ(1, ObjectHashTable::cast(weakset->table())->NumberOfElements()); |
| 127 CHECK_EQ( | 127 CHECK_EQ( |
| 128 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 128 0, ObjectHashTable::cast(weakset->table())->NumberOfDeletedElements()); |
| 129 heap->CollectAllGarbage(false); | 129 heap->CollectAllGarbage(false); |
| 130 CHECK_EQ(1, NumberOfWeakCalls); | 130 CHECK_EQ(1, NumberOfWeakCalls); |
| 131 CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 131 CHECK_EQ(0, ObjectHashTable::cast(weakset->table())->NumberOfElements()); |
| 132 CHECK_EQ( | 132 CHECK_EQ( |
| 133 1, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 133 1, ObjectHashTable::cast(weakset->table())->NumberOfDeletedElements()); |
| 134 } | 134 } |
| 135 | 135 |
| 136 | 136 |
| 137 TEST(Shrinking) { | 137 TEST(WeakSet_Shrinking) { |
| 138 LocalContext context; | 138 LocalContext context; |
| 139 Isolate* isolate = GetIsolateFrom(&context); | 139 Isolate* isolate = GetIsolateFrom(&context); |
| 140 Factory* factory = isolate->factory(); | 140 Factory* factory = isolate->factory(); |
| 141 Heap* heap = isolate->heap(); | 141 Heap* heap = isolate->heap(); |
| 142 HandleScope scope(isolate); | 142 HandleScope scope(isolate); |
| 143 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); | 143 Handle<JSWeakSet> weakset = AllocateJSWeakSet(isolate); |
| 144 | 144 |
| 145 // Check initial capacity. | 145 // Check initial capacity. |
| 146 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); | 146 CHECK_EQ(32, ObjectHashTable::cast(weakset->table())->Capacity()); |
| 147 | 147 |
| 148 // Fill up weak map to trigger capacity change. | 148 // Fill up weak set to trigger capacity change. |
| 149 { | 149 { |
| 150 HandleScope scope(isolate); | 150 HandleScope scope(isolate); |
| 151 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 151 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 152 for (int i = 0; i < 32; i++) { | 152 for (int i = 0; i < 32; i++) { |
| 153 Handle<JSObject> object = factory->NewJSObjectFromMap(map); | 153 Handle<JSObject> object = factory->NewJSObjectFromMap(map); |
| 154 PutIntoWeakMap(weakmap, object, Handle<Smi>(Smi::FromInt(i), isolate)); | 154 PutIntoWeakSet(weakset, object, Handle<Smi>(Smi::FromInt(i), isolate)); |
| 155 } | 155 } |
| 156 } | 156 } |
| 157 | 157 |
| 158 // Check increased capacity. | 158 // Check increased capacity. |
| 159 CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity()); | 159 CHECK_EQ(128, ObjectHashTable::cast(weakset->table())->Capacity()); |
| 160 | 160 |
| 161 // Force a full GC. | 161 // Force a full GC. |
| 162 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 162 CHECK_EQ(32, ObjectHashTable::cast(weakset->table())->NumberOfElements()); |
| 163 CHECK_EQ( | 163 CHECK_EQ( |
| 164 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 164 0, ObjectHashTable::cast(weakset->table())->NumberOfDeletedElements()); |
| 165 heap->CollectAllGarbage(false); | 165 heap->CollectAllGarbage(false); |
| 166 CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 166 CHECK_EQ(0, ObjectHashTable::cast(weakset->table())->NumberOfElements()); |
| 167 CHECK_EQ( | 167 CHECK_EQ( |
| 168 32, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 168 32, ObjectHashTable::cast(weakset->table())->NumberOfDeletedElements()); |
| 169 | 169 |
| 170 // Check shrunk capacity. | 170 // Check shrunk capacity. |
| 171 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); | 171 CHECK_EQ(32, ObjectHashTable::cast(weakset->table())->Capacity()); |
| 172 } | 172 } |
| 173 | 173 |
| 174 | 174 |
| 175 // Test that weak map values on an evacuation candidate which are not reachable | 175 // Test that weak set values on an evacuation candidate which are not reachable |
| 176 // by other paths are correctly recorded in the slots buffer. | 176 // by other paths are correctly recorded in the slots buffer. |
| 177 TEST(Regress2060a) { | 177 TEST(WeakSet_Regress2060a) { |
| 178 FLAG_always_compact = true; | 178 FLAG_always_compact = true; |
| 179 LocalContext context; | 179 LocalContext context; |
| 180 Isolate* isolate = GetIsolateFrom(&context); | 180 Isolate* isolate = GetIsolateFrom(&context); |
| 181 Factory* factory = isolate->factory(); | 181 Factory* factory = isolate->factory(); |
| 182 Heap* heap = isolate->heap(); | 182 Heap* heap = isolate->heap(); |
| 183 HandleScope scope(isolate); | 183 HandleScope scope(isolate); |
| 184 Handle<JSFunction> function = | 184 Handle<JSFunction> function = |
| 185 factory->NewFunction(factory->function_string(), factory->null_value()); | 185 factory->NewFunction(factory->function_string(), factory->null_value()); |
| 186 Handle<JSObject> key = factory->NewJSObject(function); | 186 Handle<JSObject> key = factory->NewJSObject(function); |
| 187 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); | 187 Handle<JSWeakSet> weakset = AllocateJSWeakSet(isolate); |
| 188 | 188 |
| 189 // Start second old-space page so that values land on evacuation candidate. | 189 // Start second old-space page so that values land on evacuation candidate. |
| 190 Page* first_page = heap->old_pointer_space()->anchor()->next_page(); | 190 Page* first_page = heap->old_pointer_space()->anchor()->next_page(); |
| 191 factory->NewFixedArray(900 * KB / kPointerSize, TENURED); | 191 factory->NewFixedArray(900 * KB / kPointerSize, TENURED); |
| 192 | 192 |
| 193 // Fill up weak map with values on an evacuation candidate. | 193 // Fill up weak set with values on an evacuation candidate. |
| 194 { | 194 { |
| 195 HandleScope scope(isolate); | 195 HandleScope scope(isolate); |
| 196 for (int i = 0; i < 32; i++) { | 196 for (int i = 0; i < 32; i++) { |
| 197 Handle<JSObject> object = factory->NewJSObject(function, TENURED); | 197 Handle<JSObject> object = factory->NewJSObject(function, TENURED); |
| 198 CHECK(!heap->InNewSpace(object->address())); | 198 CHECK(!heap->InNewSpace(object->address())); |
| 199 CHECK(!first_page->Contains(object->address())); | 199 CHECK(!first_page->Contains(object->address())); |
| 200 PutIntoWeakMap(weakmap, key, object); | 200 PutIntoWeakSet(weakset, key, object); |
| 201 } | 201 } |
| 202 } | 202 } |
| 203 | 203 |
| 204 // Force compacting garbage collection. | 204 // Force compacting garbage collection. |
| 205 CHECK(FLAG_always_compact); | 205 CHECK(FLAG_always_compact); |
| 206 heap->CollectAllGarbage(Heap::kNoGCFlags); | 206 heap->CollectAllGarbage(Heap::kNoGCFlags); |
| 207 } | 207 } |
| 208 | 208 |
| 209 | 209 |
| 210 // Test that weak map keys on an evacuation candidate which are reachable by | 210 // Test that weak set keys on an evacuation candidate which are reachable by |
| 211 // other strong paths are correctly recorded in the slots buffer. | 211 // other strong paths are correctly recorded in the slots buffer. |
| 212 TEST(Regress2060b) { | 212 TEST(WeakSet_Regress2060b) { |
| 213 FLAG_always_compact = true; | 213 FLAG_always_compact = true; |
| 214 #ifdef VERIFY_HEAP | 214 #ifdef VERIFY_HEAP |
| 215 FLAG_verify_heap = true; | 215 FLAG_verify_heap = true; |
| 216 #endif | 216 #endif |
| 217 | 217 |
| 218 LocalContext context; | 218 LocalContext context; |
| 219 Isolate* isolate = GetIsolateFrom(&context); | 219 Isolate* isolate = GetIsolateFrom(&context); |
| 220 Factory* factory = isolate->factory(); | 220 Factory* factory = isolate->factory(); |
| 221 Heap* heap = isolate->heap(); | 221 Heap* heap = isolate->heap(); |
| 222 HandleScope scope(isolate); | 222 HandleScope scope(isolate); |
| 223 Handle<JSFunction> function = | 223 Handle<JSFunction> function = |
| 224 factory->NewFunction(factory->function_string(), factory->null_value()); | 224 factory->NewFunction(factory->function_string(), factory->null_value()); |
| 225 | 225 |
| 226 // Start second old-space page so that keys land on evacuation candidate. | 226 // Start second old-space page so that keys land on evacuation candidate. |
| 227 Page* first_page = heap->old_pointer_space()->anchor()->next_page(); | 227 Page* first_page = heap->old_pointer_space()->anchor()->next_page(); |
| 228 factory->NewFixedArray(900 * KB / kPointerSize, TENURED); | 228 factory->NewFixedArray(900 * KB / kPointerSize, TENURED); |
| 229 | 229 |
| 230 // Fill up weak map with keys on an evacuation candidate. | 230 // Fill up weak set with keys on an evacuation candidate. |
| 231 Handle<JSObject> keys[32]; | 231 Handle<JSObject> keys[32]; |
| 232 for (int i = 0; i < 32; i++) { | 232 for (int i = 0; i < 32; i++) { |
| 233 keys[i] = factory->NewJSObject(function, TENURED); | 233 keys[i] = factory->NewJSObject(function, TENURED); |
| 234 CHECK(!heap->InNewSpace(keys[i]->address())); | 234 CHECK(!heap->InNewSpace(keys[i]->address())); |
| 235 CHECK(!first_page->Contains(keys[i]->address())); | 235 CHECK(!first_page->Contains(keys[i]->address())); |
| 236 } | 236 } |
| 237 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); | 237 Handle<JSWeakSet> weakset = AllocateJSWeakSet(isolate); |
| 238 for (int i = 0; i < 32; i++) { | 238 for (int i = 0; i < 32; i++) { |
| 239 PutIntoWeakMap(weakmap, | 239 PutIntoWeakSet(weakset, |
| 240 keys[i], | 240 keys[i], |
| 241 Handle<Smi>(Smi::FromInt(i), isolate)); | 241 Handle<Smi>(Smi::FromInt(i), isolate)); |
| 242 } | 242 } |
| 243 | 243 |
| 244 // Force compacting garbage collection. The subsequent collections are used | 244 // Force compacting garbage collection. The subsequent collections are used |
| 245 // to verify that key references were actually updated. | 245 // to verify that key references were actually updated. |
| 246 CHECK(FLAG_always_compact); | 246 CHECK(FLAG_always_compact); |
| 247 heap->CollectAllGarbage(Heap::kNoGCFlags); | 247 heap->CollectAllGarbage(Heap::kNoGCFlags); |
| 248 heap->CollectAllGarbage(Heap::kNoGCFlags); | 248 heap->CollectAllGarbage(Heap::kNoGCFlags); |
| 249 heap->CollectAllGarbage(Heap::kNoGCFlags); | 249 heap->CollectAllGarbage(Heap::kNoGCFlags); |
| 250 } | 250 } |
| OLD | NEW |