Asyncdispatch fixes.
``return`` is now transformed into a ``return nil`` in the async iterator to work around the no-yield in ``try .. except ..`` closure iterator limitation.
This commit is contained in:
parent
694fc87b1d
commit
d0478a5637
2 changed files with 20 additions and 7 deletions
|
|
@ -10,6 +10,7 @@
|
||||||
import os, oids, tables, strutils, macros
|
import os, oids, tables, strutils, macros
|
||||||
|
|
||||||
import rawsockets
|
import rawsockets
|
||||||
|
export TPort
|
||||||
|
|
||||||
## AsyncDispatch
|
## AsyncDispatch
|
||||||
## --------
|
## --------
|
||||||
|
|
@ -18,6 +19,8 @@ import rawsockets
|
||||||
## On Windows IOCP is used and on other operating systems the selectors module
|
## On Windows IOCP is used and on other operating systems the selectors module
|
||||||
## is used instead.
|
## is used instead.
|
||||||
|
|
||||||
|
# TODO: Discarded void PFutures need to checked for exception.
|
||||||
|
|
||||||
# -- Futures
|
# -- Futures
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
@ -764,14 +767,24 @@ template createVar(futSymName: string, asyncProc: PNimrodNode,
|
||||||
result.add newNimNode(nnkYieldStmt).add(futSym) # -> yield future<x>
|
result.add newNimNode(nnkYieldStmt).add(futSym) # -> yield future<x>
|
||||||
valueReceiver = newDotExpr(futSym, newIdentNode("read")) # -> future<x>.read
|
valueReceiver = newDotExpr(futSym, newIdentNode("read")) # -> future<x>.read
|
||||||
|
|
||||||
proc processBody(node, retFutureSym: PNimrodNode): PNimrodNode {.compileTime.} =
|
proc processBody(node, retFutureSym: PNimrodNode,
|
||||||
|
subtypeName: string): PNimrodNode {.compileTime.} =
|
||||||
result = node
|
result = node
|
||||||
case node.kind
|
case node.kind
|
||||||
of nnkReturnStmt:
|
of nnkReturnStmt:
|
||||||
result = newNimNode(nnkStmtList)
|
result = newNimNode(nnkStmtList)
|
||||||
result.add newCall(newIdentNode("complete"), retFutureSym,
|
if node[0].kind == nnkEmpty:
|
||||||
if node[0].kind == nnkEmpty: newIdentNode("result") else: node[0])
|
if subtypeName != "void":
|
||||||
result.add newNimNode(nnkYieldStmt).add(newNilLit())
|
result.add newCall(newIdentNode("complete"), retFutureSym,
|
||||||
|
newIdentNode("result"))
|
||||||
|
else:
|
||||||
|
result.add newCall(newIdentNode("complete"), retFutureSym)
|
||||||
|
else:
|
||||||
|
result.add newCall(newIdentNode("complete"), retFutureSym,
|
||||||
|
node[0].processBody(retFutureSym, subtypeName))
|
||||||
|
|
||||||
|
result.add newNimNode(nnkReturnStmt).add(newNilLit())
|
||||||
|
return # Don't process the children of this return stmt
|
||||||
of nnkCommand:
|
of nnkCommand:
|
||||||
if node[0].kind == nnkIdent and node[0].ident == !"await":
|
if node[0].kind == nnkIdent and node[0].ident == !"await":
|
||||||
case node[1].kind
|
case node[1].kind
|
||||||
|
|
@ -819,7 +832,7 @@ proc processBody(node, retFutureSym: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||||
else: discard
|
else: discard
|
||||||
|
|
||||||
for i in 0 .. <result.len:
|
for i in 0 .. <result.len:
|
||||||
result[i] = processBody(result[i], retFutureSym)
|
result[i] = processBody(result[i], retFutureSym, subtypeName)
|
||||||
#echo(treeRepr(result))
|
#echo(treeRepr(result))
|
||||||
|
|
||||||
proc getName(node: PNimrodNode): string {.compileTime.} =
|
proc getName(node: PNimrodNode): string {.compileTime.} =
|
||||||
|
|
@ -867,7 +880,7 @@ macro async*(prc: stmt): stmt {.immediate.} =
|
||||||
# -> <proc_body>
|
# -> <proc_body>
|
||||||
# -> complete(retFuture, result)
|
# -> complete(retFuture, result)
|
||||||
var iteratorNameSym = genSym(nskIterator, $prc[0].getName & "Iter")
|
var iteratorNameSym = genSym(nskIterator, $prc[0].getName & "Iter")
|
||||||
var procBody = prc[6].processBody(retFutureSym)
|
var procBody = prc[6].processBody(retFutureSym, subtypeName)
|
||||||
if subtypeName != "void":
|
if subtypeName != "void":
|
||||||
procBody.insert(0, newNimNode(nnkVarSection).add(
|
procBody.insert(0, newNimNode(nnkVarSection).add(
|
||||||
newIdentDefs(newIdentNode("result"), returnType[1]))) # -> var result: T
|
newIdentDefs(newIdentNode("result"), returnType[1]))) # -> var result: T
|
||||||
|
|
|
||||||
|
|
@ -430,7 +430,7 @@ proc generateHeaders(r: TURL, httpMethod: THttpMethod,
|
||||||
add(result, "\c\L")
|
add(result, "\c\L")
|
||||||
|
|
||||||
type
|
type
|
||||||
PAsyncHttpClient = ref object
|
PAsyncHttpClient* = ref object
|
||||||
socket: PAsyncSocket
|
socket: PAsyncSocket
|
||||||
connected: bool
|
connected: bool
|
||||||
currentURL: TURL ## Where we are currently connected.
|
currentURL: TURL ## Where we are currently connected.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue