Skip to content

Commit

Permalink
repl: fix prepareStackTrace frames array order
Browse files Browse the repository at this point in the history
The second parameter of `Error.prepareStackTrace` is an array of
reversed call site frames.
  • Loading branch information
legendecas committed Nov 21, 2023
1 parent 676d7fb commit 2a5b908
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
17 changes: 6 additions & 11 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,18 @@
const {
ArrayPrototypeAt,
ArrayPrototypeFilter,
ArrayPrototypeFindIndex,
ArrayPrototypeFindLastIndex,
ArrayPrototypeForEach,
ArrayPrototypeIncludes,
ArrayPrototypeJoin,
ArrayPrototypeMap,
ArrayPrototypePop,
ArrayPrototypePush,
ArrayPrototypePushApply,
ArrayPrototypeReverse,
ArrayPrototypeShift,
ArrayPrototypeSlice,
ArrayPrototypeSome,
ArrayPrototypeSort,
ArrayPrototypeSplice,
ArrayPrototypeUnshift,
Boolean,
Error,
Expand Down Expand Up @@ -153,6 +151,7 @@ const {
},
isErrorStackTraceLimitWritable,
overrideStackTrace,
getInternalPrepareStackTrace,
} = require('internal/errors');
const { sendInspectorCommand } = require('internal/util/inspector');
const { getOptionValue } = require('internal/options');
Expand Down Expand Up @@ -679,23 +678,19 @@ function REPLServer(prompt,
if (typeof stackFrames === 'object') {
// Search from the bottom of the call stack to
// find the first frame with a null function name
const idx = ArrayPrototypeFindIndex(
ArrayPrototypeReverse(stackFrames),
const idx = ArrayPrototypeFindLastIndex(
stackFrames,
(frame) => frame.getFunctionName() === null,
);
// If found, get rid of it and everything below it
frames = ArrayPrototypeSplice(stackFrames, idx + 1);
frames = ArrayPrototypeSlice(stackFrames, 0, idx);
} else {
frames = stackFrames;
}
// FIXME(devsnek): this is inconsistent with the checks
// that the real prepareStackTrace dispatch uses in
// lib/internal/errors.js.
if (typeof Error.prepareStackTrace === 'function') {
return Error.prepareStackTrace(error, frames);
}
ArrayPrototypePush(frames, error);
return ArrayPrototypeJoin(ArrayPrototypeReverse(frames), '\n at ');
return getInternalPrepareStackTrace()(error, frames);
});
decorateErrorStack(e);

Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-repl-pretty-custom-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ const origPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (err, stack) => {
if (err instanceof SyntaxError)
return err.toString();
stack.push(err);
return stack.reverse().join('--->\n');
// Insert the error at the beginning of the stack
stack.unshift(err);
return stack.join('--->\n');
};

process.on('uncaughtException', (e) => {
Expand Down

0 comments on commit 2a5b908

Please sign in to comment.