Minor fixes for asyncjs (#8894)

* Minor fixes for asyncjs

Mark internal procedures as used
Allow .async. for more node kinds
Make .async. work for nkStmtList

* Fix logic error in both the async macros
This commit is contained in:
LemonBoy 2018-09-07 16:42:38 +02:00 • committed by Andreas Rumpf
commit 90025e242d
2 changed files with 13 additions and 5 deletions

View file

@ -92,7 +92,10 @@ proc isFutureVoid(node: NimNode): bool =
node[1].kind == nnkIdent and $node[1] == "void" node[1].kind == nnkIdent and $node[1] == "void"
proc generateJsasync(arg: NimNode): NimNode = proc generateJsasync(arg: NimNode): NimNode =
assert arg.kind == nnkProcDef if arg.kind notin {nnkProcDef, nnkLambda, nnkMethodDef, nnkDo}:
error("Cannot transform this node kind into an async proc." &
" proc/method definition or lambda node expected.")
result = arg result = arg
var isVoid = false var isVoid = false
let jsResolve = ident("jsResolve") let jsResolve = ident("jsResolve")
@ -108,7 +111,7 @@ proc generateJsasync(arg: NimNode): NimNode =
if len(code) > 0: if len(code) > 0:
var awaitFunction = quote: var awaitFunction = quote:
proc await[T](f: Future[T]): T {.importcpp: "(await #)".} proc await[T](f: Future[T]): T {.importcpp: "(await #)", used.}
result.body.add(awaitFunction) result.body.add(awaitFunction)
var resolve: NimNode var resolve: NimNode
@ -117,7 +120,7 @@ proc generateJsasync(arg: NimNode): NimNode =
var `jsResolve` {.importcpp: "undefined".}: Future[void] var `jsResolve` {.importcpp: "undefined".}: Future[void]
else: else:
resolve = quote: resolve = quote:
proc jsResolve[T](a: T): Future[T] {.importcpp: "#".} proc jsResolve[T](a: T): Future[T] {.importcpp: "#", used.}
result.body.add(resolve) result.body.add(resolve)
else: else:
result.body = newEmptyNode() result.body = newEmptyNode()
@ -137,7 +140,12 @@ proc generateJsasync(arg: NimNode): NimNode =
macro async*(arg: untyped): untyped = macro async*(arg: untyped): untyped =
## Macro which converts normal procedures into ## Macro which converts normal procedures into
## javascript-compatible async procedures ## javascript-compatible async procedures
generateJsasync(arg) if arg.kind == nnkStmtList:
result = newStmtList()
for oneProc in arg:
result.add generateJsasync(oneProc)
else:
result = generateJsasync(arg)
proc newPromise*[T](handler: proc(resolve: proc(response: T))): Future[T] {.importcpp: "(new Promise(#))".} proc newPromise*[T](handler: proc(resolve: proc(response: T))): Future[T] {.importcpp: "(new Promise(#))".}
## A helper for wrapping callback-based functions ## A helper for wrapping callback-based functions

View file

@ -327,8 +327,8 @@ macro async*(prc: untyped): untyped =
## Macro which processes async procedures into the appropriate ## Macro which processes async procedures into the appropriate
## iterators and yield statements. ## iterators and yield statements.
if prc.kind == nnkStmtList: if prc.kind == nnkStmtList:
for oneProc in prc:
result = newStmtList() result = newStmtList()
for oneProc in prc:
result.add asyncSingleProc(oneProc) result.add asyncSingleProc(oneProc)
else: else:
result = asyncSingleProc(prc) result = asyncSingleProc(prc)