OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <stdint.h> |
| 6 #include <stdlib.h> |
| 7 #include <string.h> |
| 8 |
| 9 #include "src/managed.h" |
| 10 |
| 11 #include "src/objects-inl.h" |
| 12 #include "test/cctest/cctest.h" |
| 13 |
| 14 using namespace v8::base; |
| 15 using namespace v8::internal; |
| 16 |
| 17 class DeleteRecorder { |
| 18 public: |
| 19 explicit DeleteRecorder(bool* deleted) : deleted_(deleted) { |
| 20 *deleted_ = false; |
| 21 } |
| 22 ~DeleteRecorder() { *deleted_ = true; } |
| 23 static void Deleter(void* value) { |
| 24 delete reinterpret_cast<DeleteRecorder*>(value); |
| 25 } |
| 26 |
| 27 private: |
| 28 bool* deleted_; |
| 29 }; |
| 30 |
| 31 TEST(ManagedCollect) { |
| 32 Isolate* isolate = CcTest::InitIsolateOnce(); |
| 33 bool deleted1 = false; |
| 34 bool deleted2 = false; |
| 35 DeleteRecorder* d1 = new DeleteRecorder(&deleted1); |
| 36 DeleteRecorder* d2 = new DeleteRecorder(&deleted2); |
| 37 Isolate::ManagedObjectFinalizer* finalizer = |
| 38 isolate->RegisterForReleaseAtTeardown(d2, DeleteRecorder::Deleter); |
| 39 { |
| 40 HandleScope scope(isolate); |
| 41 auto handle = Managed<DeleteRecorder>::New(isolate, d1); |
| 42 USE(handle); |
| 43 } |
| 44 |
| 45 CcTest::CollectAllAvailableGarbage(); |
| 46 |
| 47 CHECK(deleted1); |
| 48 CHECK(!deleted2); |
| 49 isolate->UnregisterFromReleaseAtTeardown(&finalizer); |
| 50 CHECK_NULL(finalizer); |
| 51 delete d2; |
| 52 CHECK(deleted2); |
| 53 } |
| 54 |
| 55 TEST(DisposeCollect) { |
| 56 v8::Isolate::CreateParams create_params; |
| 57 create_params.array_buffer_allocator = |
| 58 CcTest::InitIsolateOnce()->array_buffer_allocator(); |
| 59 |
| 60 v8::Isolate* isolate = v8::Isolate::New(create_params); |
| 61 isolate->Enter(); |
| 62 Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 63 bool deleted1 = false; |
| 64 bool deleted2 = false; |
| 65 DeleteRecorder* d1 = new DeleteRecorder(&deleted1); |
| 66 DeleteRecorder* d2 = new DeleteRecorder(&deleted2); |
| 67 { |
| 68 HandleScope scope(i_isolate); |
| 69 auto handle = Managed<DeleteRecorder>::New(i_isolate, d1); |
| 70 USE(handle); |
| 71 } |
| 72 i_isolate->RegisterForReleaseAtTeardown(d2, DeleteRecorder::Deleter); |
| 73 |
| 74 isolate->Exit(); |
| 75 isolate->Dispose(); |
| 76 CHECK(deleted1); |
| 77 CHECK(deleted2); |
| 78 } |
OLD | NEW |