Skip to content

Commit fc02cf5

Browse files
committed
src: expose granular SetIsolateUpForNode
This PR exposes a new embedder-focused API: SetIsolateUpForNode. It maintains previous behavior for the single-param version of SetIsolateUpForNode and changes no defaults, but was designed to be flexible by allowing for embedders to conditionally override all callbacks and flags set by the previous two-param version of SetIsolateUpForNode. PR-URL: #30150 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com>
1 parent 75dc893 commit fc02cf5

File tree

4 files changed

+88
-33
lines changed

4 files changed

+88
-33
lines changed

src/api/environment.cc

+47-27
Original file line numberDiff line numberDiff line change
@@ -196,36 +196,56 @@ void SetIsolateCreateParamsForNode(Isolate::CreateParams* params) {
196196
}
197197
}
198198

199-
void SetIsolateUpForNode(v8::Isolate* isolate, IsolateSettingCategories cat) {
200-
switch (cat) {
201-
case IsolateSettingCategories::kErrorHandlers:
202-
isolate->AddMessageListenerWithErrorLevel(
203-
errors::PerIsolateMessageListener,
204-
Isolate::MessageErrorLevel::kMessageError |
205-
Isolate::MessageErrorLevel::kMessageWarning);
206-
isolate->SetAbortOnUncaughtExceptionCallback(
207-
ShouldAbortOnUncaughtException);
208-
isolate->SetFatalErrorHandler(OnFatalError);
209-
isolate->SetPrepareStackTraceCallback(PrepareStackTraceCallback);
210-
break;
211-
case IsolateSettingCategories::kMisc:
212-
isolate->SetMicrotasksPolicy(MicrotasksPolicy::kExplicit);
213-
isolate->SetAllowWasmCodeGenerationCallback(
214-
AllowWasmCodeGenerationCallback);
215-
isolate->SetPromiseRejectCallback(task_queue::PromiseRejectCallback);
216-
isolate->SetHostCleanupFinalizationGroupCallback(
217-
HostCleanupFinalizationGroupCallback);
218-
v8::CpuProfiler::UseDetailedSourcePositionsForProfiling(isolate);
219-
break;
220-
default:
221-
UNREACHABLE();
222-
break;
223-
}
199+
void SetIsolateErrorHandlers(v8::Isolate* isolate, const IsolateSettings& s) {
200+
if (s.flags & MESSAGE_LISTENER_WITH_ERROR_LEVEL)
201+
isolate->AddMessageListenerWithErrorLevel(
202+
errors::PerIsolateMessageListener,
203+
Isolate::MessageErrorLevel::kMessageError |
204+
Isolate::MessageErrorLevel::kMessageWarning);
205+
206+
auto* abort_callback = s.should_abort_on_uncaught_exception_callback ?
207+
s.should_abort_on_uncaught_exception_callback :
208+
ShouldAbortOnUncaughtException;
209+
isolate->SetAbortOnUncaughtExceptionCallback(abort_callback);
210+
211+
auto* fatal_error_cb = s.fatal_error_callback ?
212+
s.fatal_error_callback : OnFatalError;
213+
isolate->SetFatalErrorHandler(fatal_error_cb);
214+
215+
auto* prepare_stack_trace_cb = s.prepare_stack_trace_callback ?
216+
s.prepare_stack_trace_callback : PrepareStackTraceCallback;
217+
isolate->SetPrepareStackTraceCallback(prepare_stack_trace_cb);
218+
}
219+
220+
void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s) {
221+
isolate->SetMicrotasksPolicy(s.policy);
222+
223+
auto* allow_wasm_codegen_cb = s.allow_wasm_code_generation_callback ?
224+
s.allow_wasm_code_generation_callback : AllowWasmCodeGenerationCallback;
225+
isolate->SetAllowWasmCodeGenerationCallback(allow_wasm_codegen_cb);
226+
227+
auto* promise_reject_cb = s.promise_reject_callback ?
228+
s.promise_reject_callback : task_queue::PromiseRejectCallback;
229+
isolate->SetPromiseRejectCallback(promise_reject_cb);
230+
231+
auto* host_cleanup_cb = s.host_cleanup_finalization_group_callback ?
232+
s.host_cleanup_finalization_group_callback :
233+
HostCleanupFinalizationGroupCallback;
234+
isolate->SetHostCleanupFinalizationGroupCallback(host_cleanup_cb);
235+
236+
if (s.flags & DETAILED_SOURCE_POSITIONS_FOR_PROFILING)
237+
v8::CpuProfiler::UseDetailedSourcePositionsForProfiling(isolate);
238+
}
239+
240+
void SetIsolateUpForNode(v8::Isolate* isolate,
241+
const IsolateSettings& settings) {
242+
SetIsolateErrorHandlers(isolate, settings);
243+
SetIsolateMiscHandlers(isolate, settings);
224244
}
225245

226246
void SetIsolateUpForNode(v8::Isolate* isolate) {
227-
SetIsolateUpForNode(isolate, IsolateSettingCategories::kErrorHandlers);
228-
SetIsolateUpForNode(isolate, IsolateSettingCategories::kMisc);
247+
IsolateSettings settings;
248+
SetIsolateUpForNode(isolate, settings);
229249
}
230250

