about summary refs log tree commit diff stats
path: root/src/io/promise.nim
blob: 55dcfbf01374b3711158758c001634f57d330f7c (plain) (blame)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import std/tables

import monoucha/quickjs
import monoucha/javascript
import monoucha/jsutils
import monoucha/jsopaque
import monoucha/tojs
import types/opt

type
  PromiseState = enum
    psPending, psFulfilled, psRejected

  EmptyPromise* = ref object of RootObj
    cb: (proc())
    next: EmptyPromise
    opaque: pointer
    state*: PromiseState

  Promise*[T] = ref object of EmptyPromise
    res: T
    get: GetValueProc[T]

  GetValueProc[T] = (proc(opaque: pointer; res: var T))

  PromiseMap* = object
    tab: Table[int, EmptyPromise]
    opaque*: pointer

proc newPromise*[T](): Promise[T] =
  return Promise[T]()

proc newPromiseMap*(opaque: pointer): PromiseMap =
  return PromiseMap(
    opaque: opaque
  )

proc addPromise*[T](map: var PromiseMap; id: int; get: GetValueProc[T]):
    Promise[T] =
  let promise = Promise[T](get: get, opaque: map.opaque)
  map.tab[id] = promise
  return promise

proc addEmptyPromise*(map: var PromiseMap; id: int): EmptyPromise =
  let promise = EmptyPromise(opaque: map.opaque)
  map.tab[id] = promise
  return promise

proc resolve*(promise: EmptyPromise) =
  var promise = promise
  while true:
    if promise.cb != nil:
      promise.cb()
    promise.cb = nil
    promise.state = psFulfilled
    promise = promise.next
    if promise == nil:
      break
    promise.next = nil

proc resolve*[T](promise: Promise[T]; res: T) =
  if promise.get != nil:
    promise.get(promise.opaque, promise.res)
    promise.get = nil
  promise.res = res
  promise.resolve()

proc resolve*(map: var PromiseMap; promiseid: int) =
  var promise: EmptyPromise
  if map.tab.pop(promiseid, promise):
    promise.resolve()

proc newResolvedPromise*(): EmptyPromise =
  let res = EmptyPromise()
  res.resolve()
  return res

func empty*(map: PromiseMap): bool =
  map.tab.len == 0

proc then*(promise: EmptyPromise; cb: (proc())): EmptyPromise {.discardable.} =
  promise.cb = cb
  promise.next = EmptyPromise()
  if promise.state == psFulfilled:
    promise.resolve()
  return promise.next

proc then*(promise: EmptyPromise; cb: (proc(): EmptyPromise)): EmptyPromise
    {.discardable.} =
  let next = EmptyPromise()
  promise.then(proc() =
    var p2 = cb()
    if p2 != nil:
      p2.then(proc() =
        next.resolve())
    else:
      next.resolve())
  return next

proc then*[T](promise: Promise[T]; cb: (proc(x: T))): EmptyPromise
    {.discardable.} =
  return promise.then(proc() =
    if promise.get != nil:
      promise.get(promise.opaque, promise.res)
      promise.get = nil
    cb(promise.res))

proc then*[T](promise: EmptyPromise; cb: (proc(): Promise[T])): Promise[T]
    {.discardable.} =
  let next = Promise[T]()
  promise.then(proc() =
    var p2 = cb()
    if p2 != nil:
      p2.then(proc(x: T) =
        next.res = x
        next.resolve())
    else:
      next.resolve())
  return next

proc then*[T](promise: Promise[T]; cb: (proc(x: T): EmptyPromise)): EmptyPromise
    {.discardable.} =
  let next = EmptyPromise()
  promise.then(proc(x: T) =
    let p2 = cb(x)
    if p2 != nil:
      p2.then(proc() =
        next.resolve())
    else:
      next.resolve())
  return next

proc then*[T](promise: EmptyPromise; cb: (proc(): T)): Promise[T]
    {.discardable.} =
  let next = Promise[T]()
  promise.then(proc() =
    next.res = cb()
    next.resolve())
  return next

proc then*[T, U](promise: Promise[T]; cb: (proc(x: T): U)): Promise[U]
    {.discardable.} =
  let next = Promise[U]()
  promise.then(proc(x: T) =
    next.res = cb(x)
    next.resolve())
  return next

proc then*[T, U](promise: Promise[T]; cb: (proc(x: T): Promise[U])): Promise[U]
    {.discardable.} =
  let next = Promise[U]()
  promise.then(proc(x: T) =
    let p2 = cb(x)
    if p2 != nil:
      p2.then(proc(y: U) =
        next.res = y
        next.resolve())
    else:
      next.resolve())
  return next

