fatal.nim 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2019 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. {.push profiler: off.}
  10. const
  11. gotoBasedExceptions = compileOption("exceptions", "goto")
  12. quirkyExceptions = compileOption("exceptions", "quirky")
  13. when hostOS == "standalone":
  14. include "$projectpath/panicoverride"
  15. func sysFatal(exceptn: typedesc, message: string) {.inline.} =
  16. panic(message)
  17. func sysFatal(exceptn: typedesc, message, arg: string) {.inline.} =
  18. rawoutput(message)
  19. panic(arg)
  20. elif (quirkyExceptions or defined(nimPanics)) and not defined(nimscript):
  21. import ansi_c
  22. func name(t: typedesc): string {.magic: "TypeTrait".}
  23. func sysFatal(exceptn: typedesc, message, arg: string) {.inline, noreturn.} =
  24. when nimvm:
  25. # TODO when doAssertRaises works in CT, add a test for it
  26. raise (ref exceptn)(msg: message & arg)
  27. else:
  28. {.noSideEffect.}:
  29. writeStackTrace()
  30. var buf = newStringOfCap(200)
  31. add(buf, "Error: unhandled exception: ")
  32. add(buf, message)
  33. add(buf, arg)
  34. add(buf, " [")
  35. add(buf, name exceptn)
  36. add(buf, "]\n")
  37. cstderr.rawWrite buf
  38. rawQuit 1
  39. func sysFatal(exceptn: typedesc, message: string) {.inline, noreturn.} =
  40. sysFatal(exceptn, message, "")
  41. else:
  42. func sysFatal(exceptn: typedesc, message: string) {.inline, noreturn.} =
  43. raise (ref exceptn)(msg: message)
  44. func sysFatal(exceptn: typedesc, message, arg: string) {.inline, noreturn.} =
  45. raise (ref exceptn)(msg: message & arg)
  46. {.pop.}