summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorflywind <43030857+xflywind@users.noreply.github.com>2020-11-25 02:45:06 +0800
committerGitHub <noreply@github.com>2020-11-24 19:45:06 +0100
commit19e224866ba6cfe2c6cdc49e435a275b532f0acb (patch)
tree2bccf626f783314e09b6f940f5a0ddee60b02454
parent3fed85437b981a01cc368166841d9d89d99434f0 (diff)
downloadNim-19e224866ba6cfe2c6cdc49e435a275b532f0acb.tar.gz
add simple writeStackTrace for JS backend (#16016)
* add simple writeStackTrace for JS backend

* add testcase for writeStackTrace

* changelog
-rw-r--r--changelog.md2
-rw-r--r--lib/system.nim2
-rw-r--r--lib/system/jssys.nim5
-rw-r--r--tests/js/twritestacktrace.nim12
4 files changed, 20 insertions, 1 deletions
diff --git a/changelog.md b/changelog.md
index 4c18ddf1e..bfbe59e1a 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 716eb296a..be41c1893 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 c4c671ea3..8865558fe 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 000000000..2fe2b1987
--- /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()