std/asyncjs allow transforming proc types (#21356)

* Add test case

* Implement JS async transform for nnkProcTy
This commit is contained in:
Jake Leahy 2023-02-14 18:14:19 +11:00 • committed by GitHub
commit cac49694c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View file

@ -100,10 +100,18 @@ proc isFutureVoid(node: NimNode): bool =
node[1].kind == nnkIdent and $node[1] == "void"
proc generateJsasync(arg: NimNode): NimNode =
if arg.kind notin {nnkProcDef, nnkLambda, nnkMethodDef, nnkDo}:
if arg.kind notin {nnkProcDef, nnkLambda, nnkMethodDef, nnkDo, nnkProcTy}:
error("Cannot transform this node kind into an async proc." &
" proc/method definition or lambda node expected.")
# Transform type X = proc (): something {.async.}
# into type X = proc (): Future[something]
if arg.kind == nnkProcTy:
result = arg
if arg[0][0].kind == nnkEmpty:
result[0][0] = quote do: Future[void]
return result
result = arg
var isVoid = false
let jsResolve = ident("jsResolve")

View file

@ -94,4 +94,9 @@ proc main() {.async.} =
doAssert "foobar: 7" in $reason.message
echo "done" # justified here to make sure we're running this, since it's inside `async`
block asyncPragmaInType:
type Handler = proc () {.async.}
proc foo() {.async.} = discard
var x: Handler = foo
discard main()