Skip to content

Commit 32379a7

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

File tree

4 files changed

+7
-56
lines changed

4 files changed

+7
-56
lines changed

src/env-inl.h

Lines changed: 2 additions & 20 deletions
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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ using v8::TryCatch;
7070
using v8::Uint32;
7171
using v8::Undefined;
7272
using v8::Value;
73-
using v8::WrapperDescriptor;
7473
using worker::Worker;
7574

7675
int const ContextEmbedderTag::kNodeContextTag = 0x6e6f64;
@@ -542,30 +541,11 @@ IsolateData::IsolateData(Isolate* isolate,
542541

543542
uint16_t cppgc_id = kDefaultCppGCEmebdderID;
544543
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);
544+
// All CppHeap's support only one way of wrapping objects.
562545
} else {
563546
cpp_heap_ = CppHeap::Create(
564547
platform,
565-
CppHeapCreateParams{
566-
{},
567-
WrapperDescriptor(
568-
BaseObject::kEmbedderType, BaseObject::kSlot, cppgc_id)});
548+
CppHeapCreateParams{{}});
569549
isolate->AttachCppHeap(cpp_heap_.get());
570550
}
571551
// We do not care about overflow since we just want this to be different

test/addons/cppgc-object/binding.cc

Lines changed: 0 additions & 5 deletions
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

Lines changed: 3 additions & 9 deletions
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)