Index: test/unittests/managed-unittests.cc |
diff --git a/test/unittests/managed-unittests.cc b/test/unittests/managed-unittests.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a3193e3b5dbd098b2df8a8fd0e872e7e2fe0db01 |
--- /dev/null |
+++ b/test/unittests/managed-unittests.cc |
@@ -0,0 +1,91 @@ |
+// Copyright 2017 the V8 project authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "include/v8.h" |
+#include "src/flags.h" |
+#include "src/managed.h" |
+#include "src/objects-inl.h" |
+ |
+#include "test/unittests/test-utils.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using namespace v8::base; |
+using namespace v8::internal; |
+ |
+using ManagedTest = TestWithIsolate; |
+ |
+class DeleteRecorder { |
+ public: |
+ explicit DeleteRecorder(bool* deleted) : deleted_(deleted) { |
+ *deleted_ = false; |
+ } |
+ ~DeleteRecorder() { *deleted_ = true; } |
+ static void Deleter(void* value) { |
+ delete reinterpret_cast<DeleteRecorder*>(value); |
+ } |
+ |
+ private: |
+ bool* deleted_; |
+}; |
+ |
+class ScopedExposeGc { |
+ public: |
+ ScopedExposeGc() : was_exposed_(i::FLAG_expose_gc) { |
+ i::FLAG_expose_gc = true; |
+ } |
+ ~ScopedExposeGc() { i::FLAG_expose_gc = was_exposed_; } |
+ |
+ private: |
+ const bool was_exposed_; |
+}; |
+ |
+TEST_F(ManagedTest, ManagedCollect) { |
+ bool deleted1 = false; |
+ bool deleted2 = false; |
+ DeleteRecorder* d1 = new DeleteRecorder(&deleted1); |
+ DeleteRecorder* d2 = new DeleteRecorder(&deleted2); |
+ Isolate::ManagedLifeline* list_node = |
+ isolate()->RegisterForReleaseAtTeardown(d2, DeleteRecorder::Deleter); |
+ { |
+ HandleScope scope(isolate()); |
+ auto handle = Managed<DeleteRecorder>::New(isolate(), d1); |
+ USE(handle); |
+ } |
+ |
+ v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate()); |
+ { |
+ ScopedExposeGc expose_gc; |
+ v8_isolate->RequestGarbageCollectionForTesting( |
+ v8::Isolate::kFullGarbageCollection); |
+ } |
+ CHECK(deleted1); |
+ CHECK(!deleted2); |
+ isolate()->UnregisterFromReleaseAtTeardown(list_node); |
+ CHECK_NULL(list_node); |
+ delete d2; |
+} |
+ |
+TEST_F(ManagedTest, DisposeCollect) { |
+ v8::Isolate::CreateParams create_params; |
+ create_params.array_buffer_allocator = isolate()->array_buffer_allocator(); |
+ |
+ v8::Isolate* new_isolate = v8::Isolate::New(create_params); |
+ new_isolate->Enter(); |
+ Isolate* i_new_isolate = reinterpret_cast<i::Isolate*>(new_isolate); |
+ bool deleted1 = false; |
+ bool deleted2 = false; |
+ DeleteRecorder* d1 = new DeleteRecorder(&deleted1); |
+ DeleteRecorder* d2 = new DeleteRecorder(&deleted2); |
+ { |
+ HandleScope scope(i_new_isolate); |
+ auto handle = Managed<DeleteRecorder>::New(i_new_isolate, d1); |
+ USE(handle); |
+ } |
+ i_new_isolate->RegisterForReleaseAtTeardown(d2, DeleteRecorder::Deleter); |
+ |
+ new_isolate->Exit(); |
+ new_isolate->Dispose(); |
+ CHECK(deleted1); |
+ CHECK(deleted2); |
+} |