proc then*[T, U](promise: Promise[T]; cb: (proc(x: T): Opt[Promise[U]])):
    Promise[Opt[U]] {.discardable.} =
  let next = Promise[Opt[U]]()
  promise.then(proc(x: T) =
    let p2 = cb(x)
    if p2.isSome:
      p2.get.then(proc(y: U) =
        next.res = opt(y)
        next.resolve())
    else:
      next.resolve())
  return next

proc all*(promises: seq[EmptyPromise]): EmptyPromise =
  let res = EmptyPromise()
  var i = 0
  for promise in promises:
    promise.then(proc() =
      inc i
      if i == promises.len:
        res.resolve())
  if promises.len == 0:
    res.resolve()
  return res

# * Promise is converted to a JS promise which will be resolved when the Nim
#   promise is resolved.

proc promiseThenCallback(ctx: JSContext; this_val: JSValue; argc: cint;
    argv: ptr UncheckedArray[JSValue]; magic: cint;
    func_data: ptr UncheckedArray[JSValue]): JSValue {.cdecl.} =
  let fun = func_data[0]
  let op = JS_GetOpaque(fun, JS_GetClassID(fun))
  if op != nil:
    let p = cast[EmptyPromise](op)
    p.resolve()
    GC_unref(p)
    JS_SetOpaque(fun, nil)
  return JS_UNDEFINED

proc fromJS*(ctx: JSContext; val: JSValue; res: var EmptyPromise): Opt[void] =
  if not JS_IsObject(val):
    JS_ThrowTypeError(ctx, "value is not an object")
    return err()
  res = EmptyPromise()
  GC_ref(res)
  let tmp = JS_NewObject(ctx)
  JS_SetOpaque(tmp, cast[pointer](res))
  let fun = JS_NewCFunctionData(ctx, promiseThenCallback, 0, 0, 1,
    tmp.toJSValueArray())
  JS_FreeValue(ctx, tmp)
  let val = JS_Invoke(ctx, val, ctx.getOpaque().strRefs[jstThen], 1,
    fun.toJSValueArray())
  JS_FreeValue(ctx, fun)
  if JS_IsException(val):
    JS_FreeValue(ctx, val)
    GC_unref(res)
    res = nil
    return err()
  JS_FreeValue(ctx, val)
  return ok()

proc toJS*(ctx: JSContext; promise: EmptyPromise): JSValue =
  if promise == nil:
    return JS_NULL
  var resolvingFuncs: array[2, JSValue]
  let jsPromise = JS_NewPromiseCapability(ctx, resolvingFuncs.toJSValueArray())
  if JS_IsException(jsPromise):
    return JS_EXCEPTION
  promise.then(proc() =
    let res = JS_Call(ctx, resolvingFuncs[0], JS_UNDEFINED, 0, nil)
    JS_FreeValue(ctx, res)
    JS_FreeValue(ctx, resolvingFuncs[0])
    JS_FreeValue(ctx, resolvingFuncs[1]))
  return jsPromise

proc toJS*[T](ctx: JSContext; promise: Promise[T]): JSValue =
  if promise == nil:
    return JS_NULL
  var resolvingFuncs: array[2, JSValue]
  let jsPromise = JS_NewPromiseCapability(ctx, resolvingFuncs.toJSValueArray())
  if JS_IsException(jsPromise):
    return JS_EXCEPTION
  promise.then(proc(x: T) =
    let x = toJS(ctx, x)
    let res = JS_Call(ctx, resolvingFuncs[0], JS_UNDEFINED, 1,
      x.toJSValueArray())
    JS_FreeValue(ctx, res)
    JS_FreeValue(ctx, x)
    JS_FreeValue(ctx, resolvingFuncs[0])
    JS_FreeValue(ctx, resolvingFuncs[1]))
  return jsPromise

proc toJS*[T, E](ctx: JSContext; promise: Promise[Result[T, E]]): JSValue =
  if promise == nil:
    return JS_NULL
  var resolvingFuncs: array[2, JSValue]
  let jsPromise = JS_NewPromiseCapability(ctx, resolvingFuncs.toJSValueArray())
  if JS_IsException(jsPromise):
    return JS_EXCEPTION
  promise.then(proc(x: Result[T, E]) =
    if x.isSome:
      let x = when T is void:
        JS_UNDEFINED
      else:
        toJS(ctx, x.get)
      let res = JS_Call(ctx, resolvingFuncs[0], JS_UNDEFINED, 1,
        x.toJSValueArray())
      JS_FreeValue(ctx, res)
      JS_FreeValue(ctx, x)
    else: # err
      let x = when E is void:
        JS_UNDEFINED
      else:
        toJS(ctx, x.error)
      let res = JS_Call(ctx, resolvingFuncs[1], JS_UNDEFINED, 1,
        x.toJSValueArray())
      JS_FreeValue(ctx, res)
      JS_FreeValue(ctx, x)
    JS_FreeValue(ctx, resolvingFuncs[0])
    JS_FreeValue(ctx, resolvingFuncs[1]))
  return jsPromise