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: https://github.com/nodejs/node/pull/43521
    2. Create cppgc-managed wrapper objects when they do not own the
       CppHeap. Refs: https://github.com/nodejs/node/pull/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: 9327503128
PR-URL: https://github.com/nodejs/node/pull/48660
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
This commit is contained in:
Joyee Cheung 2023-06-26 19:40:22 +02:00
parent 3f4c127f92
commit 44027fbca8
No known key found for this signature in database
GPG Key ID: 92B78A53C8303B8D
4 changed files with 51 additions and 1 deletions

View File

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

View File

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

View File

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

View File

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