diff options
Diffstat (limited to 'src/api/environment.cc')
-rw-r--r-- | src/api/environment.cc | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/api/environment.cc b/src/api/environment.cc index 548b685dfe..5dfac00647 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -13,6 +13,8 @@ #endif namespace node { +using errors::TryCatchScope; +using v8::Array; using v8::Context; using v8::EscapableHandleScope; using v8::Function; @@ -45,6 +47,41 @@ static bool ShouldAbortOnUncaughtException(Isolate* isolate) { !env->inside_should_not_abort_on_uncaught_scope(); } +static MaybeLocal<Value> PrepareStackTraceCallback(Local<Context> context, + Local<Value> exception, + Local<Array> trace) { + Environment* env = Environment::GetCurrent(context); + if (env == nullptr) { + MaybeLocal<String> s = exception->ToString(context); + return s.IsEmpty() ? + MaybeLocal<Value>() : + MaybeLocal<Value>(s.ToLocalChecked()); + } + Local<Function> prepare = env->prepare_stack_trace_callback(); + if (prepare.IsEmpty()) { + MaybeLocal<String> s = exception->ToString(context); + return s.IsEmpty() ? + MaybeLocal<Value>() : + MaybeLocal<Value>(s.ToLocalChecked()); + } + Local<Value> args[] = { + context->Global(), + exception, + trace, + }; + // This TryCatch + Rethrow is required by V8 due to details around exception + // handling there. For C++ callbacks, V8 expects a scheduled exception (which + // is what ReThrow gives us). Just returning the empty MaybeLocal would leave + // us with a pending exception. + TryCatchScope try_catch(env); + MaybeLocal<Value> result = prepare->Call( + context, Undefined(env->isolate()), arraysize(args), args); + if (try_catch.HasCaught() && !try_catch.HasTerminated()) { + try_catch.ReThrow(); + } + return result; +} + void* NodeArrayBufferAllocator::Allocate(size_t size) { if (zero_fill_field_ || per_process::cli_options->zero_fill_all_buffers) return UncheckedCalloc(size); @@ -166,6 +203,7 @@ void SetIsolateUpForNode(v8::Isolate* isolate, IsolateSettingCategories cat) { isolate->SetAbortOnUncaughtExceptionCallback( ShouldAbortOnUncaughtException); isolate->SetFatalErrorHandler(OnFatalError); + isolate->SetPrepareStackTraceCallback(PrepareStackTraceCallback); break; case IsolateSettingCategories::kMisc: isolate->SetMicrotasksPolicy(MicrotasksPolicy::kExplicit); |