summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2014-09-09 01:16:28 +0100
committerDominik Picheta <dominikpicheta@googlemail.com>2014-09-09 01:16:28 +0100
commitdd33069d593878e0e7c073ca3ef81157e105667c (patch)
tree4f5bcec4b3ec272b4d89d34cf45bd7a7d39081a6 /lib
parent6689fa68f5ee3868b128b308cb237be5c4aa5d98 (diff)
downloadNim-dd33069d593878e0e7c073ca3ef81157e105667c.tar.gz
Implements getCurrentException for try in async procs. Ref #1487.
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/asyncdispatch.nim17
-rw-r--r--lib/system.nim6
2 files changed, 18 insertions, 5 deletions
diff --git a/lib/pure/asyncdispatch.nim b/lib/pure/asyncdispatch.nim
index 1cf54b922..f3d37f9d2 100644
--- a/lib/pure/asyncdispatch.nim
+++ b/lib/pure/asyncdispatch.nim
@@ -973,7 +973,7 @@ template createCb*(retFutureSym, iteratorNameSym,
   #{.pop.}
 proc generateExceptionCheck(futSym,
     tryStmt, rootReceiver, fromNode: PNimrodNode): PNimrodNode {.compileTime.} =
-  if tryStmt.len == 1:
+  if tryStmt.kind == nnkNilLit:
     result = rootReceiver
   else:
     var exceptionChecks: seq[tuple[cond, body: PNimrodNode]] = @[]
@@ -1007,8 +1007,14 @@ proc generateExceptionCheck(futSym,
     let elseNode = newNimNode(nnkElse, fromNode)
     elseNode.add newNimNode(nnkStmtList, fromNode)
     elseNode[0].add rootReceiver
+
+    let ifBody = newStmtList()
+    ifBody.add newCall(newIdentNode("setCurrentException"), errorNode)
+    ifBody.add newIfStmt(exceptionChecks)
+    ifBody.add newCall(newIdentNode("setCurrentException"), newNilLit())
+
     result = newIfStmt(
-      (newDotExpr(futSym, newIdentNode("failed")), newIfStmt(exceptionChecks))
+      (newDotExpr(futSym, newIdentNode("failed")), ifBody)
     )
     result.add elseNode
 
@@ -1224,6 +1230,8 @@ proc recvLine*(socket: TAsyncFD): Future[string] {.async.} =
   ## If the socket is disconnected in the middle of a line (before ``\r\L``
   ## is read) then line will be set to ``""``.
   ## The partial line **will be lost**.
+  ##
+  ## **Warning**: This assumes that lines are delimited by ``\r\l``.
   
   template addNLIfEmpty(): stmt =
     if result.len == 0:
@@ -1236,9 +1244,8 @@ proc recvLine*(socket: TAsyncFD): Future[string] {.async.} =
     if c.len == 0:
       return ""
     if c == "\r":
-      c = await recv(socket, 1, {SocketFlag.SafeDisconn, SocketFlag.Peek})
-      if c.len > 0 and c == "\L":
-        discard await recv(socket, 1)
+      c = await recv(socket, 1)
+      assert c == "\l"
       addNLIfEmpty()
       return
     elif c == "\L":
diff --git a/lib/system.nim b/lib/system.nim
index 9153af16c..ad99306ad 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -2630,6 +2630,12 @@ when not defined(JS): #and not defined(NimrodVM):
         excHandler.hasRaiseAction = true
         excHandler.raiseAction = action
 
+    proc setCurrentException*(exc: ref Exception) {.inline, gcsafe.} =
+      ## sets the current exception.
+      ##
+      ## **Warning**: Only use this if you know what you are doing.
+      currException = exc
+
   {.push stack_trace: off, profiler:off.}
   when defined(endb) and not defined(NimrodVM):
     include "system/debugger"