OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 #include "global-handles.h" |
| 29 |
| 30 #include "cctest.h" |
| 31 |
| 32 using namespace v8::internal; |
| 33 using v8::UniqueId; |
| 34 |
| 35 static int NumberOfWeakCalls = 0; |
| 36 static void WeakPointerCallback(v8::Isolate* isolate, |
| 37 v8::Persistent<v8::Value> handle, |
| 38 void* id) { |
| 39 ASSERT(id == reinterpret_cast<void*>(1234)); |
| 40 NumberOfWeakCalls++; |
| 41 handle.Dispose(isolate); |
| 42 } |
| 43 |
| 44 static List<Object*> skippable_objects; |
| 45 static List<Object*> can_skip_called_objects; |
| 46 |
| 47 static bool CanSkipCallback(Heap* heap, Object** pointer) { |
| 48 can_skip_called_objects.Add(*pointer); |
| 49 return skippable_objects.Contains(*pointer); |
| 50 } |
| 51 |
| 52 static void ResetCanSkipData() { |
| 53 skippable_objects.Clear(); |
| 54 can_skip_called_objects.Clear(); |
| 55 } |
| 56 |
| 57 class TestRetainedObjectInfo : public v8::RetainedObjectInfo { |
| 58 public: |
| 59 TestRetainedObjectInfo() : has_been_disposed_(false) {} |
| 60 |
| 61 bool has_been_disposed() { return has_been_disposed_; } |
| 62 |
| 63 virtual void Dispose() { |
| 64 ASSERT(!has_been_disposed_); |
| 65 has_been_disposed_ = true; |
| 66 } |
| 67 |
| 68 virtual bool IsEquivalent(v8::RetainedObjectInfo* other) { |
| 69 return other == this; |
| 70 } |
| 71 |
| 72 virtual intptr_t GetHash() { return 0; } |
| 73 |
| 74 virtual const char* GetLabel() { return "whatever"; } |
| 75 |
| 76 private: |
| 77 bool has_been_disposed_; |
| 78 }; |
| 79 |
| 80 class TestObjectVisitor : public ObjectVisitor { |
| 81 public: |
| 82 virtual void VisitPointers(Object** start, Object** end) { |
| 83 for (Object** o = start; o != end; ++o) |
| 84 visited.Add(*o); |
| 85 } |
| 86 |
| 87 List<Object*> visited; |
| 88 }; |
| 89 |
| 90 TEST(IterateObjectGroupsOldApi) { |
| 91 CcTest::InitializeVM(); |
| 92 GlobalHandles* global_handles = Isolate::Current()->global_handles(); |
| 93 |
| 94 v8::HandleScope handle_scope(CcTest::isolate()); |
| 95 |
| 96 Handle<Object> g1s1 = |
| 97 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
| 98 Handle<Object> g1s2 = |
| 99 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
| 100 global_handles->MakeWeak(g1s1.location(), |
| 101 reinterpret_cast<void*>(1234), |
| 102 NULL, |
| 103 &WeakPointerCallback); |
| 104 global_handles->MakeWeak(g1s2.location(), |
| 105 reinterpret_cast<void*>(1234), |
| 106 NULL, |
| 107 &WeakPointerCallback); |
| 108 |
| 109 Handle<Object> g2s1 = |
| 110 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
| 111 Handle<Object> g2s2 = |
| 112 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
| 113 global_handles->MakeWeak(g2s1.location(), |
| 114 reinterpret_cast<void*>(1234), |
| 115 NULL, |
| 116 &WeakPointerCallback); |
| 117 global_handles->MakeWeak(g2s2.location(), |
| 118 reinterpret_cast<void*>(1234), |
| 119 NULL, |
| 120 &WeakPointerCallback); |
| 121 |
| 122 TestRetainedObjectInfo info1; |
| 123 TestRetainedObjectInfo info2; |
| 124 { |
| 125 Object** g1_objects[] = { g1s1.location(), g1s2.location() }; |
| 126 Object** g2_objects[] = { g2s1.location(), g2s2.location() }; |
| 127 |
| 128 global_handles->AddObjectGroup(g1_objects, 2, &info1); |
| 129 global_handles->AddObjectGroup(g2_objects, 2, &info2); |
| 130 } |
| 131 |
| 132 // Iterate the object groups. First skip all. |
| 133 { |
| 134 ResetCanSkipData(); |
| 135 skippable_objects.Add(*g1s1.location()); |
| 136 skippable_objects.Add(*g1s2.location()); |
| 137 skippable_objects.Add(*g2s1.location()); |
| 138 skippable_objects.Add(*g2s2.location()); |
| 139 TestObjectVisitor visitor; |
| 140 global_handles->IterateObjectGroups(&visitor, &CanSkipCallback); |
| 141 |
| 142 // CanSkipCallback was called for all objects. |
| 143 ASSERT(can_skip_called_objects.length() == 4); |
| 144 ASSERT(can_skip_called_objects.Contains(*g1s1.location())); |
| 145 ASSERT(can_skip_called_objects.Contains(*g1s2.location())); |
| 146 ASSERT(can_skip_called_objects.Contains(*g2s1.location())); |
| 147 ASSERT(can_skip_called_objects.Contains(*g2s2.location())); |
| 148 |
| 149 // Nothing was visited. |
| 150 ASSERT(visitor.visited.length() == 0); |
| 151 ASSERT(!info1.has_been_disposed()); |
| 152 ASSERT(!info2.has_been_disposed()); |
| 153 } |
| 154 |
| 155 // Iterate again, now only skip the second object group. |
| 156 { |
| 157 ResetCanSkipData(); |
| 158 // The first grough should still be visited, since only one object is |
| 159 // skipped. |
| 160 skippable_objects.Add(*g1s1.location()); |
| 161 skippable_objects.Add(*g2s1.location()); |
| 162 skippable_objects.Add(*g2s2.location()); |
| 163 TestObjectVisitor visitor; |
| 164 global_handles->IterateObjectGroups(&visitor, &CanSkipCallback); |
| 165 |
| 166 // CanSkipCallback was called for all objects. |
| 167 ASSERT(can_skip_called_objects.length() == 3 || |
| 168 can_skip_called_objects.length() == 4); |
| 169 ASSERT(can_skip_called_objects.Contains(*g1s2.location())); |
| 170 ASSERT(can_skip_called_objects.Contains(*g2s1.location())); |
| 171 ASSERT(can_skip_called_objects.Contains(*g2s2.location())); |
| 172 |
| 173 // The first group was visited. |
| 174 ASSERT(visitor.visited.length() == 2); |
| 175 ASSERT(visitor.visited.Contains(*g1s1.location())); |
| 176 ASSERT(visitor.visited.Contains(*g1s2.location())); |
| 177 ASSERT(info1.has_been_disposed()); |
| 178 ASSERT(!info2.has_been_disposed()); |
| 179 } |
| 180 |
| 181 // Iterate again, don't skip anything. |
| 182 { |
| 183 ResetCanSkipData(); |
| 184 TestObjectVisitor visitor; |
| 185 global_handles->IterateObjectGroups(&visitor, &CanSkipCallback); |
| 186 |
| 187 // CanSkipCallback was called for all objects. |
| 188 fprintf(stderr, "can skip len %d\n", can_skip_called_objects.length()); |
| 189 ASSERT(can_skip_called_objects.length() == 1); |
| 190 ASSERT(can_skip_called_objects.Contains(*g2s1.location()) || |
| 191 can_skip_called_objects.Contains(*g2s2.location())); |
| 192 |
| 193 // The second group was visited. |
| 194 ASSERT(visitor.visited.length() == 2); |
| 195 ASSERT(visitor.visited.Contains(*g2s1.location())); |
| 196 ASSERT(visitor.visited.Contains(*g2s2.location())); |
| 197 ASSERT(info2.has_been_disposed()); |
| 198 } |
| 199 } |
| 200 |
| 201 TEST(IterateObjectGroups) { |
| 202 CcTest::InitializeVM(); |
| 203 GlobalHandles* global_handles = Isolate::Current()->global_handles(); |
| 204 |
| 205 v8::HandleScope handle_scope(CcTest::isolate()); |
| 206 |
| 207 Handle<Object> g1s1 = |
| 208 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
| 209 Handle<Object> g1s2 = |
| 210 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
| 211 global_handles->MakeWeak(g1s1.location(), |
| 212 reinterpret_cast<void*>(1234), |
| 213 NULL, |
| 214 &WeakPointerCallback); |
| 215 global_handles->MakeWeak(g1s2.location(), |
| 216 reinterpret_cast<void*>(1234), |
| 217 NULL, |
| 218 &WeakPointerCallback); |
| 219 |
| 220 Handle<Object> g2s1 = |
| 221 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
| 222 Handle<Object> g2s2 = |
| 223 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
| 224 global_handles->MakeWeak(g2s1.location(), |
| 225 reinterpret_cast<void*>(1234), |
| 226 NULL, |
| 227 &WeakPointerCallback); |
| 228 global_handles->MakeWeak(g2s2.location(), |
| 229 reinterpret_cast<void*>(1234), |
| 230 NULL, |
| 231 &WeakPointerCallback); |
| 232 |
| 233 TestRetainedObjectInfo info1; |
| 234 TestRetainedObjectInfo info2; |
| 235 { |
| 236 global_handles->SetObjectGroupId(g2s1.location(), UniqueId(2)); |
| 237 global_handles->SetObjectGroupId(g2s2.location(), UniqueId(2)); |
| 238 global_handles->SetRetainedObjectInfo(UniqueId(2), &info2); |
| 239 global_handles->SetObjectGroupId(g1s1.location(), UniqueId(1)); |
| 240 global_handles->SetObjectGroupId(g1s2.location(), UniqueId(1)); |
| 241 global_handles->SetRetainedObjectInfo(UniqueId(1), &info1); |
| 242 } |
| 243 |
| 244 // Iterate the object groups. First skip all. |
| 245 { |
| 246 ResetCanSkipData(); |
| 247 skippable_objects.Add(*g1s1.location()); |
| 248 skippable_objects.Add(*g1s2.location()); |
| 249 skippable_objects.Add(*g2s1.location()); |
| 250 skippable_objects.Add(*g2s2.location()); |
| 251 TestObjectVisitor visitor; |
| 252 global_handles->IterateObjectGroups(&visitor, &CanSkipCallback); |
| 253 |
| 254 // CanSkipCallback was called for all objects. |
| 255 ASSERT(can_skip_called_objects.length() == 4); |
| 256 ASSERT(can_skip_called_objects.Contains(*g1s1.location())); |
| 257 ASSERT(can_skip_called_objects.Contains(*g1s2.location())); |
| 258 ASSERT(can_skip_called_objects.Contains(*g2s1.location())); |
| 259 ASSERT(can_skip_called_objects.Contains(*g2s2.location())); |
| 260 |
| 261 // Nothing was visited. |
| 262 ASSERT(visitor.visited.length() == 0); |
| 263 ASSERT(!info1.has_been_disposed()); |
| 264 ASSERT(!info2.has_been_disposed()); |
| 265 } |
| 266 |
| 267 // Iterate again, now only skip the second object group. |
| 268 { |
| 269 ResetCanSkipData(); |
| 270 // The first grough should still be visited, since only one object is |
| 271 // skipped. |
| 272 skippable_objects.Add(*g1s1.location()); |
| 273 skippable_objects.Add(*g2s1.location()); |
| 274 skippable_objects.Add(*g2s2.location()); |
| 275 TestObjectVisitor visitor; |
| 276 global_handles->IterateObjectGroups(&visitor, &CanSkipCallback); |
| 277 |
| 278 // CanSkipCallback was called for all objects. |
| 279 ASSERT(can_skip_called_objects.length() == 3 || |
| 280 can_skip_called_objects.length() == 4); |
| 281 ASSERT(can_skip_called_objects.Contains(*g1s2.location())); |
| 282 ASSERT(can_skip_called_objects.Contains(*g2s1.location())); |
| 283 ASSERT(can_skip_called_objects.Contains(*g2s2.location())); |
| 284 |
| 285 // The first group was visited. |
| 286 ASSERT(visitor.visited.length() == 2); |
| 287 ASSERT(visitor.visited.Contains(*g1s1.location())); |
| 288 ASSERT(visitor.visited.Contains(*g1s2.location())); |
| 289 ASSERT(info1.has_been_disposed()); |
| 290 ASSERT(!info2.has_been_disposed()); |
| 291 } |
| 292 |
| 293 // Iterate again, don't skip anything. |
| 294 { |
| 295 ResetCanSkipData(); |
| 296 TestObjectVisitor visitor; |
| 297 global_handles->IterateObjectGroups(&visitor, &CanSkipCallback); |
| 298 |
| 299 // CanSkipCallback was called for all objects. |
| 300 fprintf(stderr, "can skip len %d\n", can_skip_called_objects.length()); |
| 301 ASSERT(can_skip_called_objects.length() == 1); |
| 302 ASSERT(can_skip_called_objects.Contains(*g2s1.location()) || |
| 303 can_skip_called_objects.Contains(*g2s2.location())); |
| 304 |
| 305 // The second group was visited. |
| 306 ASSERT(visitor.visited.length() == 2); |
| 307 ASSERT(visitor.visited.Contains(*g2s1.location())); |
| 308 ASSERT(visitor.visited.Contains(*g2s2.location())); |
| 309 ASSERT(info2.has_been_disposed()); |
| 310 } |
| 311 } |
OLD | NEW |