Skip to content

Commit 30329d0

Browse files
joyeecheungtargos
authored andcommitted
src: remove dependency on wrapper-descriptor-based CppHeap
As V8 has moved away from wrapper-descriptor-based CppHeap, this patch: 1. Create the CppHeap without using wrapper descirptors. 2. Deprecates node::SetCppgcReference() in favor of v8::Object::Wrap() since the wrapper descriptor is no longer relevant. It is still kept as a compatibility layer for addons that need to also work on Node.js versions without v8::Object::Wrap().
1 parent aafed12 commit 30329d0

File tree

6 files changed

+45
-104
lines changed

6 files changed

+45
-104
lines changed

src/env-inl.h

-25
Original file line numberDiff line numberDiff line change
@@ -62,31 +62,6 @@ inline uv_loop_t* IsolateData::event_loop() const {
6262
return event_loop_;
6363
}
6464

65-
inline void IsolateData::SetCppgcReference(v8::Isolate* isolate,
66-
v8::Local<v8::Object> object,
67-
void* wrappable) {
68-
v8::CppHeap* heap = isolate->GetCppHeap();
69-
CHECK_NOT_NULL(heap);
70-
v8::WrapperDescriptor descriptor = heap->wrapper_descriptor();
71-
uint16_t required_size = std::max(descriptor.wrappable_instance_index,
72-
descriptor.wrappable_type_index);
73-
CHECK_GT(object->InternalFieldCount(), required_size);
74-
75-
uint16_t* id_ptr = nullptr;
76-
{
77-
Mutex::ScopedLock lock(isolate_data_mutex_);
78-
auto it =
79-
wrapper_data_map_.find(descriptor.embedder_id_for_garbage_collected);
80-
CHECK_NE(it, wrapper_data_map_.end());
81-
id_ptr = &(it->second->cppgc_id);
82-
}
83-
84-
object->SetAlignedPointerInInternalField(descriptor.wrappable_type_index,
85-
id_ptr);
86-
object->SetAlignedPointerInInternalField(descriptor.wrappable_instance_index,
87-
wrappable);
88-
}
89-
9065
inline uint16_t* IsolateData::embedder_id_for_cppgc() const {
9166
return &(wrapper_data_->cppgc_id);
9267
}

src/env.cc

+19-30
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "util-inl.h"
2424
#include "v8-cppgc.h"
2525
#include "v8-profiler.h"
26+
#include "v8-sandbox.h" // v8::Object::Wrap(), v8::Object::Unwrap()
2627

2728
#include <algorithm>
2829
#include <atomic>
@@ -70,7 +71,6 @@ using v8::TryCatch;
7071
using v8::Uint32;
7172
using v8::Undefined;
7273
using v8::Value;
73-
using v8::WrapperDescriptor;
7474
using worker::Worker;
7575

7676
int const ContextEmbedderTag::kNodeContextTag = 0x6e6f64;
@@ -520,6 +520,14 @@ void IsolateData::CreateProperties() {
520520
CreateEnvProxyTemplate(this);
521521
}
522522

