Skip to content

Commit

Permalink
deps: V8: cherry-pick 93275031284c
Browse files Browse the repository at this point in the history
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 <mlippautz@chromium.org>
    Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
    Cr-Commit-Position: refs/heads/main@{#88490}

Refs: v8/v8@9327503
PR-URL: nodejs#48660
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
  • Loading branch information
joyeecheung committed Aug 17, 2023
1 parent 00fc8bb commit eba74b7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

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

##### V8 defaults for Node.js #####

Expand Down
5 changes: 5 additions & 0 deletions deps/v8/include/v8-cppgc.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ class V8_EXPORT CppHeap {
void CollectGarbageInYoungGenerationForTesting(
cppgc::EmbedderStackState stack_state);

/**
* \returns the wrapper descriptor of this CppHeap.
*/
v8::WrapperDescriptor wrapper_descriptor() const;

private:
CppHeap() = default;

Expand Down
4 changes: 4 additions & 0 deletions deps/v8/src/heap/cppgc-js/cpp-heap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ void CppHeap::CollectGarbageInYoungGenerationForTesting(
internal::CppHeap::CollectionType::kMinor, stack_state);
}

v8::WrapperDescriptor CppHeap::wrapper_descriptor() const {
return internal::CppHeap::From(this)->wrapper_descriptor();
}

namespace internal {

namespace {
Expand Down
41 changes: 41 additions & 0 deletions deps/v8/test/unittests/heap/cppgc-js/unified-heap-unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -706,4 +706,45 @@ TEST_F(UnifiedHeapTest, TracedReferenceHandlesDoNotLeak) {
EXPECT_EQ(initial_count, final_count + 1);
}

namespace {
class Wrappable2 final : public cppgc::GarbageCollected<Wrappable2> {
public:
static size_t destructor_call_count;
void Trace(cppgc::Visitor* visitor) const {}
~Wrappable2() { destructor_call_count++; }
};

size_t Wrappable2::destructor_call_count = 0;
} // namespace

TEST_F(UnifiedHeapTest, WrapperDescriptorGetter) {
v8::Isolate* isolate = v8_isolate();
v8::HandleScope scope(isolate);
auto* wrappable_object =
cppgc::MakeGarbageCollected<Wrappable2>(allocation_handle());
v8::WrapperDescriptor descriptor =
isolate->GetCppHeap()->wrapper_descriptor();
v8::Local<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(isolate);
int size = std::max(descriptor.wrappable_type_index,
descriptor.wrappable_instance_index) +
1;
tmpl->SetInternalFieldCount(size);
v8::Local<v8::Object> api_object =
tmpl->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
api_object->SetAlignedPointerInInternalField(
descriptor.wrappable_type_index,
&descriptor.embedder_id_for_garbage_collected);
api_object->SetAlignedPointerInInternalField(
descriptor.wrappable_instance_index, wrappable_object);

Wrappable2::destructor_call_count = 0;
EXPECT_EQ(0u, Wrappable2::destructor_call_count);
CollectGarbageWithoutEmbedderStack(cppgc::Heap::SweepingType::kAtomic);
EXPECT_EQ(0u, Wrappable2::destructor_call_count);
api_object->SetAlignedPointerInInternalField(
descriptor.wrappable_instance_index, nullptr);
CollectGarbageWithoutEmbedderStack(cppgc::Heap::SweepingType::kAtomic);
EXPECT_EQ(1u, Wrappable2::destructor_call_count);
}

} // namespace v8::internal

0 comments on commit eba74b7

Please sign in to comment.