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