Skip to content

Commit a10668c

Browse files
targosgahaas
andcommitted
src: adapt to V8's CppHeap API changes
Closes: #52718 Co-authored-by: Andreas Haas <[email protected]>
1 parent d8cc375 commit a10668c

File tree

9 files changed

+45
-43
lines changed

9 files changed

+45
-43
lines changed

Diff for: src/api/environment.cc

+16-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ using errors::TryCatchScope;
2525
using v8::Array;
2626
using v8::Boolean;
2727
using v8::Context;
28+
using v8::CppHeap;
29+
using v8::CppHeapCreateParams;
2830
using v8::EscapableHandleScope;
2931
using v8::Function;
3032
using v8::FunctionCallbackInfo;
@@ -304,6 +306,10 @@ Isolate* NewIsolate(Isolate::CreateParams* params,
304306
MultiIsolatePlatform* platform,
305307
const SnapshotData* snapshot_data,
306308
const IsolateSettings& settings) {
309+
if (params->cpp_heap == nullptr) {
310+
params->cpp_heap =
311+
CppHeap::Create(platform, CppHeapCreateParams{{}}).release();
312+
}
307313
Isolate* isolate = Isolate::Allocate();
308314
if (isolate == nullptr) return nullptr;
309315

@@ -345,9 +351,13 @@ Isolate* NewIsolate(ArrayBufferAllocator* allocator,
345351
uv_loop_t* event_loop,
346352
MultiIsolatePlatform* platform,
347353
const EmbedderSnapshotData* snapshot_data,
348-
const IsolateSettings& settings) {
354+
const IsolateSettings& settings,
355+
std::unique_ptr<CppHeap> cpp_heap) {
349356
Isolate::CreateParams params;
350357
if (allocator != nullptr) params.array_buffer_allocator = allocator;
358+
if (cpp_heap) {
359+
params.cpp_heap = cpp_heap.release();
360+
}
351361
return NewIsolate(&params,
352362
event_loop,
353363
platform,
@@ -359,9 +369,13 @@ Isolate* NewIsolate(std::shared_ptr<ArrayBufferAllocator> allocator,
359369
uv_loop_t* event_loop,
360370
MultiIsolatePlatform* platform,
361371
const EmbedderSnapshotData* snapshot_data,
362-
const IsolateSettings& settings) {
372+
const IsolateSettings& settings,
373+
std::unique_ptr<CppHeap> cpp_heap) {
363374
Isolate::CreateParams params;
364375
if (allocator) params.array_buffer_allocator_shared = allocator;
376+
if (cpp_heap) {
377+
params.cpp_heap = cpp_heap.release();
378+
}
365379
return NewIsolate(&params,
366380
event_loop,
367381
platform,

Diff for: src/env.cc

-20
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ using v8::BackingStore;
4444
using v8::BackingStoreInitializationMode;
4545
using v8::Boolean;
4646
using v8::Context;
47-
using v8::CppHeap;
48-
using v8::CppHeapCreateParams;
4947
using v8::EmbedderGraph;
5048
using v8::EscapableHandleScope;
5149
using v8::ExternalMemoryAccounter;
@@ -580,19 +578,10 @@ IsolateData::IsolateData(Isolate* isolate,
580578
platform_(platform),
581579
snapshot_data_(snapshot_data),
582580
options_(std::move(options)) {
583-
v8::CppHeap* cpp_heap = isolate->GetCppHeap();
584-
585581
uint16_t cppgc_id = kDefaultCppGCEmbedderID;
586582
// We do not care about overflow since we just want this to be different
587583
// from the cppgc id.
588584
uint16_t non_cppgc_id = cppgc_id + 1;
589-
if (cpp_heap == nullptr) {
590-
cpp_heap_ = CppHeap::Create(platform, v8::CppHeapCreateParams{{}});
591-
// TODO(joyeecheung): pass it into v8::Isolate::CreateParams and let V8
592-
// own it when we can keep the isolate registered/task runner discoverable
593-
// during isolate disposal.
594-
isolate->AttachCppHeap(cpp_heap_.get());
595-
}
596585

597586
{
598587
// GC could still be run after the IsolateData is destroyed, so we store
@@ -616,15 +605,6 @@ IsolateData::IsolateData(Isolate* isolate,
616605
}
617606
}
618607

619-
IsolateData::~IsolateData() {
620-
if (cpp_heap_ != nullptr) {
621-
v8::Locker locker(isolate_);
622-
// The CppHeap must be detached before being terminated.
623-
isolate_->DetachCppHeap();
624-
cpp_heap_->Terminate();
625-
}
626-
}
627-
628608
// Deprecated API, embedders should use v8::Object::Wrap() directly instead.
629609
void SetCppgcReference(Isolate* isolate,
630610
Local<Object> object,

Diff for: src/env.h

-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
143143
ArrayBufferAllocator* node_allocator = nullptr,
144144
const EmbedderSnapshotData* embedder_snapshot_data = nullptr,
145145
std::shared_ptr<PerIsolateOptions> options = nullptr);
146-
~IsolateData();
147146

148147
SET_MEMORY_INFO_NAME(IsolateData)
149148
SET_SELF_SIZE(IsolateData)
@@ -246,7 +245,6 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
246245
const SnapshotData* snapshot_data_;
247246
std::optional<SnapshotConfig> snapshot_config_;
248247

249-
std::unique_ptr<v8::CppHeap> cpp_heap_;
250248
std::shared_ptr<PerIsolateOptions> options_;
251249
worker::Worker* worker_context_ = nullptr;
252250
PerIsolateWrapperData* wrapper_data_;

Diff for: src/node.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,14 @@ InitializeOncePerProcessInternal(const std::vector<std::string>& args,
12111211
result->platform_ = per_process::v8_platform.Platform();
12121212
}
12131213

1214+
if (!(flags & ProcessInitializationFlags::kNoInitializeCppgc)) {
1215+
v8::PageAllocator* allocator = nullptr;
1216+
if (result->platform_ != nullptr) {
1217+
allocator = result->platform_->GetPageAllocator();
1218+
}
1219+
cppgc::InitializeProcess(allocator);
1220+
}
1221+
12141222
if (!(flags & ProcessInitializationFlags::kNoInitializeV8)) {
12151223
V8::Initialize();
12161224

@@ -1220,14 +1228,6 @@ InitializeOncePerProcessInternal(const std::vector<std::string>& args,
12201228
absl::SetMutexDeadlockDetectionMode(absl::OnDeadlockCycle::kIgnore);
12211229
}
12221230

1223-
if (!(flags & ProcessInitializationFlags::kNoInitializeCppgc)) {
1224-
v8::PageAllocator* allocator = nullptr;
1225-
if (result->platform_ != nullptr) {
1226-
allocator = result->platform_->GetPageAllocator();
1227-
}
1228-
cppgc::InitializeProcess(allocator);
1229-
}
1230-
12311231
#if NODE_USE_V8_WASM_TRAP_HANDLER
12321232
bool use_wasm_trap_handler =
12331233
!per_process::cli_options->disable_wasm_trap_handler;

Diff for: src/node.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -580,13 +580,15 @@ NODE_EXTERN v8::Isolate* NewIsolate(
580580
struct uv_loop_s* event_loop,
581581
MultiIsolatePlatform* platform,
582582
const EmbedderSnapshotData* snapshot_data = nullptr,
583-
const IsolateSettings& settings = {});
583+
const IsolateSettings& settings = {},
584+
std::unique_ptr<v8::CppHeap> cpp_heap = {});
584585
NODE_EXTERN v8::Isolate* NewIsolate(
585586
std::shared_ptr<ArrayBufferAllocator> allocator,
586587
struct uv_loop_s* event_loop,
587588
MultiIsolatePlatform* platform,
588589
const EmbedderSnapshotData* snapshot_data = nullptr,
589-
const IsolateSettings& settings = {});
590+
const IsolateSettings& settings = {},
591+
std::unique_ptr<v8::CppHeap> cpp_heap = {});
590592

591593
// Creates a new context with Node.js-specific tweaks.
592594
NODE_EXTERN v8::Local<v8::Context> NewContext(

Diff for: src/node_main_instance.cc

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
namespace node {
2626

2727
using v8::Context;
28+
using v8::CppHeap;
29+
using v8::CppHeapCreateParams;
2830
using v8::HandleScope;
2931
using v8::Isolate;
3032
using v8::Local;
@@ -44,6 +46,8 @@ NodeMainInstance::NodeMainInstance(const SnapshotData* snapshot_data,
4446
isolate_params_(std::make_unique<Isolate::CreateParams>()),
4547
snapshot_data_(snapshot_data) {
4648
isolate_params_->array_buffer_allocator = array_buffer_allocator_.get();
49+
isolate_params_->cpp_heap =
50+
CppHeap::Create(platform_, CppHeapCreateParams{{}}).release();
4751

4852
isolate_ =
4953
NewIsolate(isolate_params_.get(), event_loop, platform, snapshot_data);

Diff for: src/node_worker.cc

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ using v8::Array;
2323
using v8::ArrayBuffer;
2424
using v8::Boolean;
2525
using v8::Context;
26+
using v8::CppHeap;
27+
using v8::CppHeapCreateParams;
2628
using v8::Float64Array;
2729
using v8::FunctionCallbackInfo;
2830
using v8::FunctionTemplate;
@@ -162,6 +164,8 @@ class WorkerThreadData {
162164
SetIsolateCreateParamsForNode(&params);
163165
w->UpdateResourceConstraints(&params.constraints);
164166
params.array_buffer_allocator_shared = allocator;
167+
params.cpp_heap =
168+
CppHeap::Create(w->platform_, CppHeapCreateParams{{}}).release();
165169
Isolate* isolate =
166170
NewIsolate(&params, &loop_, w->platform_, w->snapshot_data());
167171
if (isolate == nullptr) {

Diff for: test/cctest/test_cppgc.cc

+6-9
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,17 @@ int CppGCed::kDestructCount = 0;
4646
int CppGCed::kTraceCount = 0;
4747

4848
TEST_F(NodeZeroIsolateTestFixture, ExistingCppHeapTest) {
49-
v8::Isolate* isolate =
50-
node::NewIsolate(allocator.get(), &current_loop, platform.get());
51-
5249
// Create and attach the CppHeap before we set up the IsolateData so that
5350
// it recognizes the existing heap.
5451
std::unique_ptr<v8::CppHeap> cpp_heap =
5552
v8::CppHeap::Create(platform.get(), v8::CppHeapCreateParams{{}});
5653

57-
// TODO(joyeecheung): pass it into v8::Isolate::CreateParams and let V8
58-
// own it when we can keep the isolate registered/task runner discoverable
59-
// during isolate disposal.
60-
isolate->AttachCppHeap(cpp_heap.get());
54+
v8::Isolate* isolate = node::NewIsolate(allocator.get(),
55+
&current_loop,
56+
platform.get(),
57+
nullptr,
58+
{},
59+
std::move(cpp_heap));
6160

6261
// Try creating Context + IsolateData + Environment.
6362
{
@@ -102,8 +101,6 @@ TEST_F(NodeZeroIsolateTestFixture, ExistingCppHeapTest) {
102101
platform->DrainTasks(isolate);
103102

104103
// Cleanup.
105-
isolate->DetachCppHeap();
106-
cpp_heap->Terminate();
107104
platform->DrainTasks(isolate);
108105
}
109106

Diff for: test/cctest/test_environment.cc

+3
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,9 @@ TEST_F(NodeZeroIsolateTestFixture, CtrlCWithOnlySafeTerminationTest) {
625625
// Allocate and initialize Isolate.
626626
v8::Isolate::CreateParams create_params;
627627
create_params.array_buffer_allocator = allocator.get();
628+
create_params.cpp_heap =
629+
v8::CppHeap::Create(platform.get(), v8::CppHeapCreateParams{{}})
630+
.release();
628631
v8::Isolate* isolate = v8::Isolate::Allocate();
629632
CHECK_NOT_NULL(isolate);
630633
platform->RegisterIsolate(isolate, &current_loop);

0 commit comments

Comments
 (0)