mstackframes.nim 868 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import std/stackframes
  2. # line 5
  3. var count = 0
  4. proc main1(n: int) =
  5. setFrameMsg $("main1", n)
  6. if n > 0:
  7. main1(n-1)
  8. proc main2(n: int) =
  9. count.inc
  10. setFrameMsg $("main2", n, count)
  11. proc bar() =
  12. setFrameMsg $("bar ",)
  13. if n < 3: raise newException(CatchableError, "on purpose")
  14. bar()
  15. main2(n-1)
  16. proc main() =
  17. var z = 0
  18. setFrameMsg "\n z: " & $z, prefix = ""
  19. # multiple calls inside a frame are possible
  20. z.inc
  21. setFrameMsg "\n z: " & $z, prefix = ""
  22. try:
  23. main2(5)
  24. except CatchableError:
  25. main1(10) # goes deep and then unwinds; sanity check to ensure `setFrameMsg` from inside
  26. # `main1` won't invalidate the stacktrace; if StackTraceEntry.frameMsg
  27. # were a reference instead of a copy, this would fail.
  28. let e = getCurrentException()
  29. let trace = e.getStackTrace
  30. echo trace
  31. main()