clean up goto exceptions; remove the setjmp.h dep (#23259)

(cherry picked from commit e3350cbe6f)
This commit is contained in:
ringabout
2024-01-27 14:57:07 +08:00
committed by narimiran
parent 96917755cd
commit 45f1b19942
3 changed files with 53 additions and 35 deletions

View File

@@ -2077,7 +2077,7 @@ when not defined(js) and declared(alloc0) and declared(dealloc):
inc(i)
dealloc(a)
when notJSnotNims:
when notJSnotNims and not gotoBasedExceptions:
type
PSafePoint = ptr TSafePoint
TSafePoint {.compilerproc, final.} = object

View File

@@ -19,8 +19,9 @@ proc nimFrame(s: PFrame) {.compilerRtl, inl, exportc: "nimFrame".} = discard
proc popFrame {.compilerRtl, inl.} = discard
proc setFrame(s: PFrame) {.compilerRtl, inl.} = discard
proc pushSafePoint(s: PSafePoint) {.compilerRtl, inl.} = discard
proc popSafePoint {.compilerRtl, inl.} = discard
when not gotoBasedExceptions:
proc pushSafePoint(s: PSafePoint) {.compilerRtl, inl.} = discard
proc popSafePoint {.compilerRtl, inl.} = discard
proc pushCurrentException(e: ref Exception) {.compilerRtl, inl.} = discard
proc popCurrentException {.compilerRtl, inl.} = discard

View File

@@ -75,24 +75,39 @@ when NimStackTraceMsgs:
var frameMsgBuf* {.threadvar.}: string
var
framePtr {.threadvar.}: PFrame
excHandler {.threadvar.}: PSafePoint
# list of exception handlers
# a global variable for the root of all try blocks
currException {.threadvar.}: ref Exception
gcFramePtr {.threadvar.}: GcFrame
type
FrameState = tuple[gcFramePtr: GcFrame, framePtr: PFrame,
excHandler: PSafePoint, currException: ref Exception]
when not gotoBasedExceptions:
var
excHandler {.threadvar.}: PSafePoint
# list of exception handlers
# a global variable for the root of all try blocks
gcFramePtr {.threadvar.}: GcFrame
when gotoBasedExceptions:
type
FrameState = tuple[framePtr: PFrame,
currException: ref Exception]
else:
type
FrameState = tuple[gcFramePtr: GcFrame, framePtr: PFrame,
excHandler: PSafePoint, currException: ref Exception]
proc getFrameState*(): FrameState {.compilerRtl, inl.} =
return (gcFramePtr, framePtr, excHandler, currException)
when gotoBasedExceptions:
return (framePtr, currException)
else:
return (gcFramePtr, framePtr, excHandler, currException)
proc setFrameState*(state: FrameState) {.compilerRtl, inl.} =
gcFramePtr = state.gcFramePtr
framePtr = state.framePtr
excHandler = state.excHandler
currException = state.currException
when gotoBasedExceptions:
framePtr = state.framePtr
currException = state.currException
else:
gcFramePtr = state.gcFramePtr
framePtr = state.framePtr
excHandler = state.excHandler
currException = state.currException
proc getFrame*(): PFrame {.compilerRtl, inl.} = framePtr
@@ -114,20 +129,21 @@ when false:
proc setFrame*(s: PFrame) {.compilerRtl, inl.} =
framePtr = s
proc getGcFrame*(): GcFrame {.compilerRtl, inl.} = gcFramePtr
proc popGcFrame*() {.compilerRtl, inl.} = gcFramePtr = gcFramePtr.prev
proc setGcFrame*(s: GcFrame) {.compilerRtl, inl.} = gcFramePtr = s
proc pushGcFrame*(s: GcFrame) {.compilerRtl, inl.} =
s.prev = gcFramePtr
zeroMem(cast[pointer](cast[int](s)+%sizeof(GcFrameHeader)), s.len*sizeof(pointer))
gcFramePtr = s
when not gotoBasedExceptions:
proc getGcFrame*(): GcFrame {.compilerRtl, inl.} = gcFramePtr
proc popGcFrame*() {.compilerRtl, inl.} = gcFramePtr = gcFramePtr.prev
proc setGcFrame*(s: GcFrame) {.compilerRtl, inl.} = gcFramePtr = s
proc pushGcFrame*(s: GcFrame) {.compilerRtl, inl.} =
s.prev = gcFramePtr
zeroMem(cast[pointer](cast[int](s)+%sizeof(GcFrameHeader)), s.len*sizeof(pointer))
gcFramePtr = s
proc pushSafePoint(s: PSafePoint) {.compilerRtl, inl.} =
s.prev = excHandler
excHandler = s
proc pushSafePoint(s: PSafePoint) {.compilerRtl, inl.} =
s.prev = excHandler
excHandler = s
proc popSafePoint {.compilerRtl, inl.} =
excHandler = excHandler.prev
proc popSafePoint {.compilerRtl, inl.} =
excHandler = excHandler.prev
proc pushCurrentException(e: sink(ref Exception)) {.compilerRtl, inl.} =
e.up = currException
@@ -407,15 +423,16 @@ proc reportUnhandledError(e: ref Exception) {.nodestroy, gcsafe.} =
when hostOS != "any":
reportUnhandledErrorAux(e)
proc nimLeaveFinally() {.compilerRtl.} =
when defined(cpp) and not defined(noCppExceptions) and not gotoBasedExceptions:
{.emit: "throw;".}
else:
if excHandler != nil:
c_longjmp(excHandler.context, 1)
when not gotoBasedExceptions:
proc nimLeaveFinally() {.compilerRtl.} =
when defined(cpp) and not defined(noCppExceptions) and not gotoBasedExceptions:
{.emit: "throw;".}
else:
reportUnhandledError(currException)
rawQuit(1)
if excHandler != nil:
c_longjmp(excHandler.context, 1)
else:
reportUnhandledError(currException)
rawQuit(1)
when gotoBasedExceptions:
var nimInErrorMode {.threadvar.}: bool