Skip to content

Commit bf4c39d

Browse files
codebytereMylesBorins
authored andcommitted
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 <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]> Reviewed-By: David Carlier <[email protected]>
1 parent c2757d1 commit bf4c39d

File tree

4 files changed

+87
-33
lines changed

4 files changed

+87
-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

+30
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,39 @@ class NODE_EXTERN MultiIsolatePlatform : public v8::Platform {
280280
void* data) = 0;
281281
};
282282

283+
enum IsolateSettingsFlags {
284+
MESSAGE_LISTENER_WITH_ERROR_LEVEL = 1 << 0,
285+
DETAILED_SOURCE_POSITIONS_FOR_PROFILING = 1 << 1
286+
};
287+
288+
struct IsolateSettings {
289+
uint64_t flags = MESSAGE_LISTENER_WITH_ERROR_LEVEL |
290+
DETAILED_SOURCE_POSITIONS_FOR_PROFILING;
291+
v8::MicrotasksPolicy policy = v8::MicrotasksPolicy::kExplicit;
292+
293+
// Error handling callbacks
294+
v8::Isolate::AbortOnUncaughtExceptionCallback
295+
should_abort_on_uncaught_exception_callback = nullptr;
296+
v8::FatalErrorCallback fatal_error_callback = nullptr;
297+
v8::PrepareStackTraceCallback prepare_stack_trace_callback = nullptr;
298+
299+
// Miscellaneous callbacks
300+
v8::PromiseRejectCallback promise_reject_callback = nullptr;
301+
v8::AllowWasmCodeGenerationCallback
302+
allow_wasm_code_generation_callback = nullptr;
303+
v8::HostCleanupFinalizationGroupCallback
304+
host_cleanup_finalization_group_callback = nullptr;
305+
};
306+
307+
// Overriding IsolateSettings may produce unexpected behavior
308+
// in Node.js core functionality, so proceed at your own risk.
309+
NODE_EXTERN void SetIsolateUpForNode(v8::Isolate* isolate,
310+
const IsolateSettings& settings);
311+
283312
// Set a number of callbacks for the `isolate`, in particular the Node.js
284313
// uncaught exception listener.
285314
NODE_EXTERN void SetIsolateUpForNode(v8::Isolate* isolate);
315+
286316
// Creates a new isolate with Node.js-specific settings.
287317
// This is a convenience method equivalent to using SetIsolateCreateParams(),
288318
// Isolate::Allocate(), MultiIsolatePlatform::RegisterIsolate(),

src/node_internals.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ struct InitializationResult {
313313
};
314314
InitializationResult InitializeOncePerProcess(int argc, char** argv);
315315
void TearDownOncePerProcess();
316-
enum class IsolateSettingCategories { kErrorHandlers, kMisc };
317-
void SetIsolateUpForNode(v8::Isolate* isolate, IsolateSettingCategories cat);
316+
void SetIsolateErrorHandlers(v8::Isolate* isolate, const IsolateSettings& s);
317+
void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s);
318318
void SetIsolateCreateParamsForNode(v8::Isolate::CreateParams* params);
319319

320320
#if HAVE_INSPECTOR

src/node_main_instance.cc

+8-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ NodeMainInstance::NodeMainInstance(Isolate* isolate,
3535
owns_isolate_(false),
3636
deserialize_mode_(false) {
3737
isolate_data_.reset(new IsolateData(isolate_, event_loop, platform, nullptr));
38-
SetIsolateUpForNode(isolate_, IsolateSettingCategories::kMisc);
38+
39+
IsolateSettings misc;
40+
SetIsolateMiscHandlers(isolate_, misc);
3941
}
4042

4143
std::unique_ptr<NodeMainInstance> NodeMainInstance::Create(
@@ -79,11 +81,12 @@ NodeMainInstance::NodeMainInstance(
7981
platform,
8082
array_buffer_allocator_.get(),
8183
per_isolate_data_indexes));
82-
SetIsolateUpForNode(isolate_, IsolateSettingCategories::kMisc);
84+
IsolateSettings s;
85+
SetIsolateMiscHandlers(isolate_, s);
8386
if (!deserialize_mode_) {
8487
// If in deserialize mode, delay until after the deserialization is
8588
// complete.
86-
SetIsolateUpForNode(isolate_, IsolateSettingCategories::kErrorHandlers);
89+
SetIsolateErrorHandlers(isolate_, s);
8790
}
8891
}
8992

@@ -201,7 +204,8 @@ std::unique_ptr<Environment> NodeMainInstance::CreateMainEnvironment(
201204
context =
202205
Context::FromSnapshot(isolate_, kNodeContextIndex).ToLocalChecked();
203206
InitializeContextRuntime(context);
204-
SetIsolateUpForNode(isolate_, IsolateSettingCategories::kErrorHandlers);
207+
IsolateSettings s;
208+
SetIsolateErrorHandlers(isolate_, s);
205209
} else {
206210
context = NewContext(isolate_);
207211
}

0 commit comments

Comments
 (0)