mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-03 03:32:32 +00:00
This implements "deterministic" exception handling for Nim based on goto instead of setjmp. This means raising an exception is much cheaper than in C++'s table based implementations. Supports hard realtime systems. Default for --gc:arc and the C target because it's generally a good idea and arc is all about deterministic behavior. Note: This implies that fatal runtime traps are not catchable anymore! This needs to be documented.
66 lines
1.6 KiB
Nim
66 lines
1.6 KiB
Nim
#
|
|
#
|
|
# Nim's Runtime Library
|
|
# (c) Copyright 2019 Andreas Rumpf
|
|
#
|
|
# See the file "copying.txt", included in this
|
|
# distribution, for details about the copyright.
|
|
#
|
|
|
|
{.push profiler: off.}
|
|
|
|
when defined(nimHasExceptionsQuery):
|
|
const gotoBasedExceptions = compileOption("exceptions", "goto")
|
|
else:
|
|
const gotoBasedExceptions = false
|
|
|
|
when hostOS == "standalone":
|
|
include "$projectpath/panicoverride"
|
|
|
|
proc sysFatal(exceptn: typedesc, message: string) {.inline.} =
|
|
panic(message)
|
|
|
|
proc sysFatal(exceptn: typedesc, message, arg: string) {.inline.} =
|
|
rawoutput(message)
|
|
panic(arg)
|
|
|
|
elif (defined(nimQuirky) or gotoBasedExceptions) and not defined(nimscript):
|
|
import ansi_c
|
|
|
|
proc name(t: typedesc): string {.magic: "TypeTrait".}
|
|
|
|
proc sysFatal(exceptn: typedesc, message, arg: string) {.inline, noreturn.} =
|
|
writeStackTrace()
|
|
var buf = newStringOfCap(200)
|
|
add(buf, "Error: unhandled exception: ")
|
|
add(buf, message)
|
|
add(buf, arg)
|
|
add(buf, " [")
|
|
add(buf, name exceptn)
|
|
add(buf, "]\n")
|
|
cstderr.rawWrite buf
|
|
quit 1
|
|
|
|
proc sysFatal(exceptn: typedesc, message: string) {.inline, noreturn.} =
|
|
sysFatal(exceptn, message, "")
|
|
|
|
else:
|
|
proc sysFatal(exceptn: typedesc, message: string) {.inline, noreturn.} =
|
|
when declared(owned):
|
|
var e: owned(ref exceptn)
|
|
else:
|
|
var e: ref exceptn
|
|
new(e)
|
|
e.msg = message
|
|
raise e
|
|
|
|
proc sysFatal(exceptn: typedesc, message, arg: string) {.inline, noreturn.} =
|
|
when declared(owned):
|
|
var e: owned(ref exceptn)
|
|
else:
|
|
var e: ref exceptn
|
|
new(e)
|
|
e.msg = message & arg
|
|
raise e
|
|
{.pop.}
|