- Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathasyncjs.nim
269 lines (231 loc) · 8.73 KB
/
asyncjs.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#
#
# Nim's Runtime Library
# (c) Copyright 2017 Nim Authors
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
## This module implements types and macros for writing asynchronous code
## for the JS backend. It provides tools for interaction with JavaScript async API-s
## and libraries, writing async procedures in Nim and converting callback-based code
## to promises.
##
## A Nim procedure is asynchronous when it includes the `{.async.}` pragma. It
## should always have a `Future[T]` return type or not have a return type at all.
## A `Future[void]` return type is assumed by default.
##
## This is roughly equivalent to the `async` keyword in JavaScript code.
##
## ```nim
## proc loadGame(name: string): Future[Game] {.async.} =
## # code
## ```
##
## should be equivalent to
##
## ```javascript
## async function loadGame(name) {
## // code
## }
## ```
##
## A call to an asynchronous procedure usually needs `await` to wait for
## the completion of the `Future`.
##
## ```nim
## var game = await loadGame(name)
## ```
##
## Often, you might work with callback-based API-s. You can wrap them with
## asynchronous procedures using promises and `newPromise`:
##
## ```nim
## proc loadGame(name: string): Future[Game] =
## var promise = newPromise() do (resolve: proc(response: Game)):
## cbBasedLoadGame(name) do (game: Game):
## resolve(game)
## return promise
## ```
##
## Forward definitions work properly, you just need to always add the `{.async.}` pragma:
##
## ```nim
## proc loadGame(name: string): Future[Game] {.async.}
## ```
##
## JavaScript compatibility
## ========================
##
## Nim currently generates `async/await` JavaScript code which is supported in modern
## EcmaScript and most modern versions of browsers, Node.js and Electron.
## If you need to use this module with older versions of JavaScript, you can
## use a tool that backports the resulting JavaScript code, as babel.
# xxx code: javascript above gives `LanguageXNotSupported` warning.
whennotdefined(js) andnotdefined(nimsuggest):
{.fatal: "Module asyncjs is designed to be used with the JavaScript backend.".}
import std/jsffi
import std/macros
import std/private/since
type
Future*[T] =refobject
future*: T
## Wraps the return type of an asynchronous procedure.
PromiseJs* {.importjs: "Promise".} =refobject
## A JavaScript Promise.
procreplaceReturn(node: varNimNode) =
var z =0
for s in node:
var son = node[z]
let jsResolve =ident("jsResolve")
if son.kind == nnkReturnStmt:
let value =if son[0].kind != nnkEmpty: nnkCall.newTree(jsResolve, son[0]) else: jsResolve
node[z] = nnkReturnStmt.newTree(value)
elif son.kind == nnkAsgn and son[0].kind == nnkIdent and$son[0] =="result":
node[z] = nnkAsgn.newTree(son[0], nnkCall.newTree(jsResolve, son[1]))
elif son.kind inRoutineNodes:
discard
else:
replaceReturn(son)
inc z
procisFutureVoid(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"
procgenerateJsasync(arg: NimNode): NimNode=
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] =quotedo: Future[void]
returnresult
result= arg
var isVoid =false
let jsResolve =ident("jsResolve")
if arg.params[0].kind == nnkEmpty:
result.params[0] = nnkBracketExpr.newTree(ident("Future"), ident("void"))
isVoid =true
elifisFutureVoid(arg.params[0]):
isVoid =true
var code =result.body
replaceReturn(code)
result.body = nnkStmtList.newTree()
iflen(code) >0:
var awaitFunction = quote:
procawait[T](f: Future[T]): T {.importjs: "(await #)", used.}
result.body.add(awaitFunction)
var resolve: NimNode
if isVoid:
resolve = quote:
var `jsResolve` {.importjs: "undefined".}: Future[void]
else:
resolve = quote:
procjsResolve[T](a: T): Future[T] {.importjs: "#", used.}
procjsResolve[T](a: Future[T]): Future[T] {.importjs: "#", used.}
result.body.add(resolve)
else:
result.body =newEmptyNode()
for child in code:
result.body.add(child)
iflen(code) >0and isVoid:
var voidFix = quote:
return `jsResolve`
result.body.add(voidFix)
let asyncPragma = quote:
{.codegenDecl: "async function $2($3)".}
result.addPragma(asyncPragma[0])
macroasync*(arg: untyped): untyped=
## Macro which converts normal procedures into
## javascript-compatible async procedures.
if arg.kind == nnkStmtList:
result=newStmtList()
for oneProc in arg:
result.addgenerateJsasync(oneProc)
else:
result=generateJsasync(arg)
procnewPromise*[T](handler: proc(resolve: proc(response: T))): Future[T] {.importjs: "(new Promise(#))".}
## A helper for wrapping callback-based functions
## into promises and async procedures.
procnewPromise*(handler: proc(resolve: proc())): Future[void] {.importjs: "(new Promise(#))".}
## A helper for wrapping callback-based functions
## into promises and async procedures.
templatemaybeFuture(T): untyped=
# avoids `Future[Future[T]]`
when T isFuture: T
else: Future[T]
since (1, 5, 1):
#[
TODO:
* map `Promise.all()`
* proc toString*(a: Error): cstring {.importjs: "#.toString()".}
Note:
We probably can't have a `waitFor` in js in browser (single threaded), but maybe it would be possible
in in nodejs, see https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options
and https://stackoverflow.com/questions/61377358/javascript-wait-for-async-call-to-finish-before-returning-from-function-witho
]#
typeError* {.importjs: "Error".} =refobjectofJsRoot
## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
message*: cstring
name*: cstring
typeOnReject*=proc(reason: Error)
procthen*[T](future: Future[T], onSuccess: proc, onReject: OnReject=nil): auto=
## See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
## Returns a `Future` from the return type of `onSuccess(T.default)`.
runnableExamples("-r:off"):
from std/sugar import `=>`
procfn(n: int): Future[int] {.async.} =
if n >=7: raisenewException(ValueError, "foobar: "&$n)
else: result= n *2
procasyncFact(n: int): Future[int] {.async.} =
if n >0: result= n *awaitasyncFact(n-1)
else: result=1
procmain() {.async.} =
block: # then
assertasyncFact(3).await ==3*2
assertasyncFact(3).then(asyncFact).await ==6*5*4*3*2
let x1 =awaitfn(3)
assert x1 ==3*2
let x2 =awaitfn(4)
.then((a: int) => a.float)
.then((a: float) =>$a)
assert x2 =="8.0"
block: # then with `onReject` callback
var witness =1
awaitfn(6).then((a: int) => (witness =2), (r: Error) => (witness =3))
assert witness ==2
awaitfn(7).then((a: int) => (witness =2), (r: Error) => (witness =3))
assert witness ==3
templateimpl(call): untyped=
# see D20210421T014713
whentypeof(block: call) isvoid:
var ret: Future[void]
else:
var ret =default(maybeFuture(typeof(call)))
typeof(ret)
when T isvoid:
type A =impl(onSuccess())
else:
type A =impl(onSuccess(default(T)))
var ret: A
{.emit: "`ret` = `future`.then(`onSuccess`, `onReject`);".}
return ret
proccatch*[T](future: Future[T], onReject: OnReject): Future[void] =
## See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
runnableExamples("-r:off"):
from std/sugar import `=>`
from std/strutils import contains
procfn(n: int): Future[int] {.async.} =
if n >=7: raisenewException(ValueError, "foobar: "&$n)
else: result= n *2
procmain() {.async.} =
var reason: Error
awaitfn(6).catch((r: Error) => (reason = r)) # note: `()` are needed, `=> reason = r` would not work
assert reason ==nil
awaitfn(7).catch((r: Error) => (reason = r))
assert reason !=nil
assert"foobar: 7"in$reason.message
discardmain()
{.emit: "`result` = `future`.catch(`onReject`);".}