add simple writeStackTrace for JS backend (#16016)

* add simple writeStackTrace for JS backend

* add testcase for writeStackTrace

* changelog
This commit is contained in:
flywind
2020-11-25 02:45:06 +08:00
committed by GitHub
parent 3fed85437b
commit 19e224866b
4 changed files with 20 additions and 1 deletions

View File

@@ -47,6 +47,8 @@
- `repr` now doesn't insert trailing newline; previous behavior was very inconsistent,
see #16034. Use `-d:nimLegacyReprWithNewline` for previous behavior.
- `writeStackTrace` is available in JS backend now.
## Language changes
- `nimscript` now handles `except Exception as e`

View File

@@ -1765,7 +1765,7 @@ when notJSnotNims and defined(nimSeqsV2):
{.pop.}
when notJSnotNims:
when not defined(nimscript):
proc writeStackTrace*() {.tags: [], gcsafe, raises: [].}
## Writes the current stack trace to ``stderr``. This is only works
## for debug builds. Since it's usually used for debugging, this

View File

@@ -105,6 +105,11 @@ proc rawWriteStackTrace(): string =
else:
result = "No stack traceback available\n"
proc writeStackTrace() =
var trace = rawWriteStackTrace()
trace.setLen(trace.len - 1)
echo trace
proc getStackTrace*(): string = rawWriteStackTrace()
proc getStackTrace*(e: ref Exception): string = e.trace

View File

@@ -0,0 +1,12 @@
discard """
cmd: "nim js --panics:on $file"
output: '''Traceback (most recent call last)
twritestacktrace.nim(12) at module twritestacktrace
twritestacktrace.nim(10) at twritestacktrace.hello
'''
"""
proc hello() =
writeStackTrace()
hello()