Skip to content

Commit d6862b0

Browse files
joyeecheungUlisesGascon
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: #43521 2. Create cppgc-managed wrapper objects when they do not own the CppHeap. Refs: #45704 Bug: v8:13960 Change-Id: If31f4d56c5ead59dc0d56f937494d23d631f7438 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4598833 Reviewed-by: Michael Lippautz <mlippautz@chromium.org> Commit-Queue: Michael Lippautz <mlippautz@chromium.org> Cr-Commit-Position: refs/heads/main@{#88490} Refs: v8/v8@9327503 PR-URL: #48660 Backport-PR-URL: #49187 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Refs: #40786 Refs: https://docs.google.com/document/d/1ny2Qz_EsUnXGKJRGxoA-FXIE2xpLgaMAN6jD7eAkqFQ/edit
1 parent 00fc8bb commit d6862b0

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

common.gypi

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
# Reset this number to 0 on major V8 upgrades.
3838
# Increment by one for each non-official patch applied to deps/v8.
39-
'v8_embedder_string': '-node.13',
39+
'v8_embedder_string': '-node.14',
4040

4141
##### V8 defaults for Node.js #####
4242

deps/v8/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

deps/v8/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 {

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

+41
Original file line numberDiff line numberDiff line change
@@ -706,4 +706,45 @@ TEST_F(UnifiedHeapTest, TracedReferenceHandlesDoNotLeak) {
706706
EXPECT_EQ(initial_count, final_count + 1);
707707
}
708708

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

0 commit comments

Comments
 (0)