summary refs log tree commit diff stats
path: root/lib/js
diff options
context:
space:
mode:
authorAlexander Ivanov <alehander42@gmail.com>2017-12-19 13:57:37 +0200
committerAlexander Ivanov <alehander42@gmail.com>2017-12-19 13:57:37 +0200
commit7f6afa9e9b554799cf9a39d0f8cc7d35e47a2cb4 (patch)
treec8be829b63bb8694fdffb3be4cfa6493b3634a9e /lib/js
parent85ac3130aa80e784fcd84c5a38122c61f4a8860d (diff)
downloadNim-7f6afa9e9b554799cf9a39d0f8cc7d35e47a2cb4.tar.gz
Make asyncjs Future[void] play nicely with last line discardable calls
Diffstat (limited to 'lib/js')
-rw-r--r--lib/js/asyncjs.nim32
1 files changed, 29 insertions, 3 deletions
diff --git a/lib/js/asyncjs.nim b/lib/js/asyncjs.nim
index bde3d787f..73dbfb1e7 100644
--- a/lib/js/asyncjs.nim
+++ b/lib/js/asyncjs.nim
@@ -83,20 +83,46 @@ proc replaceReturn(node: var NimNode) =
       replaceReturn(son)
     inc z
 
+proc isFutureVoid(node: NimNode): bool =
+  result = node.kind == nnkBracketExpr and
+           node[0].kind == nnkIdent and $node[0] == "Future" and
+           node[1].kind == nnkIdent and $node[1] == "void"
+
 proc generateJsasync(arg: NimNode): NimNode =
   assert arg.kind == nnkProcDef
   result = arg
+  var isVoid = false
   if arg.params[0].kind == nnkEmpty:
     result.params[0] = nnkBracketExpr.newTree(ident("Future"), ident("void"))
+    isVoid = true
+  elif isFutureVoid(arg.params[0]):
+    isVoid = true
   var code = result.body
   replaceReturn(code)
   result.body = nnkStmtList.newTree()
-  var q = quote:
+
+  var awaitFunction = quote:
     proc await[T](f: Future[T]): T {.importcpp: "(await #)".}
-    proc jsResolve[T](a: T): Future[T] {.importcpp: "#".}
-  result.body.add(q)
+  result.body.add(awaitFunction)
+
+  var resolve: NimNode
+  var jsResolveNode = ident("jsResolve")
+  if isVoid:
+    resolve = quote:
+      var `jsResolveNode` {.importcpp: "undefined".}: Future[void]
+  else:
+    resolve = quote:
+      proc jsResolve[T](a: T): Future[T] {.importcpp: "#".}
+  result.body.add(resolve)
+
   for child in code:
     result.body.add(child)
+
+  if isVoid:
+    var voidFix = quote:
+      return `jsResolveNode`
+    result.body.add(voidFix)
+
   result.pragma = quote:
     {.codegenDecl: "async function $2($3)".}