1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-29 03:19:54 +00:00

Add line, col to error messages when available.

This commit is contained in:
Calvin Rose 2019-09-22 15:13:21 -05:00
parent a032529437
commit 803c3fc235

View File

@ -150,8 +150,44 @@ void janet_stacktrace(JanetFiber *fiber, Janet err) {
if (frame->func && frame->pc) { if (frame->func && frame->pc) {
int32_t off = (int32_t)(frame->pc - def->bytecode); int32_t off = (int32_t)(frame->pc - def->bytecode);
if (def->sourcemap) { if (def->sourcemap) {
/* Try to get line and column information */
JanetSourceMapping mapping = def->sourcemap[off]; JanetSourceMapping mapping = def->sourcemap[off];
char buf[1024];
size_t nread;
int32_t offset = 0;
int32_t line = 1;
int32_t col = 1;
int notdone = 1;
char last = 0;
FILE *f;
if (def->source && (f = fopen((const char *)def->source, "rb"))) {
while (notdone && (nread = fread(buf, 1, sizeof(buf), f)) > 0) {
for(size_t i = 0; i < nread; i++) {
char c = buf[i];
if (c == '\r') {
line++;
col = 1;
} if (c == '\n') {
col = 1;
if (last != '\r') {
line++;
}
} else {
col++;
}
last = c;
if (offset == mapping.start) {
fprintf(out, " on line %d, column %d", line, col);
notdone = 0;
break;
}
offset++;
}
}
fclose(f);
} else {
fprintf(out, " at (%d:%d)", mapping.start, mapping.end); fprintf(out, " at (%d:%d)", mapping.start, mapping.end);
}
} else { } else {
fprintf(out, " pc=%d", off); fprintf(out, " pc=%d", off);
} }