Skip to content

Commit

Permalink
src,lib: fix assert when Error.prepareStackTrace not overridden
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Aug 17, 2023
1 parent b4a2be4 commit 40a5d30
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
13 changes: 11 additions & 2 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const { openSync, closeSync, readSync } = require('fs');
const { inspect } = require('internal/util/inspect');
const { isPromise, isRegExp } = require('internal/util/types');
const { EOL } = require('internal/constants');
const { getEmbedderOptions } = require('internal/options');
const { BuiltinModule } = require('internal/bootstrap/realm');
const { isError, deprecate } = require('internal/util');

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

overrideStackTrace.set(err, (_, stack) => stack);
const call = err.stack[0];
let call;
if (getEmbedderOptions().hasPrepareStackTraceCallback) {
overrideStackTrace.set(err, (_, stack) => stack);
call = err.stack[0];
} else {
const tmpPrepare = Error.prepareStackTrace;
Error.prepareStackTrace = (_, stack) => stack;
call = err.stack[0];
Error.prepareStackTrace = tmpPrepare;
}

const filename = call.getFileName();
const line = call.getLineNumber() - 1;
Expand Down
5 changes: 4 additions & 1 deletion src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,10 @@ void SetIsolateErrorHandlers(v8::Isolate* isolate, const IsolateSettings& s) {
auto* prepare_stack_trace_cb = s.prepare_stack_trace_callback ?
s.prepare_stack_trace_callback : PrepareStackTraceCallback;
isolate->SetPrepareStackTraceCallback(prepare_stack_trace_cb);
}
} else {
auto env = Environment::GetCurrent(isolate);
env->set_prepare_stack_trace_callback(Local<Function>());
}
}

void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s) {
Expand Down
5 changes: 5 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,11 @@ void GetEmbedderOptions(const FunctionCallbackInfo<Value>& args) {
Local<Context> context = env->context();
Local<Object> ret = Object::New(isolate);

if (ret->Set(context,
FIXED_ONE_BYTE_STRING(env->isolate(), "hasPrepareStackTraceCallback"),
Boolean::New(isolate, !env->prepare_stack_trace_callback().IsEmpty()))
.IsNothing()) return;

if (ret->Set(context,
FIXED_ONE_BYTE_STRING(env->isolate(), "shouldNotRegisterESMLoader"),
Boolean::New(isolate, env->should_not_register_esm_loader()))
Expand Down

0 comments on commit 40a5d30

Please sign in to comment.