2323#include " util-inl.h"
2424#include " v8-cppgc.h"
2525#include " v8-profiler.h"
26+ #include " v8-sandbox.h"
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 ;
@@ -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, embeders
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.
523531constexpr uint16_t kDefaultCppGCEmebdderID = 0x90de ;
524532Mutex IsolateData::isolate_data_mutex_;
525533std::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.
606594void 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
612601void IsolateData::MemoryInfo (MemoryTracker* tracker) const {
0 commit comments