Skip to content

Commit

Permalink
Added cursors in the right places of the async macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray Imber committed Jan 19, 2020
1 parent a5e6707 commit 39c1131
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/pure/asyncmacro.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ proc skipStmtList(node: NimNode): NimNode {.compileTime.} =
template createCb(retFutureSym, iteratorNameSym,
strName, identName, futureVarCompletions: untyped) =
bind finished
let retFutUnown = unown retFutureSym

var nameIterVar = iteratorNameSym
var nameIterVar {.cursor.} = iteratorNameSym
proc identName {.closure.} =
try:
if not nameIterVar.finished:
Expand All @@ -39,7 +38,7 @@ template createCb(retFutureSym, iteratorNameSym,
break

if next == nil:
if not retFutUnown.finished:
if not retFutureSym.finished:
let msg = "Async procedure ($1) yielded `nil`, are you await'ing a " &
"`nil` Future?"
raise newException(AssertionError, msg % strName)
Expand All @@ -50,12 +49,12 @@ template createCb(retFutureSym, iteratorNameSym,
{.pop.}
except:
futureVarCompletions
if retFutUnown.finished:
if retFutureSym.finished:
# Take a look at tasyncexceptions for the bug which this fixes.
# That test explains it better than I can here.
raise
else:
retFutUnown.fail(getCurrentException())
retFutureSym.fail(getCurrentException())
identName()

template useVar(result: var NimNode, futureVarNode: NimNode, valueReceiver,
Expand Down Expand Up @@ -252,6 +251,7 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =

# -> var retFuture = newFuture[T]()
var retFutureSym = genSym(nskVar, "retFuture")
var retFutureCursorSym = genSym(nskVar, "retFutureCursor")
var subRetType =
if returnType.kind == nnkEmpty: newIdentNode("void")
else: baseType
Expand All @@ -262,6 +262,8 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =
newIdentNode("newFuture"),
subRetType),
newLit(prcName)))) # Get type from return type of this proc
outerProcBody.add newVarStmt(nnkPragmaExpr.newTree(retFutureCursorSym, nnkPragma.newTree(newIdentNode("cursor"))), retFutureSym)

This comment has been minimized.

Copy link
@disruptek

disruptek Jan 20, 2020

girthy



# -> iterator nameIter(): FutureBase {.closure.} =
# -> {.push warning[resultshadowed]: off.}
Expand All @@ -270,7 +272,7 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =
# -> <proc_body>
# -> complete(retFuture, result)
var iteratorNameSym = genSym(nskIterator, $prcName & "Iter")
var procBody = prc.body.processBody(retFutureSym, subtypeIsVoid,
var procBody = prc.body.processBody(retFutureCursorSym, subtypeIsVoid,
futureVarIdents)
# don't do anything with forward bodies (empty)
if procBody.kind != nnkEmpty:
Expand All @@ -290,10 +292,10 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =

procBody.add(
newCall(newIdentNode("complete"),
retFutureSym, newIdentNode("result"))) # -> complete(retFuture, result)
retFutureCursorSym, newIdentNode("result"))) # -> complete(retFuture, result)
else:
# -> complete(retFuture)
procBody.add(newCall(newIdentNode("complete"), retFutureSym))
procBody.add(newCall(newIdentNode("complete"), retFutureCursorSym))

var closureIterator = newProc(iteratorNameSym, [parseExpr("owned(FutureBase)")],
procBody, nnkIteratorDef)
Expand All @@ -310,7 +312,7 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =
# NOTE: The NimAsyncContinueSuffix is checked for in asyncfutures.nim to produce
# friendlier stack traces:
var cbName = genSym(nskProc, prcName & NimAsyncContinueSuffix)
var procCb = getAst createCb(retFutureSym, iteratorNameSym,
var procCb = getAst createCb(retFutureCursorSym, iteratorNameSym,
newStrLitNode(prcName),
cbName,
createFutureVarCompletions(futureVarIdents, nil))
Expand All @@ -328,7 +330,7 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =
result.params[0] = parseExpr("owned(Future[void])")
if procBody.kind != nnkEmpty:
result.body = outerProcBody
#echo(treeRepr(result))
echo(treeRepr(result))
#if prcName == "recvLineInto":
# echo(toStrLit(result))

Expand Down

0 comments on commit 39c1131

Please sign in to comment.