23
23
#include " util-inl.h"
24
24
#include " v8-cppgc.h"
25
25
#include " v8-profiler.h"
26
+ #include " v8-sandbox.h" // v8::Object::Wrap(), v8::Object::Unwrap()
26
27
27
28
#include < algorithm>
28
29
#include < atomic>
@@ -71,7 +72,6 @@ using v8::TryCatch;
71
72
using v8::Uint32;
72
73
using v8::Undefined;
73
74
using v8::Value;
74
- using v8::WrapperDescriptor;
75
75
using worker::Worker;
76
76
77
77
int const ContextEmbedderTag::kNodeContextTag = 0x6e6f64 ;
@@ -533,6 +533,14 @@ void IsolateData::CreateProperties() {
533
533
CreateEnvProxyTemplate (this );
534
534
}
535
535
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.
536
544
constexpr uint16_t kDefaultCppGCEmbedderID = 0x90de ;
537
545
Mutex IsolateData::isolate_data_mutex_;
538
546
std::unordered_map<uint16_t , std::unique_ptr<PerIsolateWrapperData>>
@@ -554,36 +562,16 @@ IsolateData::IsolateData(Isolate* isolate,
554
562
v8::CppHeap* cpp_heap = isolate->GetCppHeap ();
555
563
556
564
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
- }
584
565
// We do not care about overflow since we just want this to be different
585
566
// from the cppgc id.
586
567
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
+ }
587
575
588
576
{
589
577
// GC could still be run after the IsolateData is destroyed, so we store
@@ -615,11 +603,12 @@ IsolateData::~IsolateData() {
615
603
}
616
604
}
617
605
618
- // Public API
606
+ // Deprecated API, embedders should use v8::Object::Wrap() directly instead.
619
607
void SetCppgcReference (Isolate* isolate,
620
608
Local<Object> object,
621
609
void * wrappable) {
622
- IsolateData::SetCppgcReference (isolate, object, wrappable);
610
+ v8::Object::Wrap<v8::CppHeapPointerTag::kDefaultTag >(
611
+ isolate, object, wrappable);
623
612
}
624
613
625
614
void IsolateData::MemoryInfo (MemoryTracker* tracker) const {
0 commit comments