Skip to content

Commit 40a5d30

Browse files
committed
src,lib: fix assert when Error.prepareStackTrace not overridden
1 parent b4a2be4 commit 40a5d30

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

lib/assert.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const { openSync, closeSync, readSync } = require('fs');
6565
const { inspect } = require('internal/util/inspect');
6666
const { isPromise, isRegExp } = require('internal/util/types');
6767
const { EOL } = require('internal/constants');
68+
const { getEmbedderOptions } = require('internal/options');
6869
const { BuiltinModule } = require('internal/bootstrap/realm');
6970
const { isError, deprecate } = require('internal/util');
7071

@@ -293,8 +294,16 @@ function getErrMessage(message, fn) {
293294
ErrorCaptureStackTrace(err, fn);
294295
if (errorStackTraceLimitIsWritable) Error.stackTraceLimit = tmpLimit;
295296

296-
overrideStackTrace.set(err, (_, stack) => stack);
297-
const call = err.stack[0];
297+
let call;
298+
if (getEmbedderOptions().hasPrepareStackTraceCallback) {
299+
overrideStackTrace.set(err, (_, stack) => stack);
300+
call = err.stack[0];
301+
} else {
302+
const tmpPrepare = Error.prepareStackTrace;
303+
Error.prepareStackTrace = (_, stack) => stack;
304+
call = err.stack[0];
305+
Error.prepareStackTrace = tmpPrepare;
306+
}
298307

299308
const filename = call.getFileName();
300309
const line = call.getLineNumber() - 1;

src/api/environment.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,10 @@ void SetIsolateErrorHandlers(v8::Isolate* isolate, const IsolateSettings& s) {
269269
auto* prepare_stack_trace_cb = s.prepare_stack_trace_callback ?
270270
s.prepare_stack_trace_callback : PrepareStackTraceCallback;
271271
isolate->SetPrepareStackTraceCallback(prepare_stack_trace_cb);
272-
}
272+
} else {
273+
auto env = Environment::GetCurrent(isolate);
274+
env->set_prepare_stack_trace_callback(Local<Function>());
275+
}
273276
}
274277

275278
void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s) {

src/node_options.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,11 @@ void GetEmbedderOptions(const FunctionCallbackInfo<Value>& args) {
12331233
Local<Context> context = env->context();
12341234
Local<Object> ret = Object::New(isolate);
12351235

1236+
if (ret->Set(context,
1237+
FIXED_ONE_BYTE_STRING(env->isolate(), "hasPrepareStackTraceCallback"),
1238+
Boolean::New(isolate, !env->prepare_stack_trace_callback().IsEmpty()))
1239+
.IsNothing()) return;
1240+
12361241
if (ret->Set(context,
12371242
FIXED_ONE_BYTE_STRING(env->isolate(), "shouldNotRegisterESMLoader"),
12381243
Boolean::New(isolate, env->should_not_register_esm_loader()))

0 commit comments

Comments
 (0)