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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
open Js_of_ocaml
open Fmlib_std
module Value =
struct
type t = Js.Unsafe.any
let null: t =
Js.(Unsafe.inject null)
let undefined: t =
Js.(Unsafe.inject undefined)
let int (i: int): t =
Js.Unsafe.inject i
let float (v: float): t =
Js.Unsafe.inject v
let bool (b: bool): t =
Js.(Unsafe.coerce (bool b))
let string (s: string): t =
Js.(Unsafe.coerce (string s))
let _object (arr: (string * t) array): t =
Js.Unsafe.obj arr
let array (arr: t array): t =
Js.(Unsafe.coerce (array arr))
let function1 (f: t -> t): t =
Js.Unsafe.inject f
let function2 (f: t -> t -> t): t =
Js.Unsafe.inject f
let function3 (f: t -> t -> t -> t): t =
Js.Unsafe.inject f
end
module Decode =
struct
let str_boolean: Js.js_string Js.t =
Js.string "boolean"
let str_function: Js.js_string Js.t =
Js.string "function"
let str_string: Js.js_string Js.t =
Js.string "string"
let str_number: Js.js_string Js.t =
Js.string "number"
let is_function (v: Value.t): bool =
Js.(typeof v == str_function)
let is_boolean (v: Value.t): bool =
Js.(typeof v == str_boolean)
let is_string (v: Value.t): bool =
Js.(typeof v == str_string)
let is_number (v: Value.t): bool =
Js.(typeof v == str_number)
type 'a t = Value.t -> 'a option
let return (a: 'a): 'a t =
fun _ -> Some a
let fail: 'a t =
fun _ -> None
let (let* ) (m: 'a t) (f: 'a -> 'b t): 'b t =
fun v ->
Option.(
let* a = m v in
f a v
)
let (>>=) = (let* )
let (</>) (p: 'a t) (q: 'a t): 'a t =
fun v ->
match p v with
| None ->
q v
| Some _ as r ->
r
let map (f: 'a -> 'b) (m: 'a t): 'b t =
let* a = m in
return (f a)
let null (a: 'a): 'a t =
fun obj ->
if obj == Value.null then
Some a
else
None
let undefined (a: 'a): 'a t =
fun obj ->
if obj == Value.undefined then
Some a
else
None
let float: float t =
fun v ->
if is_number v then
Some (Js.(Unsafe.coerce v)##valueOf ())
else
None
let int: int t =
let* v = float in
let open Float in
let i = to_int v in
if equal v (of_int i) then
return i
else
fail
let bool: bool t =
fun v ->
if is_boolean v then
Some Js.(Unsafe.coerce v |> to_bool)
else
None
let string: string t =
fun v ->
if is_string v then
Some Js.(Unsafe.coerce v |> to_string)
else
None
let _function: (Value.t array -> Value.t) t =
fun v ->
if is_function v then
Some (fun args -> Js.Unsafe.fun_call v args)
else
None
let _method: (Value.t -> Value.t array -> Value.t) t =
fun v ->
if is_function v then
Some (fun obj args -> Js.Unsafe.call v obj args)
else
None
let field (name: string) (decode: 'a t): 'a t =
fun obj ->
let open Js in
Option.(
let* v = Optdef.to_option (Unsafe.get obj (string name)) in
decode v
)
let array (decode: 'a t): 'a array t =
fun obj ->
let open Js in
if Unsafe.global##._Array##isArray obj then
let js_arr = (Unsafe.coerce obj) in
let len = js_arr##.length in
let rec i lst =
if i = len then
Some (Array.of_list (List.rev lst))
else
let open Option in
let* e = array_get js_arr i |> Optdef.to_option in
let* a = decode e in
extract (i + 1) (a :: lst)
in
extract 0 []
else
None
let option (decode: 'a t): 'a option t =
map Option.return decode
</>
null None
end
module Main =
struct
let raise_js (message: string): 'a =
let js_msg = Js.string message in
Js.(
new%js
error_constr
js_msg
|> Js_error.of_error
|> Js_error.raise_
)
let log_string (str: string): unit =
Js.(Unsafe.global##.console##log (string str))
let log_value (value: Value.t): unit =
Js.(Unsafe.global##.console##log value)
let export (obj: (string * Value.t) array): unit =
Js.export_all (Value._object obj)
let make_global (name: string) (v: Value.t): unit =
Js.(Unsafe.(set global (Js.string name) v))
let get_global (name: string): Value.t option =
let open Js in
Unsafe.(get global (Js.string name)) |> Opt.to_option
let new_global (cname: string) (args: Value.t array): Value.t =
match get_global cname with
| None ->
assert false
| Some constr ->
Js.Unsafe.new_obj constr args
let decode_callback (cb: Value.t) (err: string): Value.t -> unit =
match Decode._function cb with
| None ->
raise_js err
| Some cb ->
fun v -> cb [|v|] |> ignore
let decode_data
(dec: 'a Decode.t) (data: Value.t) (err: string)
: 'a
=
match dec data with
| None ->
raise_js err
| Some state ->
state
type ('state,'msg) node_function =
'state -> (Value.t -> unit) -> 'msg -> unit
let node_module
(decode: 'state Decode.t)
(msg_decode: 'msg Decode.t)
(node_function: ('state, 'msg) node_function)
: unit
=
let js_function data callback =
let callback =
decode_callback callback
"provided callback is not a function"
and data =
decode_data decode data
"cannot decode input data"
in
let f =
node_function data callback
in
Value.function1
(fun msg ->
match msg_decode msg with
| None ->
log_string "cannot decode message";
log_value msg;
Value.undefined
| Some msg ->
f msg;
Value.undefined)
in
export [| "init", Value.function2 js_function |]
type ('state, 'msg) browser_function =
'state -> string option -> (Value.t -> unit) -> 'msg -> unit
let browser_application
(app_name: string)
(state_decode: 'state Decode.t)
(msg_decode: 'msg Decode.t)
(browser_function: ('state, 'msg) browser_function)
: unit
=
let js_function state element callback =
let callback =
decode_callback callback
"provided callback is not a function"
and state =
decode_data state_decode state
"cannot decode state"
and element =
decode_data Decode.(option string) element
"cannot decode a nullable element id"
in
let f =
browser_function state element callback
in
Value.function1
(fun msg ->
match msg_decode msg with
| None ->
log_string "cannot decode message";
log_value msg;
Value.undefined
| Some msg ->
f msg;
Value.undefined)
in
make_global
app_name
(Value.function3 js_function)
end