summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2014-06-19 00:05:27 +0100
committerDominik Picheta <dominikpicheta@googlemail.com>2014-06-19 00:05:27 +0100
commit7ebbc09578a0204a9bcf361a351e514dec0219c3 (patch)
tree59e756fbb7df35d585aa0efd060b363e7497c941
parent390bbbdd051d34e6b1bc793af924a71fcc9b952e (diff)
downloadNim-7ebbc09578a0204a9bcf361a351e514dec0219c3.tar.gz
Allow async lambdas. Other socket modifications.
-rw-r--r--lib/pure/asyncdispatch.nim27
-rw-r--r--lib/pure/asynchttpserver.nim7
-rw-r--r--lib/pure/net.nim2
-rw-r--r--lib/pure/rawsockets.nim1
4 files changed, 25 insertions, 12 deletions
diff --git a/lib/pure/asyncdispatch.nim b/lib/pure/asyncdispatch.nim
index 87ee83ad9..6d9e605f1 100644
--- a/lib/pure/asyncdispatch.nim
+++ b/lib/pure/asyncdispatch.nim
@@ -27,9 +27,11 @@ export TPort
 
 
 # TODO: Discarded void PFutures need to checked for exception.
-# TODO: Exceptions are currently uncatchable due to the limitation that 
-# you cannot have yield in a try stmt. Perhaps I can get the macro to put
-# a user's try except around ``future.read``.
+# TODO: ``except`` statement (without `try`) does not work.
+# TODO: Multiple exception names in a ``except`` don't work.
+# TODO: The effect system (raises: []) has trouble with my try transformation.
+# TODO: Can't await in a 'except' body
+
 
 # -- Futures
 
@@ -922,14 +924,17 @@ proc getName(node: PNimrodNode): string {.compileTime.} =
     return $node[1].ident
   of nnkIdent:
     return $node.ident
+  of nnkEmpty:
+    return "anonymous"
   else:
-    assert false
+    error("Unknown name.")
 
 macro async*(prc: stmt): stmt {.immediate.} =
   ## Macro which processes async procedures into the appropriate
   ## iterators and yield statements.
-
-  expectKind(prc, nnkProcDef)
+  if prc.kind notin {nnkProcDef, nnkLambda}:
+    error("Cannot transform this node kind into an async proc." &
+          " Proc definition or lambda node expected.")
 
   hint("Processing " & prc[0].getName & " as an async proc.")
 
@@ -941,7 +946,9 @@ macro async*(prc: stmt): stmt {.immediate.} =
     if $returnType[0] != "PFuture":
       error("Expected return type of 'PFuture' got '" & $returnType[0] & "'")
 
-  let subtypeIsVoid = returnType.kind == nnkEmpty
+  let subtypeIsVoid = returnType.kind == nnkEmpty or
+        (returnType.kind == nnkBracketExpr and
+         returnType[1].kind == nnkIdent and returnType[1].ident == !"void")
 
   var outerProcBody = newNimNode(nnkStmtList)
 
@@ -990,17 +997,19 @@ macro async*(prc: stmt): stmt {.immediate.} =
 
   # Remove the 'async' pragma.
   for i in 0 .. <result[4].len:
-    if result[4][i].ident == !"async":
+    if result[4][i].kind == nnkIdent and result[4][i].ident == !"async":
       result[4].del(i)
   if subtypeIsVoid:
     # Add discardable pragma.
-    result[4].add(newIdentNode("discardable"))
+    if prc.kind == nnkProcDef: # TODO: This is a workaround for #1287
+      result[4].add(newIdentNode("discardable"))
     if returnType.kind == nnkEmpty:
       # Add PFuture[void]
       result[3][0] = parseExpr("PFuture[void]")
 
   result[6] = outerProcBody
 
+  #echo(treeRepr(result))
   #echo(toStrLit(result))
 
 proc recvLine*(socket: TAsyncFD): PFuture[string] {.async.} =
diff --git a/lib/pure/asynchttpserver.nim b/lib/pure/asynchttpserver.nim
index 6c2414d99..005c56ebc 100644
--- a/lib/pure/asynchttpserver.nim
+++ b/lib/pure/asynchttpserver.nim
@@ -14,12 +14,13 @@
 import strtabs, asyncnet, asyncdispatch, parseutils, parseurl, strutils
 type
   TRequest* = object
-    client: PAsyncSocket # TODO: Separate this into a Response object?
+    client*: PAsyncSocket # TODO: Separate this into a Response object?
     reqMethod*: string
     headers*: PStringTable
     protocol*: tuple[orig: string, major, minor: int]
     url*: TURL
     hostname*: string ## The hostname of the client that made the request.
+    body*: string # TODO
 
   PAsyncHttpServer* = ref object
     socket: PAsyncSocket
@@ -169,6 +170,10 @@ proc serve*(server: PAsyncHttpServer, port: TPort,
     var fut = await server.socket.acceptAddr()
     processClient(fut.client, fut.address, callback)
 
+proc close*(server: PAsyncHttpServer) =
+  ## Terminates the async http server instance.
+  server.socket.close()
+
 when isMainModule:
   var server = newAsyncHttpServer()
   proc cb(req: TRequest) {.async.} =
diff --git a/lib/pure/net.nim b/lib/pure/net.nim
index 2f1a6fa46..e34c88327 100644
--- a/lib/pure/net.nim
+++ b/lib/pure/net.nim
@@ -11,7 +11,7 @@
 
 {.deadCodeElim: on.}
 import rawsockets, os, strutils, unsigned, parseutils, times
-export TPort
+export TPort, `$`
 
 const useWinVersion = defined(Windows) or defined(nimdoc)
 
diff --git a/lib/pure/rawsockets.nim b/lib/pure/rawsockets.nim
index 07b647b68..94189fd89 100644
--- a/lib/pure/rawsockets.nim
+++ b/lib/pure/rawsockets.nim
@@ -39,7 +39,6 @@ export
   MSG_PEEK
 
 type
-  
   TPort* = distinct uint16  ## port type
   
   TDomain* = enum   ## domain, which specifies the protocol family of the