Skip to content

Commit b2c46c1

Browse files
isheludkomlippautz
authored andcommitted
Don't use WrapperDescriptor and instead use Wrap/Unwrap APIs (nodejs#187) (nodejs#188)
Co-authored-by: Michael Lippautz <[email protected]>
1 parent dfb1bb9 commit b2c46c1

File tree

4 files changed

+7
-56
lines changed

4 files changed

+7
-56
lines changed

src/env-inl.h

+2-20
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,8 @@ inline uv_loop_t* IsolateData::event_loop() const {
6565
inline void IsolateData::SetCppgcReference(v8::Isolate* isolate,
6666
v8::Local<v8::Object> object,
6767
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);
68+
v8::Object::Wrap<v8::CppHeapPointerTag::kDefaultTag>(
69+
isolate, object, wrappable);
8870
}
8971

9072
inline uint16_t* IsolateData::embedder_id_for_cppgc() const {

src/env.cc

+2-22
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ using v8::TryCatch;
7171
using v8::Uint32;
7272
using v8::Undefined;
7373
using v8::Value;
74-
using v8::WrapperDescriptor;
7574
using worker::Worker;
7675

7776
int const ContextEmbedderTag::kNodeContextTag = 0x6e6f64;
@@ -571,30 +570,11 @@ IsolateData::IsolateData(Isolate* isolate,
571570

572571
uint16_t cppgc_id = kDefaultCppGCEmbedderID;
573572
if (cpp_heap != nullptr) {
574-
// The general convention of the wrappable layout for cppgc in the
575-
// ecosystem is:
576-
// [ 0 ] -> embedder id
577-
// [ 1 ] -> wrappable instance
578-
// If the Isolate includes a CppHeap attached by another embedder,
579-
// And if they also use the field 0 for the ID, we DCHECK that
580-
// the layout matches our layout, and record the embedder ID for cppgc
581-
// to avoid accidentally enabling cppgc on non-cppgc-managed wrappers .
582-
v8::WrapperDescriptor descriptor = cpp_heap->wrapper_descriptor();
583-
if (descriptor.wrappable_type_index == BaseObject::kEmbedderType) {
584-
cppgc_id = descriptor.embedder_id_for_garbage_collected;
585-
DCHECK_EQ(descriptor.wrappable_instance_index, BaseObject::kSlot);
586-
}
587-
// If the CppHeap uses the slot we use to put non-cppgc-traced BaseObject
588-
// for embedder ID, V8 could accidentally enable cppgc on them. So
589-
// safe guard against this.
590-
DCHECK_NE(descriptor.wrappable_type_index, BaseObject::kSlot);
573+
// All CppHeap's support only one way of wrapping objects.
591574
} else {
592575
cpp_heap_ = CppHeap::Create(
593576
platform,
594-
CppHeapCreateParams{
595-
{},
596-
WrapperDescriptor(
597-
BaseObject::kEmbedderType, BaseObject::kSlot, cppgc_id)});
577+
CppHeapCreateParams{{}});
598578
isolate->AttachCppHeap(cpp_heap_.get());
599579
}
600580
// We do not care about overflow since we just want this to be different

test/addons/cppgc-object/binding.cc

-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ class CppGCed : public cppgc::GarbageCollected<CppGCed> {
2525
v8::Local<v8::Context> context) {
2626
auto ft = v8::FunctionTemplate::New(context->GetIsolate(), New);
2727
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);
3328
return ft->GetFunction(context).ToLocalChecked();
3429
}
3530

test/cctest/test_cppgc.cc

+3-9
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ class CppGCed : public cppgc::GarbageCollected<CppGCed> {
2525
v8::Local<v8::Object> js_object = args.This();
2626
CppGCed* gc_object = cppgc::MakeGarbageCollected<CppGCed>(
2727
isolate->GetCppHeap()->GetAllocationHandle());
28-
js_object->SetAlignedPointerInInternalField(kWrappableTypeIndex,
29-
&kEmbedderID);
30-
js_object->SetAlignedPointerInInternalField(kWrappableInstanceIndex,
31-
gc_object);
28+
v8::Object::Wrap<v8::CppHeapPointerTag::kDefaultTag>(
29+
isolate, js_object, gc_object);
3230
kConstructCount++;
3331
args.GetReturnValue().Set(js_object);
3432
}
@@ -37,7 +35,6 @@ class CppGCed : public cppgc::GarbageCollected<CppGCed> {
3735
v8::Local<v8::Context> context) {
3836
auto ft = v8::FunctionTemplate::New(context->GetIsolate(), New);
3937
auto ot = ft->InstanceTemplate();
40-
ot->SetInternalFieldCount(2);
4138
return ft->GetFunction(context).ToLocalChecked();
4239
}
4340

@@ -60,10 +57,7 @@ TEST_F(NodeZeroIsolateTestFixture, ExistingCppHeapTest) {
6057
// it recognizes the existing heap.
6158
std::unique_ptr<v8::CppHeap> cpp_heap = v8::CppHeap::Create(
6259
platform.get(),
63-
v8::CppHeapCreateParams(
64-
{},
65-
v8::WrapperDescriptor(
66-
kWrappableTypeIndex, kWrappableInstanceIndex, kEmbedderID)));
60+
v8::CppHeapCreateParams({}));
6761
isolate->AttachCppHeap(cpp_heap.get());
6862

6963
// Try creating Context + IsolateData + Environment.

0 commit comments

Comments
 (0)