···11let logfn = ref (fun (_ : string) -> ())
22+23module Param = struct
33- type 'a t =
44- { name : string option
55- ; description : string list
66- ; typedef : 'a Rpc.Types.def
77- ; version : Rpc.Version.t option
88- }
44+ type 'a t = {
55+ name : string option;
66+ description : string list;
77+ typedef : 'a Rpc.Types.def;
88+ version : Rpc.Version.t option;
99+ }
9101011 type boxed = Boxed : 'a t -> boxed
1112···1920end
20212122module Error = struct
2222- type 'a t =
2323- { def : 'a Rpc.Types.def
2424- ; raiser : 'a -> exn
2525- ; matcher : exn -> 'a option
2626- }
2323+ type 'a t = {
2424+ def : 'a Rpc.Types.def;
2525+ raiser : 'a -> exn;
2626+ matcher : exn -> 'a option;
2727+ }
27282829 module type ERROR = sig
2930 type t
30313132 val t : t Rpc.Types.def
3333+3234 val internal_error_of : exn -> t option
3335 end
3436···3840 let () =
3941 let printer = function
4042 | Exn x ->
4141- Some
4242- (Printf.sprintf
4343- "IDL Error: %s"
4444- (Rpcmarshal.marshal T.t.Rpc.Types.ty x |> Rpc.to_string))
4343+ Some
4444+ (Printf.sprintf "IDL Error: %s"
4545+ (Rpcmarshal.marshal T.t.Rpc.Types.ty x |> Rpc.to_string))
4546 | _ -> None
4647 in
4748 Printexc.register_printer printer
48494949-5050 let error =
5151- { def = T.t
5252- ; raiser =
5353- (function
5454- | e -> Exn e)
5555- ; matcher =
5656- (function
5757- | Exn e -> Some e
5858- | e -> T.internal_error_of e)
5151+ {
5252+ def = T.t;
5353+ raiser = (function e -> Exn e);
5454+ matcher = (function Exn e -> Some e | e -> T.internal_error_of e);
5955 }
6056 end
6157end
62586359module Interface = struct
6464- type description =
6565- { name : string
6666- ; namespace : string option
6767- ; description : string list
6868- ; version : Rpc.Version.t
6969- }
6060+ type description = {
6161+ name : string;
6262+ namespace : string option;
6363+ description : string list;
6464+ version : Rpc.Version.t;
6565+ }
7066end
71677268module type RPC = sig
7369 type implementation
7070+7471 type 'a res
7272+7573 type ('a, 'b) comp
7474+7675 type _ fn
77767877 val implement : Interface.description -> implementation
7878+7979 val ( @-> ) : 'a Param.t -> 'b fn -> ('a -> 'b) fn
8080+8081 val returning : 'a Param.t -> 'b Error.t -> ('a, 'b) comp fn
8282+8183 val declare : string -> string list -> 'a fn -> 'a res
8484+8285 val declare_notification : string -> string list -> 'a fn -> 'a res
8386end
8487···8689 type 'a t
87908891 val return : 'a -> 'a t
9292+8993 val bind : 'a t -> ('a -> 'b t) -> 'b t
9494+9095 val fail : exn -> 'a t
9196end
92979398exception MarshalError of string
9999+94100exception UnknownMethod of string
101101+95102exception UnboundImplementation of string list
103103+96104exception NoDescription
9710598106let get_wire_name description name =
99107 match description with
100108 | None -> name
101101- | Some d ->
102102- (match d.Interface.namespace with
103103- | Some ns -> Printf.sprintf "%s.%s" ns name
104104- | None -> name)
105105-109109+ | Some d -> (
110110+ match d.Interface.namespace with
111111+ | Some ns -> Printf.sprintf "%s.%s" ns name
112112+ | None -> name)
106113107114let get_arg call has_named name is_opt =
108108- match has_named, name, call.Rpc.params with
109109- | true, Some n, Rpc.Dict named :: unnamed ->
110110- (match List.partition (fun (x, _) -> x = n) named with
111111- | (_, arg) :: dups, others when is_opt ->
112112- Ok
113113- (Rpc.Enum [ arg ], { call with Rpc.params = Rpc.Dict (dups @ others) :: unnamed })
114114- | (_, arg) :: dups, others ->
115115- Ok (arg, { call with Rpc.params = Rpc.Dict (dups @ others) :: unnamed })
116116- | [], _others when is_opt -> Ok (Rpc.Enum [], call)
117117- | _, _ -> Error (`Msg (Printf.sprintf "Expecting named argument '%s'" n)))
118118- | true, None, Rpc.Dict named :: unnamed ->
119119- (match unnamed with
120120- | head :: tail -> Ok (head, { call with Rpc.params = Rpc.Dict named :: tail })
121121- | _ -> Error (`Msg "Incorrect number of arguments"))
115115+ match (has_named, name, call.Rpc.params) with
116116+ | true, Some n, Rpc.Dict named :: unnamed -> (
117117+ match List.partition (fun (x, _) -> x = n) named with
118118+ | (_, arg) :: dups, others when is_opt ->
119119+ Ok
120120+ ( Rpc.Enum [ arg ],
121121+ { call with Rpc.params = Rpc.Dict (dups @ others) :: unnamed } )
122122+ | (_, arg) :: dups, others ->
123123+ Ok
124124+ (arg, { call with Rpc.params = Rpc.Dict (dups @ others) :: unnamed })
125125+ | [], _others when is_opt -> Ok (Rpc.Enum [], call)
126126+ | _, _ -> Error (`Msg (Printf.sprintf "Expecting named argument '%s'" n)))
127127+ | true, None, Rpc.Dict named :: unnamed -> (
128128+ match unnamed with
129129+ | head :: tail ->
130130+ Ok (head, { call with Rpc.params = Rpc.Dict named :: tail })
131131+ | _ -> Error (`Msg "Incorrect number of arguments"))
122132 | true, _, _ ->
123123- Error
124124- (`Msg
125125- "Marshalling error: Expecting dict as first argument when named parameters exist")
133133+ Error
134134+ (`Msg
135135+ "Marshalling error: Expecting dict as first argument when named \
136136+ parameters exist")
126137 | false, None, head :: tail -> Ok (head, { call with Rpc.params = tail })
127138 | false, None, [] -> Error (`Msg "Incorrect number of arguments")
128139 | false, Some _, _ -> failwith "Can't happen by construction"
129129-130140131141module Make (M : MONAD) = struct
132142 module type RPCTRANSFORMER = sig
133143 type 'a box
144144+134145 type ('a, 'b) resultb = ('a, 'b) result box
146146+135147 type rpcfn = Rpc.call -> Rpc.response M.t
136148137149 val lift : ('a -> 'b M.t) -> 'a -> 'b box
150150+138151 val bind : 'a box -> ('a -> 'b M.t) -> 'b box
152152+139153 val return : 'a -> 'a box
154154+140155 val get : 'a box -> 'a M.t
156156+141157 val ( !@ ) : 'a box -> 'a M.t
158158+142159 val put : 'a M.t -> 'a box
160160+143161 val ( ~@ ) : 'a M.t -> 'a box
144162 end
145163146164 module T = struct
147165 type 'a box = { box : 'a M.t }
166166+148167 type ('a, 'b) resultb = ('a, 'b) result box
168168+149169 type rpcfn = Rpc.call -> Rpc.response M.t
150170151171 let lift f x = { box = f x }
172172+152173 let bind { box = x } f = { box = M.bind x f }
174174+153175 let return x = { box = M.return x }
176176+154177 let get { box = x } = x
178178+155179 let ( !@ ) = get
180180+156181 let put x = { box = x }
182182+157183 let ( ~@ ) = put
158184 end
159185160186 type client_implementation = unit
187187+161188 type server_implementation = (string, T.rpcfn option) Hashtbl.t
162189163190 module ErrM : sig
164191 val return : 'a -> ('a, 'b) T.resultb
192192+165193 val return_err : 'b -> ('a, 'b) T.resultb
166194167167- val checked_bind
168168- : ('a, 'b) T.resultb
169169- -> ('a -> ('c, 'd) T.resultb)
170170- -> ('b -> ('c, 'd) T.resultb)
171171- -> ('c, 'd) T.resultb
195195+ val checked_bind :
196196+ ('a, 'b) T.resultb ->
197197+ ('a -> ('c, 'd) T.resultb) ->
198198+ ('b -> ('c, 'd) T.resultb) ->
199199+ ('c, 'd) T.resultb
172200173173- val bind : ('a, 'b) T.resultb -> ('a -> ('c, 'b) T.resultb) -> ('c, 'b) T.resultb
174174- val ( >>= ) : ('a, 'b) T.resultb -> ('a -> ('c, 'b) T.resultb) -> ('c, 'b) T.resultb
201201+ val bind :
202202+ ('a, 'b) T.resultb -> ('a -> ('c, 'b) T.resultb) -> ('c, 'b) T.resultb
203203+204204+ val ( >>= ) :
205205+ ('a, 'b) T.resultb -> ('a -> ('c, 'b) T.resultb) -> ('c, 'b) T.resultb
175206 end = struct
176207 let return x = T.put (M.return (Ok x))
208208+177209 let return_err e = T.put (M.return (Error e))
178210179211 let checked_bind x f f1 =
180180- T.bind
181181- x
182182- T.(
183183- function
184184- | Ok x -> !@(f x)
185185- | Error x -> !@(f1 x))
186186-212212+ T.bind x T.(function Ok x -> !@(f x) | Error x -> !@(f1 x))
187213188214 let bind x f = checked_bind x f return_err
215215+189216 let ( >>= ) x f = bind x f
190217 end
191218192219 module GenClient () = struct
193220 type implementation = client_implementation
221221+194222 type 'a res = T.rpcfn -> 'a
223223+195224 type ('a, 'b) comp = ('a, 'b) T.resultb
196225197226 type _ fn =
···199228 | Returning : ('a Param.t * 'b Error.t) -> ('a, 'b) comp fn
200229201230 let description = ref None
231231+202232 let strict = ref false
233233+203234 let make_strict () = strict := true
204235205236 let implement x =
206237 description := Some x;
207238 ()
208239209209-210240 let returning a err = Returning (a, err)
241241+211242 let ( @-> ) t f = Function (t, f)
212243213244 let declare_ is_notification name _ ty (rpc : T.rpcfn) =
214214- let rec inner : type b. (string * Rpc.t) list option * Rpc.t list -> b fn -> b =
245245+ let rec inner :
246246+ type b. (string * Rpc.t) list option * Rpc.t list -> b fn -> b =
215247 fun (named, unnamed) -> function
216216- | Function (t, f) ->
217217- let cur_named =
218218- match named with
219219- | Some l -> l
220220- | None -> []
221221- in
222222- fun v ->
223223- (match t.Param.name with
224224- | Some n ->
225225- (match t.Param.typedef.Rpc.Types.ty, v with
226226- | Rpc.Types.Option ty, Some v' ->
227227- let marshalled = Rpcmarshal.marshal ty v' in
228228- inner (Some ((n, marshalled) :: cur_named), unnamed) f
229229- | Rpc.Types.Option _ty, None -> inner (Some cur_named, unnamed) f
230230- | ty, v ->
231231- let marshalled = Rpcmarshal.marshal ty v in
232232- inner (Some ((n, marshalled) :: cur_named), unnamed) f)
233233- | None ->
234234- let marshalled = Rpcmarshal.marshal t.Param.typedef.Rpc.Types.ty v in
235235- inner (named, marshalled :: unnamed) f)
248248+ | Function (t, f) -> (
249249+ let cur_named = match named with Some l -> l | None -> [] in
250250+ fun v ->
251251+ match t.Param.name with
252252+ | Some n -> (
253253+ match (t.Param.typedef.Rpc.Types.ty, v) with
254254+ | Rpc.Types.Option ty, Some v' ->
255255+ let marshalled = Rpcmarshal.marshal ty v' in
256256+ inner (Some ((n, marshalled) :: cur_named), unnamed) f
257257+ | Rpc.Types.Option _ty, None ->
258258+ inner (Some cur_named, unnamed) f
259259+ | ty, v ->
260260+ let marshalled = Rpcmarshal.marshal ty v in
261261+ inner (Some ((n, marshalled) :: cur_named), unnamed) f)
262262+ | None ->
263263+ let marshalled =
264264+ Rpcmarshal.marshal t.Param.typedef.Rpc.Types.ty v
265265+ in
266266+ inner (named, marshalled :: unnamed) f)
236267 | Returning (t, e) ->
237237- let wire_name = get_wire_name !description name in
238238- let args =
239239- match named with
240240- | None -> List.rev unnamed
241241- | Some l -> Rpc.Dict l :: List.rev unnamed
242242- in
243243- let call' = Rpc.call wire_name args in
244244- let call = { call' with is_notification } in
245245- let rpc = T.put (rpc call) in
246246- let res =
247247- T.bind rpc (fun r ->
248248- if r.Rpc.success
249249- then (
250250- match
251251- Rpcmarshal.unmarshal t.Param.typedef.Rpc.Types.ty r.Rpc.contents
252252- with
253253- | Ok x -> M.return (Ok x)
254254- | Error (`Msg x) -> M.fail (MarshalError x))
255255- else (
256256- match Rpcmarshal.unmarshal e.Error.def.Rpc.Types.ty r.Rpc.contents with
257257- | Ok x ->
258258- if !strict then M.fail (e.Error.raiser x) else M.return (Error x)
259259- | Error (`Msg x) -> M.fail (MarshalError x)))
260260- in
261261- res
268268+ let wire_name = get_wire_name !description name in
269269+ let args =
270270+ match named with
271271+ | None -> List.rev unnamed
272272+ | Some l -> Rpc.Dict l :: List.rev unnamed
273273+ in
274274+ let call' = Rpc.call wire_name args in
275275+ let call = { call' with is_notification } in
276276+ let rpc = T.put (rpc call) in
277277+ let res =
278278+ T.bind rpc (fun r ->
279279+ if r.Rpc.success then
280280+ match
281281+ Rpcmarshal.unmarshal t.Param.typedef.Rpc.Types.ty
282282+ r.Rpc.contents
283283+ with
284284+ | Ok x -> M.return (Ok x)
285285+ | Error (`Msg x) -> M.fail (MarshalError x)
286286+ else
287287+ match
288288+ Rpcmarshal.unmarshal e.Error.def.Rpc.Types.ty
289289+ r.Rpc.contents
290290+ with
291291+ | Ok x ->
292292+ if !strict then M.fail (e.Error.raiser x)
293293+ else M.return (Error x)
294294+ | Error (`Msg x) -> M.fail (MarshalError x))
295295+ in
296296+ res
262297 in
263298 inner (None, []) ty
264299300300+ let declare_notification name a ty (rpc : T.rpcfn) =
301301+ declare_ true name a ty rpc
265302266266- let declare_notification name a ty (rpc : T.rpcfn) = declare_ true name a ty rpc
267303 let declare name a ty (rpc : T.rpcfn) = declare_ false name a ty rpc
268304 end
269305···275311 match fn with
276312 | None -> key :: acc
277313 | Some fn ->
278278- Hashtbl.add impl key fn;
279279- acc)
280280- hashtbl
281281- []
314314+ Hashtbl.add impl key fn;
315315+ acc)
316316+ hashtbl []
282317 in
283318 if unbound_impls <> [] then raise (UnboundImplementation unbound_impls);
284319 fun call ->
285320 let fn =
286286- try Hashtbl.find impl call.Rpc.name with
287287- | Not_found ->
321321+ try Hashtbl.find impl call.Rpc.name
322322+ with Not_found ->
288323 !logfn "1";
289289- Hashtbl.iter (fun key _ -> !logfn ("method: " ^ key ^ (Hashtbl.hash key |> string_of_int)); !logfn key) impl;
324324+ Hashtbl.iter
325325+ (fun key _ ->
326326+ !logfn ("method: " ^ key ^ (Hashtbl.hash key |> string_of_int));
327327+ !logfn key)
328328+ impl;
290329 let _h = Hashtbl.hash call.Rpc.name in
291330292292- !logfn (Printf.sprintf "Unknown method: %s %d" call.Rpc.name (Hashtbl.hash call.Rpc.name));
331331+ !logfn
332332+ (Printf.sprintf "Unknown method: %s %d" call.Rpc.name
333333+ (Hashtbl.hash call.Rpc.name));
293334 !logfn call.Rpc.name;
294335 raise (UnknownMethod call.Rpc.name)
295336 in
296337 fn call
297338298298-299339 let combine hashtbls =
300340 let result = Hashtbl.create 16 in
301341 List.iter (Hashtbl.iter (fun k v -> Hashtbl.add result k v)) hashtbls;
302342 result
303303-304343305344 module GenServer () = struct
306345 type implementation = server_implementation
346346+307347 type ('a, 'b) comp = ('a, 'b) T.resultb
348348+308349 type 'a res = 'a -> unit
309350310351 type _ fn =
···312353 | Returning : ('a Param.t * 'b Error.t) -> ('a, 'b) comp fn
313354314355 let funcs = Hashtbl.create 20
356356+315357 let description = ref None
316358317359 let implement x =
318360 description := Some x;
319361 funcs
320362321321-322363 let returning a b = Returning (a, b)
364364+323365 let ( @-> ) t f = Function (t, f)
324366325367 let rec has_named_args : type a. a fn -> bool = function
326326- | Function (t, f) ->
327327- (match t.Param.name with
328328- | Some _ -> true
329329- | None -> has_named_args f)
368368+ | Function (t, f) -> (
369369+ match t.Param.name with Some _ -> true | None -> has_named_args f)
330370 | Returning (_, _) -> false
331331-332371333372 let declare_ : bool -> string -> string list -> 'a fn -> 'a res =
334373 fun is_notification name _ ty ->
···336375 (* We do not know the wire name yet as the description may still be unset *)
337376 Hashtbl.add funcs name None;
338377 fun impl ->
339339- ((* Sanity check: ensure the description has been set before we declare
340340- any RPCs. Here we raise an exception immediately and let everything fail. *)
341341- match !description with
342342- | Some _ -> ()
343343- | None -> raise NoDescription);
378378+ (* Sanity check: ensure the description has been set before we declare
379379+ any RPCs. Here we raise an exception immediately and let everything fail. *)
380380+ (match !description with Some _ -> () | None -> raise NoDescription);
344381 let rpcfn =
345382 let has_named = has_named_args ty in
346383 let rec inner : type a. a fn -> a -> T.rpcfn =
347384 fun f impl call ->
348385 match f with
349349- | Function (t, f) ->
350350- let is_opt =
351351- match t.Param.typedef.Rpc.Types.ty with
352352- | Rpc.Types.Option _ -> true
353353- | _ -> false
354354- in
355355- (match get_arg call has_named t.Param.name is_opt with
356356- | Ok (x, y) -> M.return (x, y)
357357- | Error (`Msg m) -> M.fail (MarshalError m))
358358- >>= fun (arg_rpc, call') ->
359359- let z = Rpcmarshal.unmarshal t.Param.typedef.Rpc.Types.ty arg_rpc in
360360- (match z with
361361- | Ok arg -> inner f (impl arg) call'
362362- | Error (`Msg m) -> M.fail (MarshalError m))
386386+ | Function (t, f) -> (
387387+ let is_opt =
388388+ match t.Param.typedef.Rpc.Types.ty with
389389+ | Rpc.Types.Option _ -> true
390390+ | _ -> false
391391+ in
392392+ (match get_arg call has_named t.Param.name is_opt with
393393+ | Ok (x, y) -> M.return (x, y)
394394+ | Error (`Msg m) -> M.fail (MarshalError m))
395395+ >>= fun (arg_rpc, call') ->
396396+ let z =
397397+ Rpcmarshal.unmarshal t.Param.typedef.Rpc.Types.ty arg_rpc
398398+ in
399399+ match z with
400400+ | Ok arg -> inner f (impl arg) call'
401401+ | Error (`Msg m) -> M.fail (MarshalError m))
363402 | Returning (t, e) ->
364364- T.bind impl (function
403403+ T.bind impl (function
365404 | Ok x ->
366366- let res =
367367- Rpc.success (Rpcmarshal.marshal t.Param.typedef.Rpc.Types.ty x)
368368- in
369369- M.return { res with is_notification }
405405+ let res =
406406+ Rpc.success
407407+ (Rpcmarshal.marshal t.Param.typedef.Rpc.Types.ty x)
408408+ in
409409+ M.return { res with is_notification }
370410 | Error y ->
371371- let res =
372372- Rpc.failure (Rpcmarshal.marshal e.Error.def.Rpc.Types.ty y)
373373- in
374374- M.return { res with is_notification })
375375- |> T.get
411411+ let res =
412412+ Rpc.failure
413413+ (Rpcmarshal.marshal e.Error.def.Rpc.Types.ty y)
414414+ in
415415+ M.return { res with is_notification })
416416+ |> T.get
376417 in
377418 inner ty impl
378419 in
···380421 (* The wire name might be different from the name *)
381422 let wire_name = get_wire_name !description name in
382423 Hashtbl.add funcs wire_name (Some rpcfn)
383383-384424385425 let declare_notification name a ty = declare_ true name a ty
426426+386427 let declare name a ty = declare_ false name a ty
387428 end
388429end
389430390431module ExnM = struct
391391- type 'a t =
392392- | V of 'a
393393- | E of exn
432432+ type 'a t = V of 'a | E of exn
394433395434 let return x = V x
396435397397- let lift f x =
398398- match f x with
399399- | y -> V y
400400- | exception e -> E e
436436+ let lift f x = match f x with y -> V y | exception e -> E e
401437402402-403403- let bind x (f : 'a -> 'b t) : 'b t =
404404- match x with
405405- | V x -> f x
406406- | E e -> E e
407407-438438+ let bind x (f : 'a -> 'b t) : 'b t = match x with V x -> f x | E e -> E e
408439409440 let ( >>= ) = bind
441441+410442 let fail e = E e
411443412412- let run = function
413413- | V x -> x
414414- | E e -> raise e
444444+ let run = function V x -> x | E e -> raise e
415445end
416446417447module IdM = struct
418448 type 'a t = T of 'a
419449420450 let return x = T x
451451+421452 let lift f x = T (f x)
453453+422454 let bind (T x) f = f x
455455+423456 let ( >>= ) = bind
457457+424458 let fail e = raise e
459459+425460 let run (T x) = x
426461end
427462···435470436471 let internalerror : (string, t) Rpc.Types.tag =
437472 let open Rpc.Types in
438438- { tname = "InternalError"
439439- ; tdescription = [ "Internal Error" ]
440440- ; tversion = Some (1, 0, 0)
441441- ; tcontents = Basic String
442442- ; tpreview =
443443- (function
444444- | InternalError s -> Some s)
445445- ; treview = (fun s -> InternalError s)
473473+ {
474474+ tname = "InternalError";
475475+ tdescription = [ "Internal Error" ];
476476+ tversion = Some (1, 0, 0);
477477+ tcontents = Basic String;
478478+ tpreview = (function InternalError s -> Some s);
479479+ treview = (fun s -> InternalError s);
446480 }
447481448448-449482 (* And then we can create the 'variant' type *)
450483 let t : t Rpc.Types.variant =
451484 let open Rpc.Types in
452452- { vname = "t"
453453- ; variants = [ BoxedTag internalerror ]
454454- ; vversion = Some (1, 0, 0)
455455- ; vdefault = Some (InternalError "Unknown error tag!")
456456- ; vconstructor =
485485+ {
486486+ vname = "t";
487487+ variants = [ BoxedTag internalerror ];
488488+ vversion = Some (1, 0, 0);
489489+ vdefault = Some (InternalError "Unknown error tag!");
490490+ vconstructor =
457491 (fun s t ->
458492 match s with
459459- | "InternalError" -> begin
493493+ | "InternalError" -> (
460494 match t.tget (Basic String) with
461495 | Ok s -> Ok (internalerror.treview s)
462462- | Error y -> Error y
463463- end
464464- | s -> Error (`Msg (Printf.sprintf "Unknown tag '%s'" s)))
496496+ | Error y -> Error y)
497497+ | s -> Error (`Msg (Printf.sprintf "Unknown tag '%s'" s)));
465498 }
466466-467499468500 let def =
469501 let open Rpc.Types in
470470- { name = "default_error"
471471- ; description = [ "Errors declared as part of the interface" ]
472472- ; ty = Variant t
502502+ {
503503+ name = "default_error";
504504+ description = [ "Errors declared as part of the interface" ];
505505+ ty = Variant t;
473506 }
474474-475507476508 let err =
477509 let open Error in
478478- { def
479479- ; raiser =
480480- (function
481481- | InternalError s -> raise (InternalErrorExn s))
482482- ; matcher =
483483- (function
484484- | InternalErrorExn s -> Some (InternalError s)
485485- | _ -> None)
510510+ {
511511+ def;
512512+ raiser = (function InternalError s -> raise (InternalErrorExn s));
513513+ matcher =
514514+ (function InternalErrorExn s -> Some (InternalError s) | _ -> None);
486515 }
487516end
488517489518module Exn = struct
490519 type rpcfn = Rpc.call -> Rpc.response
520520+491521 type client_implementation = unit
522522+492523 type server_implementation = (string, rpcfn option) Hashtbl.t
493524494525 module GenClient (R : sig
···496527 end) =
497528 struct
498529 type implementation = client_implementation
530530+499531 type ('a, 'b) comp = 'a
532532+500533 type 'a res = 'a
501534502535 type _ fn =
···509542 description := Some x;
510543 ()
511544545545+ let returning a err = Returning (a, err)
512546513513- let returning a err = Returning (a, err)
514547 let ( @-> ) t f = Function (t, f)
515548516549 let declare_ is_notification name _ ty =
517517- let rec inner : type b. (string * Rpc.t) list option * Rpc.t list -> b fn -> b =
550550+ let rec inner :
551551+ type b. (string * Rpc.t) list option * Rpc.t list -> b fn -> b =
518552 fun (named, unnamed) -> function
519519- | Function (t, f) ->
520520- let cur_named =
521521- match named with
522522- | Some l -> l
523523- | None -> []
524524- in
525525- fun v ->
526526- (match t.Param.name with
527527- | Some n ->
528528- (match t.Param.typedef.Rpc.Types.ty, v with
529529- | Rpc.Types.Option ty, Some v' ->
530530- let marshalled = Rpcmarshal.marshal ty v' in
531531- inner (Some ((n, marshalled) :: cur_named), unnamed) f
532532- | Rpc.Types.Option _ty, None -> inner (Some cur_named, unnamed) f
533533- | ty, v ->
534534- let marshalled = Rpcmarshal.marshal ty v in
535535- inner (Some ((n, marshalled) :: cur_named), unnamed) f)
536536- | None ->
537537- let marshalled = Rpcmarshal.marshal t.Param.typedef.Rpc.Types.ty v in
538538- inner (named, marshalled :: unnamed) f)
539539- | Returning (t, e) ->
540540- let wire_name = get_wire_name !description name in
541541- let args =
542542- match named with
543543- | None -> List.rev unnamed
544544- | Some l -> Rpc.Dict l :: List.rev unnamed
545545- in
546546- let call' = Rpc.call wire_name args in
547547- let call = { call' with is_notification } in
548548- let r = R.rpc call in
549549- if r.Rpc.success
550550- then (
551551- match Rpcmarshal.unmarshal t.Param.typedef.Rpc.Types.ty r.Rpc.contents with
552552- | Ok x -> x
553553- | Error (`Msg x) -> raise (MarshalError x))
554554- else (
555555- match Rpcmarshal.unmarshal e.Error.def.Rpc.Types.ty r.Rpc.contents with
556556- | Ok x -> raise (e.Error.raiser x)
557557- | Error (`Msg x) -> raise (MarshalError x))
553553+ | Function (t, f) -> (
554554+ let cur_named = match named with Some l -> l | None -> [] in
555555+ fun v ->
556556+ match t.Param.name with
557557+ | Some n -> (
558558+ match (t.Param.typedef.Rpc.Types.ty, v) with
559559+ | Rpc.Types.Option ty, Some v' ->
560560+ let marshalled = Rpcmarshal.marshal ty v' in
561561+ inner (Some ((n, marshalled) :: cur_named), unnamed) f
562562+ | Rpc.Types.Option _ty, None ->
563563+ inner (Some cur_named, unnamed) f
564564+ | ty, v ->
565565+ let marshalled = Rpcmarshal.marshal ty v in
566566+ inner (Some ((n, marshalled) :: cur_named), unnamed) f)
567567+ | None ->
568568+ let marshalled =
569569+ Rpcmarshal.marshal t.Param.typedef.Rpc.Types.ty v
570570+ in
571571+ inner (named, marshalled :: unnamed) f)
572572+ | Returning (t, e) -> (
573573+ let wire_name = get_wire_name !description name in
574574+ let args =
575575+ match named with
576576+ | None -> List.rev unnamed
577577+ | Some l -> Rpc.Dict l :: List.rev unnamed
578578+ in
579579+ let call' = Rpc.call wire_name args in
580580+ let call = { call' with is_notification } in
581581+ let r = R.rpc call in
582582+ if r.Rpc.success then
583583+ match
584584+ Rpcmarshal.unmarshal t.Param.typedef.Rpc.Types.ty r.Rpc.contents
585585+ with
586586+ | Ok x -> x
587587+ | Error (`Msg x) -> raise (MarshalError x)
588588+ else
589589+ match
590590+ Rpcmarshal.unmarshal e.Error.def.Rpc.Types.ty r.Rpc.contents
591591+ with
592592+ | Ok x -> raise (e.Error.raiser x)
593593+ | Error (`Msg x) -> raise (MarshalError x))
558594 in
559595 inner (None, []) ty
560596561561-562597 let declare name a ty = declare_ false name a ty
598598+563599 let declare_notification name a ty = declare_ true name a ty
564600 end
565601···571607 match fn with
572608 | None -> key :: acc
573609 | Some fn ->
574574- Hashtbl.add impl key fn;
575575- acc)
576576- hashtbl
577577- []
610610+ Hashtbl.add impl key fn;
611611+ acc)
612612+ hashtbl []
578613 in
579614 if unbound_impls <> [] then raise (UnboundImplementation unbound_impls);
580615 fun call ->
581616 let fn =
582582- try Hashtbl.find impl call.Rpc.name with
583583- | Not_found ->
617617+ try Hashtbl.find impl call.Rpc.name
618618+ with Not_found ->
584619 !logfn "2";
585620 Hashtbl.iter (fun key _ -> !logfn ("method: " ^ key)) impl;
586621 !logfn (Printf.sprintf "Unknown method: %s" call.Rpc.name);
···588623 in
589624 fn call
590625591591-592626 let combine hashtbls =
593627 let result = Hashtbl.create 16 in
594628 List.iter (Hashtbl.iter (fun k v -> Hashtbl.add result k v)) hashtbls;
595629 result
596630597597-598631 module GenServer () = struct
599632 type implementation = server_implementation
633633+600634 type ('a, 'b) comp = 'a
635635+601636 type 'a res = 'a -> unit
602637603638 type _ fn =
···605640 | Returning : ('a Param.t * 'b Error.t) -> ('a, _) comp fn
606641607642 let funcs = Hashtbl.create 20
643643+608644 let description = ref None
609645610646 let implement x =
611647 description := Some x;
612648 funcs
613649614614-615650 let returning a b = Returning (a, b)
651651+616652 let ( @-> ) t f = Function (t, f)
617653618654 type boxed_error = BoxedError : 'a Error.t -> boxed_error
···621657 | Function (_, f) -> get_error_ty f
622658 | Returning (_, e) -> BoxedError e
623659624624-625660 let rec has_named_args : type a. a fn -> bool = function
626626- | Function (t, f) ->
627627- (match t.Param.name with
628628- | Some _ -> true
629629- | None -> has_named_args f)
661661+ | Function (t, f) -> (
662662+ match t.Param.name with Some _ -> true | None -> has_named_args f)
630663 | Returning (_, _) -> false
631631-632664633665 let declare_ : bool -> string -> string list -> 'a fn -> 'a res =
634666 fun is_notification name _ ty ->
635667 (* We do not know the wire name yet as the description may still be unset *)
636668 Hashtbl.add funcs name None;
637669 fun impl ->
638638- ((* Sanity check: ensure the description has been set before we declare
639639- any RPCs *)
640640- match !description with
641641- | Some _ -> ()
642642- | None -> raise NoDescription);
670670+ (* Sanity check: ensure the description has been set before we declare
671671+ any RPCs *)
672672+ (match !description with Some _ -> () | None -> raise NoDescription);
643673 let rpcfn =
644674 let has_named = has_named_args ty in
645675 let rec inner : type a. a fn -> a -> Rpc.call -> Rpc.response =
···647677 try
648678 match f with
649679 | Function (t, f) ->
650650- let is_opt =
651651- match t.Param.typedef.Rpc.Types.ty with
652652- | Rpc.Types.Option _ -> true
653653- | _ -> false
654654- in
655655- let arg_rpc, call' =
656656- match get_arg call has_named t.Param.name is_opt with
657657- | Ok (x, y) -> x, y
658658- | Error (`Msg m) -> raise (MarshalError m)
659659- in
660660- let z = Rpcmarshal.unmarshal t.Param.typedef.Rpc.Types.ty arg_rpc in
661661- let arg =
662662- match z with
663663- | Ok arg -> arg
664664- | Error (`Msg m) -> raise (MarshalError m)
665665- in
666666- inner f (impl arg) call'
680680+ let is_opt =
681681+ match t.Param.typedef.Rpc.Types.ty with
682682+ | Rpc.Types.Option _ -> true
683683+ | _ -> false
684684+ in
685685+ let arg_rpc, call' =
686686+ match get_arg call has_named t.Param.name is_opt with
687687+ | Ok (x, y) -> (x, y)
688688+ | Error (`Msg m) -> raise (MarshalError m)
689689+ in
690690+ let z =
691691+ Rpcmarshal.unmarshal t.Param.typedef.Rpc.Types.ty arg_rpc
692692+ in
693693+ let arg =
694694+ match z with
695695+ | Ok arg -> arg
696696+ | Error (`Msg m) -> raise (MarshalError m)
697697+ in
698698+ inner f (impl arg) call'
667699 | Returning (t, _) ->
668668- let call =
669669- Rpc.success (Rpcmarshal.marshal t.Param.typedef.Rpc.Types.ty impl)
670670- in
671671- { call with is_notification }
672672- with
673673- | e ->
700700+ let call =
701701+ Rpc.success
702702+ (Rpcmarshal.marshal t.Param.typedef.Rpc.Types.ty impl)
703703+ in
704704+ { call with is_notification }
705705+ with e -> (
674706 let (BoxedError error_ty) = get_error_ty f in
675675- (match error_ty.Error.matcher e with
707707+ match error_ty.Error.matcher e with
676708 | Some y ->
677677- Rpc.failure (Rpcmarshal.marshal error_ty.Error.def.Rpc.Types.ty y)
709709+ Rpc.failure
710710+ (Rpcmarshal.marshal error_ty.Error.def.Rpc.Types.ty y)
678711 | None -> raise e)
679712 in
680713 inner ty impl
···684717 let wire_name = get_wire_name !description name in
685718 Hashtbl.add funcs wire_name (Some rpcfn)
686719687687-688720 let declare name a ty = declare_ true name a ty
721721+689722 let declare_notification name a ty = declare_ false name a ty
690723 end
691724end
+114-125
idl/rpc.ml
···1616 *)
17171818let debug = ref false
1919+1920let set_debug x = debug := x
2121+2022let get_debug () = !debug
21232224type msg = [ `Msg of string ]
···7173 | Abstract : 'a abstract -> 'a typ
72747375 (* A type definition has a name and description *)
7474- and 'a def =
7575- { name : string
7676- ; description : string list
7777- ; ty : 'a typ
7878- }
7676+ and 'a def = { name : string; description : string list; ty : 'a typ }
79778078 and boxed_def = BoxedDef : 'a def -> boxed_def
81798282- and ('a, 's) field =
8383- { fname : string
8484- ; fdescription : string list
8585- ; fversion : Version.t option
8686- ; field : 'a typ
8787- ; fdefault : 'a option
8888- ; fget : 's -> 'a
8989- ; (* Lenses *)
9090- fset : 'a -> 's -> 's
9191- }
8080+ and ('a, 's) field = {
8181+ fname : string;
8282+ fdescription : string list;
8383+ fversion : Version.t option;
8484+ field : 'a typ;
8585+ fdefault : 'a option;
8686+ fget : 's -> 'a;
8787+ (* Lenses *)
8888+ fset : 'a -> 's -> 's;
8989+ }
92909391 and 'a boxed_field = BoxedField : ('a, 's) field -> 's boxed_field
94929595- and field_getter =
9696- { field_get : 'a. string -> 'a typ -> ('a, msg) result }
9393+ and field_getter = { field_get : 'a. string -> 'a typ -> ('a, msg) result }
97949898- and 'a structure =
9999- { sname : string
100100- ; fields : 'a boxed_field list
101101- ; version : Version.t option
102102- ; constructor : field_getter -> ('a, msg) result
103103- }
9595+ and 'a structure = {
9696+ sname : string;
9797+ fields : 'a boxed_field list;
9898+ version : Version.t option;
9999+ constructor : field_getter -> ('a, msg) result;
100100+ }
104101105105- and ('a, 's) tag =
106106- { tname : string
107107- ; tdescription : string list
108108- ; tversion : Version.t option
109109- ; tcontents : 'a typ
110110- ; tpreview : 's -> 'a option
111111- ; treview : 'a -> 's
112112- }
102102+ and ('a, 's) tag = {
103103+ tname : string;
104104+ tdescription : string list;
105105+ tversion : Version.t option;
106106+ tcontents : 'a typ;
107107+ tpreview : 's -> 'a option;
108108+ treview : 'a -> 's;
109109+ }
113110114111 and 'a boxed_tag = BoxedTag : ('a, 's) tag -> 's boxed_tag
115112116113 and tag_getter = { tget : 'a. 'a typ -> ('a, msg) result }
117114118118- and 'a variant =
119119- { vname : string
120120- ; variants : 'a boxed_tag list
121121- ; vdefault : 'a option
122122- ; vversion : Version.t option
123123- ; vconstructor : string -> tag_getter -> ('a, msg) result
124124- }
115115+ and 'a variant = {
116116+ vname : string;
117117+ variants : 'a boxed_tag list;
118118+ vdefault : 'a option;
119119+ vversion : Version.t option;
120120+ vconstructor : string -> tag_getter -> ('a, msg) result;
121121+ }
125122126126- and 'a abstract =
127127- { aname : string
128128- ; test_data : 'a list
129129- ; rpc_of : 'a -> t
130130- ; of_rpc : t -> ('a, msg) result
131131- }
123123+ and 'a abstract = {
124124+ aname : string;
125125+ test_data : 'a list;
126126+ rpc_of : 'a -> t;
127127+ of_rpc : t -> ('a, msg) result;
128128+ }
132129133130 let int = { name = "int"; ty = Basic Int; description = [ "Native integer" ] }
134134- let int32 = { name = "int32"; ty = Basic Int32; description = [ "32-bit integer" ] }
135135- let int64 = { name = "int64"; ty = Basic Int64; description = [ "64-bit integer" ] }
131131+132132+ let int32 =
133133+ { name = "int32"; ty = Basic Int32; description = [ "32-bit integer" ] }
134134+135135+ let int64 =
136136+ { name = "int64"; ty = Basic Int64; description = [ "64-bit integer" ] }
137137+136138 let bool = { name = "bool"; ty = Basic Bool; description = [ "Boolean" ] }
137139138140 let float =
139139- { name = "float"; ty = Basic Float; description = [ "Floating-point number" ] }
141141+ {
142142+ name = "float";
143143+ ty = Basic Float;
144144+ description = [ "Floating-point number" ];
145145+ }
140146147147+ let string =
148148+ { name = "string"; ty = Basic String; description = [ "String" ] }
141149142142- let string = { name = "string"; ty = Basic String; description = [ "String" ] }
143150 let char = { name = "char"; ty = Basic Char; description = [ "Char" ] }
151151+144152 let unit = { name = "unit"; ty = Unit; description = [ "Unit" ] }
145153146154 let default_types =
147147- [ BoxedDef int
148148- ; BoxedDef int32
149149- ; BoxedDef int64
150150- ; BoxedDef bool
151151- ; BoxedDef float
152152- ; BoxedDef string
153153- ; BoxedDef char
154154- ; BoxedDef unit
155155+ [
156156+ BoxedDef int;
157157+ BoxedDef int32;
158158+ BoxedDef int64;
159159+ BoxedDef bool;
160160+ BoxedDef float;
161161+ BoxedDef string;
162162+ BoxedDef char;
163163+ BoxedDef unit;
155164 ]
156165end
157166158167exception Runtime_error of string * t
168168+159169exception Runtime_exception of string * string
160170161171let map_strings sep fn l = String.concat sep (List.map fn l)
···171181 | DateTime s -> sprintf "D(%s)" s
172182 | Enum ts -> sprintf "[%s]" (map_strings ";" to_string ts)
173183 | Dict ts ->
174174- sprintf "{%s}" (map_strings ";" (fun (s, t) -> sprintf "%s:%s" s (to_string t)) ts)
184184+ sprintf "{%s}"
185185+ (map_strings ";" (fun (s, t) -> sprintf "%s:%s" s (to_string t)) ts)
175186 | Base64 s -> sprintf "B64(%s)" s
176187 | Null -> "N"
177188178178-179189let rpc_of_t x = x
190190+180191let rpc_of_int64 i = Int i
192192+181193let rpc_of_int32 i = Int (Int64.of_int32 i)
194194+182195let rpc_of_int i = Int (Int64.of_int i)
196196+183197let rpc_of_bool b = Bool b
198198+184199let rpc_of_float f = Float f
200200+185201let rpc_of_string s = String s
202202+186203let rpc_of_dateTime s = DateTime s
204204+187205let rpc_of_base64 s = Base64 s
206206+188207let rpc_of_unit () = Null
208208+189209let rpc_of_char x = Int (Int64.of_int (Char.code x))
190210191211let int64_of_rpc = function
192212 | Int i -> i
193213 | String s -> Int64.of_string s
194214 | x -> failwith (Printf.sprintf "Expected int64, got '%s'" (to_string x))
195195-196215197216let int32_of_rpc = function
198217 | Int i -> Int64.to_int32 i
199218 | String s -> Int32.of_string s
200219 | x -> failwith (Printf.sprintf "Expected int32, got '%s'" (to_string x))
201220202202-203221let int_of_rpc = function
204222 | Int i -> Int64.to_int i
205223 | String s -> int_of_string s
206224 | x -> failwith (Printf.sprintf "Expected int, got '%s'" (to_string x))
207207-208225209226let bool_of_rpc = function
210227 | Bool b -> b
211228 | x -> failwith (Printf.sprintf "Expected bool, got '%s'" (to_string x))
212229213213-214230let float_of_rpc = function
215231 | Float f -> f
216232 | Int i -> Int64.to_float i
217233 | Int32 i -> Int32.to_float i
218234 | String s -> float_of_string s
219235 | x -> failwith (Printf.sprintf "Expected float, got '%s'" (to_string x))
220220-221236222237let string_of_rpc = function
223238 | String s -> s
224239 | x -> failwith (Printf.sprintf "Expected string, got '%s'" (to_string x))
225240226226-227241let dateTime_of_rpc = function
228242 | DateTime s -> s
229243 | x -> failwith (Printf.sprintf "Expected DateTime, got '%s'" (to_string x))
230244231231-232232-let base64_of_rpc = function
233233- | _ -> failwith "Base64 Unhandled"
234234-245245+let base64_of_rpc = function _ -> failwith "Base64 Unhandled"
235246236247let unit_of_rpc = function
237248 | Null -> ()
238249 | x -> failwith (Printf.sprintf "Expected unit, got '%s'" (to_string x))
239250240240-241251let char_of_rpc x =
242252 let x = int_of_rpc x in
243243- if x < 0 || x > 255
244244- then failwith (Printf.sprintf "Char out of range (%d)" x)
253253+ if x < 0 || x > 255 then failwith (Printf.sprintf "Char out of range (%d)" x)
245254 else Char.chr x
246246-247255248256let t_of_rpc t = t
249257···252260 | Enum (String s :: ss) -> Enum (String (String.lowercase_ascii s) :: ss)
253261 | x -> x
254262255255-256263module ResultUnmarshallers = struct
257264 let error_msg m = Error (`Msg m)
265265+258266 let ok x = Ok x
259267260268 let int64_of_rpc = function
261269 | Int i -> ok i
262262- | String s ->
263263- (try ok (Int64.of_string s) with
264264- | _ -> error_msg (Printf.sprintf "Expected int64, got string '%s'" s))
270270+ | String s -> (
271271+ try ok (Int64.of_string s)
272272+ with _ ->
273273+ error_msg (Printf.sprintf "Expected int64, got string '%s'" s))
265274 | x -> error_msg (Printf.sprintf "Expected int64, got '%s'" (to_string x))
266266-267275268276 let int32_of_rpc = function
269277 | Int i -> ok (Int64.to_int32 i)
270270- | String s ->
271271- (try ok (Int32.of_string s) with
272272- | _ -> error_msg (Printf.sprintf "Expected int32, got string '%s'" s))
278278+ | String s -> (
279279+ try ok (Int32.of_string s)
280280+ with _ ->
281281+ error_msg (Printf.sprintf "Expected int32, got string '%s'" s))
273282 | x -> error_msg (Printf.sprintf "Expected int32, got '%s'" (to_string x))
274283275275-276284 let int_of_rpc = function
277285 | Int i -> ok (Int64.to_int i)
278278- | String s ->
279279- (try ok (int_of_string s) with
280280- | _ -> error_msg (Printf.sprintf "Expected int, got string '%s'" s))
286286+ | String s -> (
287287+ try ok (int_of_string s)
288288+ with _ -> error_msg (Printf.sprintf "Expected int, got string '%s'" s))
281289 | x -> error_msg (Printf.sprintf "Expected int, got '%s'" (to_string x))
282282-283290284291 let bool_of_rpc = function
285292 | Bool b -> ok b
286293 | x -> error_msg (Printf.sprintf "Expected bool, got '%s'" (to_string x))
287294288288-289295 let float_of_rpc = function
290296 | Float f -> ok f
291297 | Int i -> ok (Int64.to_float i)
292298 | Int32 i -> ok (Int32.to_float i)
293293- | String s ->
294294- (try ok (float_of_string s) with
295295- | _ -> error_msg (Printf.sprintf "Expected float, got string '%s'" s))
299299+ | String s -> (
300300+ try ok (float_of_string s)
301301+ with _ ->
302302+ error_msg (Printf.sprintf "Expected float, got string '%s'" s))
296303 | x -> error_msg (Printf.sprintf "Expected float, got '%s'" (to_string x))
297297-298304299305 let string_of_rpc = function
300306 | String s -> ok s
301307 | x -> error_msg (Printf.sprintf "Expected string, got '%s'" (to_string x))
302308303303-304309 let dateTime_of_rpc = function
305310 | DateTime s -> ok s
306306- | x -> error_msg (Printf.sprintf "Expected DateTime, got '%s'" (to_string x))
307307-311311+ | x ->
312312+ error_msg (Printf.sprintf "Expected DateTime, got '%s'" (to_string x))
308313309309- let base64_of_rpc = function
310310- | _ -> error_msg "Base64 Unhandled"
311311-314314+ let base64_of_rpc = function _ -> error_msg "Base64 Unhandled"
312315313316 let unit_of_rpc = function
314317 | Null -> ok ()
315318 | x -> error_msg (Printf.sprintf "Expected unit, got '%s'" (to_string x))
316319317317-318320 let char_of_rpc x =
319319- match (int_of_rpc x) with
321321+ match int_of_rpc x with
320322 | Ok x ->
321321- if x < 0 || x > 255
322322- then error_msg (Printf.sprintf "Char out of range (%d)" x)
323323+ if x < 0 || x > 255 then
324324+ error_msg (Printf.sprintf "Char out of range (%d)" x)
323325 else ok (Char.chr x)
324326 | Error y -> Error y
325327···327329end
328330329331let struct_extend rpc default_rpc =
330330- match rpc, default_rpc with
332332+ match (rpc, default_rpc) with
331333 | Dict real, Dict default_fields ->
332332- Dict
333333- (List.fold_left
334334- (fun real (f, default) ->
335335- if List.mem_assoc f real then real else (f, default) :: real)
336336- real
337337- default_fields)
334334+ Dict
335335+ (List.fold_left
336336+ (fun real (f, default) ->
337337+ if List.mem_assoc f real then real else (f, default) :: real)
338338+ real default_fields)
338339 | _, _ -> rpc
339339-340340341341type callback = string list -> t -> unit
342342343343-type call =
344344- { name : string
345345- ; params : t list
346346- ; is_notification : bool
347347- }
343343+type call = { name : string; params : t list; is_notification : bool }
348344349345let call name params = { name; params; is_notification = false }
346346+350347let notification name params = { name; params; is_notification = true }
351348352349let string_of_call call =
353353- Printf.sprintf
354354- "-> %s(%s)"
355355- call.name
350350+ Printf.sprintf "-> %s(%s)" call.name
356351 (String.concat "," (List.map to_string call.params))
357352358358-359359-type response =
360360- { success : bool
361361- ; contents : t
362362- ; is_notification : bool
363363- }
353353+type response = { success : bool; contents : t; is_notification : bool }
364354365355let string_of_response response =
366366- Printf.sprintf
367367- "<- %s(%s)"
356356+ Printf.sprintf "<- %s(%s)"
368357 (if response.success then "success" else "failure")
369358 (to_string response.contents)
370370-371359372360(* is_notification is to be set as true only if the call was a notification *)
373361374362let success v = { success = true; contents = v; is_notification = false }
363363+375364let failure v = { success = false; contents = v; is_notification = false }
+89-58
idl/rpc.mli
···1515 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1616 *)
17171818-(** {2 Value} *)
1918type msg = [ `Msg of string ]
1919+(** {2 Value} *)
20202121type t =
2222 | Int of int64
···6565 | Variant : 'a variant -> 'a typ
6666 | Abstract : 'a abstract -> 'a typ
67676868- and 'a def =
6969- { name : string
7070- ; description : string list
7171- ; ty : 'a typ
7272- }
6868+ and 'a def = { name : string; description : string list; ty : 'a typ }
73697470 and boxed_def = BoxedDef : 'a def -> boxed_def
75717676- and ('a, 's) field =
7777- { fname : string
7878- ; fdescription : string list
7979- ; fversion : Version.t option
8080- ; field : 'a typ
8181- ; fdefault : 'a option
8282- ; fget : 's -> 'a
8383- ; fset : 'a -> 's -> 's
8484- }
7272+ and ('a, 's) field = {
7373+ fname : string;
7474+ fdescription : string list;
7575+ fversion : Version.t option;
7676+ field : 'a typ;
7777+ fdefault : 'a option;
7878+ fget : 's -> 'a;
7979+ fset : 'a -> 's -> 's;
8080+ }
85818682 and 'a boxed_field = BoxedField : ('a, 's) field -> 's boxed_field
87838888- and field_getter =
8989- { field_get : 'a. string -> 'a typ -> ('a, msg) result }
8484+ and field_getter = { field_get : 'a. string -> 'a typ -> ('a, msg) result }
90859191- and 'a structure =
9292- { sname : string
9393- ; fields : 'a boxed_field list
9494- ; version : Version.t option
9595- ; constructor : field_getter -> ('a, msg) result
9696- }
8686+ and 'a structure = {
8787+ sname : string;
8888+ fields : 'a boxed_field list;
8989+ version : Version.t option;
9090+ constructor : field_getter -> ('a, msg) result;
9191+ }
97929898- and ('a, 's) tag =
9999- { tname : string
100100- ; tdescription : string list
101101- ; tversion : Version.t option
102102- ; tcontents : 'a typ
103103- ; tpreview : 's -> 'a option
104104- ; (* Prism *)
105105- treview : 'a -> 's
106106- }
9393+ and ('a, 's) tag = {
9494+ tname : string;
9595+ tdescription : string list;
9696+ tversion : Version.t option;
9797+ tcontents : 'a typ;
9898+ tpreview : 's -> 'a option;
9999+ (* Prism *)
100100+ treview : 'a -> 's;
101101+ }
107102108103 and 'a boxed_tag = BoxedTag : ('a, 's) tag -> 's boxed_tag
109104110105 and tag_getter = { tget : 'a. 'a typ -> ('a, msg) result }
111106112112- and 'a variant =
113113- { vname : string
114114- ; variants : 'a boxed_tag list
115115- ; vdefault : 'a option
116116- ; vversion : Version.t option
117117- ; vconstructor : string -> tag_getter -> ('a, msg) result
118118- }
107107+ and 'a variant = {
108108+ vname : string;
109109+ variants : 'a boxed_tag list;
110110+ vdefault : 'a option;
111111+ vversion : Version.t option;
112112+ vconstructor : string -> tag_getter -> ('a, msg) result;
113113+ }
119114120120- and 'a abstract =
121121- { aname : string
122122- ; test_data : 'a list
123123- ; rpc_of : 'a -> t
124124- ; of_rpc : t -> ('a, msg) result
125125- }
115115+ and 'a abstract = {
116116+ aname : string;
117117+ test_data : 'a list;
118118+ rpc_of : 'a -> t;
119119+ of_rpc : t -> ('a, msg) result;
120120+ }
126121127122 val int : int def
123123+128124 val int32 : int32 def
125125+129126 val int64 : int64 def
127127+130128 val bool : bool def
129129+131130 val float : float def
131131+132132 val string : string def
133133+133134 val char : char def
135135+134136 val unit : unit def
137137+135138 val default_types : boxed_def list
136139end
137140138141(** {2 Basic constructors} *)
139142140143val rpc_of_int64 : int64 -> t
144144+141145val rpc_of_int32 : int32 -> t
146146+142147val rpc_of_int : int -> t
148148+143149val rpc_of_bool : bool -> t
150150+144151val rpc_of_float : float -> t
152152+145153val rpc_of_string : string -> t
154154+146155val rpc_of_dateTime : string -> t
156156+147157val rpc_of_base64 : string -> t
158158+148159val rpc_of_t : t -> t
160160+149161val rpc_of_unit : unit -> t
162162+150163val rpc_of_char : char -> t
164164+151165val int64_of_rpc : t -> int64
166166+152167val int32_of_rpc : t -> int32
168168+153169val int_of_rpc : t -> int
170170+154171val bool_of_rpc : t -> bool
172172+155173val float_of_rpc : t -> float
174174+156175val string_of_rpc : t -> string
176176+157177val dateTime_of_rpc : t -> string
178178+158179val base64_of_rpc : t -> string
180180+159181val t_of_rpc : t -> t
182182+160183val char_of_rpc : t -> char
184184+161185val unit_of_rpc : t -> unit
162186163187module ResultUnmarshallers : sig
164188 val int64_of_rpc : t -> (int64, msg) result
189189+165190 val int32_of_rpc : t -> (int32, msg) result
191191+166192 val int_of_rpc : t -> (int, msg) result
193193+167194 val bool_of_rpc : t -> (bool, msg) result
195195+168196 val float_of_rpc : t -> (float, msg) result
197197+169198 val string_of_rpc : t -> (string, msg) result
199199+170200 val dateTime_of_rpc : t -> (string, msg) result
201201+171202 val base64_of_rpc : t -> (string, msg) result
203203+172204 val t_of_rpc : t -> (t, msg) result
205205+173206 val unit_of_rpc : t -> (unit, msg) result
207207+174208 val char_of_rpc : t -> (char, msg) result
175209end
176210···178212179213type callback = string list -> t -> unit
180214181181-type call =
182182- { name : string
183183- ; params : t list
184184- ; is_notification : bool
185185- }
215215+type call = { name : string; params : t list; is_notification : bool }
186216187217val call : string -> t list -> call
218218+188219val notification : string -> t list -> call
220220+189221val string_of_call : call -> string
190222191223(** {2 Responses} *)
192224193193-type response =
194194- { success : bool
195195- ; contents : t
196196- ; is_notification : bool
197197- }
225225+type response = { success : bool; contents : t; is_notification : bool }
198226199227val string_of_response : response -> string
228228+200229val success : t -> response
230230+201231val failure : t -> response
202232203233(** {2 Run-time errors} *)
204234205235exception Runtime_error of string * t
236236+206237exception Runtime_exception of string * string
207238208208-(** {2 Debug options} *)
209239val set_debug : bool -> unit
240240+(** {2 Debug options} *)
210241211242val get_debug : unit -> bool
212243213213-(** Helper *)
214244val lowerfn : t -> t
245245+(** Helper *)
215246247247+val struct_extend : t -> t -> t
216248(** [struct_extend rpc1 rpc2] first checks that [rpc1] and [rpc2] are both
217249 * dictionaries. If this is the case then [struct_extend] will create a new
218250 * [Rpc.t] which contains all key-value pairs from [rpc1], as well as all
219251 * key-value pairs from [rpc2] for which the key does not exist in [rpc1]. *)
220220-val struct_extend : t -> t -> t
+185-177
idl/rpcmarshal.ml
···5566let tailrec_map f l = List.rev_map f l |> List.rev
7788-let (>>|) x f = match x with | Ok x -> Ok (f x) | Error y -> Error y
99-let (>>=) x f = match x with | Ok x -> f x | Error y -> Error y
88+let ( >>| ) x f = match x with Ok x -> Ok (f x) | Error y -> Error y
99+1010+let ( >>= ) x f = match x with Ok x -> f x | Error y -> Error y
1111+1012let return x = Ok x
1313+1114let ok x = Ok x
12151316let rec unmarshal : type a. a typ -> Rpc.t -> (a, err) result =
···1720 let list_helper typ l =
1821 List.fold_left
1922 (fun acc v ->
2020- match acc, unmarshal typ v with
2323+ match (acc, unmarshal typ v) with
2124 | Ok a, Ok v -> Ok (v :: a)
2225 | _, Error (`Msg s) ->
2323- Error
2424- (`Msg
2525- (Printf.sprintf
2626- "Failed to unmarshal array: %s (when unmarshalling: %s)"
2727- s
2828- (Rpc.to_string v)))
2626+ Error
2727+ (`Msg
2828+ (Printf.sprintf
2929+ "Failed to unmarshal array: %s (when unmarshalling: %s)" s
3030+ (Rpc.to_string v)))
2931 | x, _ -> x)
3030- (Ok [])
3131- l
3232+ (Ok []) l
3233 >>| List.rev
3334 in
3435 match t with
···4142 | Basic Char -> int_of_rpc v >>| Char.chr
4243 | DateTime -> dateTime_of_rpc v
4344 | Base64 -> base64_of_rpc v
4444- | Array typ ->
4545- (match v with
4646- | Enum xs -> list_helper typ xs >>| Array.of_list
4747- | _ -> Error (`Msg "Expecting Array"))
4848- | List (Tuple (Basic String, typ)) ->
4949- (match v with
5050- | Dict xs ->
5151- let keys = tailrec_map fst xs in
5252- let vs = tailrec_map snd xs in
5353- list_helper typ vs >>= fun vs -> return (List.combine keys vs)
5454- | _ -> Error (`Msg "Unhandled"))
5555- | Dict (basic, typ) ->
5656- (match v with
5757- | Dict xs ->
5858- (match basic with
5959- | String ->
6060- let keys = tailrec_map fst xs in
6161- let vs = tailrec_map snd xs in
6262- list_helper typ vs >>= fun vs -> return (List.combine keys vs)
6363- | _ -> Error (`Msg "Expecting something other than a Dict type"))
6464- | _ -> Error (`Msg "Unhandled"))
6565- | List typ ->
6666- (match v with
6767- | Enum xs -> list_helper typ xs
6868- | _ -> Error (`Msg "Expecting array"))
4545+ | Array typ -> (
4646+ match v with
4747+ | Enum xs -> list_helper typ xs >>| Array.of_list
4848+ | _ -> Error (`Msg "Expecting Array"))
4949+ | List (Tuple (Basic String, typ)) -> (
5050+ match v with
5151+ | Dict xs ->
5252+ let keys = tailrec_map fst xs in
5353+ let vs = tailrec_map snd xs in
5454+ list_helper typ vs >>= fun vs -> return (List.combine keys vs)
5555+ | _ -> Error (`Msg "Unhandled"))
5656+ | Dict (basic, typ) -> (
5757+ match v with
5858+ | Dict xs -> (
5959+ match basic with
6060+ | String ->
6161+ let keys = tailrec_map fst xs in
6262+ let vs = tailrec_map snd xs in
6363+ list_helper typ vs >>= fun vs -> return (List.combine keys vs)
6464+ | _ -> Error (`Msg "Expecting something other than a Dict type"))
6565+ | _ -> Error (`Msg "Unhandled"))
6666+ | List typ -> (
6767+ match v with
6868+ | Enum xs -> list_helper typ xs
6969+ | _ -> Error (`Msg "Expecting array"))
6970 | Unit -> unit_of_rpc v
7070- | Option t ->
7171- (match v with
7272- | Enum [ x ] -> unmarshal t x >>= fun x -> return (Some x)
7373- | Enum [] -> return None
7474- | y ->
7575- Error (`Msg
7676- (Printf.sprintf "Expecting an Enum value, got '%s'" (Rpc.to_string y))))
7777- | Tuple (t1, t2) ->
7878- (match v, t2 with
7979- | Rpc.Enum list, Tuple (_, _) ->
8080- unmarshal t1 (List.hd list)
8181- >>= fun v1 -> unmarshal t2 (Rpc.Enum (List.tl list)) >>= fun v2 -> Ok (v1, v2)
8282- | Rpc.Enum [ x; y ], _ ->
8383- unmarshal t1 x >>= fun v1 -> unmarshal t2 y >>= fun v2 -> Ok (v1, v2)
8484- | Rpc.Enum _, _ -> Error (`Msg "Too many items in a tuple!")
8585- | _, _ -> Error (`Msg "Expecting Rpc.Enum when unmarshalling a tuple"))
8686- | Tuple3 (t1, t2, t3) ->
8787- (match v with
8888- | Rpc.Enum [ x; y; z ] ->
8989- unmarshal t1 x
9090- >>= fun v1 ->
9191- unmarshal t2 y >>= fun v2 -> unmarshal t3 z >>= fun v3 -> Ok (v1, v2, v3)
9292- | Rpc.Enum _ ->
9393- Error (`Msg "Expecting precisely 3 items when unmarshalling a Tuple3")
9494- | _ -> Error (`Msg "Expecting Rpc.Enum when unmarshalling a tuple3"))
9595- | Tuple4 (t1, t2, t3, t4) ->
9696- (match v with
9797- | Rpc.Enum [ x; y; z; a ] ->
9898- unmarshal t1 x
9999- >>= fun v1 ->
100100- unmarshal t2 y
101101- >>= fun v2 ->
102102- unmarshal t3 z >>= fun v3 -> unmarshal t4 a >>= fun v4 -> Ok (v1, v2, v3, v4)
103103- | Rpc.Enum _ ->
104104- Error (`Msg
105105- "Expecting precisely 4 items in an Enum when unmarshalling a Tuple4")
106106- | _ -> Error (`Msg "Expecting Rpc.Enum when unmarshalling a tuple4"))
107107- | Struct { constructor; sname; _ } ->
108108- (match v with
109109- | Rpc.Dict keys' ->
110110- let keys = List.map (fun (s, v) -> String.lowercase_ascii s, v) keys' in
111111- constructor
112112- { field_get =
113113- (let x : type a. string -> a typ -> (a, Rpc.msg) result =
114114- fun s ty ->
115115- let s = String.lowercase_ascii s in
116116- match ty with
117117- | Option x ->
118118- (try List.assoc s keys |> unmarshal x >>= fun o -> return (Some o) with
119119- | _ -> return None)
120120- | y ->
121121- (try List.assoc s keys |> unmarshal y with
122122- | Not_found ->
123123- Error (`Msg
124124- (Printf.sprintf
125125- "No value found for key: '%s' when unmarshalling '%s'"
126126- s
127127- sname)))
128128- in
129129- x)
130130- }
131131- | _ -> Error (`Msg (Printf.sprintf "Expecting Rpc.Dict when unmarshalling a '%s'" sname)))
7171+ | Option t -> (
7272+ match v with
7373+ | Enum [ x ] -> unmarshal t x >>= fun x -> return (Some x)
7474+ | Enum [] -> return None
7575+ | y ->
7676+ Error
7777+ (`Msg
7878+ (Printf.sprintf "Expecting an Enum value, got '%s'"
7979+ (Rpc.to_string y))))
8080+ | Tuple (t1, t2) -> (
8181+ match (v, t2) with
8282+ | Rpc.Enum list, Tuple (_, _) ->
8383+ unmarshal t1 (List.hd list) >>= fun v1 ->
8484+ unmarshal t2 (Rpc.Enum (List.tl list)) >>= fun v2 -> Ok (v1, v2)
8585+ | Rpc.Enum [ x; y ], _ ->
8686+ unmarshal t1 x >>= fun v1 ->
8787+ unmarshal t2 y >>= fun v2 -> Ok (v1, v2)
8888+ | Rpc.Enum _, _ -> Error (`Msg "Too many items in a tuple!")
8989+ | _, _ -> Error (`Msg "Expecting Rpc.Enum when unmarshalling a tuple"))
9090+ | Tuple3 (t1, t2, t3) -> (
9191+ match v with
9292+ | Rpc.Enum [ x; y; z ] ->
9393+ unmarshal t1 x >>= fun v1 ->
9494+ unmarshal t2 y >>= fun v2 ->
9595+ unmarshal t3 z >>= fun v3 -> Ok (v1, v2, v3)
9696+ | Rpc.Enum _ ->
9797+ Error (`Msg "Expecting precisely 3 items when unmarshalling a Tuple3")
9898+ | _ -> Error (`Msg "Expecting Rpc.Enum when unmarshalling a tuple3"))
9999+ | Tuple4 (t1, t2, t3, t4) -> (
100100+ match v with
101101+ | Rpc.Enum [ x; y; z; a ] ->
102102+ unmarshal t1 x >>= fun v1 ->
103103+ unmarshal t2 y >>= fun v2 ->
104104+ unmarshal t3 z >>= fun v3 ->
105105+ unmarshal t4 a >>= fun v4 -> Ok (v1, v2, v3, v4)
106106+ | Rpc.Enum _ ->
107107+ Error
108108+ (`Msg
109109+ "Expecting precisely 4 items in an Enum when unmarshalling a \
110110+ Tuple4")
111111+ | _ -> Error (`Msg "Expecting Rpc.Enum when unmarshalling a tuple4"))
112112+ | Struct { constructor; sname; _ } -> (
113113+ match v with
114114+ | Rpc.Dict keys' ->
115115+ let keys =
116116+ List.map (fun (s, v) -> (String.lowercase_ascii s, v)) keys'
117117+ in
118118+ constructor
119119+ {
120120+ field_get =
121121+ (let x : type a. string -> a typ -> (a, Rpc.msg) result =
122122+ fun s ty ->
123123+ let s = String.lowercase_ascii s in
124124+ match ty with
125125+ | Option x -> (
126126+ try
127127+ List.assoc s keys |> unmarshal x >>= fun o ->
128128+ return (Some o)
129129+ with _ -> return None)
130130+ | y -> (
131131+ try List.assoc s keys |> unmarshal y
132132+ with Not_found ->
133133+ Error
134134+ (`Msg
135135+ (Printf.sprintf
136136+ "No value found for key: '%s' when \
137137+ unmarshalling '%s'"
138138+ s sname)))
139139+ in
140140+ x);
141141+ }
142142+ | _ ->
143143+ Error
144144+ (`Msg
145145+ (Printf.sprintf "Expecting Rpc.Dict when unmarshalling a '%s'"
146146+ sname)))
132147 | Variant { vconstructor; _ } ->
133133- (match v with
134134- | Rpc.String name -> ok (name, Rpc.Null)
135135- | Rpc.Enum [ Rpc.String name; contents ] -> ok (name, contents)
136136- | _ -> Error (`Msg "Expecting String or Enum when unmarshalling a variant"))
137137- >>= fun (name, contents) ->
138138- let constr = { tget = (fun typ -> unmarshal typ contents) } in
139139- vconstructor name constr
148148+ (match v with
149149+ | Rpc.String name -> ok (name, Rpc.Null)
150150+ | Rpc.Enum [ Rpc.String name; contents ] -> ok (name, contents)
151151+ | _ ->
152152+ Error (`Msg "Expecting String or Enum when unmarshalling a variant"))
153153+ >>= fun (name, contents) ->
154154+ let constr = { tget = (fun typ -> unmarshal typ contents) } in
155155+ vconstructor name constr
140156 | Abstract { of_rpc; _ } -> of_rpc v
141141-142157143158let rec marshal : type a. a typ -> a -> Rpc.t =
144159 fun t v ->
···160175 | Base64 -> rpc_of_base64 v
161176 | Array typ -> Enum (tailrec_map (marshal typ) (Array.to_list v))
162177 | List (Tuple (Basic String, typ)) ->
163163- Dict (tailrec_map (fun (x, y) -> x, marshal typ y) v)
178178+ Dict (tailrec_map (fun (x, y) -> (x, marshal typ y)) v)
164179 | List typ -> Enum (tailrec_map (marshal typ) v)
165165- | Dict (String, typ) -> Rpc.Dict (tailrec_map (fun (k, v) -> k, marshal typ v) v)
180180+ | Dict (String, typ) ->
181181+ Rpc.Dict (tailrec_map (fun (k, v) -> (k, marshal typ v)) v)
166182 | Dict (basic, typ) ->
167167- Rpc.Enum
168168- (tailrec_map (fun (k, v) -> Rpc.Enum [ rpc_of_basic basic k; marshal typ v ]) v)
183183+ Rpc.Enum
184184+ (tailrec_map
185185+ (fun (k, v) -> Rpc.Enum [ rpc_of_basic basic k; marshal typ v ])
186186+ v)
169187 | Unit -> rpc_of_unit v
170188 | Option ty ->
171171- Rpc.Enum
172172- (match v with
173173- | Some x -> [ marshal ty x ]
174174- | None -> [])
175175- | Tuple (x, (Tuple (_, _) as y)) ->
176176- (match marshal y (snd v) with
177177- | Rpc.Enum xs -> Rpc.Enum (marshal x (fst v) :: xs)
178178- | _ -> failwith "Marshalling a tuple should always give an Enum")
189189+ Rpc.Enum (match v with Some x -> [ marshal ty x ] | None -> [])
190190+ | Tuple (x, (Tuple (_, _) as y)) -> (
191191+ match marshal y (snd v) with
192192+ | Rpc.Enum xs -> Rpc.Enum (marshal x (fst v) :: xs)
193193+ | _ -> failwith "Marshalling a tuple should always give an Enum")
179194 | Tuple (x, y) -> Rpc.Enum [ marshal x (fst v); marshal y (snd v) ]
180195 | Tuple3 (x, y, z) ->
181181- let vx, vy, vz = v in
182182- Rpc.Enum [ marshal x vx; marshal y vy; marshal z vz ]
196196+ let vx, vy, vz = v in
197197+ Rpc.Enum [ marshal x vx; marshal y vy; marshal z vz ]
183198 | Tuple4 (x, y, z, a) ->
184184- let vx, vy, vz, va = v in
185185- Rpc.Enum [ marshal x vx; marshal y vy; marshal z vz; marshal a va ]
199199+ let vx, vy, vz, va = v in
200200+ Rpc.Enum [ marshal x vx; marshal y vy; marshal z vz; marshal a va ]
186201 | Struct { fields; _ } ->
187187- let fields =
202202+ let fields =
203203+ List.fold_left
204204+ (fun acc f ->
205205+ match f with
206206+ | BoxedField f -> (
207207+ let value = marshal f.field (f.fget v) in
208208+ match (f.field, value) with
209209+ | Option _, Rpc.Enum [] -> acc
210210+ | Option _, Rpc.Enum [ x ] -> (f.fname, x) :: acc
211211+ | _, _ -> (f.fname, value) :: acc))
212212+ [] fields
213213+ in
214214+ Rpc.Dict fields
215215+ | Variant { variants; _ } ->
188216 List.fold_left
189189- (fun acc f ->
190190- match f with
191191- | BoxedField f ->
192192- let value = marshal f.field (f.fget v) in
193193- (match f.field, value with
194194- | Option _, Rpc.Enum [] -> acc
195195- | Option _, Rpc.Enum [ x ] -> (f.fname, x) :: acc
196196- | _, _ -> (f.fname, value) :: acc))
197197- []
198198- fields
199199- in
200200- Rpc.Dict fields
201201- | Variant { variants; _ } ->
202202- List.fold_left
203203- (fun acc t ->
204204- match t with
205205- | BoxedTag t ->
206206- (match t.tpreview v with
207207- | Some x ->
208208- (match marshal t.tcontents x with
209209- | Rpc.Null -> Rpc.String t.tname
210210- | y -> Rpc.Enum [ Rpc.String t.tname; y ])
211211- | None -> acc))
212212- Rpc.Null
213213- variants
217217+ (fun acc t ->
218218+ match t with
219219+ | BoxedTag t -> (
220220+ match t.tpreview v with
221221+ | Some x -> (
222222+ match marshal t.tcontents x with
223223+ | Rpc.Null -> Rpc.String t.tname
224224+ | y -> Rpc.Enum [ Rpc.String t.tname; y ])
225225+ | None -> acc))
226226+ Rpc.Null variants
214227 | Abstract { rpc_of; _ } -> rpc_of v
215228216216-217229let ocaml_of_basic : type a. a basic -> string = function
218230 | Int64 -> "int64"
219231 | Int32 -> "int32"
···223235 | Bool -> "bool"
224236 | Char -> "char"
225237226226-227238let rec ocaml_of_t : type a. a typ -> string = function
228239 | Basic b -> ocaml_of_basic b
229240 | DateTime -> "string"
230241 | Base64 -> "base64"
231242 | Array t -> ocaml_of_t t ^ " list"
232243 | List t -> ocaml_of_t t ^ " list"
233233- | Dict (b, t) -> Printf.sprintf "(%s * %s) list" (ocaml_of_basic b) (ocaml_of_t t)
244244+ | Dict (b, t) ->
245245+ Printf.sprintf "(%s * %s) list" (ocaml_of_basic b) (ocaml_of_t t)
234246 | Unit -> "unit"
235247 | Option t -> ocaml_of_t t ^ " option"
236248 | Tuple (a, b) -> Printf.sprintf "(%s * %s)" (ocaml_of_t a) (ocaml_of_t b)
237249 | Tuple3 (a, b, c) ->
238238- Printf.sprintf "(%s * %s * %s)" (ocaml_of_t a) (ocaml_of_t b) (ocaml_of_t c)
250250+ Printf.sprintf "(%s * %s * %s)" (ocaml_of_t a) (ocaml_of_t b)
251251+ (ocaml_of_t c)
239252 | Tuple4 (a, b, c, d) ->
240240- Printf.sprintf
241241- "(%s * %s * %s * %s)"
242242- (ocaml_of_t a)
243243- (ocaml_of_t b)
244244- (ocaml_of_t c)
245245- (ocaml_of_t d)
253253+ Printf.sprintf "(%s * %s * %s * %s)" (ocaml_of_t a) (ocaml_of_t b)
254254+ (ocaml_of_t c) (ocaml_of_t d)
246255 | Struct { fields; _ } ->
247247- let fields =
248248- List.map
249249- (function
250250- | BoxedField f -> Printf.sprintf "%s: %s;" f.fname (ocaml_of_t f.field))
251251- fields
252252- in
253253- Printf.sprintf "{ %s }" (String.concat " " fields)
256256+ let fields =
257257+ List.map
258258+ (function
259259+ | BoxedField f ->
260260+ Printf.sprintf "%s: %s;" f.fname (ocaml_of_t f.field))
261261+ fields
262262+ in
263263+ Printf.sprintf "{ %s }" (String.concat " " fields)
254264 | Variant { variants; _ } ->
255255- let tags =
256256- List.map
257257- (function
258258- | BoxedTag t ->
259259- Printf.sprintf
260260- "| %s (%s) (** %s *)"
261261- t.tname
262262- (ocaml_of_t t.tcontents)
263263- (String.concat " " t.tdescription))
264264- variants
265265- in
266266- String.concat " " tags
265265+ let tags =
266266+ List.map
267267+ (function
268268+ | BoxedTag t ->
269269+ Printf.sprintf "| %s (%s) (** %s *)" t.tname
270270+ (ocaml_of_t t.tcontents)
271271+ (String.concat " " t.tdescription))
272272+ variants
273273+ in
274274+ String.concat " " tags
267275 | Abstract _ -> "<abstract>"
+69-40
idl/toplevel_api.ml
···33open Rpc
44open Idl
5566-(** An area to be highlighted *)
77-type highlight =
88- { line1 : int
99- ; line2 : int
1010- ; col1 : int
1111- ; col2 : int
1212- }
66+type highlight = { line1 : int; line2 : int; col1 : int; col2 : int }
137[@@deriving rpcty]
88+(** An area to be highlighted *)
1491515-(** Represents the result of executing a toplevel phrase *)
1616-type exec_result =
1717- { stdout : string option
1818- ; stderr : string option
1919- ; sharp_ppf : string option
2020- ; caml_ppf : string option
2121- ; highlight : highlight option
2222- }
1010+type exec_result = {
1111+ stdout : string option;
1212+ stderr : string option;
1313+ sharp_ppf : string option;
1414+ caml_ppf : string option;
1515+ highlight : highlight option;
1616+}
2317[@@deriving rpcty]
1818+(** Represents the result of executing a toplevel phrase *)
24192525-(** The result returned by a 'complete' call. *)
2626-type completion_result =
2727- { n : int
2828- (** The position in the input string from where the completions may be
2020+type completion_result = {
2121+ n : int;
2222+ (** The position in the input string from where the completions may be
2923 inserted *)
3030- ; completions : string list (** The list of possible completions *)
3131- }
2424+ completions : string list; (** The list of possible completions *)
2525+}
3226[@@deriving rpcty]
2727+(** The result returned by a 'complete' call. *)
2828+2929+type string_list = string list [@@deriving rpcty]
3030+(** Used by setup *)
3131+3232+type string_string_list = (string * string) list [@@deriving rpcty]
3333+(** Used by setup *)
33343435(** For now we are only using a simple error type *)
3536type err = InternalError of string [@@deriving rpcty]
···49505051 let description =
5152 Interface.
5252- { name = "Toplevel"
5353- ; namespace = None
5454- ; description =
5555- [ "Functions for manipulating the toplevel worker thread" ]
5656- ; version = 1, 0, 0
5353+ {
5454+ name = "Toplevel";
5555+ namespace = None;
5656+ description =
5757+ [ "Functions for manipulating the toplevel worker thread" ];
5858+ version = (1, 0, 0);
5759 }
58605961 let implementation = implement description
···66686769 let completion_p = Param.mk completion_result
68707171+ let cmas =
7272+ Param.mk ~name:"cmas"
7373+ ~description:
7474+ [
7575+ "A list of pairs. The first element of the pair is a urls to a";
7676+ "cma file pre-compiled to javascript. The second item is the";
7777+ "name of the function to be invoked to load the cma file";
7878+ "(ie, the cma was compiled with --wrap-func).";
7979+ "These will be loaded synchronously during the init call.";
8080+ ]
8181+ string_string_list
8282+8383+ let cmis =
8484+ Param.mk ~name:"cmis"
8585+ ~description:
8686+ [
8787+ "A list of urls of cmi files. These files will be loaded on demand";
8888+ "during evaluation of toplevel phrases.";
8989+ ]
9090+ string_list
9191+9292+ let init =
9393+ declare "init"
9494+ [ "Initialise the toplevel." ]
9595+ (cmas @-> cmis @-> returning unit_p err)
9696+6997 let setup =
7070- declare
7171- "setup"
7272- [ "Initialise the toplevel. Return value is the initial blurb "
7373- ; "printed when starting a toplevel."
9898+ declare "setup"
9999+ [
100100+ "Start the toplevel. Return value is the initial blurb ";
101101+ "printed when starting a toplevel. Note that the toplevel";
102102+ "must be initialised first.";
74103 ]
75104 (unit_p @-> returning exec_result_p err)
7610577106 let exec =
7878- declare
7979- "exec"
8080- [ "Execute a phrase using the toplevel. The toplevel must have been"
8181- ; "Initialised first."
107107+ declare "exec"
108108+ [
109109+ "Execute a phrase using the toplevel. The toplevel must have been";
110110+ "Initialised first.";
82111 ]
83112 (phrase_p @-> returning exec_result_p err)
8411385114 let complete =
8686- declare
8787- "complete"
8888- [ "Find completions of the incomplete phrase. Completion occurs at the"
8989- ; "end of the phrase passed in. If completion is required at a point"
9090- ; "other than the end of a string, then take the substring before calling"
9191- ; "this API."
115115+ declare "complete"
116116+ [
117117+ "Find completions of the incomplete phrase. Completion occurs at the";
118118+ "end of the phrase passed in. If completion is required at a point";
119119+ "other than the end of a string, then take the substring before calling";
120120+ "this API.";
92121 ]
93122 (phrase_p @-> returning completion_p err)
94123end
+54-7
idl/toplevel_api_gen.ml
···2222 line1: int ;
2323 line2: int ;
2424 col1: int ;
2525- col2: int }[@@ocaml.doc " An area to be highlighted "][@@deriving rpcty]
2525+ col2: int }[@@deriving rpcty][@@ocaml.doc " An area to be highlighted "]
2626include
2727 struct
2828 let _ = fun (_ : highlight) -> ()
···122122 stderr: string option ;
123123 sharp_ppf: string option ;
124124 caml_ppf: string option ;
125125- highlight: highlight option }[@@ocaml.doc
126126- " Represents the result of executing a toplevel phrase "]
127127-[@@deriving rpcty]
125125+ highlight: highlight option }[@@deriving rpcty][@@ocaml.doc
126126+ " Represents the result of executing a toplevel phrase "]
128127include
129128 struct
130129 let _ = fun (_ : exec_result) -> ()
···254253 [@ocaml.doc
255254 " The position in the input string from where the completions may be\n inserted "];
256255 completions: string list [@ocaml.doc " The list of possible completions "]}
257257-[@@ocaml.doc " The result returned by a 'complete' call. "][@@deriving rpcty]
256256+[@@deriving rpcty][@@ocaml.doc " The result returned by a 'complete' call. "]
258257include
259258 struct
260259 let _ = fun (_ : completion_result) -> ()
···318317 and _ = typ_of_completion_result
319318 and _ = completion_result
320319 end[@@ocaml.doc "@inline"][@@merlin.hide ]
320320+type string_list = string list[@@deriving rpcty][@@ocaml.doc
321321+ " Used by setup "]
322322+include
323323+ struct
324324+ let _ = fun (_ : string_list) -> ()
325325+ let rec typ_of_string_list =
326326+ Rpc.Types.List (let open Rpc.Types in Basic String)
327327+ and string_list =
328328+ {
329329+ Rpc.Types.name = "string_list";
330330+ Rpc.Types.description = ["Used by setup"];
331331+ Rpc.Types.ty = typ_of_string_list
332332+ }
333333+ let _ = typ_of_string_list
334334+ and _ = string_list
335335+ end[@@ocaml.doc "@inline"][@@merlin.hide ]
336336+type string_string_list = (string * string) list[@@deriving rpcty][@@ocaml.doc
337337+ " Used by setup "]
338338+include
339339+ struct
340340+ let _ = fun (_ : string_string_list) -> ()
341341+ let rec typ_of_string_string_list =
342342+ Rpc.Types.Dict (Rpc.Types.String, (let open Rpc.Types in Basic String))
343343+ and string_string_list =
344344+ {
345345+ Rpc.Types.name = "string_string_list";
346346+ Rpc.Types.description = ["Used by setup"];
347347+ Rpc.Types.ty = typ_of_string_string_list
348348+ }
349349+ let _ = typ_of_string_string_list
350350+ and _ = string_string_list
351351+ end[@@ocaml.doc "@inline"][@@merlin.hide ]
321352type err =
322353 | InternalError of string [@@ocaml.doc
323354 " For now we are only using a simple error type "]
···391422 let phrase_p = Param.mk Types.string
392423 let exec_result_p = Param.mk exec_result
393424 let completion_p = Param.mk completion_result
425425+ let cmas =
426426+ Param.mk ~name:"cmas"
427427+ ~description:["A list of pairs. The first element of the pair is a urls to a";
428428+ "cma file pre-compiled to javascript. The second item is the";
429429+ "name of the function to be invoked to load the cma file";
430430+ "(ie, the cma was compiled with --wrap-func).";
431431+ "These will be loaded synchronously during the init call."]
432432+ string_string_list
433433+ let cmis =
434434+ Param.mk ~name:"cmis"
435435+ ~description:["A list of urls of cmi files. These files will be loaded on demand";
436436+ "during evaluation of toplevel phrases."] string_list
437437+ let init =
438438+ declare "init" ["Initialise the toplevel."]
439439+ (cmas @-> (cmis @-> (returning unit_p err)))
394440 let setup =
395441 declare "setup"
396396- ["Initialise the toplevel. Return value is the initial blurb ";
397397- "printed when starting a toplevel."]
442442+ ["Start the toplevel. Return value is the initial blurb ";
443443+ "printed when starting a toplevel. Note that the toplevel";
444444+ "must be initialised first."]
398445 (unit_p @-> (returning exec_result_p err))
399446 let exec =
400447 declare "exec"
···1818val add_keyword : string -> unit
1919(** Add a new OCaml keyword. *)
20202121+type location = int * int
2122(** Type of a string-location. It is composed of a start and stop offsets (in
2223 bytes). *)
2323-type location = int * int
24242525(** Result of a function processing a programx. *)
2626type 'a result =
···2929 (** The function failed. Arguments are a list of locations to highlight in
3030 the source and an error message. *)
31313232-(** Exception raised by a parser when it need more data. *)
3332exception Need_more
3333+(** Exception raised by a parser when it need more data. *)
34343535-val parse_toplevel_phrase
3636- : (string -> bool -> Parsetree.toplevel_phrase result) ref
3535+val parse_toplevel_phrase :
3636+ (string -> bool -> Parsetree.toplevel_phrase result) ref
3737(** [parse_toplevel_phrase] is the function used to parse a phrase typed in the
3838 toplevel.
3939···50505151 Except for {!Need_more}, the function must not raise any exception. *)
52525353-val parse_toplevel_phrase_default
5454- : string
5555- -> bool
5656- -> Parsetree.toplevel_phrase result
5353+val parse_toplevel_phrase_default :
5454+ string -> bool -> Parsetree.toplevel_phrase result
5755(** The default parser for toplevel phrases. It uses the standard ocaml parser. *)
58565957val parse_default : (Lexing.lexbuf -> 'a) -> string -> bool -> 'a result
+2-4
lib/uTop_complete.mli
···991010(** OCaml completion. *)
11111212-val complete
1313- : phrase_terminator:string
1414- -> input:string
1515- -> int * (string * string) list
1212+val complete :
1313+ phrase_terminator:string -> input:string -> int * (string * string) list
1614(** [complete ~phrase_terminator ~input] returns the start of the completed word
1715 in [input] and the list of possible completions with their suffixes. *)
1816
+14-16
lib/uTop_token.ml
···1212 The type of tokens is semi-structured: parentheses construct and quotations
1313 are nested and others tokens are flat list. *)
14141515+type location = {
1616+ idx1 : int; (** Start position in unicode characters. *)
1717+ idx2 : int; (** Stop position in unicode characters. *)
1818+ ofs1 : int; (** Start position in bytes. *)
1919+ ofs2 : int; (** Stop position in bytes. *)
2020+}
1521(** Locations in the source string, which is encoded in UTF-8. *)
1616-type location =
1717- { idx1 : int (** Start position in unicode characters. *)
1818- ; idx2 : int (** Stop position in unicode characters. *)
1919- ; ofs1 : int (** Start position in bytes. *)
2020- ; ofs2 : int (** Stop position in bytes. *)
2121- }
22222323type t =
2424 | Symbol of string
···3737 | Comment_reg (** Regular comment. *)
3838 | Comment_doc (** Documentation comment. *)
39394040-and quotation_item =
4141- | Quot_data
4242- | Quot_anti of antiquotation
4040+and quotation_item = Quot_data | Quot_anti of antiquotation
43414444-and antiquotation =
4545- { a_opening : location (** Location of the opening [$]. *)
4646- ; a_closing : location option (** Location of the closing [$]. *)
4747- ; a_name : (location * location) option
4848- (** Location of the name and colon if any. *)
4949- ; a_contents : (t * location) list (** Contents of the location. *)
5050- }
4242+and antiquotation = {
4343+ a_opening : location; (** Location of the opening [$]. *)
4444+ a_closing : location option; (** Location of the closing [$]. *)
4545+ a_name : (location * location) option;
4646+ (** Location of the name and colon if any. *)
4747+ a_contents : (t * location) list; (** Contents of the location. *)
4848+}
+112-103
lib/worker.ml
···1717 if cur - beg > 0 then [ String.sub p beg (cur - beg) ] else []
1818 else if sep p.[cur] then
1919 String.sub p beg (cur - beg) :: split (cur + 1) (cur + 1)
2020- else
2121- split beg (cur + 1)
2020+ else split beg (cur + 1)
2221 in
2322 split 0 0
2423···2625 match
2726 split_char ~sep:(function '+' | '-' | '~' -> true | _ -> false) v
2827 with
2929- | [] ->
3030- assert false
2828+ | [] -> assert false
3129 | x :: _ ->
3232- List.map
3333- int_of_string
3434- (split_char ~sep:(function '.' -> true | _ -> false) x)
3030+ List.map int_of_string
3131+ (split_char ~sep:(function '.' -> true | _ -> false) x)
35323633 let current = split Sys.ocaml_version
37343835 let compint (a : int) b = compare a b
39364037 let rec compare v v' =
4141- match v, v' with
4242- | [ x ], [ y ] ->
4343- compint x y
4444- | [], [] ->
4545- 0
4646- | [], y :: _ ->
4747- compint 0 y
4848- | x :: _, [] ->
4949- compint x 0
5050- | x :: xs, y :: ys ->
5151- (match compint x y with 0 -> compare xs ys | n -> n)
3838+ match (v, v') with
3939+ | [ x ], [ y ] -> compint x y
4040+ | [], [] -> 0
4141+ | [], y :: _ -> compint 0 y
4242+ | x :: _, [] -> compint x 0
4343+ | x :: xs, y :: ys -> (
4444+ match compint x y with 0 -> compare xs ys | n -> n)
5245end
53465447let exec' s =
···6255 if Version.compare Version.current [ 4; 07 ] >= 0 then exec' "open Stdlib";
6356 let header1 = Printf.sprintf " %s version %%s" "OCaml" in
6457 let header2 =
6565- Printf.sprintf
6666- " Compiled with Js_of_ocaml version %s"
5858+ Printf.sprintf " Compiled with Js_of_ocaml version %s"
6759 Js_of_ocaml.Sys_js.js_of_ocaml_version
6860 in
6961 exec' (Printf.sprintf "Format.printf \"%s@.\" Sys.ocaml_version;;" header1);
7062 exec' (Printf.sprintf "Format.printf \"%s@.\";;" header2);
7163 exec' "#enable \"pretty\";;";
7264 exec' "#disable \"shortvar\";;";
7373- Toploop.add_directive
7474- "load_js"
6565+ Toploop.add_directive "load_js"
7566 (Toploop.Directive_string
7667 (fun name -> Js_of_ocaml.Js.Unsafe.global##load_script_ name))
7768 Toploop.{ section = ""; doc = "Load a javascript script" };
···80718172let setup_printers () =
8273 exec' "let _print_unit fmt (_ : 'a) : 'a = Format.pp_print_string fmt \"()\"";
8383- Topdirs.dir_install_printer
8484- Format.std_formatter
7474+ Topdirs.dir_install_printer Format.std_formatter
8575 Longident.(Lident "_print_unit")
86768777let stdout_buff = Buffer.create 100
···123113 Format.pp_print_flush pp_result ();
124114 IdlM.ErrM.return
125115 Toplevel_api_gen.
126126- { stdout = buff_opt stdout_buff
127127- ; stderr = buff_opt stderr_buff
128128- ; sharp_ppf = buff_opt code_buff
129129- ; caml_ppf = buff_opt res_buff
130130- ; highlight = !highlighted
116116+ {
117117+ stdout = buff_opt stdout_buff;
118118+ stderr = buff_opt stderr_buff;
119119+ sharp_ppf = buff_opt code_buff;
120120+ caml_ppf = buff_opt res_buff;
121121+ highlight = !highlighted;
131122 }
132123133133-let setup functions () =
124124+let sync_get url =
125125+ let open Js_of_ocaml in
126126+ let x = XmlHttpRequest.create () in
127127+ x##.responseType := Js.string "arraybuffer";
128128+ x##_open (Js.string "GET") (Js.string url) Js._false;
129129+ x##send Js.null;
130130+ match x##.status with
131131+ | 200 ->
132132+ Js.Opt.case
133133+ (File.CoerceTo.arrayBuffer x##.response)
134134+ (fun () ->
135135+ Firebug.console##log (Js.string "Failed to receive file");
136136+ None)
137137+ (fun b -> Some (Typed_array.String.of_arrayBuffer b))
138138+ | _ -> None
139139+140140+let load_resource files =
141141+ let open Js_of_ocaml in
142142+ fun ~prefix ~path ->
143143+ Firebug.console##log
144144+ (Js.string
145145+ (Printf.sprintf "here we are, loading prefix=%s path=%s" prefix path));
146146+ (* let abs_filename = Filename.concat prefix path in *)
147147+ if List.mem_assoc path files then (
148148+ Firebug.console##log (Js.string "path is in files");
149149+ let f = sync_get (List.assoc path files) in
150150+ match f with
151151+ | Some content ->
152152+ Firebug.console##log
153153+ (Js.string
154154+ (Printf.sprintf "Got result (length=%d)" (String.length content)));
155155+ (* Sys_js.update_file ~name:abs_filename ~content; *)
156156+ Some content
157157+ | None -> None)
158158+ else (
159159+ Firebug.console##log (Js.string "path is NOT in files");
160160+ None)
161161+162162+let functions : (unit -> unit) list option ref = ref None
163163+164164+let init cmas cmis =
165165+ let open Js_of_ocaml in
166166+ try
167167+ Clflags.no_check_prims := true;
168168+ let cmi_files = List.map (fun cmi -> (Filename.basename cmi, cmi)) cmis in
169169+ Sys_js.mount ~path:"/dynamic/cmis" (load_resource cmi_files);
170170+ List.iter
171171+ (fun (path, _) -> Sys_js.register_lazy ("/dynamic/cmis/" ^ path))
172172+ cmi_files;
173173+ Topdirs.dir_directory "/dynamic/cmis";
174174+ Js_of_ocaml.Worker.import_scripts (List.map fst cmas);
175175+ functions :=
176176+ Some
177177+ (List.map
178178+ (fun func_name ->
179179+ Firebug.console##log (Js.string ("Function: " ^ func_name));
180180+ let func = Js.Unsafe.js_expr func_name in
181181+ fun () ->
182182+ Js.Unsafe.fun_call func [| Js.Unsafe.inject Dom_html.window |])
183183+ (List.map snd cmas));
184184+ IdlM.ErrM.return ()
185185+ with e ->
186186+ IdlM.ErrM.return_err (Toplevel_api_gen.InternalError (Printexc.to_string e))
187187+188188+let setup () =
189189+ let open Js_of_ocaml in
134190 try
135135- Js_of_ocaml.Sys_js.set_channel_flusher stdout (Buffer.add_string stdout_buff);
136136- Js_of_ocaml.Sys_js.set_channel_flusher stderr (Buffer.add_string stderr_buff);
137137- setup functions ();
191191+ Sys_js.set_channel_flusher stdout
192192+ (Buffer.add_string stdout_buff);
193193+ Sys_js.set_channel_flusher stderr
194194+ (Buffer.add_string stderr_buff);
195195+ (match !functions with
196196+ | Some l -> setup l ()
197197+ | None -> failwith "Error: toplevel has not been initialised");
138198 setup_printers ();
139199 IdlM.ErrM.return
140200 Toplevel_api_gen.
141141- { stdout = buff_opt stdout_buff
142142- ; stderr = buff_opt stderr_buff
143143- ; sharp_ppf = None
144144- ; caml_ppf = None
145145- ; highlight = None
201201+ {
202202+ stdout = buff_opt stdout_buff;
203203+ stderr = buff_opt stderr_buff;
204204+ sharp_ppf = None;
205205+ caml_ppf = None;
206206+ highlight = None;
146207 }
147208 with e ->
148209 IdlM.ErrM.return_err (Toplevel_api_gen.InternalError (Printexc.to_string e))
···151212 let contains_double_underscore s =
152213 let len = String.length s in
153214 let rec aux i =
154154- if i > len - 2 then
155155- false
156156- else if s.[i] = '_' && s.[i + 1] = '_' then
157157- true
158158- else
159159- aux (i + 1)
215215+ if i > len - 2 then false
216216+ else if s.[i] = '_' && s.[i + 1] = '_' then true
217217+ else aux (i + 1)
160218 in
161219 aux 0
162220 in
···169227170228let server process e =
171229 let call : Rpc.call = Marshal.from_bytes e 0 in
172172- M.bind (process call) (fun response -> Js_of_ocaml.Worker.post_message (Marshal.to_string response []));
230230+ M.bind (process call) (fun response ->
231231+ Js_of_ocaml.Worker.post_message (Marshal.to_string response []));
173232 ()
174233175175- let sync_get url =
176176- let open Js_of_ocaml in
177177- let x = XmlHttpRequest.create () in
178178- x##.responseType := (Js.string "arraybuffer");
179179- x##_open (Js.string "GET") (Js.string url) Js._false;
180180- x##send Js.null;
181181- match x##.status with
182182- | 200 ->
183183- Js.Opt.case
184184- (File.CoerceTo.arrayBuffer x##.response)
185185- (fun () ->
186186- Firebug.console##log (Js.string "Failed to receive file");
187187- None)
188188- (fun b ->
189189- Some (Typed_array.String.of_arrayBuffer b))
190190- | _ ->
191191- None
192192-193193-let load_resource files =
194194- let open Js_of_ocaml in
195195- fun ~prefix ~path ->
196196- Firebug.console##log (Js.string (Printf.sprintf "here we are, loading prefix=%s path=%s" prefix path));
197197- (* let abs_filename = Filename.concat prefix path in *)
198198- if List.mem_assoc path files
199199- then begin
200200- Firebug.console##log (Js.string "path is in files");
201201- let f = sync_get (List.assoc path files) in
202202- match f with
203203- | Some content ->
204204- Firebug.console##log (Js.string (Printf.sprintf "Got result (length=%d)" (String.length content)));
205205- (* Sys_js.update_file ~name:abs_filename ~content; *)
206206- Some content
207207- | None ->
208208- None
209209- end
210210- else
211211- (Firebug.console##log (Js.string "path is NOT in files");
212212- None)
213213-214214-let run files cmis functions =
234234+let run () =
215235 (* Here we bind the server stub functions to the implementations *)
216236 let open Js_of_ocaml in
217237 try
218218- Js_top_worker_rpc.Idl.logfn := (fun s -> Js_of_ocaml.(Firebug.console##log ( s)));
219219- ignore cmis;
220220- Clflags.no_check_prims := true;
221221- let cmi_files = List.map (fun cmi ->
222222- (Filename.basename cmi, cmi)) cmis in
223223- Sys_js.mount ~path:"/dynamic/cmis" (load_resource cmi_files);
224224- List.iter (fun (path, _) -> Sys_js.register_lazy ("/dynamic/cmis/" ^ path)) cmi_files;
225225- Topdirs.dir_directory "/dynamic/cmis";
226226- Js_of_ocaml.Worker.import_scripts files;
227227- let functions = List.map (fun func_name ->
228228- Firebug.console##log (Js.string ("Function: " ^ func_name ));
229229- let func = Js.Unsafe.js_expr func_name in
230230- fun () -> Js.Unsafe.fun_call func [| Js.Unsafe.inject Dom_html.window |])
231231- functions in
238238+ (Js_top_worker_rpc.Idl.logfn :=
239239+ fun s -> Js_of_ocaml.(Firebug.console##log s));
232240 Server.complete complete;
233241 Server.exec execute;
234234- Server.setup (setup functions);
242242+ Server.setup setup;
243243+ Server.init init;
235244 let rpc_fn = IdlM.server Server.implementation in
236245 Js_of_ocaml.Worker.set_onmessage (server rpc_fn);
237237- Firebug.console##log (Js.string "All finished");
238238- with e ->
239239- Firebug.console##log (Js.string ("Exception: " ^ Printexc.to_string e))
246246+ Firebug.console##log (Js.string "All finished")
247247+ with e ->
248248+ Firebug.console##log (Js.string ("Exception: " ^ Printexc.to_string e))