Skip to content

Commit 9327503

Browse files
joyeecheungV8 LUCI CQ
authored and
V8 LUCI CQ
committed
[cppgc] expose wrapper descriptor on CppHeap
This makes it possible for embedders to: 1. Avoid creating wrapper objects that happen to have a layout that leads V8 to consider the object cppgc-managed while it's not. Refs: nodejs/node#43521 2. Create cppgc-managed wrapper objects when they do not own the CppHeap. Refs: nodejs/node#45704 Bug: v8:13960 Change-Id: If31f4d56c5ead59dc0d56f937494d23d631f7438 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4598833 Reviewed-by: Michael Lippautz <[email protected]> Commit-Queue: Michael Lippautz <[email protected]> Cr-Commit-Position: refs/heads/main@{#88490}
1 parent 297f933 commit 9327503

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

include/v8-cppgc.h

+5
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ class V8_EXPORT CppHeap {
177177
void CollectGarbageInYoungGenerationForTesting(
178178
cppgc::EmbedderStackState stack_state);
179179

180+
/**
181+
* \returns the wrapper descriptor of this CppHeap.
182+
*/
183+
v8::WrapperDescriptor wrapper_descriptor() const;
184+
180185
private:
181186
CppHeap() = default;
182187

src/heap/cppgc-js/cpp-heap.cc

+4
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ void CppHeap::CollectGarbageInYoungGenerationForTesting(
147147
internal::CppHeap::CollectionType::kMinor, stack_state);
148148
}
149149

150+
v8::WrapperDescriptor CppHeap::wrapper_descriptor() const {
151+
return internal::CppHeap::From(this)->wrapper_descriptor();
152+
}
153+
150154
namespace internal {
151155

152156
namespace {

test/unittests/heap/cppgc-js/unified-heap-unittest.cc

+41
Original file line numberDiff line numberDiff line change
@@ -710,4 +710,45 @@ TEST_F(UnifiedHeapTest, TracedReferenceHandlesDoNotLeak) {
710710
EXPECT_EQ(initial_count, final_count + 1);
711711
}
712712

713+
namespace {
714+
class Wrappable2 final : public cppgc::GarbageCollected<Wrappable2> {
715+
public:
716+
static size_t destructor_call_count;
717+
void Trace(cppgc::Visitor* visitor) const {}
718+
~Wrappable2() { destructor_call_count++; }
719+
};
720+
721+
size_t Wrappable2::destructor_call_count = 0;
722+
} // namespace
723+
724+
TEST_F(UnifiedHeapTest, WrapperDescriptorGetter) {
725+
v8::Isolate* isolate = v8_isolate();
726+
v8::HandleScope scope(isolate);
727+
auto* wrappable_object =
728+
cppgc::MakeGarbageCollected<Wrappable2>(allocation_handle());
729+
v8::WrapperDescriptor descriptor =
730+
isolate->GetCppHeap()->wrapper_descriptor();
731+
v8::Local<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(isolate);
732+
int size = std::max(descriptor.wrappable_type_index,
733+
descriptor.wrappable_instance_index) +
734+
1;
735+
tmpl->SetInternalFieldCount(size);
736+
v8::Local<v8::Object> api_object =
737+
tmpl->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
738+
api_object->SetAlignedPointerInInternalField(
739+
descriptor.wrappable_type_index,
740+
&descriptor.embedder_id_for_garbage_collected);
741+
api_object->SetAlignedPointerInInternalField(
742+
descriptor.wrappable_instance_index, wrappable_object);
743+
744+
Wrappable2::destructor_call_count = 0;
745+
EXPECT_EQ(0u, Wrappable2::destructor_call_count);
746+
CollectGarbageWithoutEmbedderStack(cppgc::Heap::SweepingType::kAtomic);
747+
EXPECT_EQ(0u, Wrappable2::destructor_call_count);
748+
api_object->SetAlignedPointerInInternalField(
749+
descriptor.wrappable_instance_index, nullptr);
750+
CollectGarbageWithoutEmbedderStack(cppgc::Heap::SweepingType::kAtomic);
751+
EXPECT_EQ(1u, Wrappable2::destructor_call_count);
752+
}
753+
713754
} // namespace v8::internal

0 commit comments

Comments
 (0)