Skip to content

Commit 9fdfcc4

Browse files
joyeecheungminglechen
authored andcommitted
deps: V8: cherry-pick 93275031284c
Original commit message: [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#43521 2. Create cppgc-managed wrapper objects when they do not own the CppHeap. Refs: nodejs#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} Refs: v8/v8@9327503 PR-URL: nodejs#48660 Backport-PR-URL: nodejs#49187 Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Jiawen Geng <[email protected]> Refs: nodejs#40786 Refs: https://docs.google.com/document/d/1ny2Qz_EsUnXGKJRGxoA-FXIE2xpLgaMAN6jD7eAkqFQ/edit
1 parent c4a375e commit 9fdfcc4

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

deps/v8/include/v8-cppgc.h

Lines changed: 5 additions & 0 deletions
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

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

Lines changed: 4 additions & 0 deletions
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 {

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,4 +707,45 @@ TEST_F(UnifiedHeapTest, TracedReferenceHandlesDoNotLeak) {
707707
EXPECT_EQ(initial_count, final_count + 1);
708708
}
709709

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

0 commit comments

Comments
 (0)