make goto based exceptions available for 'nim cpp' (#13244)

* make goto based exceptions available for 'nim cpp'
* optimize seq.add to be comparable to C++'s emplace_back
This commit is contained in:
Andreas Rumpf 2020-01-25 20:00:13 +01:00 • committed by GitHub
commit 6efac70181
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 29 additions and 18 deletions

View file

@ -61,7 +61,7 @@ var
currException {.threadvar.}: ref Exception
gcFramePtr {.threadvar.}: GcFrame
when defined(cpp) and not defined(noCppExceptions):
when defined(cpp) and not defined(noCppExceptions) and not gotoBasedExceptions:
var
raiseCounter {.threadvar.}: uint
@ -410,7 +410,7 @@ proc reportUnhandledError(e: ref Exception) {.nodestroy.} =
discard()
proc nimLeaveFinally() {.compilerRtl.} =
when defined(cpp) and not defined(noCppExceptions):
when defined(cpp) and not defined(noCppExceptions) and not gotoBasedExceptions:
{.emit: "throw;".}
else:
if excHandler != nil:
@ -439,7 +439,7 @@ proc raiseExceptionAux(e: sink(ref Exception)) {.nodestroy.} =
if not localRaiseHook(e): return
if globalRaiseHook != nil:
if not globalRaiseHook(e): return
when defined(cpp) and not defined(noCppExceptions):
when defined(cpp) and not defined(noCppExceptions) and not gotoBasedExceptions:
if e == currException:
{.emit: "throw;".}
else:
@ -544,7 +544,7 @@ proc nimFrame(s: PFrame) {.compilerRtl, inl, raises: [].} =
framePtr = s
if s.calldepth == nimCallDepthLimit: callDepthLimitReached()
when defined(cpp) and appType != "lib" and
when defined(cpp) and appType != "lib" and not gotoBasedExceptions and
not defined(js) and not defined(nimscript) and
hostOS != "standalone" and not defined(noCppExceptions):

View file

@ -80,7 +80,7 @@ proc addEntry(entry: LogEntry) =
#inc gLog.count
#gLog.disabled = false
proc memTrackerWrite(address: pointer; size: int; file: cstring; line: int) {.compilerProc.} =
proc memTrackerWrite(address: pointer; size: int; file: cstring; line: int) {.compilerproc.} =
addEntry LogEntry(op: "write", address: address,
size: size, file: file, line: line, thread: myThreadId())

View file

@ -92,7 +92,7 @@ proc grow*[T](x: var seq[T]; newLen: Natural; value: T) =
for i in oldLen .. newLen-1:
xu.p.data[i] = value
proc add*[T](x: var seq[T]; value: sink T) {.magic: "AppendSeqElem", noSideEffect.} =
proc add*[T](x: var seq[T]; value: sink T) {.magic: "AppendSeqElem", noSideEffect, nodestroy.} =
## Generic proc for adding a data item `y` to a container `x`.
##
## For containers that have an order, `add` means *append*. New generic
@ -104,6 +104,10 @@ proc add*[T](x: var seq[T]; value: sink T) {.magic: "AppendSeqElem", noSideEffec
if xu.p == nil or xu.p.cap < oldLen+1:
xu.p = cast[typeof(xu.p)](prepareSeqAdd(oldLen, xu.p, 1, sizeof(T)))
xu.len = oldLen+1
# .nodestroy means `xu.p.data[oldLen] = value` is compiled into a
# copyMem(). This is fine as know by construction that
# in `xu.p.data[oldLen]` there is nothing to destroy.
# We also save the `wasMoved + destroy` pair for the sink parameter.
xu.p.data[oldLen] = value
proc setLen[T](s: var seq[T], newlen: Natural) =