Skip to content

Commit 151654e

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

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

lib/.eslintrc.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ rules:
2323
message: Use an error exported by the internal/errors module.
2424
- selector: CallExpression[callee.object.name='Error'][callee.property.name='captureStackTrace']
2525
message: Please use `require('internal/errors').hideStackFrames()` instead.
26-
- selector: AssignmentExpression:matches([left.name='prepareStackTrace'], [left.property.name='prepareStackTrace'])
27-
message: Use 'overrideStackTrace' from 'lib/internal/errors.js' instead of 'Error.prepareStackTrace'.
2826
- selector: ThrowStatement > NewExpression[callee.name=/^ERR_[A-Z_]+$/] > ObjectExpression:first-child:not(:has([key.name='message']):has([key.name='code']):has([key.name='syscall']))
2927
message: The context passed into SystemError constructor must have .code, .syscall and .message.
3028
no-restricted-globals:

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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ 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+
} else {
273+
auto env = Environment::GetCurrent(isolate);
274+
env->set_prepare_stack_trace_callback(Local<Function>());
272275
}
273276
}
274277

src/node_options.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,13 @@ 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(
1238+
env->isolate(), "hasPrepareStackTraceCallback"),
1239+
Boolean::New(isolate,
1240+
!env->prepare_stack_trace_callback().IsEmpty()))
1241+
.IsNothing()) return;
1242+
12361243
if (ret->Set(context,
12371244
FIXED_ONE_BYTE_STRING(env->isolate(), "shouldNotRegisterESMLoader"),
12381245
Boolean::New(isolate, env->should_not_register_esm_loader()))

0 commit comments

Comments
 (0)