523+
// Previously, the general convention of the wrappable layout for cppgc in
524+
// the ecosystem is:
525+
// [ 0 ] -> embedder id
526+
// [ 1 ] -> wrappable instance
527+
// Now V8 has deprecated this layout-based tracing enablement, embedders
528+
// should simply use v8::Object::Wrap() and v8::Object::Unwrap(). We preserve
529+
// this layout only to distinguish internally how the memory of a Node.js
530+
// wrapper is managed or whether a wrapper is managed by Node.js.
523531
constexpr uint16_t kDefaultCppGCEmebdderID = 0x90de;
524532
Mutex IsolateData::isolate_data_mutex_;
525533
std::unordered_map<uint16_t, std::unique_ptr<PerIsolateWrapperData>>
@@ -541,36 +549,16 @@ IsolateData::IsolateData(Isolate* isolate,
541549
v8::CppHeap* cpp_heap = isolate->GetCppHeap();
542550

543551
uint16_t cppgc_id = kDefaultCppGCEmebdderID;
544-
if (cpp_heap != nullptr) {
545-
// The general convention of the wrappable layout for cppgc in the
546-
// ecosystem is:
547-
// [ 0 ] -> embedder id
548-
// [ 1 ] -> wrappable instance
549-
// If the Isolate includes a CppHeap attached by another embedder,
550-
// And if they also use the field 0 for the ID, we DCHECK that
551-
// the layout matches our layout, and record the embedder ID for cppgc
552-
// to avoid accidentally enabling cppgc on non-cppgc-managed wrappers .
553-
v8::WrapperDescriptor descriptor = cpp_heap->wrapper_descriptor();
554-
if (descriptor.wrappable_type_index == BaseObject::kEmbedderType) {
555-
cppgc_id = descriptor.embedder_id_for_garbage_collected;
556-
DCHECK_EQ(descriptor.wrappable_instance_index, BaseObject::kSlot);
557-
}
558-
// If the CppHeap uses the slot we use to put non-cppgc-traced BaseObject
559-
// for embedder ID, V8 could accidentally enable cppgc on them. So
560-
// safe guard against this.
561-
DCHECK_NE(descriptor.wrappable_type_index, BaseObject::kSlot);
562-
} else {
563-
cpp_heap_ = CppHeap::Create(
564-
platform,
565-
CppHeapCreateParams{
566-
{},
567-
WrapperDescriptor(
568-
BaseObject::kEmbedderType, BaseObject::kSlot, cppgc_id)});
569-
isolate->AttachCppHeap(cpp_heap_.get());
570-
}
571552
// We do not care about overflow since we just want this to be different
572553
// from the cppgc id.
573554
uint16_t non_cppgc_id = cppgc_id + 1;
555+
if (cpp_heap == nullptr) {
556+
cpp_heap_ = CppHeap::Create(platform, v8::CppHeapCreateParams{{}});
557+
// TODO(joyeecheung): pass it into v8::Isolate::CreateParams and let V8
558+
// own it when we can keep the isolate registered/task runner discoverable
559+
// during isolate disposal.
560+
isolate->AttachCppHeap(cpp_heap_.get());
561+
}
574562

575563
{
576564
// GC could still be run after the IsolateData is destroyed, so we store
@@ -602,11 +590,12 @@ IsolateData::~IsolateData() {
602590
}
603591
}
604592

605-
// Public API
593+
// Deprecated API, embedders should use v8::Object::Wrap() directly instead.
606594
void SetCppgcReference(Isolate* isolate,
607595
Local<Object> object,
608596
void* wrappable) {
609-
IsolateData::SetCppgcReference(isolate, object, wrappable);
597+
v8::Object::Wrap<v8::CppHeapPointerTag::kDefaultTag>(
598+
isolate, object, wrappable);
610599
}
611600

612601
void IsolateData::MemoryInfo(MemoryTracker* tracker) const {

src/env.h

-4
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,6 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
165165
uint16_t* embedder_id_for_cppgc() const;
166166
uint16_t* embedder_id_for_non_cppgc() const;
167167

168-
static inline void SetCppgcReference(v8::Isolate* isolate,
169-
v8::Local<v8::Object> object,
170-
void* wrappable);
171-
172168
inline uv_loop_t* event_loop() const;
173169
inline MultiIsolatePlatform* platform() const;
174170
inline const SnapshotData* snapshot_data() const;

src/node.h

+8-18
Original file line numberDiff line numberDiff line change
@@ -1548,24 +1548,14 @@ void RegisterSignalHandler(int signal,
15481548
bool reset_handler = false);
15491549
#endif // _WIN32
15501550

1551-
// Configure the layout of the JavaScript object with a cppgc::GarbageCollected
1552-
// instance so that when the JavaScript object is reachable, the garbage
1553-
// collected instance would have its Trace() method invoked per the cppgc
1554-
// contract. To make it work, the process must have called
1555-
// cppgc::InitializeProcess() before, which is usually the case for addons
1556-
// loaded by the stand-alone Node.js executable. Embedders of Node.js can use
1557-
// either need to call it themselves or make sure that
1558-
// ProcessInitializationFlags::kNoInitializeCppgc is *not* set for cppgc to
1559-
// work.
1560-
// If the CppHeap is owned by Node.js, which is usually the case for addon,
1561-
// the object must be created with at least two internal fields available,
1562-
// and the first two internal fields would be configured by Node.js.
1563-
// This may be superseded by a V8 API in the future, see
1564-
// https://bugs.chromium.org/p/v8/issues/detail?id=13960. Until then this
1565-
// serves as a helper for Node.js isolates.
1566-
NODE_EXTERN void SetCppgcReference(v8::Isolate* isolate,
1567-
v8::Local<v8::Object> object,
1568-
void* wrappable);
1551+
// This is kept as a compatibility layer for addons to wrap cppgc-managed
1552+
// objects on Node.js versions without v8::Object::Wrap(). Addons created to
1553+
// work with only Node.js versions with v8::Object::Wrap() should use that
1554+
// instead.
1555+
NODE_DEPRECATED("Use v8::Object::Wrap()",
1556+
NODE_EXTERN void SetCppgcReference(v8::Isolate* isolate,
1557+
v8::Local<v8::Object> object,
1558+
void* wrappable));
15691559

15701560
} // namespace node
15711561

test/addons/cppgc-object/binding.cc

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
#include <assert.h>
12
#include <cppgc/allocation.h>
23
#include <cppgc/garbage-collected.h>
34
#include <cppgc/heap.h>
45
#include <node.h>
56
#include <v8-cppgc.h>
7+
#include <v8-sandbox.h>
68
#include <v8.h>
79
#include <algorithm>
810

@@ -15,21 +17,17 @@ class CppGCed : public cppgc::GarbageCollected<CppGCed> {
1517
static void New(const v8::FunctionCallbackInfo<v8::Value>& args) {
1618
v8::Isolate* isolate = args.GetIsolate();
1719
v8::Local<v8::Object> js_object = args.This();
18-
CppGCed* gc_object = cppgc::MakeGarbageCollected<CppGCed>(
19-
isolate->GetCppHeap()->GetAllocationHandle());
20+
auto* heap = isolate->GetCppHeap();
21+
assert(heap != nullptr);
22+
CppGCed* gc_object =
23+
cppgc::MakeGarbageCollected<CppGCed>(heap->GetAllocationHandle());
2024
node::SetCppgcReference(isolate, js_object, gc_object);
2125
args.GetReturnValue().Set(js_object);
2226
}
2327

2428
static v8::Local<v8::Function> GetConstructor(
2529
v8::Local<v8::Context> context) {
2630
auto ft = v8::FunctionTemplate::New(context->GetIsolate(), New);
27-
auto ot = ft->InstanceTemplate();
28-
v8::WrapperDescriptor descriptor =
29-
context->GetIsolate()->GetCppHeap()->wrapper_descriptor();
30-
uint16_t required_size = std::max(descriptor.wrappable_instance_index,
31-
descriptor.wrappable_type_index);
32-
ot->SetInternalFieldCount(required_size + 1);
3331
return ft->GetFunction(context).ToLocalChecked();
3432
}
3533

test/cctest/test_cppgc.cc

+12-19
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
#include <cppgc/heap.h>
44
#include <node.h>
55
#include <v8-cppgc.h>
6+
#include <v8-sandbox.h>
67
#include <v8.h>
78
#include "node_test_fixture.h"
89

910
// This tests that Node.js can work with an existing CppHeap.
1011

11-
// Mimic the Blink layout.
12-
static int kWrappableTypeIndex = 0;
13-
static int kWrappableInstanceIndex = 1;
14-
static uint16_t kEmbedderID = 0x1;
15-
1612
// Mimic a class that does not know about Node.js.
1713
class CppGCed : public cppgc::GarbageCollected<CppGCed> {
1814
public:
@@ -23,21 +19,18 @@ class CppGCed : public cppgc::GarbageCollected<CppGCed> {
2319
static void New(const v8::FunctionCallbackInfo<v8::Value>& args) {
2420
v8::Isolate* isolate = args.GetIsolate();
2521
v8::Local<v8::Object> js_object = args.This();
26-
CppGCed* gc_object = cppgc::MakeGarbageCollected<CppGCed>(
27-
isolate->GetCppHeap()->GetAllocationHandle());
28-
js_object->SetAlignedPointerInInternalField(kWrappableTypeIndex,
29-
&kEmbedderID);
30-
js_object->SetAlignedPointerInInternalField(kWrappableInstanceIndex,
31-
gc_object);
22+
auto* heap = isolate->GetCppHeap();
23+
CHECK_NOT_NULL(heap);
24+
CppGCed* gc_object =
25+
cppgc::MakeGarbageCollected<CppGCed>(heap->GetAllocationHandle());
26+
node::SetCppgcReference(isolate, js_object, gc_object);
3227
kConstructCount++;
3328
args.GetReturnValue().Set(js_object);
3429
}
3530

3631
static v8::Local<v8::Function> GetConstructor(
3732
v8::Local<v8::Context> context) {
3833
auto ft = v8::FunctionTemplate::New(context->GetIsolate(), New);
39-
auto ot = ft->InstanceTemplate();
40-
ot->SetInternalFieldCount(2);
4134
return ft->GetFunction(context).ToLocalChecked();
4235
}
4336

@@ -58,12 +51,12 @@ TEST_F(NodeZeroIsolateTestFixture, ExistingCppHeapTest) {
5851

5952
// Create and attach the CppHeap before we set up the IsolateData so that
6053
// it recognizes the existing heap.
61-
std::unique_ptr<v8::CppHeap> cpp_heap = v8::CppHeap::Create(
62-
platform.get(),
63-
v8::CppHeapCreateParams(
64-
{},
65-
v8::WrapperDescriptor(
66-
kWrappableTypeIndex, kWrappableInstanceIndex, kEmbedderID)));
54+
std::unique_ptr<v8::CppHeap> cpp_heap =
55+
v8::CppHeap::Create(platform.get(), v8::CppHeapCreateParams{{}});
56+
57+
// TODO(joyeecheung): pass it into v8::Isolate::CreateParams and let V8
58+
// own it when we can keep the isolate registered/task runner discoverable
59+
// during isolate disposal.
6760
isolate->AttachCppHeap(cpp_heap.get());
6861

6962
// Try creating Context + IsolateData + Environment.

0 commit comments

Comments
 (0)