summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorShelley Vohr <shelley.vohr@gmail.com>2019-10-25 21:14:36 -0700
committerShelley Vohr <shelley.vohr@gmail.com>2019-11-01 10:22:37 -0700
commitfc02cf586a4b146195a5f09a21fb34269657c484 (patch)
treeae6c1bc391aa720b4ad7bb765dc219d6ecfcc15f /src
parent75dc8938a40100a53323ed87159a1ab2f149ceca (diff)
downloadnode-new-fc02cf586a4b146195a5f09a21fb34269657c484.tar.gz
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: https://github.com/nodejs/node/pull/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>
Diffstat (limited to 'src')
-rw-r--r--src/api/environment.cc74
-rw-r--r--src/node.h31
-rw-r--r--src/node_internals.h4
-rw-r--r--src/node_main_instance.cc12
4 files changed, 88 insertions, 33 deletions
diff --git a/src/api/environment.cc b/src/api/environment.cc
index e6a87d5a93..846e4a873d 100644
--- a/src/api/environment.cc
+++ b/src/api/environment.cc
@@ -196,36 +196,56 @@ void SetIsolateCreateParamsForNode(Isolate::CreateParams* params) {
}
}
-void SetIsolateUpForNode(v8::Isolate* isolate, IsolateSettingCategories cat) {
- switch (cat) {
- case IsolateSettingCategories::kErrorHandlers:
- isolate->AddMessageListenerWithErrorLevel(
- errors::PerIsolateMessageListener,
- Isolate::MessageErrorLevel::kMessageError |
- Isolate::MessageErrorLevel::kMessageWarning);
- isolate->SetAbortOnUncaughtExceptionCallback(
- ShouldAbortOnUncaughtException);
- isolate->SetFatalErrorHandler(OnFatalError);
- isolate->SetPrepareStackTraceCallback(PrepareStackTraceCallback);
- break;
- case IsolateSettingCategories::kMisc:
- isolate->SetMicrotasksPolicy(MicrotasksPolicy::kExplicit);
- isolate->SetAllowWasmCodeGenerationCallback(
- AllowWasmCodeGenerationCallback);
- isolate->SetPromiseRejectCallback(task_queue::PromiseRejectCallback);
- isolate->SetHostCleanupFinalizationGroupCallback(
- HostCleanupFinalizationGroupCallback);
- v8::CpuProfiler::UseDetailedSourcePositionsForProfiling(isolate);
- break;
- default:
- UNREACHABLE();
- break;
- }
+void SetIsolateErrorHandlers(v8::Isolate* isolate, const IsolateSettings& s) {
+ if (s.flags & MESSAGE_LISTENER_WITH_ERROR_LEVEL)
+ isolate->AddMessageListenerWithErrorLevel(
+ errors::PerIsolateMessageListener,
+ Isolate::MessageErrorLevel::kMessageError |
+ Isolate::MessageErrorLevel::kMessageWarning);
+
+ auto* abort_callback = s.should_abort_on_uncaught_exception_callback ?
+ s.should_abort_on_uncaught_exception_callback :
+ ShouldAbortOnUncaughtException;
+ isolate->SetAbortOnUncaughtExceptionCallback(abort_callback);
+
+ auto* fatal_error_cb = s.fatal_error_callback ?
+ s.fatal_error_callback : OnFatalError;
+ isolate->SetFatalErrorHandler(fatal_error_cb);
+
+ auto* prepare_stack_trace_cb = s.prepare_stack_trace_callback ?
+ s.prepare_stack_trace_callback : PrepareStackTraceCallback;
+ isolate->SetPrepareStackTraceCallback(prepare_stack_trace_cb);
+}
+
+void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s) {
+ isolate->SetMicrotasksPolicy(s.policy);
+
+ auto* allow_wasm_codegen_cb = s.allow_wasm_code_generation_callback ?
+ s.allow_wasm_code_generation_callback : AllowWasmCodeGenerationCallback;
+ isolate->SetAllowWasmCodeGenerationCallback(allow_wasm_codegen_cb);
+
+ auto* promise_reject_cb = s.promise_reject_callback ?
+ s.promise_reject_callback : task_queue::PromiseRejectCallback;
+ isolate->SetPromiseRejectCallback(promise_reject_cb);
+
+ auto* host_cleanup_cb = s.host_cleanup_finalization_group_callback ?
+ s.host_cleanup_finalization_group_callback :
+ HostCleanupFinalizationGroupCallback;
+ isolate->SetHostCleanupFinalizationGroupCallback(host_cleanup_cb);
+
+ if (s.flags & DETAILED_SOURCE_POSITIONS_FOR_PROFILING)
+ v8::CpuProfiler::UseDetailedSourcePositionsForProfiling(isolate);
+}
+
+void SetIsolateUpForNode(v8::Isolate* isolate,
+ const IsolateSettings& settings) {
+ SetIsolateErrorHandlers(isolate, settings);
+ SetIsolateMiscHandlers(isolate, settings);
}
void SetIsolateUpForNode(v8::Isolate* isolate) {
- SetIsolateUpForNode(isolate, IsolateSettingCategories::kErrorHandlers);
- SetIsolateUpForNode(isolate, IsolateSettingCategories::kMisc);
+ IsolateSettings settings;
+ SetIsolateUpForNode(isolate, settings);
}
Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
diff --git a/src/node.h b/src/node.h
index cae0673a37..240399d1ed 100644
--- a/src/node.h
+++ b/src/node.h
@@ -291,9 +291,40 @@ class NODE_EXTERN MultiIsolatePlatform : public v8::Platform {
NODE_EXTERN void SetIsolateCreateParams(v8::Isolate::CreateParams* params,
ArrayBufferAllocator* allocator
= nullptr);
+
+enum IsolateSettingsFlags {
+ MESSAGE_LISTENER_WITH_ERROR_LEVEL = 1 << 0,
+ DETAILED_SOURCE_POSITIONS_FOR_PROFILING = 1 << 1
+};
+
+struct IsolateSettings {
+ uint64_t flags = MESSAGE_LISTENER_WITH_ERROR_LEVEL |
+ DETAILED_SOURCE_POSITIONS_FOR_PROFILING;
+ v8::MicrotasksPolicy policy = v8::MicrotasksPolicy::kExplicit;
+
+ // Error handling callbacks
+ v8::Isolate::AbortOnUncaughtExceptionCallback
+ should_abort_on_uncaught_exception_callback = nullptr;
+ v8::FatalErrorCallback fatal_error_callback = nullptr;
+ v8::PrepareStackTraceCallback prepare_stack_trace_callback = nullptr;
+
+ // Miscellaneous callbacks
+ v8::PromiseRejectCallback promise_reject_callback = nullptr;
+ v8::AllowWasmCodeGenerationCallback
+ allow_wasm_code_generation_callback = nullptr;
+ v8::HostCleanupFinalizationGroupCallback
+ host_cleanup_finalization_group_callback = nullptr;
+};
+
+// Overriding IsolateSettings may produce unexpected behavior
+// in Node.js core functionality, so proceed at your own risk.
+NODE_EXTERN void SetIsolateUpForNode(v8::Isolate* isolate,
+ const IsolateSettings& settings);
+
// Set a number of callbacks for the `isolate`, in particular the Node.js
// uncaught exception listener.
NODE_EXTERN void SetIsolateUpForNode(v8::Isolate* isolate);
+
// Creates a new isolate with Node.js-specific settings.
// This is a convenience method equivalent to using SetIsolateCreateParams(),
// Isolate::Allocate(), MultiIsolatePlatform::RegisterIsolate(),
diff --git a/src/node_internals.h b/src/node_internals.h
index 0f4c32bb94..4ec883c891 100644
--- a/src/node_internals.h
+++ b/src/node_internals.h
@@ -296,8 +296,8 @@ struct InitializationResult {
};
InitializationResult InitializeOncePerProcess(int argc, char** argv);
void TearDownOncePerProcess();
-enum class IsolateSettingCategories { kErrorHandlers, kMisc };
-void SetIsolateUpForNode(v8::Isolate* isolate, IsolateSettingCategories cat);
+void SetIsolateErrorHandlers(v8::Isolate* isolate, const IsolateSettings& s);
+void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s);
void SetIsolateCreateParamsForNode(v8::Isolate::CreateParams* params);
#if HAVE_INSPECTOR
diff --git a/src/node_main_instance.cc b/src/node_main_instance.cc
index 0744063e0d..1c1a09280b 100644
--- a/src/node_main_instance.cc
+++ b/src/node_main_instance.cc
@@ -30,7 +30,9 @@ NodeMainInstance::NodeMainInstance(Isolate* isolate,
owns_isolate_(false),
deserialize_mode_(false) {
isolate_data_.reset(new IsolateData(isolate_, event_loop, platform, nullptr));
- SetIsolateUpForNode(isolate_, IsolateSettingCategories::kMisc);
+
+ IsolateSettings misc;
+ SetIsolateMiscHandlers(isolate_, misc);
}
std::unique_ptr<NodeMainInstance> NodeMainInstance::Create(
@@ -74,11 +76,12 @@ NodeMainInstance::NodeMainInstance(
platform,
array_buffer_allocator_.get(),
per_isolate_data_indexes));
- SetIsolateUpForNode(isolate_, IsolateSettingCategories::kMisc);
+ IsolateSettings s;
+ SetIsolateMiscHandlers(isolate_, s);
if (!deserialize_mode_) {
// If in deserialize mode, delay until after the deserialization is
// complete.
- SetIsolateUpForNode(isolate_, IsolateSettingCategories::kErrorHandlers);
+ SetIsolateErrorHandlers(isolate_, s);
}
}
@@ -182,7 +185,8 @@ std::unique_ptr<Environment> NodeMainInstance::CreateMainEnvironment(
context =
Context::FromSnapshot(isolate_, kNodeContextIndex).ToLocalChecked();
InitializeContextRuntime(context);
- SetIsolateUpForNode(isolate_, IsolateSettingCategories::kErrorHandlers);
+ IsolateSettings s;
+ SetIsolateErrorHandlers(isolate_, s);
} else {
context = NewContext(isolate_);
}