Skip to content

Commit b21e7c7

Browse files
committed
embedding: make NewIsolate() API more flexible
Split the API up into its essential parts, namely setting up the creation parameters for the Isolate, creating it, and performing Node.js-specific customization afterwards. PR-URL: #26525 Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 0e3addd commit b21e7c7

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

src/api/environment.cc

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,30 +161,25 @@ void FreeArrayBufferAllocator(ArrayBufferAllocator* allocator) {
161161
delete allocator;
162162
}
163163

164-
Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
165-
Isolate::CreateParams params;
166-
params.array_buffer_allocator = allocator;
164+
void SetIsolateCreateParams(Isolate::CreateParams* params,
165+
ArrayBufferAllocator* allocator) {
166+
if (allocator != nullptr)
167+
params->array_buffer_allocator = allocator;
167168

168169
double total_memory = uv_get_total_memory();
169170
if (total_memory > 0) {
170171
// V8 defaults to 700MB or 1.4GB on 32 and 64 bit platforms respectively.
171172
// This default is based on browser use-cases. Tell V8 to configure the
172173
// heap based on the actual physical memory.
173-
params.constraints.ConfigureDefaults(total_memory, 0);
174+
params->constraints.ConfigureDefaults(total_memory, 0);
174175
}
175176

176177
#ifdef NODE_ENABLE_VTUNE_PROFILING
177-
params.code_event_handler = vTune::GetVtuneCodeEventHandler();
178+
params->code_event_handler = vTune::GetVtuneCodeEventHandler();
178179
#endif
180+
}
179181

180-
Isolate* isolate = Isolate::Allocate();
181-
if (isolate == nullptr) return nullptr;
182-
183-
// Register the isolate on the platform before the isolate gets initialized,
184-
// so that the isolate can access the platform during initialization.
185-
per_process::v8_platform.Platform()->RegisterIsolate(isolate, event_loop);
186-
Isolate::Initialize(isolate, params);
187-
182+
void SetIsolateUpForNode(v8::Isolate* isolate) {
188183
isolate->AddMessageListenerWithErrorLevel(
189184
OnMessage,
190185
Isolate::MessageErrorLevel::kMessageError |
@@ -193,7 +188,29 @@ Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
193188
isolate->SetMicrotasksPolicy(MicrotasksPolicy::kExplicit);
194189
isolate->SetFatalErrorHandler(OnFatalError);
195190
isolate->SetAllowWasmCodeGenerationCallback(AllowWasmCodeGenerationCallback);
191+
isolate->SetPromiseRejectCallback(task_queue::PromiseRejectCallback);
196192
v8::CpuProfiler::UseDetailedSourcePositionsForProfiling(isolate);
193+
}
194+
195+
Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
196+
return NewIsolate(allocator, event_loop, GetMainThreadMultiIsolatePlatform());
197+
}
198+
199+
Isolate* NewIsolate(ArrayBufferAllocator* allocator,
200+
uv_loop_t* event_loop,
201+
MultiIsolatePlatform* platform) {
202+
Isolate::CreateParams params;
203+
SetIsolateCreateParams(&params, allocator);
204+
205+
Isolate* isolate = Isolate::Allocate();
206+
if (isolate == nullptr) return nullptr;
207+
208+
// Register the isolate on the platform before the isolate gets initialized,
209+
// so that the isolate can access the platform during initialization.
210+
platform->RegisterIsolate(isolate, event_loop);
211+
Isolate::Initialize(isolate, params);
212+
213+
SetIsolateUpForNode(isolate);
197214

198215
return isolate;
199216
}

src/env.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,6 @@ Environment::Environment(IsolateData* isolate_data,
253253
if (options_->no_force_async_hooks_checks) {
254254
async_hooks_.no_force_checks();
255255
}
256-
257-
// TODO(addaleax): the per-isolate state should not be controlled by
258-
// a single Environment.
259-
isolate()->SetPromiseRejectCallback(task_queue::PromiseRejectCallback);
260256
}
261257

262258
CompileFnEntry::CompileFnEntry(Environment* env, uint32_t id)

src/node.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,24 @@ class NODE_EXTERN MultiIsolatePlatform : public v8::Platform {
270270
void* data) = 0;
271271
};
272272

273+
// Set up some Node.js-specific defaults for `params`, in particular
274+
// the ArrayBuffer::Allocator if it is provided, memory limits, and
275+
// possibly a code event handler.
276+
NODE_EXTERN void SetIsolateCreateParams(v8::Isolate::CreateParams* params,
277+
ArrayBufferAllocator* allocator
278+
= nullptr);
279+
// Set a number of callbacks for the `isolate`, in particular the Node.js
280+
// uncaught exception listener.
281+
NODE_EXTERN void SetIsolateUpForNode(v8::Isolate* isolate);
273282
// Creates a new isolate with Node.js-specific settings.
283+
// This is a convenience method equivalent to using SetIsolateCreateParams(),
284+
// Isolate::Allocate(), MultiIsolatePlatform::RegisterIsolate(),
285+
// Isolate::Initialize(), and SetIsolateUpForNode().
274286
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator,
275287
struct uv_loop_s* event_loop);
288+
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator,
289+
struct uv_loop_s* event_loop,
290+
MultiIsolatePlatform* platform);
276291

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

0 commit comments

Comments
 (0)