|
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>> |
@@ -570,36 +578,16 @@ IsolateData::IsolateData(Isolate* isolate, |
570 | 578 | v8::CppHeap* cpp_heap = isolate->GetCppHeap(); |
571 | 579 |
|
572 | 580 | uint16_t cppgc_id = kDefaultCppGCEmbedderID; |
573 | | - if (cpp_heap != nullptr) { |
574 | | - // The general convention of the wrappable layout for cppgc in the |
575 | | - // ecosystem is: |
576 | | - // [ 0 ] -> embedder id |
577 | | - // [ 1 ] -> wrappable instance |
578 | | - // If the Isolate includes a CppHeap attached by another embedder, |
579 | | - // And if they also use the field 0 for the ID, we DCHECK that |
580 | | - // the layout matches our layout, and record the embedder ID for cppgc |
581 | | - // to avoid accidentally enabling cppgc on non-cppgc-managed wrappers . |
582 | | - v8::WrapperDescriptor descriptor = cpp_heap->wrapper_descriptor(); |
583 | | - if (descriptor.wrappable_type_index == BaseObject::kEmbedderType) { |
584 | | - cppgc_id = descriptor.embedder_id_for_garbage_collected; |
585 | | - DCHECK_EQ(descriptor.wrappable_instance_index, BaseObject::kSlot); |
586 | | - } |
587 | | - // If the CppHeap uses the slot we use to put non-cppgc-traced BaseObject |
588 | | - // for embedder ID, V8 could accidentally enable cppgc on them. So |
589 | | - // safe guard against this. |
590 | | - DCHECK_NE(descriptor.wrappable_type_index, BaseObject::kSlot); |
591 | | - } else { |
592 | | - cpp_heap_ = CppHeap::Create( |
593 | | - platform, |
594 | | - CppHeapCreateParams{ |
595 | | - {}, |
596 | | - WrapperDescriptor( |
597 | | - BaseObject::kEmbedderType, BaseObject::kSlot, cppgc_id)}); |
598 | | - isolate->AttachCppHeap(cpp_heap_.get()); |
599 | | - } |
600 | 581 | // We do not care about overflow since we just want this to be different |
601 | 582 | // from the cppgc id. |
602 | 583 | uint16_t non_cppgc_id = cppgc_id + 1; |
| 584 | + if (cpp_heap == nullptr) { |
| 585 | + cpp_heap_ = CppHeap::Create(platform, v8::CppHeapCreateParams{{}}); |
| 586 | + // TODO(joyeecheung): pass it into v8::Isolate::CreateParams and let V8 |
| 587 | + // own it when we can keep the isolate registered/task runner discoverable |
| 588 | + // during isolate disposal. |
| 589 | + isolate->AttachCppHeap(cpp_heap_.get()); |
| 590 | + } |
603 | 591 |
|
604 | 592 | { |
605 | 593 | // GC could still be run after the IsolateData is destroyed, so we store |
@@ -631,11 +619,12 @@ IsolateData::~IsolateData() { |
631 | 619 | } |
632 | 620 | } |
633 | 621 |
|
634 | | -// Public API |
| 622 | +// Deprecated API, embedders should use v8::Object::Wrap() directly instead. |
635 | 623 | void SetCppgcReference(Isolate* isolate, |
636 | 624 | Local<Object> object, |
637 | 625 | void* wrappable) { |
638 | | - IsolateData::SetCppgcReference(isolate, object, wrappable); |
| 626 | + v8::Object::Wrap<v8::CppHeapPointerTag::kDefaultTag>( |
| 627 | + isolate, object, wrappable); |
639 | 628 | } |
640 | 629 |
|
641 | 630 | void IsolateData::MemoryInfo(MemoryTracker* tracker) const { |
|
0 commit comments