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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 // Keep global reference to the key. | 90 // Keep global reference to the key. |
91 Handle<Object> key; | 91 Handle<Object> key; |
92 { | 92 { |
93 HandleScope scope(isolate); | 93 HandleScope scope(isolate); |
94 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 94 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
95 Handle<JSObject> object = factory->NewJSObjectFromMap(map); | 95 Handle<JSObject> object = factory->NewJSObjectFromMap(map); |
96 key = global_handles->Create(*object); | 96 key = global_handles->Create(*object); |
97 } | 97 } |
98 CHECK(!global_handles->IsWeak(key.location())); | 98 CHECK(!global_handles->IsWeak(key.location())); |
99 | 99 |
100 // Put entry into weak map. | 100 // Put two chained entries into weak map. |
101 { | 101 { |
102 HandleScope scope(isolate); | 102 HandleScope scope(isolate); |
103 PutIntoWeakMap(weakmap, | 103 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
104 Handle<JSObject>(JSObject::cast(*key)), | 104 Handle<JSObject> object = factory->NewJSObjectFromMap(map); |
105 Handle<Smi>(Smi::FromInt(23), isolate)); | 105 PutIntoWeakMap(weakmap, Handle<JSObject>(JSObject::cast(*key)), object); |
| 106 PutIntoWeakMap(weakmap, object, Handle<Smi>(Smi::FromInt(23), isolate)); |
106 } | 107 } |
107 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 108 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
108 | 109 |
109 // Force a full GC. | 110 // Force a full GC. |
110 heap->CollectAllGarbage(false); | 111 heap->CollectAllGarbage(false); |
111 CHECK_EQ(0, NumberOfWeakCalls); | 112 CHECK_EQ(0, NumberOfWeakCalls); |
112 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 113 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
113 CHECK_EQ( | 114 CHECK_EQ( |
114 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 115 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
115 | 116 |
116 // Make the global reference to the key weak. | 117 // Make the global reference to the key weak. |
117 { | 118 { |
118 HandleScope scope(isolate); | 119 HandleScope scope(isolate); |
119 std::pair<Handle<Object>*, int> handle_and_id(&key, 1234); | 120 std::pair<Handle<Object>*, int> handle_and_id(&key, 1234); |
120 GlobalHandles::MakeWeak(key.location(), | 121 GlobalHandles::MakeWeak(key.location(), |
121 reinterpret_cast<void*>(&handle_and_id), | 122 reinterpret_cast<void*>(&handle_and_id), |
122 &WeakPointerCallback); | 123 &WeakPointerCallback); |
123 } | 124 } |
124 CHECK(global_handles->IsWeak(key.location())); | 125 CHECK(global_handles->IsWeak(key.location())); |
125 | 126 |
126 // Force a full GC. | 127 // Force a full GC. |
127 // Perform two consecutive GCs because the first one will only clear | 128 // Perform two consecutive GCs because the first one will only clear |
128 // weak references whereas the second one will also clear weak maps. | 129 // weak references whereas the second one will also clear weak maps. |
129 heap->CollectAllGarbage(false); | 130 heap->CollectAllGarbage(false); |
130 CHECK_EQ(1, NumberOfWeakCalls); | 131 CHECK_EQ(1, NumberOfWeakCalls); |
131 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 132 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
132 CHECK_EQ( | 133 CHECK_EQ( |
133 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 134 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
134 heap->CollectAllGarbage(false); | 135 heap->CollectAllGarbage(false); |
135 CHECK_EQ(1, NumberOfWeakCalls); | 136 CHECK_EQ(1, NumberOfWeakCalls); |
136 CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 137 CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
137 CHECK_EQ( | 138 CHECK_EQ(2, |
138 1, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 139 ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
139 } | 140 } |
140 | 141 |
141 | 142 |
142 TEST(Shrinking) { | 143 TEST(Shrinking) { |
143 LocalContext context; | 144 LocalContext context; |
144 Isolate* isolate = GetIsolateFrom(&context); | 145 Isolate* isolate = GetIsolateFrom(&context); |
145 Factory* factory = isolate->factory(); | 146 Factory* factory = isolate->factory(); |
146 Heap* heap = isolate->heap(); | 147 Heap* heap = isolate->heap(); |
147 HandleScope scope(isolate); | 148 HandleScope scope(isolate); |
148 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); | 149 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 { | 266 { |
266 HandleScope scope(isolate); | 267 HandleScope scope(isolate); |
267 AllocateJSWeakMap(isolate); | 268 AllocateJSWeakMap(isolate); |
268 SimulateIncrementalMarking(heap); | 269 SimulateIncrementalMarking(heap); |
269 } | 270 } |
270 // The weak map is marked black here but leaving the handle scope will make | 271 // The weak map is marked black here but leaving the handle scope will make |
271 // the object unreachable. Aborting incremental marking will clear all the | 272 // the object unreachable. Aborting incremental marking will clear all the |
272 // marking bits which makes the weak map garbage. | 273 // marking bits which makes the weak map garbage. |
273 heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); | 274 heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
274 } | 275 } |
OLD | NEW |