···3030 type t
31313232 val t : t Rpc.Types.def
3333-3433 val internal_error_of : exn -> t option
3534 end
3635···67666867module type RPC = sig
6968 type implementation
7070-7169 type 'a res
7272-7370 type ('a, 'b) comp
7474-7571 type _ fn
76727773 val implement : Interface.description -> implementation
7878-7974 val ( @-> ) : 'a Param.t -> 'b fn -> ('a -> 'b) fn
8080-8175 val returning : 'a Param.t -> 'b Error.t -> ('a, 'b) comp fn
8282-8376 val declare : string -> string list -> 'a fn -> 'a res
8484-8577 val declare_notification : string -> string list -> 'a fn -> 'a res
8678end
8779···8981 type 'a t
90829183 val return : 'a -> 'a t
9292-9384 val bind : 'a t -> ('a -> 'b t) -> 'b t
9494-9585 val fail : exn -> 'a t
9686end
97879888exception MarshalError of string
9999-10089exception UnknownMethod of string
101101-10290exception UnboundImplementation of string list
103103-10491exception NoDescription
1059210693let get_wire_name description name =
···141128module Make (M : MONAD) = struct
142129 module type RPCTRANSFORMER = sig
143130 type 'a box
144144-145131 type ('a, 'b) resultb = ('a, 'b) result box
146146-147132 type rpcfn = Rpc.call -> Rpc.response M.t
148133149134 val lift : ('a -> 'b M.t) -> 'a -> 'b box
150150-151135 val bind : 'a box -> ('a -> 'b M.t) -> 'b box
152152-153136 val return : 'a -> 'a box
154154-155137 val get : 'a box -> 'a M.t
156156-157138 val ( !@ ) : 'a box -> 'a M.t
158158-159139 val put : 'a M.t -> 'a box
160160-161140 val ( ~@ ) : 'a M.t -> 'a box
162141 end
163142164143 module T = struct
165144 type 'a box = { box : 'a M.t }
166166-167145 type ('a, 'b) resultb = ('a, 'b) result box
168168-169146 type rpcfn = Rpc.call -> Rpc.response M.t
170147171148 let lift f x = { box = f x }
172172-173149 let bind { box = x } f = { box = M.bind x f }
174174-175150 let return x = { box = M.return x }
176176-177151 let get { box = x } = x
178178-179152 let ( !@ ) = get
180180-181153 let put x = { box = x }
182182-183154 let ( ~@ ) = put
184155 end
185156186157 type client_implementation = unit
187187-188158 type server_implementation = (string, T.rpcfn option) Hashtbl.t
189159190160 module ErrM : sig
191161 val return : 'a -> ('a, 'b) T.resultb
192192-193162 val return_err : 'b -> ('a, 'b) T.resultb
194163195164 val checked_bind :
···205174 ('a, 'b) T.resultb -> ('a -> ('c, 'b) T.resultb) -> ('c, 'b) T.resultb
206175 end = struct
207176 let return x = T.put (M.return (Ok x))
208208-209177 let return_err e = T.put (M.return (Error e))
210178211179 let checked_bind x f f1 =
212180 T.bind x T.(function Ok x -> !@(f x) | Error x -> !@(f1 x))
213181214182 let bind x f = checked_bind x f return_err
215215-216183 let ( >>= ) x f = bind x f
217184 end
218185219186 module GenClient () = struct
220187 type implementation = client_implementation
221221-222188 type 'a res = T.rpcfn -> 'a
223223-224189 type ('a, 'b) comp = ('a, 'b) T.resultb
225190226191 type _ fn =
···228193 | Returning : ('a Param.t * 'b Error.t) -> ('a, 'b) comp fn
229194230195 let description = ref None
231231-232196 let strict = ref false
233233-234197 let make_strict () = strict := true
235198236199 let implement x =
···238201 ()
239202240203 let returning a err = Returning (a, err)
241241-242204 let ( @-> ) t f = Function (t, f)
243205244206 let declare_ is_notification name _ ty (rpc : T.rpcfn) =
···343305344306 module GenServer () = struct
345307 type implementation = server_implementation
346346-347308 type ('a, 'b) comp = ('a, 'b) T.resultb
348348-349309 type 'a res = 'a -> unit
350310351311 type _ fn =
···353313 | Returning : ('a Param.t * 'b Error.t) -> ('a, 'b) comp fn
354314355315 let funcs = Hashtbl.create 20
356356-357316 let description = ref None
358317359318 let implement x =
···361320 funcs
362321363322 let returning a b = Returning (a, b)
364364-365323 let ( @-> ) t f = Function (t, f)
366324367325 let rec has_named_args : type a. a fn -> bool = function
···423381 Hashtbl.add funcs wire_name (Some rpcfn)
424382425383 let declare_notification name a ty = declare_ true name a ty
426426-427384 let declare name a ty = declare_ false name a ty
428385 end
429386end
···432389 type 'a t = V of 'a | E of exn
433390434391 let return x = V x
435435-436392 let lift f x = match f x with y -> V y | exception e -> E e
437437-438393 let bind x (f : 'a -> 'b t) : 'b t = match x with V x -> f x | E e -> E e
439439-440394 let ( >>= ) = bind
441441-442395 let fail e = E e
443443-444396 let run = function V x -> x | E e -> raise e
445397end
446398···448400 type 'a t = T of 'a
449401450402 let return x = T x
451451-452403 let lift f x = T (f x)
453453-454404 let bind (T x) f = f x
455455-456405 let ( >>= ) = bind
457457-458406 let fail e = raise e
459459-460407 let run (T x) = x
461408end
462409···517464518465module Exn = struct
519466 type rpcfn = Rpc.call -> Rpc.response
520520-521467 type client_implementation = unit
522522-523468 type server_implementation = (string, rpcfn option) Hashtbl.t
524469525470 module GenClient (R : sig
···527472 end) =
528473 struct
529474 type implementation = client_implementation
530530-531475 type ('a, 'b) comp = 'a
532532-533476 type 'a res = 'a
534477535478 type _ fn =
···543486 ()
544487545488 let returning a err = Returning (a, err)
546546-547489 let ( @-> ) t f = Function (t, f)
548490549491 let declare_ is_notification name _ ty =
···595537 inner (None, []) ty
596538597539 let declare name a ty = declare_ false name a ty
598598-599540 let declare_notification name a ty = declare_ true name a ty
600541 end
601542···630571631572 module GenServer () = struct
632573 type implementation = server_implementation
633633-634574 type ('a, 'b) comp = 'a
635635-636575 type 'a res = 'a -> unit
637576638577 type _ fn =
···640579 | Returning : ('a Param.t * 'b Error.t) -> ('a, _) comp fn
641580642581 let funcs = Hashtbl.create 20
643643-644582 let description = ref None
645583646584 let implement x =
···648586 funcs
649587650588 let returning a b = Returning (a, b)
651651-652589 let ( @-> ) t f = Function (t, f)
653590654591 type boxed_error = BoxedError : 'a Error.t -> boxed_error
···718655 Hashtbl.add funcs wire_name (Some rpcfn)
719656720657 let declare name a ty = declare_ true name a ty
721721-722658 let declare_notification name a ty = declare_ false name a ty
723659 end
724660end
-21
idl/rpc.ml
···1616 *)
17171818let debug = ref false
1919-2019let set_debug x = debug := x
2121-2220let get_debug () = !debug
23212422type msg = [ `Msg of string ]
···74727573 (* A type definition has a name and description *)
7674 and 'a def = { name : string; description : string list; ty : 'a typ }
7777-7875 and boxed_def = BoxedDef : 'a def -> boxed_def
79768077 and ('a, 's) field = {
···8986 }
90879188 and 'a boxed_field = BoxedField : ('a, 's) field -> 's boxed_field
9292-9389 and field_getter = { field_get : 'a. string -> 'a typ -> ('a, msg) result }
94909591 and 'a structure = {
···109105 }
110106111107 and 'a boxed_tag = BoxedTag : ('a, 's) tag -> 's boxed_tag
112112-113108 and tag_getter = { tget : 'a. 'a typ -> ('a, msg) result }
114109115110 and 'a variant = {
···148143 { name = "string"; ty = Basic String; description = [ "String" ] }
149144150145 let char = { name = "char"; ty = Basic Char; description = [ "Char" ] }
151151-152146 let unit = { name = "unit"; ty = Unit; description = [ "Unit" ] }
153147154148 let default_types =
···165159end
166160167161exception Runtime_error of string * t
168168-169162exception Runtime_exception of string * string
170163171164let map_strings sep fn l = String.concat sep (List.map fn l)
···187180 | Null -> "N"
188181189182let rpc_of_t x = x
190190-191183let rpc_of_int64 i = Int i
192192-193184let rpc_of_int32 i = Int (Int64.of_int32 i)
194194-195185let rpc_of_int i = Int (Int64.of_int i)
196196-197186let rpc_of_bool b = Bool b
198198-199187let rpc_of_float f = Float f
200200-201188let rpc_of_string s = String s
202202-203189let rpc_of_dateTime s = DateTime s
204204-205190let rpc_of_base64 s = Base64 s
206206-207191let rpc_of_unit () = Null
208208-209192let rpc_of_char x = Int (Int64.of_int (Char.code x))
210193211194let int64_of_rpc = function
···262245263246module ResultUnmarshallers = struct
264247 let error_msg m = Error (`Msg m)
265265-266248 let ok x = Ok x
267249268250 let int64_of_rpc = function
···339321 | _, _ -> rpc
340322341323type callback = string list -> t -> unit
342342-343324type call = { name : string; params : t list; is_notification : bool }
344325345326let call name params = { name; params; is_notification = false }
346346-347327let notification name params = { name; params; is_notification = true }
348328349329let string_of_call call =
···360340(* is_notification is to be set as true only if the call was a notification *)
361341362342let success v = { success = true; contents = v; is_notification = false }
363363-364343let failure v = { success = false; contents = v; is_notification = false }
-48
idl/rpc.mli
···6666 | Abstract : 'a abstract -> 'a typ
67676868 and 'a def = { name : string; description : string list; ty : 'a typ }
6969-7069 and boxed_def = BoxedDef : 'a def -> boxed_def
71707271 and ('a, 's) field = {
···8079 }
81808281 and 'a boxed_field = BoxedField : ('a, 's) field -> 's boxed_field
8383-8482 and field_getter = { field_get : 'a. string -> 'a typ -> ('a, msg) result }
85838684 and 'a structure = {
···10199 }
102100103101 and 'a boxed_tag = BoxedTag : ('a, 's) tag -> 's boxed_tag
104104-105102 and tag_getter = { tget : 'a. 'a typ -> ('a, msg) result }
106103107104 and 'a variant = {
···120117 }
121118122119 val int : int def
123123-124120 val int32 : int32 def
125125-126121 val int64 : int64 def
127127-128122 val bool : bool def
129129-130123 val float : float def
131131-132124 val string : string def
133133-134125 val char : char def
135135-136126 val unit : unit def
137137-138127 val default_types : boxed_def list
139128end
140129141130(** {2 Basic constructors} *)
142131143132val rpc_of_int64 : int64 -> t
144144-145133val rpc_of_int32 : int32 -> t
146146-147134val rpc_of_int : int -> t
148148-149135val rpc_of_bool : bool -> t
150150-151136val rpc_of_float : float -> t
152152-153137val rpc_of_string : string -> t
154154-155138val rpc_of_dateTime : string -> t
156156-157139val rpc_of_base64 : string -> t
158158-159140val rpc_of_t : t -> t
160160-161141val rpc_of_unit : unit -> t
162162-163142val rpc_of_char : char -> t
164164-165143val int64_of_rpc : t -> int64
166166-167144val int32_of_rpc : t -> int32
168168-169145val int_of_rpc : t -> int
170170-171146val bool_of_rpc : t -> bool
172172-173147val float_of_rpc : t -> float
174174-175148val string_of_rpc : t -> string
176176-177149val dateTime_of_rpc : t -> string
178178-179150val base64_of_rpc : t -> string
180180-181151val t_of_rpc : t -> t
182182-183152val char_of_rpc : t -> char
184184-185153val unit_of_rpc : t -> unit
186154187155module ResultUnmarshallers : sig
188156 val int64_of_rpc : t -> (int64, msg) result
189189-190157 val int32_of_rpc : t -> (int32, msg) result
191191-192158 val int_of_rpc : t -> (int, msg) result
193193-194159 val bool_of_rpc : t -> (bool, msg) result
195195-196160 val float_of_rpc : t -> (float, msg) result
197197-198161 val string_of_rpc : t -> (string, msg) result
199199-200162 val dateTime_of_rpc : t -> (string, msg) result
201201-202163 val base64_of_rpc : t -> (string, msg) result
203203-204164 val t_of_rpc : t -> (t, msg) result
205205-206165 val unit_of_rpc : t -> (unit, msg) result
207207-208166 val char_of_rpc : t -> (char, msg) result
209167end
210168211169(** {2 Calls} *)
212170213171type callback = string list -> t -> unit
214214-215172type call = { name : string; params : t list; is_notification : bool }
216173217174val call : string -> t list -> call
218218-219175val notification : string -> t list -> call
220220-221176val string_of_call : call -> string
222177223178(** {2 Responses} *)
···225180type response = { success : bool; contents : t; is_notification : bool }
226181227182val string_of_response : response -> string
228228-229183val success : t -> response
230230-231184val failure : t -> response
232185233186(** {2 Run-time errors} *)
234187235188exception Runtime_error of string * t
236236-237189exception Runtime_exception of string * string
238190239191val set_debug : bool -> unit
-4
idl/rpcmarshal.ml
···44type err = [ `Msg of string ]
5566let tailrec_map f l = List.rev_map f l |> List.rev
77-87let ( >>| ) x f = match x with Ok x -> Ok (f x) | Error y -> Error y
99-108let ( >>= ) x f = match x with Ok x -> f x | Error y -> Error y
1111-129let return x = Ok x
1313-1410let ok x = Ok x
15111612let rec unmarshal : type a. a typ -> Rpc.t -> (a, err) result =
-5
idl/toplevel_api.ml
···3939 type t = err
40404141 let t = err
4242-4342 let internal_error_of e = Some (InternalError (Printexc.to_string e))
4443end)
4544···5958 }
60596160 let implementation = implement description
6262-6361 let unit_p = Param.mk Types.unit
6464-6562 let phrase_p = Param.mk Types.string
6666-6763 let exec_result_p = Param.mk exec_result
6868-6964 let completion_p = Param.mk completion_result
70657166 let cmas =
+13-15
idl/worker_rpc.ml
···99(** The assumption made in this module is that RPCs are answered in the order
1010 they are made. *)
11111212-type context =
1313- { worker : Worker.t
1414- ; timeout : int
1515- ; timeout_fn : unit -> unit
1616- ; waiting : ((Rpc.response, exn) Result.t Lwt_mvar.t * int) Queue.t
1717- }
1212+type context = {
1313+ worker : Worker.t;
1414+ timeout : int;
1515+ timeout_fn : unit -> unit;
1616+ waiting : ((Rpc.response, exn) Result.t Lwt_mvar.t * int) Queue.t;
1717+}
18181919exception Timeout
20202121let demux context msg =
2222 Lwt.async (fun () ->
2323 match Queue.take_opt context.waiting with
2424- | None ->
2525- Lwt.return ()
2424+ | None -> Lwt.return ()
2625 | Some (mv, outstanding_execution) ->
2727- Brr.G.stop_timer outstanding_execution;
2828- let msg : string = Message.Ev.data (Brr.Ev.as_type msg) in
2929- Lwt_mvar.put mv (Ok (Marshal.from_string msg 0)))
2626+ Brr.G.stop_timer outstanding_execution;
2727+ let msg : string = Message.Ev.data (Brr.Ev.as_type msg) in
2828+ Lwt_mvar.put mv (Ok (Marshal.from_string msg 0)))
30293130let start worker timeout timeout_fn =
3231 let context = { worker; timeout; timeout_fn; waiting = Queue.create () } in
···5049 Lwt_mvar.take mv >>= fun r ->
5150 match r with
5251 | Ok jv ->
5353- let response = jv in
5454- Lwt.return response
5555- | Error exn ->
5656- Lwt.fail exn
5252+ let response = jv in
5353+ Lwt.return response
5454+ | Error exn -> Lwt.fail exn
+2-2
idl/worker_rpc.mli
···55 The assumption made in this module is that RPCs are answered in the order
66 they are made. *)
7788-(** Represents the channel used to communicate with the worker *)
98type context
99+(** Represents the channel used to communicate with the worker *)
10101111+exception Timeout
1112(** When RPC calls take too long, the Lwt promise is set to failed state with
1213 this exception. *)
1313-exception Timeout
14141515val start : Brr_webworkers.Worker.t -> int -> (unit -> unit) -> context
1616(** [start worker timeout timeout_fn] initialises communications with a web