Skip to content

Commit 662d1aa

Browse files
committed
src,tools: initialize cppgc
Initialize cppgc, create a CppHeap, and attach it to the Isolate. This allows C++ addons to start using cppgc to manage objects. Refs: #40786
1 parent 3bed5f1 commit 662d1aa

6 files changed

+97
-3
lines changed

src/api/embed_helpers.cc

+19
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#include "node.h"
22
#include "env-inl.h"
33
#include "debug_utils-inl.h"
4+
#include "v8-cppgc.h"
45

56
using v8::Context;
7+
using v8::CppHeap;
8+
using v8::CppHeapCreateParams;
69
using v8::Function;
710
using v8::Global;
811
using v8::HandleScope;
@@ -13,6 +16,7 @@ using v8::Locker;
1316
using v8::Maybe;
1417
using v8::Nothing;
1518
using v8::SealHandleScope;
19+
using v8::WrapperDescriptor;
1620

1721
namespace node {
1822

@@ -78,6 +82,7 @@ struct CommonEnvironmentSetup::Impl {
7882
uv_loop_t loop;
7983
std::shared_ptr<ArrayBufferAllocator> allocator;
8084
Isolate* isolate = nullptr;
85+
std::unique_ptr<CppHeap> cpp_heap;
8186
DeleteFnPtr<IsolateData, FreeIsolateData> isolate_data;
8287
DeleteFnPtr<Environment, FreeEnvironment> env;
8388
Global<Context> context;
@@ -107,6 +112,17 @@ CommonEnvironmentSetup::CommonEnvironmentSetup(
107112
impl_->isolate = NewIsolate(impl_->allocator, &impl_->loop, platform);
108113
Isolate* isolate = impl_->isolate;
109114

115+
impl_->cpp_heap = CppHeap::Create(
116+
platform,
117+
CppHeapCreateParams {
118+
{},
119+
WrapperDescriptor(
120+
BaseObject::kEmbedderType,
121+
BaseObject::kSlot,
122+
0x90df
123+
});
124+
isolate->AttachCppHeap(impl_->cpp_heap.get());
125+
110126
{
111127
Locker locker(isolate);
112128
Isolate::Scope isolate_scope(isolate);
@@ -138,6 +154,9 @@ CommonEnvironmentSetup::~CommonEnvironmentSetup() {
138154
impl_->isolate_data.reset();
139155
}
140156

157+
impl_->cpp_heap->Terminate();
158+
isolate->DetachCppHeap();
159+
141160
bool platform_finished = false;
142161
impl_->platform->AddIsolateFinishedCallback(isolate, [](void* data) {
143162
*static_cast<bool*>(data) = true;

src/node.cc

+13
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,15 @@ InitializeOncePerProcessInternal(const std::vector<std::string>& args,
10461046
V8::Initialize();
10471047
}
10481048

1049+
if (!(flags & ProcessInitializationFlags::kNoInitializeCppgc)) {
1050+
// V8 falls back to an internal page_allocator if one isn't specified.
1051+
v8::PageAllocator *page_allocator = nullptr;
1052+
if (!(flags & ProcessInitializationFlags::kNoInitializeNodeV8Platform)) {
1053+
page_allocator = per_process:v8_platform.Platform()->GetPageAllocator();
1054+
}
1055+
cppgc::InitializeProcess(page_allocator);
1056+
}
1057+
10491058
performance::performance_v8_start = PERFORMANCE_NOW();
10501059
per_process::v8_initialized = true;
10511060

@@ -1065,6 +1074,10 @@ void TearDownOncePerProcess() {
10651074
ResetSignalHandlers();
10661075
}
10671076

1077+
if (!(flags & ProcessInitializationFlags::kNoInitializeCppgc)) {
1078+
cppgc::ShutdownProcess();
1079+
}
1080+
10681081
per_process::v8_initialized = false;
10691082
if (!(flags & ProcessInitializationFlags::kNoInitializeV8)) {
10701083
V8::Dispose();

src/node.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ enum Flags : uint64_t {
260260
kNoUseLargePages = 1 << 11,
261261
// Skip printing output for --help, --version, --v8-options.
262262
kNoPrintHelpOrVersionOutput = 1 << 12,
263+
// Do not perform cppgc initialization.
264+
kNoInitializeCppgc = 1 << 13,
263265

264266
// Emulate the behavior of InitializeNodeWithArgs() when passing
265267
// a flags argument to the InitializeOncePerProcess() replacement
@@ -268,7 +270,7 @@ enum Flags : uint64_t {
268270
kNoStdioInitialization | kNoDefaultSignalHandling | kNoInitializeV8 |
269271
kNoInitializeNodeV8Platform | kNoInitOpenSSL |
270272
kNoParseGlobalDebugVariables | kNoAdjustResourceLimits |
271-
kNoUseLargePages | kNoPrintHelpOrVersionOutput,
273+
kNoUseLargePages | kNoPrintHelpOrVersionOutput | kNoInitializeCppgc,
272274
};
273275
} // namespace ProcessFlags
274276
// TODO(addaleax): Make this the canonical name, as it is more descriptive.

src/node_main_instance.cc

+17
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "node_snapshotable.h"
1414
#include "node_v8_platform-inl.h"
1515
#include "util-inl.h"
16+
#include "v8-cppgc.h"
1617
#if defined(LEAK_SANITIZER)
1718
#include <sanitizer/lsan_interface.h>
1819
#endif
@@ -24,10 +25,13 @@
2425
namespace node {
2526

2627
using v8::Context;
28+
using v8::CppHeap;
29+
using v8::CppHeapCreateParams;
2730
using v8::HandleScope;
2831
using v8::Isolate;
2932
using v8::Local;
3033
using v8::Locker;
34+
using v8::WrapperDescriptor;
3135

3236
NodeMainInstance::NodeMainInstance(Isolate* isolate,
3337
uv_loop_t* event_loop,
@@ -84,6 +88,17 @@ NodeMainInstance::NodeMainInstance(const SnapshotData* snapshot_data,
8488
SetIsolateCreateParamsForNode(isolate_params_.get());
8589
Isolate::Initialize(isolate_, *isolate_params_);
8690

91+
cpp_heap_ = CppHeap::Create(
92+
platform,
93+
CppHeapCreateParams {
94+
{},
95+
WrapperDescriptor(
96+
BaseObject::kEmbedderType,
97+
BaseObject::kSlot,
98+
0x90df
99+
});
100+
isolate_->AttachCppHeap(cpp_heap_.get());
101+
87102
// If the indexes are not nullptr, we are not deserializing
88103
isolate_data_ = std::make_unique<IsolateData>(
89104
isolate_,
@@ -114,6 +129,8 @@ NodeMainInstance::~NodeMainInstance() {
114129
return;
115130
}
116131
// This should only be done on a main instance that owns its isolate.
132+
cpp_heap_->Terminate();
133+
isolate_->DetachCppHeap();
117134
platform_->UnregisterIsolate(isolate_);
118135
isolate_->Dispose();
119136
}

src/node_main_instance.h

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class NodeMainInstance {
8383
std::unique_ptr<ArrayBufferAllocator> array_buffer_allocator_;
8484
v8::Isolate* isolate_;
8585
MultiIsolatePlatform* platform_;
86+
std::unique_ptr<v8::CppHeap> cpp_heap_;
8687
std::unique_ptr<IsolateData> isolate_data_;
8788
std::unique_ptr<v8::Isolate::CreateParams> isolate_params_;
8889
const SnapshotData* snapshot_data_ = nullptr;

tools/install.py

+44-2
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,56 @@ def files(action):
197197
def headers(action):
198198
def wanted_v8_headers(files_arg, dest):
199199
v8_headers = [
200+
'deps/v8/include/cppgc/allocation.h',
200201
'deps/v8/include/cppgc/common.h',
201-
'deps/v8/include/libplatform/libplatform.h',
202+
'deps/v8/include/cppgc/cross-thread-persistent.h',
203+
'deps/v8/include/cppgc/custom-space.h',
204+
'deps/v8/include/cppgc/default-platform.h',
205+
'deps/v8/include/cppgc/ephemeron-pair.h',
206+
'deps/v8/include/cppgc/explicit-management.h',
207+
'deps/v8/include/cppgc/garbage-collected.h',
208+
'deps/v8/include/cppgc/heap-consistency.h',
209+
'deps/v8/include/cppgc/heap-handle.h',
210+
'deps/v8/include/cppgc/heap-state.h',
211+
'deps/v8/include/cppgc/heap-statistics.h',
212+
'deps/v8/include/cppgc/heap.h',
213+
'deps/v8/include/cppgc/internal/api-constants.h',
214+
'deps/v8/include/cppgc/internal/atomic-entry-flag.h',
215+
'deps/v8/include/cppgc/internal/base-page-handle.h',
216+
'deps/v8/include/cppgc/internal/caged-heap-local-data.h',
217+
'deps/v8/include/cppgc/internal/caged-heap.h',
218+
'deps/v8/include/cppgc/internal/compiler-specific.h',
219+
'deps/v8/include/cppgc/internal/finalizer-trait.h',
220+
'deps/v8/include/cppgc/internal/gc-info.h',
221+
'deps/v8/include/cppgc/internal/logging.h',
222+
'deps/v8/include/cppgc/internal/member-storage.h',
223+
'deps/v8/include/cppgc/internal/name-trait.h',
224+
'deps/v8/include/cppgc/internal/persistent-node.h',
225+
'deps/v8/include/cppgc/internal/pointer-policies.h',
226+
'deps/v8/include/cppgc/internal/write-barrier.h',
227+
'deps/v8/include/cppgc/liveness-broker.h',
228+
'deps/v8/include/cppgc/macros.h',
229+
'deps/v8/include/cppgc/member.h',
230+
'deps/v8/include/cppgc/name-provider.h',
231+
'deps/v8/include/cppgc/object-size-trait.h',
232+
'deps/v8/include/cppgc/persistent.h',
233+
'deps/v8/include/cppgc/platform.h',
234+
'deps/v8/include/cppgc/prefinalizer.h',
235+
'deps/v8/include/cppgc/process-heap-statistics.h',
236+
'deps/v8/include/cppgc/sentinel-pointer.h',
237+
'deps/v8/include/cppgc/source-location.h',
238+
'deps/v8/include/cppgc/testing.h',
239+
'deps/v8/include/cppgc/trace-trait.h',
240+
'deps/v8/include/cppgc/type-traits.h',
241+
'deps/v8/include/cppgc/visitor.h',
202242
'deps/v8/include/libplatform/libplatform-export.h',
243+
'deps/v8/include/libplatform/libplatform.h',
203244
'deps/v8/include/libplatform/v8-tracing.h',
204-
'deps/v8/include/v8.h',
205245
'deps/v8/include/v8-array-buffer.h',
206246
'deps/v8/include/v8-callbacks.h',
207247
'deps/v8/include/v8-container.h',
208248
'deps/v8/include/v8-context.h',
249+
'deps/v8/include/v8-cppgc.h',
209250
'deps/v8/include/v8-data.h',
210251
'deps/v8/include/v8-date.h',
211252
'deps/v8/include/v8-debug.h',
@@ -249,6 +290,7 @@ def wanted_v8_headers(files_arg, dest):
249290
'deps/v8/include/v8-version.h',
250291
'deps/v8/include/v8-wasm.h',
251292
'deps/v8/include/v8-weak-callback-info.h',
293+
'deps/v8/include/v8.h',
252294
'deps/v8/include/v8config.h',
253295
]
254296
files_arg = [name for name in files_arg if name in v8_headers]

0 commit comments

Comments
 (0)