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: Omer Katz <omerkatz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#72396}
This commit is contained in:
Michael Lippautz 2021-01-28 12:03:59 +01:00 committed by Commit Bot
parent 3a2ae154f9
commit f91949a153
3 changed files with 34 additions and 2 deletions

View File

@ -168,7 +168,18 @@ class BasicCrossThreadPersistent final : public PersistentBase,
/**
* 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.

View File

@ -194,7 +194,15 @@ class BasicPersistent final : public PersistentBase,
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* result = Get();

View File

@ -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 cppgc