Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(502)

Unified Diff: test/unittests/managed-unittests.cc

Issue 2676513008: [wasm] Managed<T> ensures T's lifetime does not leak past Isolate's (Closed)
Patch Set: link Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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);
+}
« src/isolate.h ('K') | « test/unittests/BUILD.gn ('k') | test/unittests/unittests.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698