231251
Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {

src/node.h

+31
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,40 @@ class NODE_EXTERN MultiIsolatePlatform : public v8::Platform {
291291
NODE_EXTERN void SetIsolateCreateParams(v8::Isolate::CreateParams* params,
292292
ArrayBufferAllocator* allocator
293293
= nullptr);
294+
295+
enum IsolateSettingsFlags {
296+
MESSAGE_LISTENER_WITH_ERROR_LEVEL = 1 << 0,
297+
DETAILED_SOURCE_POSITIONS_FOR_PROFILING = 1 << 1
298+
};
299+
300+
struct IsolateSettings {
301+
uint64_t flags = MESSAGE_LISTENER_WITH_ERROR_LEVEL |
302+
DETAILED_SOURCE_POSITIONS_FOR_PROFILING;
303+
v8::MicrotasksPolicy policy = v8::MicrotasksPolicy::kExplicit;
304+
305+
// Error handling callbacks
306+
v8::Isolate::AbortOnUncaughtExceptionCallback
307+
should_abort_on_uncaught_exception_callback = nullptr;
308+
v8::FatalErrorCallback fatal_error_callback = nullptr;
309+
v8::PrepareStackTraceCallback prepare_stack_trace_callback = nullptr;
310+
311+
// Miscellaneous callbacks
312+
v8::PromiseRejectCallback promise_reject_callback = nullptr;
313+
v8::AllowWasmCodeGenerationCallback
314+
allow_wasm_code_generation_callback = nullptr;
315+
v8::HostCleanupFinalizationGroupCallback
316+
host_cleanup_finalization_group_callback = nullptr;
317+
};
318+
319+
// Overriding IsolateSettings may produce unexpected behavior
320+
// in Node.js core functionality, so proceed at your own risk.
321+
NODE_EXTERN void SetIsolateUpForNode(v8::Isolate* isolate,
322+
const IsolateSettings& settings);
323+
294324
// Set a number of callbacks for the `isolate`, in particular the Node.js
295325
// uncaught exception listener.
296326
NODE_EXTERN void SetIsolateUpForNode(v8::Isolate* isolate);
327+
297328
// Creates a new isolate with Node.js-specific settings.
298329
// This is a convenience method equivalent to using SetIsolateCreateParams(),
299330
// Isolate::Allocate(), MultiIsolatePlatform::RegisterIsolate(),

src/node_internals.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ struct InitializationResult {
296296
};
297297
InitializationResult InitializeOncePerProcess(int argc, char** argv);
298298
void TearDownOncePerProcess();
299-
enum class IsolateSettingCategories { kErrorHandlers, kMisc };
300-
void SetIsolateUpForNode(v8::Isolate* isolate, IsolateSettingCategories cat);
299+
void SetIsolateErrorHandlers(v8::Isolate* isolate, const IsolateSettings& s);
300+
void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s);
301301
void SetIsolateCreateParamsForNode(v8::Isolate::CreateParams* params);
302302

303303
#if HAVE_INSPECTOR

src/node_main_instance.cc

+8-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ NodeMainInstance::NodeMainInstance(Isolate* isolate,
3030
owns_isolate_(false),
3131
deserialize_mode_(false) {
3232
isolate_data_.reset(new IsolateData(isolate_, event_loop, platform, nullptr));
33-
SetIsolateUpForNode(isolate_, IsolateSettingCategories::kMisc);
33+
34+
IsolateSettings misc;
35+
SetIsolateMiscHandlers(isolate_, misc);
3436
}
3537

3638
std::unique_ptr<NodeMainInstance> NodeMainInstance::Create(
@@ -74,11 +76,12 @@ NodeMainInstance::NodeMainInstance(
7476
platform,
7577
array_buffer_allocator_.get(),
7678
per_isolate_data_indexes));
77-
SetIsolateUpForNode(isolate_, IsolateSettingCategories::kMisc);
79+
IsolateSettings s;
80+
SetIsolateMiscHandlers(isolate_, s);
7881
if (!deserialize_mode_) {
7982
// If in deserialize mode, delay until after the deserialization is
8083
// complete.
81-
SetIsolateUpForNode(isolate_, IsolateSettingCategories::kErrorHandlers);
84+
SetIsolateErrorHandlers(isolate_, s);
8285
}
8386
}
8487

@@ -182,7 +185,8 @@ std::unique_ptr<Environment> NodeMainInstance::CreateMainEnvironment(
182185
context =
183186
Context::FromSnapshot(isolate_, kNodeContextIndex).ToLocalChecked();
184187
InitializeContextRuntime(context);
185-
SetIsolateUpForNode(isolate_, IsolateSettingCategories::kErrorHandlers);
188+
IsolateSettings s;
189+
SetIsolateErrorHandlers(isolate_, s);
186190
} else {
187191
context = NewContext(isolate_);
188192
}

0 commit comments

Comments
 (0)