Skip to content

Commit 5e64acd

Browse files
addaleaxtargos
authored andcommitted
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 <gpunathi@in.ibm.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 7671a65 commit 5e64acd

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

src/api/environment.cc

+30-12
Original file line numberDiff line numberDiff line change
@@ -164,21 +164,17 @@ void FreeArrayBufferAllocator(ArrayBufferAllocator* allocator) {
164164
delete allocator;
165165
}
166166

167-
Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
168-
Isolate::CreateParams params;
169-
params.array_buffer_allocator = allocator;
167+
void SetIsolateCreateParams(Isolate::CreateParams* params,
168+
ArrayBufferAllocator* allocator) {
169+
if (allocator != nullptr)
170+
params->array_buffer_allocator = allocator;
171+
170172
#ifdef NODE_ENABLE_VTUNE_PROFILING
171-
params.code_event_handler = vTune::GetVtuneCodeEventHandler();
173+
params->code_event_handler = vTune::GetVtuneCodeEventHandler();
172174
#endif
175+
}
173176

174-
Isolate* isolate = Isolate::Allocate();
175-
if (isolate == nullptr) return nullptr;
176-
177-
// Register the isolate on the platform before the isolate gets initialized,
178-
// so that the isolate can access the platform during initialization.
179-
per_process::v8_platform.Platform()->RegisterIsolate(isolate, event_loop);
180-
Isolate::Initialize(isolate, params);
181-
177+
void SetIsolateUpForNode(v8::Isolate* isolate) {
182178
isolate->AddMessageListenerWithErrorLevel(
183179
OnMessage,
184180
Isolate::MessageErrorLevel::kMessageError |
@@ -187,7 +183,29 @@ Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
187183
isolate->SetMicrotasksPolicy(MicrotasksPolicy::kExplicit);
188184
isolate->SetFatalErrorHandler(OnFatalError);
189185
isolate->SetAllowWasmCodeGenerationCallback(AllowWasmCodeGenerationCallback);
186+
isolate->SetPromiseRejectCallback(task_queue::PromiseRejectCallback);
190187
v8::CpuProfiler::UseDetailedSourcePositionsForProfiling(isolate);
188+
}
189+
190+
Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
191+
return NewIsolate(allocator, event_loop, GetMainThreadMultiIsolatePlatform());
192+
}
193+
194+
Isolate* NewIsolate(ArrayBufferAllocator* allocator,
195+
uv_loop_t* event_loop,
196+
MultiIsolatePlatform* platform) {
197+
Isolate::CreateParams params;
198+
SetIsolateCreateParams(&params, allocator);
199+
200+
Isolate* isolate = Isolate::Allocate();
201+
if (isolate == nullptr) return nullptr;
202+
203+
// Register the isolate on the platform before the isolate gets initialized,
204+
// so that the isolate can access the platform during initialization.
205+
platform->RegisterIsolate(isolate, event_loop);
206+
Isolate::Initialize(isolate, params);
207+
208+
SetIsolateUpForNode(isolate);
191209

192210
return isolate;
193211
}

src/env.cc

-4
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,6 @@ Environment::Environment(IsolateData* isolate_data,
261261
if (options_->no_force_async_hooks_checks) {
262262
async_hooks_.no_force_checks();
263263
}
264-
265-
// TODO(addaleax): the per-isolate state should not be controlled by
266-
// a single Environment.
267-
isolate()->SetPromiseRejectCallback(task_queue::PromiseRejectCallback);
268264
}
269265

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

src/node.h

+15
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,24 @@ class NODE_EXTERN MultiIsolatePlatform : public v8::Platform {
262262
virtual void UnregisterIsolate(v8::Isolate* isolate) = 0;
263263
};
264264

265+
// Set up some Node.js-specific defaults for `params`, in particular
266+
// the ArrayBuffer::Allocator if it is provided, memory limits, and
267+
// possibly a code event handler.
268+
NODE_EXTERN void SetIsolateCreateParams(v8::Isolate::CreateParams* params,
269+
ArrayBufferAllocator* allocator
270+
= nullptr);
271+
// Set a number of callbacks for the `isolate`, in particular the Node.js
272+
// uncaught exception listener.
273+
NODE_EXTERN void SetIsolateUpForNode(v8::Isolate* isolate);
265274
// Creates a new isolate with Node.js-specific settings.
275+
// This is a convenience method equivalent to using SetIsolateCreateParams(),
276+
// Isolate::Allocate(), MultiIsolatePlatform::RegisterIsolate(),
277+
// Isolate::Initialize(), and SetIsolateUpForNode().
266278
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator,
267279
struct uv_loop_s* event_loop);
280+
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator,
281+
struct uv_loop_s* event_loop,
282+
MultiIsolatePlatform* platform);
268283

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

0 commit comments

Comments
 (0)