Commit f91949a1 authored by Michael Lippautz's avatar Michael Lippautz Committed by Commit Bot

cppgc: Allow BasicPersistent::Clear() with incomplete type

This allows construction and destruction of empty Persistent and
friends, which simplifiest the use for embedders.

Bug: chromium:1056170
Change-Id: I4286639aa5d50f9f98654b859de10bb80cbada21
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2655505
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: 's avatarOmer Katz <omerkatz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#72396}
parent 3a2ae154
...@@ -168,7 +168,18 @@ class BasicCrossThreadPersistent final : public PersistentBase, ...@@ -168,7 +168,18 @@ class BasicCrossThreadPersistent final : public PersistentBase,
/** /**
* Clears the stored object. * Clears the stored object.
*/ */
void Clear() { Assign(nullptr); } void Clear() {
// Simplified version of `Assign()` to allow calling without a complete type
// `T`.
const void* old_value = GetValue();
if (IsValid(old_value)) {
PersistentRegionLock guard;
PersistentRegion& region = this->GetPersistentRegion(old_value);
region.FreeNode(GetNode());
SetNode(nullptr);
}
SetValue(nullptr);
}
/** /**
* Returns a pointer to the stored object and releases it. * Returns a pointer to the stored object and releases it.
......
...@@ -194,7 +194,15 @@ class BasicPersistent final : public PersistentBase, ...@@ -194,7 +194,15 @@ class BasicPersistent final : public PersistentBase,
return static_cast<T*>(const_cast<void*>(GetValue())); return static_cast<T*>(const_cast<void*>(GetValue()));
} }
void Clear() { Assign(nullptr); } void Clear() {
// Simplified version of `Assign()` to allow calling without a complete type
// `T`.
if (IsValid()) {
WeaknessPolicy::GetPersistentRegion(GetValue()).FreeNode(GetNode());
SetNode(nullptr);
}
SetValue(nullptr);
}
T* Release() { T* Release() {
T* result = Get(); T* result = Get();
......
...@@ -864,5 +864,18 @@ TEST_F(PersistentTest, PersistentTraceLocation) { ...@@ -864,5 +864,18 @@ TEST_F(PersistentTest, PersistentTraceLocation) {
} }
} }
namespace {
class IncompleteType;
} // namespace
TEST_F(PersistentTest, EmptyPersistentConstructDestructWithoutCompleteType) {
// Test ensures that empty constructor and destructor compile without having
// a complete type available.
Persistent<IncompleteType> p1;
WeakPersistent<IncompleteType> p2;
subtle::CrossThreadPersistent<IncompleteType> p3;
subtle::WeakCrossThreadPersistent<IncompleteType> p4;
}
} // namespace internal } // namespace internal
} // namespace cppgc } // namespace cppgc
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment