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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 // Do not leak handles for the hash table, it would make entries strong. | 48 // Do not leak handles for the hash table, it would make entries strong. |
49 { | 49 { |
50 HandleScope scope(isolate); | 50 HandleScope scope(isolate); |
51 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 1); | 51 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 1); |
52 weakset->set_table(*table); | 52 weakset->set_table(*table); |
53 } | 53 } |
54 return weakset; | 54 return weakset; |
55 } | 55 } |
56 | 56 |
57 static int NumberOfWeakCalls = 0; | 57 static int NumberOfWeakCalls = 0; |
58 static void WeakPointerCallback( | 58 static void WeakPointerCallback(const v8::WeakCallbackInfo<void>& data) { |
59 const v8::WeakCallbackData<v8::Value, void>& data) { | |
60 std::pair<v8::Persistent<v8::Value>*, int>* p = | 59 std::pair<v8::Persistent<v8::Value>*, int>* p = |
61 reinterpret_cast<std::pair<v8::Persistent<v8::Value>*, int>*>( | 60 reinterpret_cast<std::pair<v8::Persistent<v8::Value>*, int>*>( |
62 data.GetParameter()); | 61 data.GetParameter()); |
63 CHECK_EQ(1234, p->second); | 62 CHECK_EQ(1234, p->second); |
64 NumberOfWeakCalls++; | 63 NumberOfWeakCalls++; |
65 p->first->Reset(); | 64 p->first->Reset(); |
66 } | 65 } |
67 | 66 |
68 | 67 |
69 TEST(WeakSet_Weakness) { | 68 TEST(WeakSet_Weakness) { |
(...skipping 29 matching lines...) Expand all Loading... |
99 heap->CollectAllGarbage(false); | 98 heap->CollectAllGarbage(false); |
100 CHECK_EQ(0, NumberOfWeakCalls); | 99 CHECK_EQ(0, NumberOfWeakCalls); |
101 CHECK_EQ(1, ObjectHashTable::cast(weakset->table())->NumberOfElements()); | 100 CHECK_EQ(1, ObjectHashTable::cast(weakset->table())->NumberOfElements()); |
102 CHECK_EQ( | 101 CHECK_EQ( |
103 0, ObjectHashTable::cast(weakset->table())->NumberOfDeletedElements()); | 102 0, ObjectHashTable::cast(weakset->table())->NumberOfDeletedElements()); |
104 | 103 |
105 // Make the global reference to the key weak. | 104 // Make the global reference to the key weak. |
106 { | 105 { |
107 HandleScope scope(isolate); | 106 HandleScope scope(isolate); |
108 std::pair<Handle<Object>*, int> handle_and_id(&key, 1234); | 107 std::pair<Handle<Object>*, int> handle_and_id(&key, 1234); |
109 GlobalHandles::MakeWeak(key.location(), | 108 GlobalHandles::MakeWeak( |
110 reinterpret_cast<void*>(&handle_and_id), | 109 key.location(), reinterpret_cast<void*>(&handle_and_id), |
111 &WeakPointerCallback); | 110 &WeakPointerCallback, v8::WeakCallbackType::kParameter); |
112 } | 111 } |
113 CHECK(global_handles->IsWeak(key.location())); | 112 CHECK(global_handles->IsWeak(key.location())); |
114 | 113 |
115 // Force a full GC. | |
116 // Perform two consecutive GCs because the first one will only clear | |
117 // weak references whereas the second one will also clear weak sets. | |
118 heap->CollectAllGarbage(false); | |
119 CHECK_EQ(1, NumberOfWeakCalls); | |
120 CHECK_EQ(1, ObjectHashTable::cast(weakset->table())->NumberOfElements()); | |
121 CHECK_EQ( | |
122 0, ObjectHashTable::cast(weakset->table())->NumberOfDeletedElements()); | |
123 heap->CollectAllGarbage(false); | 114 heap->CollectAllGarbage(false); |
124 CHECK_EQ(1, NumberOfWeakCalls); | 115 CHECK_EQ(1, NumberOfWeakCalls); |
125 CHECK_EQ(0, ObjectHashTable::cast(weakset->table())->NumberOfElements()); | 116 CHECK_EQ(0, ObjectHashTable::cast(weakset->table())->NumberOfElements()); |
126 CHECK_EQ( | 117 CHECK_EQ( |
127 1, ObjectHashTable::cast(weakset->table())->NumberOfDeletedElements()); | 118 1, ObjectHashTable::cast(weakset->table())->NumberOfDeletedElements()); |
128 } | 119 } |
129 | 120 |
130 | 121 |
131 TEST(WeakSet_Shrinking) { | 122 TEST(WeakSet_Shrinking) { |
132 LocalContext context; | 123 LocalContext context; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 JSWeakCollection::Set(weakset, keys[i], smi, hash); | 231 JSWeakCollection::Set(weakset, keys[i], smi, hash); |
241 } | 232 } |
242 | 233 |
243 // Force compacting garbage collection. The subsequent collections are used | 234 // Force compacting garbage collection. The subsequent collections are used |
244 // to verify that key references were actually updated. | 235 // to verify that key references were actually updated. |
245 CHECK(FLAG_always_compact); | 236 CHECK(FLAG_always_compact); |
246 heap->CollectAllGarbage(); | 237 heap->CollectAllGarbage(); |
247 heap->CollectAllGarbage(); | 238 heap->CollectAllGarbage(); |
248 heap->CollectAllGarbage(); | 239 heap->CollectAllGarbage(); |
249 } | 240 } |
OLD | NEW |