Fix forward

This commit is contained in:
Alexander Ivanov 2017-12-19 20:50:37 +02:00
commit b3dfc93bee

View file

@ -92,33 +92,37 @@ proc generateJsasync(arg: NimNode): NimNode =
assert arg.kind == nnkProcDef assert arg.kind == nnkProcDef
result = arg result = arg
var isVoid = false var isVoid = false
var jsResolveNode = ident("jsResolve")
if arg.params[0].kind == nnkEmpty: if arg.params[0].kind == nnkEmpty:
result.params[0] = nnkBracketExpr.newTree(ident("Future"), ident("void")) result.params[0] = nnkBracketExpr.newTree(ident("Future"), ident("void"))
isVoid = true isVoid = true
elif isFutureVoid(arg.params[0]): elif isFutureVoid(arg.params[0]):
isVoid = true isVoid = true
var code = result.body var code = result.body
replaceReturn(code) replaceReturn(code)
result.body = nnkStmtList.newTree() result.body = nnkStmtList.newTree()
var awaitFunction = quote: if len(code) > 0:
proc await[T](f: Future[T]): T {.importcpp: "(await #)".} var awaitFunction = quote:
result.body.add(awaitFunction) proc await[T](f: Future[T]): T {.importcpp: "(await #)".}
result.body.add(awaitFunction)
var resolve: NimNode var resolve: NimNode
var jsResolveNode = ident("jsResolve") if isVoid:
if isVoid: resolve = quote:
resolve = quote: var `jsResolveNode` {.importcpp: "undefined".}: Future[void]
var `jsResolveNode` {.importcpp: "undefined".}: Future[void] else:
resolve = quote:
proc jsResolve[T](a: T): Future[T] {.importcpp: "#".}
result.body.add(resolve)
else: else:
resolve = quote: result.body = newEmptyNode()
proc jsResolve[T](a: T): Future[T] {.importcpp: "#".}
result.body.add(resolve)
for child in code: for child in code:
result.body.add(child) result.body.add(child)
if isVoid: if len(code) > 0 and isVoid:
var voidFix = quote: var voidFix = quote:
return `jsResolveNode` return `jsResolveNode`
result.body.add(voidFix) result.body.add(voidFix)
@ -126,6 +130,7 @@ proc generateJsasync(arg: NimNode): NimNode =
result.pragma = quote: result.pragma = quote:
{.codegenDecl: "async function $2($3)".} {.codegenDecl: "async function $2($3)".}
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