From c28a057a6bd5f20445e11d82c4028762ae1bf1b6 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Tue, 5 May 2020 05:56:15 -0700 Subject: fix js stacktraces, unify all file,line,col formatting into a single function (#14230) * fix https://github.com/timotheecour/Nim/issues/135 ; unify all file,line,col formatting into a single function --- lib/std/private/miscdollars.nim | 15 +++++++++++++++ lib/system/assertions.nim | 4 ++-- lib/system/excpt.nim | 8 +++----- lib/system/jssys.nim | 9 +++++---- 4 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 lib/std/private/miscdollars.nim (limited to 'lib') diff --git a/lib/std/private/miscdollars.nim b/lib/std/private/miscdollars.nim new file mode 100644 index 000000000..a41cf1bc1 --- /dev/null +++ b/lib/std/private/miscdollars.nim @@ -0,0 +1,15 @@ +template toLocation*(result: var string, file: string | cstring, line: int, col: int) = + ## avoids spurious allocations + # Hopefully this can be re-used everywhere so that if a user needs to customize, + # it can be done in a single place. + result.add file + if line > 0: + result.add "(" + # simplify this after moving moving `include strmantle` above import assertions` + when declared(addInt): result.addInt line + else: result.add $line + if col > 0: + result.add ", " + when declared(addInt): result.addInt col + else: result.add $col + result.add ")" diff --git a/lib/system/assertions.nim b/lib/system/assertions.nim index 9a1bdc7bc..ca3bd7bc1 100644 --- a/lib/system/assertions.nim +++ b/lib/system/assertions.nim @@ -1,17 +1,17 @@ when not declared(sysFatal): include "system/fatal" +import std/private/miscdollars # --------------------------------------------------------------------------- # helpers type InstantiationInfo = tuple[filename: string, line: int, column: int] proc `$`(x: int): string {.magic: "IntToStr", noSideEffect.} - proc `$`(info: InstantiationInfo): string = # The +1 is needed here # instead of overriding `$` (and changing its meaning), consider explicit name. - info.filename & "(" & $info.line & ", " & $(info.column+1) & ")" + result.toLocation(info.filename, info.line, info.column+1) # --------------------------------------------------------------------------- diff --git a/lib/system/excpt.nim b/lib/system/excpt.nim index f85c10598..747b145ae 100644 --- a/lib/system/excpt.nim +++ b/lib/system/excpt.nim @@ -10,6 +10,8 @@ # Exception handling code. Carefully coded so that tiny programs which do not # use the heap (and nor exceptions) do not include the GC or memory allocator. +import std/private/miscdollars + var errorMessageWriter*: (proc(msg: string) {.tags: [WriteIOEffect], benign, nimcall.}) @@ -240,11 +242,7 @@ proc auxWriteStackTrace(f: PFrame; s: var seq[StackTraceEntry]) = template addFrameEntry(s: var string, f: StackTraceEntry|PFrame) = var oldLen = s.len - add(s, f.filename) - if f.line > 0: - add(s, '(') - add(s, $f.line) - add(s, ')') + s.toLocation(f.filename, f.line, 0) for k in 1..max(1, 25-(s.len-oldLen)): add(s, ' ') add(s, f.procname) when NimStackTraceMsgs: diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim index 106283490..705a6a208 100644 --- a/lib/system/jssys.nim +++ b/lib/system/jssys.nim @@ -8,6 +8,7 @@ # include system/indexerrors +import std/private/miscdollars proc log*(s: cstring) {.importc: "console.log", varargs, nodecl.} @@ -70,7 +71,7 @@ proc getCurrentExceptionMsg*(): string = proc auxWriteStackTrace(f: PCallFrame): string = type - TempFrame = tuple[procname: cstring, line: int] + TempFrame = tuple[procname: cstring, line: int, filename: cstring] var it = f i = 0 @@ -79,6 +80,7 @@ proc auxWriteStackTrace(f: PCallFrame): string = while it != nil and i <= high(tempFrames): tempFrames[i].procname = it.procname tempFrames[i].line = it.line + tempFrames[i].filename = it.filename inc(i) inc(total) it = it.prev @@ -92,10 +94,9 @@ proc auxWriteStackTrace(f: PCallFrame): string = add(result, $(total-i)) add(result, " calls omitted) ...\n") for j in countdown(i-1, 0): + result.toLocation($tempFrames[j].filename, tempFrames[j].line, 0) + add(result, " at ") add(result, tempFrames[j].procname) - if tempFrames[j].line > 0: - add(result, ", line: ") - add(result, $tempFrames[j].line) add(result, "\n") proc rawWriteStackTrace(): string = -- cgit 1.4.1-2-gfad0