|
13 | 13 | #endif
|
14 | 14 |
|
15 | 15 | namespace node {
|
| 16 | +using errors::TryCatchScope; |
| 17 | +using v8::Array; |
16 | 18 | using v8::Context;
|
17 | 19 | using v8::EscapableHandleScope;
|
18 | 20 | using v8::Function;
|
@@ -45,6 +47,41 @@ static bool ShouldAbortOnUncaughtException(Isolate* isolate) {
|
45 | 47 | !env->inside_should_not_abort_on_uncaught_scope();
|
46 | 48 | }
|
47 | 49 |
|
| 50 | +static MaybeLocal<Value> PrepareStackTraceCallback(Local<Context> context, |
| 51 | + Local<Value> exception, |
| 52 | + Local<Array> trace) { |
| 53 | + Environment* env = Environment::GetCurrent(context); |
| 54 | + if (env == nullptr) { |
| 55 | + MaybeLocal<String> s = exception->ToString(context); |
| 56 | + return s.IsEmpty() ? |
| 57 | + MaybeLocal<Value>() : |
| 58 | + MaybeLocal<Value>(s.ToLocalChecked()); |
| 59 | + } |
| 60 | + Local<Function> prepare = env->prepare_stack_trace_callback(); |
| 61 | + if (prepare.IsEmpty()) { |
| 62 | + MaybeLocal<String> s = exception->ToString(context); |
| 63 | + return s.IsEmpty() ? |
| 64 | + MaybeLocal<Value>() : |
| 65 | + MaybeLocal<Value>(s.ToLocalChecked()); |
| 66 | + } |
| 67 | + Local<Value> args[] = { |
| 68 | + context->Global(), |
| 69 | + exception, |
| 70 | + trace, |
| 71 | + }; |
| 72 | + // This TryCatch + Rethrow is required by V8 due to details around exception |
| 73 | + // handling there. For C++ callbacks, V8 expects a scheduled exception (which |
| 74 | + // is what ReThrow gives us). Just returning the empty MaybeLocal would leave |
| 75 | + // us with a pending exception. |
| 76 | + TryCatchScope try_catch(env); |
| 77 | + MaybeLocal<Value> result = prepare->Call( |
| 78 | + context, Undefined(env->isolate()), arraysize(args), args); |
| 79 | + if (try_catch.HasCaught() && !try_catch.HasTerminated()) { |
| 80 | + try_catch.ReThrow(); |
| 81 | + } |
| 82 | + return result; |
| 83 | +} |
| 84 | + |
48 | 85 | void* NodeArrayBufferAllocator::Allocate(size_t size) {
|
49 | 86 | if (zero_fill_field_ || per_process::cli_options->zero_fill_all_buffers)
|
50 | 87 | return UncheckedCalloc(size);
|
@@ -166,6 +203,7 @@ void SetIsolateUpForNode(v8::Isolate* isolate, IsolateSettingCategories cat) {
|
166 | 203 | isolate->SetAbortOnUncaughtExceptionCallback(
|
167 | 204 | ShouldAbortOnUncaughtException);
|
168 | 205 | isolate->SetFatalErrorHandler(OnFatalError);
|
| 206 | + isolate->SetPrepareStackTraceCallback(PrepareStackTraceCallback); |
169 | 207 | break;
|
170 | 208 | case IsolateSettingCategories::kMisc:
|
171 | 209 | isolate->SetMicrotasksPolicy(MicrotasksPolicy::kExplicit);
|
|
0 commit comments