| 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 ObjectHashTable* table = ObjectHashTable::cast(table_obj); | 43 ObjectHashTable* table = ObjectHashTable::cast(table_obj); |
| 44 weakmap->set_table(table); | 44 weakmap->set_table(table); |
| 45 weakmap->set_next(Smi::FromInt(0)); | 45 weakmap->set_next(Smi::FromInt(0)); |
| 46 return weakmap; | 46 return weakmap; |
| 47 } | 47 } |
| 48 | 48 |
| 49 static void PutIntoWeakMap(Handle<JSWeakMap> weakmap, | 49 static void PutIntoWeakMap(Handle<JSWeakMap> weakmap, |
| 50 Handle<JSObject> key, | 50 Handle<JSObject> key, |
| 51 int value) { | 51 int value) { |
| 52 Handle<ObjectHashTable> table = PutIntoObjectHashTable( | 52 Handle<ObjectHashTable> table = PutIntoObjectHashTable( |
| 53 Handle<ObjectHashTable>(weakmap->table()), | 53 Handle<ObjectHashTable>(ObjectHashTable::cast(weakmap->table())), |
| 54 Handle<JSObject>(JSObject::cast(*key)), | 54 Handle<JSObject>(JSObject::cast(*key)), |
| 55 Handle<Smi>(Smi::FromInt(value))); | 55 Handle<Smi>(Smi::FromInt(value))); |
| 56 weakmap->set_table(*table); | 56 weakmap->set_table(*table); |
| 57 } | 57 } |
| 58 | 58 |
| 59 static int NumberOfWeakCalls = 0; | 59 static int NumberOfWeakCalls = 0; |
| 60 static void WeakPointerCallback(v8::Persistent<v8::Value> handle, void* id) { | 60 static void WeakPointerCallback(v8::Persistent<v8::Value> handle, void* id) { |
| 61 ASSERT(id == reinterpret_cast<void*>(1234)); | 61 ASSERT(id == reinterpret_cast<void*>(1234)); |
| 62 NumberOfWeakCalls++; | 62 NumberOfWeakCalls++; |
| 63 handle.Dispose(); | 63 handle.Dispose(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 78 Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map); | 78 Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map); |
| 79 key = global_handles->Create(*object); | 79 key = global_handles->Create(*object); |
| 80 } | 80 } |
| 81 CHECK(!global_handles->IsWeak(key.location())); | 81 CHECK(!global_handles->IsWeak(key.location())); |
| 82 | 82 |
| 83 // Put entry into weak map. | 83 // Put entry into weak map. |
| 84 { | 84 { |
| 85 v8::HandleScope scope; | 85 v8::HandleScope scope; |
| 86 PutIntoWeakMap(weakmap, Handle<JSObject>(JSObject::cast(*key)), 23); | 86 PutIntoWeakMap(weakmap, Handle<JSObject>(JSObject::cast(*key)), 23); |
| 87 } | 87 } |
| 88 CHECK_EQ(1, weakmap->table()->NumberOfElements()); | 88 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 89 | 89 |
| 90 // Force a full GC. | 90 // Force a full GC. |
| 91 HEAP->CollectAllGarbage(false); | 91 HEAP->CollectAllGarbage(false); |
| 92 CHECK_EQ(0, NumberOfWeakCalls); | 92 CHECK_EQ(0, NumberOfWeakCalls); |
| 93 CHECK_EQ(1, weakmap->table()->NumberOfElements()); | 93 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 94 CHECK_EQ(0, weakmap->table()->NumberOfDeletedElements()); | 94 CHECK_EQ( |
| 95 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
| 95 | 96 |
| 96 // Make the global reference to the key weak. | 97 // Make the global reference to the key weak. |
| 97 { | 98 { |
| 98 v8::HandleScope scope; | 99 v8::HandleScope scope; |
| 99 global_handles->MakeWeak(key.location(), | 100 global_handles->MakeWeak(key.location(), |
| 100 reinterpret_cast<void*>(1234), | 101 reinterpret_cast<void*>(1234), |
| 101 &WeakPointerCallback); | 102 &WeakPointerCallback); |
| 102 } | 103 } |
| 103 CHECK(global_handles->IsWeak(key.location())); | 104 CHECK(global_handles->IsWeak(key.location())); |
| 104 | 105 |
| 105 // Force a full GC. | 106 // Force a full GC. |
| 106 // Perform two consecutive GCs because the first one will only clear | 107 // Perform two consecutive GCs because the first one will only clear |
| 107 // weak references whereas the second one will also clear weak maps. | 108 // weak references whereas the second one will also clear weak maps. |
| 108 HEAP->CollectAllGarbage(false); | 109 HEAP->CollectAllGarbage(false); |
| 109 CHECK_EQ(1, NumberOfWeakCalls); | 110 CHECK_EQ(1, NumberOfWeakCalls); |
| 110 CHECK_EQ(1, weakmap->table()->NumberOfElements()); | 111 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 111 CHECK_EQ(0, weakmap->table()->NumberOfDeletedElements()); | 112 CHECK_EQ( |
| 113 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
| 112 HEAP->CollectAllGarbage(false); | 114 HEAP->CollectAllGarbage(false); |
| 113 CHECK_EQ(1, NumberOfWeakCalls); | 115 CHECK_EQ(1, NumberOfWeakCalls); |
| 114 CHECK_EQ(0, weakmap->table()->NumberOfElements()); | 116 CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 115 CHECK_EQ(1, weakmap->table()->NumberOfDeletedElements()); | 117 CHECK_EQ( |
| 118 1, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
| 116 } | 119 } |
| 117 | 120 |
| 118 | 121 |
| 119 TEST(Shrinking) { | 122 TEST(Shrinking) { |
| 120 LocalContext context; | 123 LocalContext context; |
| 121 v8::HandleScope scope; | 124 v8::HandleScope scope; |
| 122 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(); | 125 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(); |
| 123 | 126 |
| 124 // Check initial capacity. | 127 // Check initial capacity. |
| 125 CHECK_EQ(32, weakmap->table()->Capacity()); | 128 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); |
| 126 | 129 |
| 127 // Fill up weak map to trigger capacity change. | 130 // Fill up weak map to trigger capacity change. |
| 128 { | 131 { |
| 129 v8::HandleScope scope; | 132 v8::HandleScope scope; |
| 130 Handle<Map> map = FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 133 Handle<Map> map = FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 131 for (int i = 0; i < 32; i++) { | 134 for (int i = 0; i < 32; i++) { |
| 132 Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map); | 135 Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map); |
| 133 PutIntoWeakMap(weakmap, object, i); | 136 PutIntoWeakMap(weakmap, object, i); |
| 134 } | 137 } |
| 135 } | 138 } |
| 136 | 139 |
| 137 // Check increased capacity. | 140 // Check increased capacity. |
| 138 CHECK_EQ(128, weakmap->table()->Capacity()); | 141 CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity()); |
| 139 | 142 |
| 140 // Force a full GC. | 143 // Force a full GC. |
| 141 CHECK_EQ(32, weakmap->table()->NumberOfElements()); | 144 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 142 CHECK_EQ(0, weakmap->table()->NumberOfDeletedElements()); | 145 CHECK_EQ( |
| 146 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
| 143 HEAP->CollectAllGarbage(false); | 147 HEAP->CollectAllGarbage(false); |
| 144 CHECK_EQ(0, weakmap->table()->NumberOfElements()); | 148 CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 145 CHECK_EQ(32, weakmap->table()->NumberOfDeletedElements()); | 149 CHECK_EQ( |
| 150 32, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
| 146 | 151 |
| 147 // Check shrunk capacity. | 152 // Check shrunk capacity. |
| 148 CHECK_EQ(32, weakmap->table()->Capacity()); | 153 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); |
| 149 } | 154 } |
| OLD | NEW |