From 98f4b80f0e8649d22d9ae8155ea4c97fd30b77ff Mon Sep 17 00:00:00 2001 From: flywind <43030857+xflywind@users.noreply.github.com> Date: Wed, 25 Nov 2020 02:45:06 +0800 Subject: [PATCH] add simple writeStackTrace for JS backend (#16016) * add simple writeStackTrace for JS backend * add testcase for writeStackTrace * changelog --- changelog.md | 2 ++ lib/system.nim | 2 +- lib/system/jssys.nim | 5 +++++ tests/js/twritestacktrace.nim | 12 ++++++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/js/twritestacktrace.nim diff --git a/changelog.md b/changelog.md index 4c18ddf1ef20a..bfbe59e1a85b8 100644 --- a/changelog.md +++ b/changelog.md @@ -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` diff --git a/lib/system.nim b/lib/system.nim index 716eb296a8690..be41c1893ce30 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -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 diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim index c4c671ea3ceb7..8865558fe5539 100644 --- a/lib/system/jssys.nim +++ b/lib/system/jssys.nim @@ -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 diff --git a/tests/js/twritestacktrace.nim b/tests/js/twritestacktrace.nim new file mode 100644 index 0000000000000..2fe2b1987a7e5 --- /dev/null +++ b/tests/js/twritestacktrace.nim @@ -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()