fatal.nim 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. when defined(nimHasExceptionsQuery):
  11. const gotoBasedExceptions = compileOption("exceptions", "goto")
  12. else:
  13. const gotoBasedExceptions = false
  14. when hostOS == "standalone":
  15. include "$projectpath/panicoverride"
  16. func sysFatal(exceptn: typedesc, message: string) {.inline.} =
  17. panic(message)
  18. func sysFatal(exceptn: typedesc, message, arg: string) {.inline.} =
  19. rawoutput(message)
  20. panic(arg)
  21. elif (defined(nimQuirky) or defined(nimPanics)) and not defined(nimscript):
  22. import ansi_c
  23. func name(t: typedesc): string {.magic: "TypeTrait".}
  24. func sysFatal(exceptn: typedesc, message, arg: string) {.inline, noreturn.} =
  25. when nimvm:
  26. # TODO when doAssertRaises works in CT, add a test for it
  27. raise (ref exceptn)(msg: message & arg)
  28. else:
  29. {.noSideEffect.}:
  30. writeStackTrace()
  31. var buf = newStringOfCap(200)
  32. add(buf, "Error: unhandled exception: ")
  33. add(buf, message)
  34. add(buf, arg)
  35. add(buf, " [")
  36. add(buf, name exceptn)
  37. add(buf, "]\n")
  38. cstderr.rawWrite buf
  39. rawQuit 1
  40. func sysFatal(exceptn: typedesc, message: string) {.inline, noreturn.} =
  41. sysFatal(exceptn, message, "")
  42. else:
  43. func sysFatal(exceptn: typedesc, message: string) {.inline, noreturn.} =
  44. raise (ref exceptn)(msg: message)
  45. func sysFatal(exceptn: typedesc, message, arg: string) {.inline, noreturn.} =
  46. raise (ref exceptn)(msg: message & arg)
  47. {.pop.}