added system.onRaise to support a condition system
This commit is contained in:
parent
42c8fd1fe2
commit
865d9cc6e6
7 changed files with 100 additions and 36 deletions
|
|
@ -1941,6 +1941,7 @@ when not defined(EcmaScript) and not defined(NimrodVM):
|
|||
prev: PSafePoint # points to next safe point ON THE STACK
|
||||
status: int
|
||||
context: C_JmpBuf
|
||||
raiseAction: proc (e: ref E_Base): bool {.closure.}
|
||||
|
||||
when defined(initAllocator):
|
||||
initAllocator()
|
||||
|
|
@ -2040,6 +2041,14 @@ when not defined(EcmaScript) and not defined(NimrodVM):
|
|||
var e = getCurrentException()
|
||||
return if e == nil: "" else: e.msg
|
||||
|
||||
proc onRaise*(action: proc(e: ref E_Base): bool{.closure.}) =
|
||||
## can be used in a ``try`` statement to setup a Lisp-like
|
||||
## `condition system`:idx:\: This prevents the 'raise' statement to
|
||||
## raise an exception but instead calls ``action``.
|
||||
## If ``action`` returns false, the exception has been handled and
|
||||
## does not propagate further through the call stack.
|
||||
if not isNil(excHandler): excHandler.raiseAction = action
|
||||
|
||||
{.push stack_trace: off, profiler:off.}
|
||||
when defined(endb):
|
||||
include "system/debugger"
|
||||
|
|
|
|||
|
|
@ -52,6 +52,7 @@ proc setFrame(s: PFrame) {.compilerRtl, inl.} =
|
|||
framePtr = s
|
||||
|
||||
proc pushSafePoint(s: PSafePoint) {.compilerRtl, inl.} =
|
||||
s.raiseAction = nil
|
||||
s.prev = excHandler
|
||||
excHandler = s
|
||||
|
||||
|
|
@ -198,8 +199,9 @@ proc raiseExceptionAux(e: ref E_Base) =
|
|||
if globalRaiseHook != nil:
|
||||
if not globalRaiseHook(e): return
|
||||
if excHandler != nil:
|
||||
pushCurrentException(e)
|
||||
c_longjmp(excHandler.context, 1)
|
||||
if isNil(excHandler.raiseAction) or excHandler.raiseAction(e):
|
||||
pushCurrentException(e)
|
||||
c_longjmp(excHandler.context, 1)
|
||||
elif e[] of EOutOfMemory:
|
||||
writeToStdErr(e.name)
|
||||
quitOrDebug()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue