Skip to content

Commit 6642f54

Browse files
TimothyGuMylesBorins
authored andcommitted
src: fix ^ in stack trace with vm's columnOffset
While VM module's columnOffset option does succeed in applying an offset to the column number in the stack trace, the wavy diagram printed does not account for potential offsets, resulting in erroneous location of `^` in the first line of the script. Before: ``` > vm.runInThisContext('throw new Error()', { columnOffset: 5 }) evalmachine.<anonymous>:1 throw new Error() ^ Error at evalmachine.<anonymous>:1:12 at ContextifyScript.Script.runInThisContext (vm.js:44:33) at Object.runInThisContext (vm.js:116:38) ``` After: ``` > vm.runInThisContext('throw new Error()', { columnOffset: 5 }) evalmachine.<anonymous>:1 throw new Error() ^ Error at evalmachine.<anonymous>:1:12 at ContextifyScript.Script.runInThisContext (vm.js:50:33) at Object.runInThisContext (vm.js:139:38) at repl:1:4 ``` PR-URL: #15771 Refs: jsdom/jsdom#2003 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent acd4924 commit 6642f54

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/node.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,7 @@ void AppendExceptionLine(Environment* env,
17401740
}
17411741

17421742
// Print (filename):(line number): (message).
1743+
ScriptOrigin origin = message->GetScriptOrigin();
17431744
node::Utf8Value filename(env->isolate(), message->GetScriptResourceName());
17441745
const char* filename_string = *filename;
17451746
int linenum = message->GetLineNumber();
@@ -1768,8 +1769,16 @@ void AppendExceptionLine(Environment* env,
17681769
// sourceline to 78 characters, and we end up not providing very much
17691770
// useful debugging info to the user if we remove 62 characters.
17701771

1772+
int script_start =
1773+
(linenum - origin.ResourceLineOffset()->Value()) == 1 ?
1774+
origin.ResourceColumnOffset()->Value() : 0;
17711775
int start = message->GetStartColumn(env->context()).FromMaybe(0);
17721776
int end = message->GetEndColumn(env->context()).FromMaybe(0);
1777+
if (start >= script_start) {
1778+
CHECK_GE(end, start);
1779+
start -= script_start;
1780+
end -= script_start;
1781+
}
17731782

17741783
char arrow[1024];
17751784
int max_off = sizeof(arrow) - 2;

test/parallel/test-vm-context.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,14 @@ assert.strictEqual(script.runInContext(ctx), false);
9393
// Error on the first line of a module should
9494
// have the correct line and column number
9595
assert.throws(() => {
96-
vm.runInContext('throw new Error()', context, {
96+
vm.runInContext(' throw new Error()', context, {
9797
filename: 'expected-filename.js',
9898
lineOffset: 32,
9999
columnOffset: 123
100100
});
101101
}, (err) => {
102-
return /expected-filename\.js:33:130/.test(err.stack);
102+
return /^ \^/m.test(err.stack) &&
103+
/expected-filename\.js:33:131/.test(err.stack);
103104
}, 'Expected appearance of proper offset in Error stack');
104105

105106
// https://github.com/nodejs/node/issues/6158

0 commit comments

Comments
 (0)