Skip to content

Commit

Permalink
add simple writeStackTrace for JS backend (nim-lang#16016)
Browse files Browse the repository at this point in the history
* add simple writeStackTrace for JS backend

* add testcase for writeStackTrace

* changelog
  • Loading branch information
ringabout authored and ardek66 committed Mar 26, 2021
1 parent 3a79102 commit 98f4b80
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/system/jssys.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions tests/js/twritestacktrace.nim
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 98f4b80

Please sign in to comment.