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;
7071using v8::Uint32;
7172using v8::Undefined;
7273using v8::Value;
73- using v8::WrapperDescriptor;
7474using worker::Worker;
7575
7676int const ContextEmbedderTag::kNodeContextTag = 0x6e6f64 ;
@@ -532,6 +532,14 @@ void IsolateData::CreateProperties() {
532532 CreateEnvProxyTemplate (this );
533533}
534534
535+ // Previously, the general convention of the wrappable layout for cppgc in
536+ // the ecosystem is:
537+ // [ 0 ] -> embedder id
538+ // [ 1 ] -> wrappable instance
539+ // Now V8 has deprecated this layout-based tracing enablement, embedders
540+ // should simply use v8::Object::Wrap() and v8::Object::Unwrap(). We preserve
541+ // this layout only to distinguish internally how the memory of a Node.js
542+ // wrapper is managed or whether a wrapper is managed by Node.js.
535543constexpr uint16_t kDefaultCppGCEmbedderID = 0x90de ;
536544Mutex IsolateData::isolate_data_mutex_;
537545std::unordered_map<uint16_t , std::unique_ptr<PerIsolateWrapperData>>
@@ -553,36 +561,16 @@ IsolateData::IsolateData(Isolate* isolate,
553561 v8::CppHeap* cpp_heap = isolate->GetCppHeap ();
554562
555563 uint16_t cppgc_id = kDefaultCppGCEmbedderID ;
556- if (cpp_heap != nullptr ) {
557- // The general convention of the wrappable layout for cppgc in the
558- // ecosystem is:
559- // [ 0 ] -> embedder id
560- // [ 1 ] -> wrappable instance
561- // If the Isolate includes a CppHeap attached by another embedder,
562- // And if they also use the field 0 for the ID, we DCHECK that
563- // the layout matches our layout, and record the embedder ID for cppgc
564- // to avoid accidentally enabling cppgc on non-cppgc-managed wrappers .
565- v8::WrapperDescriptor descriptor = cpp_heap->wrapper_descriptor ();
566- if (descriptor.wrappable_type_index == BaseObject::kEmbedderType ) {
567- cppgc_id = descriptor.embedder_id_for_garbage_collected ;
568- DCHECK_EQ (descriptor.wrappable_instance_index , BaseObject::kSlot );
569- }
570- // If the CppHeap uses the slot we use to put non-cppgc-traced BaseObject
571- // for embedder ID, V8 could accidentally enable cppgc on them. So
572- // safe guard against this.
573- DCHECK_NE (descriptor.wrappable_type_index , BaseObject::kSlot );
574- } else {
575- cpp_heap_ = CppHeap::Create (
576- platform,
577- CppHeapCreateParams{
578- {},
579- WrapperDescriptor (
580- BaseObject::kEmbedderType , BaseObject::kSlot , cppgc_id)});
581- isolate->AttachCppHeap (cpp_heap_.get ());
582- }
583564 // We do not care about overflow since we just want this to be different
584565 // from the cppgc id.
585566 uint16_t non_cppgc_id = cppgc_id + 1 ;
567+ if (cpp_heap == nullptr ) {
568+ cpp_heap_ = CppHeap::Create (platform, v8::CppHeapCreateParams{{}});
569+ // TODO(joyeecheung): pass it into v8::Isolate::CreateParams and let V8
570+ // own it when we can keep the isolate registered/task runner discoverable
571+ // during isolate disposal.
572+ isolate->AttachCppHeap (cpp_heap_.get ());
573+ }
586574
587575 {
588576 // GC could still be run after the IsolateData is destroyed, so we store
@@ -614,11 +602,12 @@ IsolateData::~IsolateData() {
614602 }
615603}
616604
617- // Public API
605+ // Deprecated API, embedders should use v8::Object::Wrap() directly instead.
618606void SetCppgcReference (Isolate* isolate,
619607 Local<Object> object,
620608 void * wrappable) {
621- IsolateData::SetCppgcReference (isolate, object, wrappable);
609+ v8::Object::Wrap<v8::CppHeapPointerTag::kDefaultTag >(
610+ isolate, object, wrappable);
622611}
623612
624613void IsolateData::MemoryInfo (MemoryTracker* tracker) const {
0 commit comments