text
stringlengths
12
786k
let int64_to_jv v = Jv . of_jv_array Jv . [ | of_int 255 ; get v " lo " ; get v " mi " ; get v " hi " ] |
let int64_of_jv v = int64_lo_mi_hi ( Jv . to_int ( Jv . Jarray . get v 1 ) ) ( Jv . to_int ( Jv . Jarray . get v 2 ) ) ( Jv . to_int ( Jv . Jarray . get v 3 ) )
let encode_ocaml_value v = let string = Jv . get ( Jv . repr " " ) " constructor " in let int64 = Jv . get ( Jv . repr 1L ) " constructor " in let replacer _key v = if Jv . instanceof v ~ cons : string then string_to_jsbytes v else if Jv . instanceof v ~ cons : int64 then int64_to_jv v else v in Jv . to_jstr ( Jv . call json " stringify " Jv . [ | repr v ; repr replacer ] ) |
let decode_unsafe_ocaml_value s = let jsarray = Jv . get ( Jv . repr ( Jv . Jarray . create 0 ) ) " constructor " in let reviver _key v = if Jstr . equal ( Jv . typeof v ) ( Jstr . v " string " ) then string_of_jsbytes v else if Jv . instanceof v ~ cons : jsarray && Jv . Jarray . length v == 4 && Jv . to_int ( Jv . Jarray . get v 0 ) = 255 then ( int64_of_jv v ) else v in Obj . magic ( Jv . call json " parse " Jv . [ | of_jstr s ; repr reviver ] ) |
module Store = struct type scope = [ ` Session | ` Persist ] let scope_store = function | ` Session -> Storage . session G . window | ` Persist -> Storage . local G . window type ' a key = Jstr . t let key_prefix = Jstr . v " k " let key = let id = ref ( - 1 ) in fun ? ns ( ) -> id := ! id + 1 ; let id = Jstr . of_int ! id in match ns with | None -> Jstr . ( key_prefix + id ) | Some ns -> Jstr . ( ns + v " " - + key_prefix + id ) let version = key ~ ns ( : Jstr . v " brr " ) ( ) let mem ( ? scope = ` Persist ) k = Storage . get_item ( scope_store scope ) k <> None let add ( ? scope = ` Persist ) k v = ( Storage . set_item ( scope_store scope ) k ( encode_ocaml_value v ) ) |> Console . log_if_error ~ use ( ) : let rem ( ? scope = ` Persist ) k = Storage . remove_item ( scope_store scope ) k let find ( ? scope = ` Persist ) k = match Storage . get_item ( scope_store scope ) k with | None -> None | Some v -> Some ( decode_unsafe_ocaml_value v ) let get ( ? scope = ` Persist ) ? absent k = let absent ( ) = match absent with | None -> invalid_arg " key unbound " | Some v -> v in match Storage . get_item ( scope_store scope ) k with | None -> absent ( ) | Some v -> decode_unsafe_ocaml_value v let clear ( ? scope = ` Persist ) ( ) = Storage . clear ( scope_store scope ) let force_version ( ? scope = ` Persist ) v = match find ~ scope version with | None -> add ~ scope version v | Some sv -> Console . log ( [ v ; sv ] ) ; if v <> sv then ( clear ~ scope ( ) ; add ~ scope version v ) let storage = Ev . Type . void ( Jstr . v " storage " ) let ev = Evr . on_target storage ( fun _ -> ( ) ) ( Window . as_target G . window ) end
eval : Jstr . t -> Jstr . t ; use : Jstr . t -> Jstr . t . t ; } The type [ t ] is how we interact with it after we found , either in the current global context or in another one in case of the web extension . ) *
type t = { version : int ; ocaml_version : Jstr . t ; jsoo_version : Jstr . t ; eval : Jstr . t -> Brr . Json . t Fut . or_error ; use : Jstr . t -> Brr . Json . t Fut . or_error ; }
let version p = p . version
let ocaml_version p = p . ocaml_version
let jsoo_version p = p . jsoo_version
let eval p = p . eval
let use p = p . use
let err_version version = Jstr . ( v " Page poke version mismatch . Should be v " + of_int poke_version + v " but found v " + of_int version + v " . \ n \ nTry to upgrade the OCaml console web extension to the \ latest version . " )
let err_miss_prop p = Jstr . ( v " Page poke property ocaml_poke . " + v p + v " is missing . " )
let find ( ) = match Jv . find Jv . global " ocaml_poke " with try let get p o = match Jv . find o p with | None -> Jv . throw ( err_miss_prop p ) | Some v -> v in let version = Jv . to_int ( get " version " o ) in if version > poke_version then Jv . throw ( err_version version ) else let ocaml_version = Jv . to_jstr ( get " ocaml_version " o ) in let jsoo_version = Jv . to_jstr ( get " jsoo_version " o ) in let eval = get " eval " o in let eval s = try Fut . ok ( Jv . apply eval [ | Jv . of_jstr s ] ) | with | Jv . Error e -> Fut . error e in let use = get " use " o in let use s = try Fut . ok ( Jv . apply use [ | Jv . of_jstr s ] ) | with | Jv . Error e -> Fut . error e in let ( ) = ignore ( Jv . apply ( get " init " o ) [ ] ) || in Fut . ok ( Some { version ; ocaml_version ; jsoo_version ; eval ; use } ) with Jv . Error e -> Fut . error e
let find_eval ' d ~ eval : js_eval = let open Fut . Result_syntax in let * undef = js_eval ( Jstr . v " globalThis . ocaml_poke == undefined " ) in if Jv . to_bool undef then Fut . ok None else let get to_t prop = let * v = js_eval Jstr . ( v " ocaml_poke . " + v prop ) in match Jv . to_option to_t v with | None -> Fut . error ( Jv . Error . v ( err_miss_prop prop ) ) | Some v -> Fut . ok v in let * version = get Jv . to_int " version " in if version > poke_version then Fut . error ( Jv . Error . v ( err_version version ) ) else let * ocaml_version = get Jv . to_jstr " ocaml_version " in let * jsoo_version = get Jv . to_jstr " jsoo_version " in let eval s = let ocaml = Brr . Json . encode ( Jv . of_jstr s ) in let expr = Jstr . ( v " ocaml_poke . eval ( " + ocaml + Jstr . v " ) " ) in ( js_eval expr ) in let use s = let ocaml = Brr . Json . encode ( Jv . of_jstr s ) in let expr = Jstr . ( v " ocaml_poke . use ( " + ocaml + Jstr . v " ) " ) in ( js_eval expr ) in let * unit = js_eval ( Jstr . v " ocaml_poke . init ( ) " ) in Fut . ok ( Some { version ; ocaml_version ; jsoo_version ; eval ; use } )
module Store = struct type t = { get : Jstr . t -> Jstr . t option Fut . or_error ; set : Jstr . t -> Jstr . t -> unit Fut . or_error } let create ~ get ~ set = { get ; set } let get s = s . get let set s = s . set let key_prefix = Jstr . v " ocaml - repl " - let page ( ? key_prefix = key_prefix ) store = let key k = Jstr . ( key_prefix + k ) in let get k = Fut . ok ( Storage . get_item store ( key k ) ) in let set k v = Fut . return ( Storage . set_item store ( key k ) v ) in { get ; set } let webext ( ? key_prefix = key_prefix ) ( ) = match Jv . find_path Jv . global [ " chrome " ; " storage " ; " local " ] with | None -> let err ( ) = Jv . throw ( Jstr . v " chrome . storage . local is undefined " ) in let get k = err ( ) and set k v = err ( ) in create ~ get ~ set | Some s -> let get k = let fut , set_fut = Fut . create ( ) in let result r = set_fut ( Ok ( Jv . find_map ' Jv . to_jstr r k ) ) in ignore @@ Jv . call s " get " [ | Jv . of_jstr_list [ k ] ; Jv . repr result ] ; | fut in let set k v = let fut , set_fut = Fut . create ( ) in let o = Jv . obj ' [ | k , Jv . of_jstr v ] | in let result r = set_fut ( Ok ( ) ) in ignore @@ Jv . call s " set " [ | o ; Jv . repr result ] ; | fut in create ~ get ~ set end
module History = struct type t = { prev : Jstr . t list ; focus : Jstr . t ; next : Jstr . t list ; } let v ~ prev = let add acc e = let e = Jstr . trim e in if Jstr . is_empty e then acc else e :: acc in let focus = Jstr . empty in { prev = List . rev ( List . fold_left add [ ] prev ) ; focus ; next = [ ] } let empty = v ~ prev [ ] : let push e es = if Jstr . is_empty e then es else match es with | e ' :: _ when Jstr . equal e e ' -> es | es -> e :: es let entries h = let next = List . filter ( fun s -> not ( Jstr . is_empty s ) ) h . next in List . rev_append ( push h . focus next ) h . prev let add h e = let e = Jstr . trim e in if Jstr . is_empty e then h else v ~ prev ( : push e ( entries h ) ) let restart h = v ~ prev ( : entries h ) let prev h current = match h . prev with | [ ] -> None | p :: ps -> let next = push ( Jstr . trim current ) ( push h . focus h . next ) in let next = if next = [ ] then [ Jstr . empty ] else next in Some ( { prev = ps ; focus = p ; next ; } , p ) let next h current = match h . next with | [ ] -> None | n :: ns -> let prev = push ( Jstr . trim current ) ( push h . focus h . prev ) in Some ( { prev ; focus = n ; next = ns } , n ) let sep sep = Jstr . ( nl + sep + nl ) let to_string ~ sep : s h = Jstr . concat ~ sep ( : sep s ) ( entries h ) let of_string ~ sep : s hs = v ~ prev ( : List . map Jstr . trim ( Jstr . cuts ~ sep ( : sep s ) hs ) ) end
module Highlight = struct let el e = match Jv . find Jv . global " hljs " with | None -> e | Some hljs -> ignore @@ Jv . call hljs " highlightBlock " [ | El . to_jv e ] ; | e end
module Text_input : sig type t val create : ? lang : Jstr . t -> prompt : El . t -> unit -> t val el : t -> El . t val input : t -> El . t val hide : t -> unit val show : t -> unit val set_has_focus : bool -> t -> unit val cursor_pos : t -> int option val set : t -> Jstr . t -> unit val get : t -> Jstr . t val update_input : t -> unit type t = { input : El . t ; overlay : El . t ; el : El . t ; } let hide t = El . set_inline_style El . Style . display ( Jstr . v " none " ) t . el let show t = El . set_inline_style El . Style . display ( Jstr . v " grid " ) t . el let create ? lang ( : plang = Jstr . v " ocaml " ) ~ prompt ( ) = let highlight = Jstr . v " highlight " in let overlay = El . pre ~ at : At . [ class ' highlight ; class ' plang ] [ ] in let input = El . textarea ~ at : At . [ rows 1 ; spellcheck ( Jstr . v " false " ) ] [ ] in let div = El . div ~ at : At . [ class ' ( Jstr . v " text - input " ) ] [ input ; overlay ] in let el = El . div ~ at : At . [ class ' ( Jstr . v " input " ) ] [ prompt ; div ] in let t = { input ; overlay ; el } in hide t ; t let el t = t . el let input t = t . input let set_has_focus focus t = El . set_has_focus focus t . input let auto_resize t = El . set_inline_style El . Style . height ( Jstr . v " auto " ) t . input ; let h = El . scroll_h t . input in El . set_inline_style El . Style . height Jstr . ( of_float h + v " px " ) t . input ; El . scroll_into_view ~ align_v ` : End t . input let cursor_pos t = let sel_start = El . prop ( El . Prop . int @@ Jstr . v " selectionStart " ) t . input in let sel_end = El . prop ( El . Prop . int @@ Jstr . v " selectionEnd " ) t . input in if sel_start = sel_end then Some sel_start else None let set_cursor_pos t pos = let args = Jv . [ | of_int pos ; of_int pos ] | in ignore @@ Jv . call ( El . to_jv t . input ) " setSelectionRange " args let get t = El . prop El . Prop . value t . input let update_overlay t = El . set_children t . overlay El . [ txt ( get t ) ] ; ignore ( Highlight . el t . overlay ) let update_input t = auto_resize t ; update_overlay t let set t s = El . set_prop El . Prop . value s t . input ; update_input t ; El . set_has_focus true t . input ; set_cursor_pos t ( Jstr . length s ) end
module Spinner = struct type t = { mutable abort : Abort . t option ; el : El . t } let el s = s . el let abort s = match s . abort with | None -> ( ) | Some a -> Abort . abort a ; s . abort <- None let hide s = abort s ; El . set_inline_style El . Style . display ( Jstr . v " none " ) s . el let create ( ) = let el = El . div ~ at : At . [ class ' ( Jstr . v " spinner " ) ] [ ] in let s = { abort = None ; el } in hide s ; s let spin s signal = Fut . bind ( Fut . tick ~ ms : 100 ) @@ fun ( ) -> let rec loop dot_count next = Fut . bind next @@ fun ( ) -> match Abort . Signal . aborted signal with | true -> Fut . ok ( ) | false -> let dots = Jstr . repeat dot_count ( Jstr . v " . " ) in El . set_children s . el [ El . txt dots ] ; let next = Fut . tick ~ ms : 450 in if dot_count >= 1 then loop 0 next else loop ( dot_count + 1 ) next in loop 1 ( Fut . return ( ) ) let show s = match s . abort with | Some a -> ( ) | None -> let abort = Abort . controller ( ) in s . abort <- Some abort ; El . set_inline_style El . Style . display ( Jstr . v " initial " ) s . el ; ignore ( spin s ( Abort . signal abort ) ) end
type t = { view : El . t ; output : El . t ; spinner : Spinner . t ; input : Text_input . t ; store : Store . t ; mutable h : History . t }
type output_kind = [ ` Past_input | ` Reply | ` Warning | ` Error | ` Info | ` Announce ]
let output r ~ kind cs = let at = At . [ class ' ( Jstr . v ( output_kind_to_class kind ) ) ] in let li = El . li ~ at cs in El . append_children r . output [ li ]
let announce_poke poke = let ocaml = let version = Brr_ocaml_poke . ocaml_version poke in let ocaml = Jstr . append ( Jstr . v " OCaml version " ) version in El . span ~ at : At . [ class ' ( Jstr . v " ocaml " ) ] [ El . txt ocaml ] in let jsoo = let version = Brr_ocaml_poke . jsoo_version poke in let jsoo = match Jstr . is_empty version with | true -> Jstr . empty | false -> Jstr . append ( Jstr . v " js_of_ocaml " ) version in El . span ~ at : At . [ class ' ( Jstr . v " jsoo " ) ] [ El . txt jsoo ] in El . pre [ ocaml ; El . txt ' " " ; jsoo ]
let ocaml_pre s = if Jstr . is_empty s then [ ] else [ Highlight . el ( El . pre ~ at : At . [ class ' ( Jstr . v " ocaml " ) ] [ El . txt s ] ) ]
let history_sep = Jstr . v " "
let history_key = Jstr . v " history "
let history_clear r = r . h <- History . empty ; r . store . set history_key Jstr . empty
let history_load r = let * h = r . store . get history_key in let history = Option . value ~ default : Jstr . empty h in r . h <- History . of_string ~ sep : history_sep history ; Fut . ok ( )
let history_prev r s = match History . prev r . h s with
let history_next r s = match History . next r . h s with
let history_save r s = let chop_end_nl s = if Jstr . ends_with ~ suffix ( : Jstr . v " \ n " ) s then Jstr . slice ~ stop ( :- 1 ) s else s in r . h <- History . add r . h ( chop_end_nl s ) ; let h = History . to_string ~ sep : history_sep r . h in ignore @@ Fut . map ( Console . log_if_error ~ use ( ) ) : @@ r . store . set history_key h
let history_clear_ui r = let clear = El . button El . [ txt ' " Clear history " ] in let clear_act _ = ignore @@ Fut . map ( Console . log_if_error ~ use ( ) ) : @@ history_clear r in Ev . listen Ev . click clear_act ( El . as_target clear ) ; clear
let history_keyboard_moves r key = let first_line_end s = match Jstr . find_sub ~ sub : Jstr . nl s with | None -> Jstr . length s | Some i -> i in let last_line_start s = match Jstr . find_last_sub ~ sub : Jstr . nl s with | None -> 0 | Some i when i = Jstr . length s - 1 -> 0 | Some i -> i + 1 in let k = Ev . as_type key in let ( key_code : int ) = Jv . Int . get ( Ev . to_jv key ) " keyCode " in let txt = Text_input . get r . input in match key_code with | 38 -> let do_prev = Ev . Keyboard . ctrl_key k || match Text_input . cursor_pos r . input with | None -> false | Some cursor when cursor > first_line_end txt -> false | Some cursor -> true in if not do_prev then ( ) else ( Ev . prevent_default key ; Text_input . set r . input ( history_prev r txt ) ) | 40 -> let do_next = Ev . Keyboard . ctrl_key k || match Text_input . cursor_pos r . input with | None -> false | Some cursor when cursor < last_line_start txt -> false | Some cursor -> true in if not do_next then ( ) else ( Ev . prevent_default key ; Text_input . set r . input ( history_next r txt ) ) | _ -> ( )
let prompt ( ) = El . span ~ at : At . [ class ' ( Jstr . v " prompt " ) ] El . [ txt ' " " ] #
let lock_input r = Text_input . hide r . input ; Spinner . show r . spinner
let unlock_input r = Spinner . hide r . spinner ; Text_input . show r . input ; Text_input . update_input r . input ; Text_input . set_has_focus true r . input
let handle_text_input r poke _e = let i = Text_input . get r . input in let enter = Jstr . v " ; ; \ n " and enter_win = Jstr . v " ; ; \ r \ n " in let submit = Jstr . ends_with ~ suffix : enter i || Jstr . ends_with ~ suffix : enter_win i in if not submit then Text_input . update_input r . input else begin history_save r i ; output r ~ kind ` : Past_input ( prompt ( ) :: ocaml_pre i ) ; lock_input r ; Text_input . set r . input Jstr . empty ; ignore @@ let * out = Fut . map ( Result . map Jv . to_jstr ) ( Brr_ocaml_poke . eval poke i ) in output r ~ kind ` : Reply ( ocaml_pre out ) ; unlock_input r ; Fut . ok ( ) end
let use_ml_file r poke file = let use = Jstr . ( v " # use " " \ + File . name file + v " " ; ; " ) \ in output r ~ kind ` : Past_input ( prompt ( ) :: ocaml_pre use ) ; let * text = Blob . text ( File . as_blob file ) in let * out = Brr_ocaml_poke . use poke text in let out = Jv . to_jstr out in output r ~ kind ` : Reply ( ocaml_pre out ) ; Fut . ok ( )
let use_ml_files r poke files = let rec loop = function | f :: fs -> let * ( ) = use_ml_file r poke f in loop fs | [ ] -> Fut . ok ( ) in let finish result = Console . log_if_error ~ use ( ) : result ; unlock_input r in lock_input r ; ignore @@ Fut . map finish ( loop files )
let use_ml_ui r poke = let on_change i = let files = El . Input . files i in El . set_prop El . Prop . value Jstr . empty i ; use_ml_files r poke files in let i = El . input ~ at : At . [ type ' ( Jstr . v " file " ) ] ( ) in let b = El . button [ El . txt ' " # use " " " \…\ ] in El . set_inline_style El . Style . display ( Jstr . v " none " ) i ; Ev . listen Ev . click ( fun e -> El . click i ) ( El . as_target b ) ; Ev . listen Ev . change ( fun e -> on_change i ) ( El . as_target i ) ; El . span [ i ; b ]
let use_ml_on_file_drag_and_drop ? drop_target r poke = let on_drop e = Ev . prevent_default e ; match Ev . Drag . data_transfer ( Ev . as_type e ) with | None -> ( ) | Some dt -> let items = Ev . Data_transfer . ( Item_list . items ( items dt ) ) in let files = List . filter_map Ev . Data_transfer . Item . get_file items in use_ml_files r poke files in let on_dragover e = Ev . prevent_default e in let t = match drop_target with None -> El . as_target r . view | Some t -> t in Ev . listen Ev . dragover on_dragover t ; Ev . listen Ev . drop on_drop t
let create ( ? store = Store . page ( Storage . local G . window ) ) view = let output = El . ol [ ] in let spinner = Spinner . create ( ) in let input = Text_input . create ~ prompt ( : prompt ( ) ) ( ) in let r = { view ; output ; input ; spinner ; store ; h = History . v ~ prev [ ] : } in El . set_children view [ output ; Text_input . el input ; Spinner . el spinner ] ; El . set_class ( Jstr . v " ocaml - ui " ) true view ; let * ( ) = history_load r in Fut . ok r
let buttons ( ? buttons = [ ] ) r poke = let panel = El . div ~ at : At . [ class ' ( Jstr . v " buttons " ) ] [ ] in let clear = history_clear_ui r in let use_ml = use_ml_ui r poke in El . set_children panel ( buttons @ [ clear ; use_ml ] ) ; panel
let setup_poke_io ? drop_target r poke = let input = El . as_target ( Text_input . input r . input ) in Ev . listen Ev . input ( handle_text_input r poke ) input ; Ev . listen Ev . keydown ( history_keyboard_moves r ) input ; use_ml_on_file_drag_and_drop ? drop_target r poke
let run ? drop_target ? buttons : bs r poke = let buttons = buttons ? buttons : bs r poke in El . append_children r . view [ buttons ] ; setup_poke_io ? drop_target r poke ; output r ~ kind ` : Announce [ announce_poke poke ] ; Text_input . show r . input ; Text_input . set_has_focus true r . input
let js_to_string o = if Jv . is_null o then " null " else if Jv . is_undefined o then " undefined " else Jv . to_string ( Jv . call o " toString " [ ] ) ||
let pp_jv ppf v = Format . pp_print_string ppf ( js_to_string v )
let pp_jstr ppf s = Format . fprintf ppf " [ @ Jstr . v % S ] " @ ( Jstr . to_string s )
let pp_jv_error ppf ( e : Jv . Error . t ) = Format . pp_print_string ppf ( js_to_string ( Jv . repr e ) )
let stdouts = ref Jstr . empty
let stdouts_reset ( ) = stdouts := Jstr . empty
let stdouts_append ~ js_string : d = stdouts := Jstr . append ! stdouts ( Obj . magic d : Jstr . t )
let resp = Buffer . create 100
let top_init ( ) = Jsoo_runtime . Sys . set_channel_output ' stdout stdouts_append ; Jsoo_runtime . Sys . set_channel_output ' stderr stdouts_append ; Js_of_ocaml_toplevel . JsooTop . initialize ( ) ; let ppf = Format . formatter_of_buffer resp in ignore ( Js_of_ocaml_toplevel . JsooTop . use ppf " # install_printer Brr_poke . pp_jstr ; ; " ) ; ignore ( Js_of_ocaml_toplevel . JsooTop . use ppf " # install_printer Brr_poke . pp_jv_error ; ; " ) ; ignore ( Js_of_ocaml_toplevel . JsooTop . use ppf " # install_printer Brr_poke . pp_jv ; ; " ) ; ( )
let top_eval phrase = let ppf = Format . formatter_of_buffer resp in stdouts_reset ( ) ; Js_of_ocaml_toplevel . JsooTop . execute true ppf ( Jstr . to_string phrase ) ; let r = Jstr . append ! stdouts ( Jstr . of_string ( Buffer . contents resp ) ) in Buffer . reset resp ; stdouts_reset ( ) ; r
let top_use phrases = let ppf = Format . formatter_of_buffer resp in stdouts_reset ( ) ; let _bool = Js_of_ocaml_toplevel . JsooTop . use ppf ( Jstr . to_string phrases ) in let r = Jstr . append ! stdouts ( Jstr . of_string ( Buffer . contents resp ) ) in stdouts_reset ( ) ; Buffer . reset resp ; r
let define ( ) = let ocaml_version = Jstr . of_string Sys . ocaml_version in let jsoo_version = Jstr . of_string Js_of_ocaml . Sys_js . js_of_ocaml_version in let o = Jv . obj [ | " version " , Jv . of_int 0 ; " ocaml_version " , Jv . of_jstr ocaml_version ; " jsoo_version " , Jv . of_jstr jsoo_version ; " init " , Jv . repr top_init ; " eval " , Jv . repr top_eval ; " use " , Jv . repr top_use ; ] | in Jv . set Jv . global " ocaml_poke " o
module Audio = struct module Param = struct module Automation_rate = struct type t = Jstr . t let a_rate = Jstr . v " a - rate " let k_rate = Jstr . v " k - rate " end type descriptor = Jv . t let descriptor ? automation_rate ? min_value ? max_value ? default_value n = let o = Jv . obj [ ] || in Jv . set o " name " ( Jv . of_jstr n ) ; Jv . Jstr . set_if_some o " automationRate " automation_rate ; Jv . Float . set_if_some o " minValue " min_value ; Jv . Float . set_if_some o " maxValue " max_value ; Jv . Float . set_if_some o " defaultValue " max_value ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) let value p = Jv . Float . get p " value " let set_value p v = Jv . Float . set p " value " v let automation_rate p = Jv . Jstr . get p " automationRate " let set_automation_rate p v = Jv . Jstr . set p " automationRate " v let default_value p = Jv . Float . get p " defaultValue " let min_value p = Jv . Float . get p " minValue " let max_value p = Jv . Float . get p " maxValue " let set_value_at_time p ~ value : v ~ time : t = ignore @@ Jv . call p " setValueAtTime " Jv . [ | of_float v ; of_float t ] | let linear_ramp_to_value_at_time p ~ value : v ~ end_time : t = ignore @@ Jv . call p " linearRampToValueAtTime " Jv . [ | of_float v ; of_float t ] | let exponential_ramp_to_value_at_time p ~ value : v ~ end_time : t = ignore @@ Jv . call p " exponentialRampToValueAtTime " Jv . [ | of_float v ; of_float t ] | let set_target_at_time p ~ target : v ~ start_time : t ~ decay_rate : r = ignore @@ Jv . call p " setTargetAtTime " Jv . [ | of_float v ; of_float t ; of_float r ] | let set_value_curve_at_time p vs ~ start_time : t ~ dur_s : d = let args = Jv . [ | Tarray . to_jv vs ; of_float t ; of_float d ] | in ignore @@ Jv . call p " setValueCurveAtTime " args let cancel_scheduled_values p ~ time : t = ignore @@ Jv . call p " cancelScheduledValues " Jv . [ | of_float t ] | let cancel_and_hold_at_time p ~ time : t = ignore @@ Jv . call p " cancelAndHoldAtTime " Jv . [ | of_float t ] | end module Listener = struct type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) let position_x l = Param . of_jv @@ Jv . call l " positionX " [ ] || let position_y l = Param . of_jv @@ Jv . call l " positionY " [ ] || let position_z l = Param . of_jv @@ Jv . call l " positionZ " [ ] || let forward_x l = Param . of_jv @@ Jv . call l " forwardX " [ ] || let forward_y l = Param . of_jv @@ Jv . call l " forwardY " [ ] || let forward_z l = Param . of_jv @@ Jv . call l " forwardZ " [ ] || let up_x l = Param . of_jv @@ Jv . call l " upX " [ ] || let up_y l = Param . of_jv @@ Jv . call l " upY " [ ] || let up_z l = Param . of_jv @@ Jv . call l " upZ " [ ] || end module Worklet = struct type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) let add_module w url = Fut . of_promise ~ ok : ignore @@ Jv . call w " addModule " Jv . [ | of_jstr url ] | module G = struct let register_processor n c = let args = Jv . [ | of_jstr n ; c ] | in ignore @@ Jv . apply ( Jv . get Jv . global " registerProcessor " ) args let current_frame ( ) = Jv . Int . get Jv . global " currentFrame " let current_time ( ) = Jv . Float . get Jv . global " currentTime " let sample_rate ( ) = Jv . Float . get Jv . global " sampleRate " end module Processor = struct type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) let port p = Brr_io . Message . Port . of_jv @@ Jv . get p " port " end end module Buffer = struct type opts = Jv . t let opts ~ channel_count : cc ~ length : l ~ sample_rate_hz : r ( ) = Jv . obj Jv . [ | " numberOfChannels " , of_int cc ; " length " , of_int l ; " sampleRate " , of_float r ] | type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) let create opts = Jv . new ' ( Jv . get Jv . global " AudioBuffer " ) [ | opts ] | let sample_rate b = Jv . Float . get b " sampleRate " let length b = Jv . Int . get b " length " let duration_s b = Jv . Float . get b " length " let channel_count b = Jv . Int . get b " numberOfChannels " let get_channel_data b ~ channel = Tarray . of_jv @@ Jv . call b " getChannelData " Jv . [ | of_int channel ] | let copy_from_channel ( ? dst_start = 0 ) b ~ channel : c ~ dst = let args = Jv . [ | Tarray . to_jv dst ; of_int c ; of_int dst_start ] | in ignore @@ Jv . call b " copyFromChannel " args let copy_to_channel ( ? dst_start = 0 ) b ~ src ~ channel : c = let args = Jv . [ | Tarray . to_jv src ; of_int c ; of_int dst_start ] | in ignore @@ Jv . call b " copyToChannel " args end module Node = struct module Channel_count_mode = struct type t = Jstr . t let max = Jstr . v " max " let clamped_max = Jstr . v " clamped - max " let explicit = Jstr . v " explicit " end module Channel_interpretation = struct type t = Jstr . t let speakers = Jstr . v " speakers " let discrete = Jstr . v " discrete " end type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) type node = t type context = Jv . t external as_target : t -> Ev . target = " % identity " let context n = Jv . get n " context " let input_count n = Jv . Int . get n " numberOfInputs " let output_count n = Jv . Int . get n " numberOfOutputs " let channel_count n = Jv . Int . get n " channelCount " let set_channel_count n c = Jv . Int . set n " channelCount " c let channel_count_mode n = Jv . Jstr . get n " channelCountMode " let set_channel_count_mode n m = Jv . Jstr . set n " channelCountMode " m let channel_interpretation n = Jv . Jstr . get n " channelInterpretation " let set_channel_interpretation n i = Jv . Jstr . set n " channelInterpretation " i let connect_node ( ? output = 0 ) ( ? input = 0 ) n ~ dst = ignore @@ Jv . call n " connect " Jv . [ | dst ; of_int output ; of_int input ] | let connect_param ( ? output = 0 ) n ~ dst = ignore @@ Jv . call n " connect " Jv . [ | dst ; of_int output ] | let disconnect n = ignore @@ Jv . call n " disconnect " [ ] || let disconnect_node ? output ? input n ~ dst = let output = Jv . of_option ~ none : Jv . undefined Jv . of_int output in let input = Jv . of_option ~ none : Jv . undefined Jv . of_int input in ignore @@ Jv . call n " disconnect " Jv . [ | dst ; output ; input ] | let disconnect_param ? output n ~ dst = let output = Jv . of_option ~ none : Jv . undefined Jv . of_int output in ignore @@ Jv . call n " disconnect " Jv . [ | dst ; output ] | module Analyser = struct type opts = Jv . t let opts ? channel_count ? channel_count_mode ? channel_interpretation ? fft_size ? max_decibels ? min_decibels ? smoothing_time_constant ( ) = let o = Jv . obj [ ] || in Jv . Int . set_if_some o " channelCount " channel_count ; Jv . Jstr . set_if_some o " channelCountMode " channel_count_mode ; Jv . Jstr . set_if_some o " channelInterpretation " channel_interpretation ; Jv . Int . set_if_some o " fftSize " fft_size ; Jv . Float . set_if_some o " minDecibels " min_decibels ; Jv . Float . set_if_some o " maxDecibels " max_decibels ; Jv . Float . set_if_some o " smoothingTimeConstant " smoothing_time_constant ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create ( ? opts = Jv . undefined ) c = Jv . new ' ( Jv . get Jv . global " AnalyserNode " ) [ | c ; opts ] | let get_float_frequency_data n a = ignore @@ Jv . call n " getFloatFrequencyData " [ | Tarray . to_jv a ] | let get_byte_frequency_data n a = ignore @@ Jv . call n " getByteFrequencyData " [ | Tarray . to_jv a ] | let get_float_time_domain_data n a = ignore @@ Jv . call n " getFloatTimeDomainData " [ | Tarray . to_jv a ] | let get_byte_time_domain_data n a = ignore @@ Jv . call n " getByteTimeDomainData " [ | Tarray . to_jv a ] | let fft_size n = Jv . Int . get n " fftSize " let set_fft_size n v = Jv . Int . set n " fftSize " v let frequency_bin_count n = Jv . Int . get n " frequencyBinCount " let min_decibels n = Jv . Float . get n " minDecibels " let set_min_decibels n v = Jv . Float . set n " minDecibels " v let max_decibels n = Jv . Float . get n " maxDecibels " let set_max_decibels n v = Jv . Float . set n " maxDecibels " v let smoothing_time_constant n = Jv . Float . get n " smoothingTimeConstant " let set_smoothing_time_constant n v = Jv . Float . set n " smoothingTimeConstant " v end module Biquad_filter = struct module Type = struct type t = Jstr . t let lowpass = Jstr . v " lowpass " let highpass = Jstr . v " highpass " let bandpass = Jstr . v " bandpass " let lowshelf = Jstr . v " lowshelf " let highshelf = Jstr . v " highshelf " let peaking = Jstr . v " peaking " let notch = Jstr . v " notch " let allpass = Jstr . v " allpass " end type opts = Jv . t let opts ? channel_count ? channel_count_mode ? channel_interpretation ? type ' ? q ? detune ? frequency ? gain ( ) = let o = Jv . obj [ ] || in Jv . Int . set_if_some o " channelCount " channel_count ; Jv . Jstr . set_if_some o " channelCountMode " channel_count_mode ; Jv . Jstr . set_if_some o " channelInterpretation " channel_interpretation ; Jv . Jstr . set_if_some o " type " type ' ; Jv . Float . set_if_some o " Q " q ; Jv . Float . set_if_some o " detune " detune ; Jv . Float . set_if_some o " frequency " frequency ; Jv . Float . set_if_some o " gain " gain ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create ( ? opts = Jv . undefined ) c = Jv . new ' ( Jv . get Jv . global " BiquadFilterNode " ) [ | c ; opts ] | let type ' n = Jv . Jstr . get n " type " let set_type n v = Jv . Jstr . set n " type " v let detune n = Param . of_jv @@ Jv . call n " detune " [ ] || let frequency n = Param . of_jv @@ Jv . call n " frequency " [ ] || let q n = Param . of_jv @@ Jv . call n " Q " [ ] || let gain n = Param . of_jv @@ Jv . call n " gain " [ ] || let get_frequency_response n ~ frequencies : f ~ mag_response : m ~ phase_response : p = let args = Tarray . [ | to_jv f ; to_jv m ; to_jv p ] | in ignore @@ Jv . call n " getFrequencyResponse " args end module Buffer_source = struct type opts = Jv . t let opts ? buffer ? detune ? loop ? loop_start ? loop_end ? playback_rate ( ) = let o = Jv . obj [ ] || in Jv . set_if_some o " buffer " buffer ; Jv . Float . set_if_some o " detune " detune ; Jv . Bool . set_if_some o " loop " loop ; Jv . Float . set_if_some o " loop_start " loop_start ; Jv . Float . set_if_some o " loop_end " loop_end ; Jv . Float . set_if_some o " playbackRate " playback_rate ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create ( ? opts = Jv . undefined ) c = Jv . new ' ( Jv . get Jv . global " AudioBufferSourceNode " ) [ | c ; opts ] | let buffer n = Jv . find_map Buffer . of_jv n " buffer " let set_buffer n v = Jv . set n " buffer " ( Jv . of_option ~ none : Jv . null Buffer . to_jv v ) let playback_rate n = Param . of_jv @@ Jv . get n " playbackRate " let detune n = Param . of_jv @@ Jv . get n " detune " let loop n = Jv . Bool . get n " loop " let set_loop n b = Jv . Bool . set n " loop " b let loop_start n = Jv . Float . get n " loopStart " let set_loop_start n v = Jv . Float . set n " loopStart " v let loop_end n = Jv . Float . get n " loopEnd " let set_loop_end n v = Jv . Float . set n " loopEnd " v let start ? time : t ? offset : o ? dur_s : d n = let t = Jv . of_option ~ none : Jv . undefined Jv . of_float t in let o = Jv . of_option ~ none : Jv . undefined Jv . of_float o in let d = Jv . of_option ~ none : Jv . undefined Jv . of_float d in ignore @@ Jv . call n " start " [ | t ; o ; d ] | let stop ? time : t n = let t = Jv . of_option ~ none : Jv . undefined Jv . of_float t in ignore @@ Jv . call n " stop " [ | t ] | end module Channel_merger = struct type opts = Jv . t let opts ? channel_count ? channel_count_mode ? channel_interpretation ? input_count ( ) = let o = Jv . obj [ ] || in Jv . Int . set_if_some o " channelCount " channel_count ; Jv . Jstr . set_if_some o " channelCountMode " channel_count_mode ; Jv . Jstr . set_if_some o " channelInterpretation " channel_interpretation ; Jv . Int . set_if_some o " numberOfInputs " input_count ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create ( ? opts = Jv . undefined ) c = Jv . new ' ( Jv . get Jv . global " ChannelMergerNode " ) [ | c ; opts ] | end module Channel_splitter = struct type opts = Jv . t let opts ? channel_count ? channel_count_mode ? channel_interpretation ? output_count ( ) = let o = Jv . obj [ ] || in Jv . Int . set_if_some o " channelCount " channel_count ; Jv . Jstr . set_if_some o " channelCountMode " channel_count_mode ; Jv . Jstr . set_if_some o " channelInterpretation " channel_interpretation ; Jv . Int . set_if_some o " numberOfOutput " output_count ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create ( ? opts = Jv . undefined ) c = Jv . new ' ( Jv . get Jv . global " ChannelSplitterNode " ) [ | c ; opts ] | end module Constant_source = struct type opts = Jv . t let opts ? offset ( ) = let o = Jv . obj [ ] || in Jv . Float . set_if_some o " offset " offset ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create ( ? opts = Jv . undefined ) c = Jv . new ' ( Jv . get Jv . global " ConstantSourceNode " ) [ | c ; opts ] | let offset n = Param . of_jv @@ Jv . get n " offset " let start ? time : t n = let t = Jv . of_option ~ none : Jv . undefined Jv . of_float t in ignore @@ Jv . call n " start " [ | t ] | let stop ? time : t n = let t = Jv . of_option ~ none : Jv . undefined Jv . of_float t in ignore @@ Jv . call n " stop " [ | t ] | end module Convolver = struct type opts = Jv . t let opts ? channel_count ? channel_count_mode ? channel_interpretation ? buffer ? disable_normalization ( ) = let o = Jv . obj [ ] || in Jv . Int . set_if_some o " channelCount " channel_count ; Jv . Jstr . set_if_some o " channelCountMode " channel_count_mode ; Jv . Jstr . set_if_some o " channelInterpretation " channel_interpretation ; Jv . set_if_some o " buffer " buffer ; Jv . Bool . set_if_some o " disableNormalization " disable_normalization ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create ( ? opts = Jv . undefined ) c = Jv . new ' ( Jv . get Jv . global " ConvolverNode " ) [ | c ; opts ] | let buffer n = Jv . find_map Buffer . of_jv n " buffer " let set_buffer n v = Jv . set n " buffer " ( Jv . of_option ~ none : Jv . null Buffer . to_jv v ) let normalize n = Jv . Bool . get n " normalize " let set_normalize n b = Jv . Bool . set n " normalize " b end module Delay = struct type opts = Jv . t let opts ? channel_count ? channel_count_mode ? channel_interpretation ? delay_time ? max_delay_time ( ) = let o = Jv . obj [ ] || in Jv . Int . set_if_some o " channelCount " channel_count ; Jv . Jstr . set_if_some o " channelCountMode " channel_count_mode ; Jv . Jstr . set_if_some o " channelInterpretation " channel_interpretation ; Jv . Float . set_if_some o " delayTime " delay_time ; Jv . Float . set_if_some o " maxDelayTime " max_delay_time ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create ( ? opts = Jv . undefined ) c = Jv . new ' ( Jv . get Jv . global " DelayNode " ) [ | c ; opts ] | let delay_time n = Param . of_jv @@ Jv . get n " delayTime " end module Destination = struct type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let max_channel_count n = Jv . Int . get n " maxChannelCount " end module Dynamics_compressor = struct type opts = Jv . t let opts ? channel_count ? channel_count_mode ? channel_interpretation ? attack ? knee ? ratio ? release ? threshold ( ) = let o = Jv . obj [ ] || in Jv . Int . set_if_some o " channelCount " channel_count ; Jv . Jstr . set_if_some o " channelCountMode " channel_count_mode ; Jv . Jstr . set_if_some o " channelInterpretation " channel_interpretation ; Jv . Float . set_if_some o " attack " attack ; Jv . Float . set_if_some o " knee " knee ; Jv . Float . set_if_some o " ratio " ratio ; Jv . Float . set_if_some o " release " release ; Jv . Float . set_if_some o " threshold " threshold ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create ( ? opts = Jv . undefined ) c = Jv . new ' ( Jv . get Jv . global " DynamicsCompressorNode " ) [ | c ; opts ] | let attack n = Param . of_jv @@ Jv . get n " attack " let knee n = Param . of_jv @@ Jv . get n " knee " let ratio n = Param . of_jv @@ Jv . get n " ratio " let reduction n = Jv . Float . get n " reduction " let release n = Param . of_jv @@ Jv . get n " release " let threshold n = Param . of_jv @@ Jv . get n " threshold " end module Gain = struct type opts = Jv . t let opts ? channel_count ? channel_count_mode ? channel_interpretation ? gain ( ) = let o = Jv . obj [ ] || in Jv . Int . set_if_some o " channelCount " channel_count ; Jv . Jstr . set_if_some o " channelCountMode " channel_count_mode ; Jv . Jstr . set_if_some o " channelInterpretation " channel_interpretation ; Jv . Float . set_if_some o " gain " gain ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create ( ? opts = Jv . undefined ) c = Jv . new ' ( Jv . get Jv . global " GainNode " ) [ | c ; opts ] | let gain n = Param . of_jv @@ Jv . get n " gain " end module Iir_filter = struct type opts = Jv . t let opts ? channel_count ? channel_count_mode ? channel_interpretation ~ feedforward ~ feedback ( ) = let o = Jv . obj [ ] || in Jv . Int . set_if_some o " channelCount " channel_count ; Jv . Jstr . set_if_some o " channelCountMode " channel_count_mode ; Jv . Jstr . set_if_some o " channelInterpretation " channel_interpretation ; Jv . set o " feedforward " ( Tarray . to_jv feedforward ) ; Jv . set o " feedback " ( Tarray . to_jv feedback ) ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create c ~ opts = Jv . new ' ( Jv . get Jv . global " IIRFilterNode " ) [ | c ; opts ] | let get_frequency_response n ~ frequencies : f ~ mag_response : m ~ phase_response : p = let args = Tarray . [ | to_jv f ; to_jv m ; to_jv p ] | in ignore @@ Jv . call n " getFrequencyResponse " args end module Media_element_source = struct type opts = Jv . t let opts ~ el ( ) = let o = Jv . obj [ ] || in Jv . set o " mediaElement " ( Brr_io . Media . El . to_jv el ) ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create c ~ opts = Jv . new ' ( Jv . get Jv . global " MediaElementAudioSourceNode " ) [ | c ; opts ] | let media_element n = Brr_io . Media . El . of_jv @@ Jv . get n " mediaElement " end module Media_stream_destination = struct type opts = Jv . t let opts ? channel_count ? channel_count_mode ? channel_interpretation ( ) = let o = Jv . obj [ ] || in Jv . Int . set_if_some o " channelCount " channel_count ; Jv . Jstr . set_if_some o " channelCountMode " channel_count_mode ; Jv . Jstr . set_if_some o " channelInterpretation " channel_interpretation ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create ( ? opts = Jv . undefined ) c = Jv . new ' ( Jv . get Jv . global " MediaStreamAudioDestinationNode " ) [ | c ; opts ] | let stream n = Brr_io . Media . Stream . of_jv @@ Jv . get n " stream " end module Media_stream_source = struct type opts = Jv . t let opts ~ stream ( ) = let o = Jv . obj [ ] || in Jv . set o " mediaStream " ( Brr_io . Media . Stream . to_jv stream ) ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create c ~ opts = Jv . new ' ( Jv . get Jv . global " MediaStreamAudioSourceNode " ) [ | c ; opts ] | let media_stream n = Brr_io . Media . Stream . of_jv @@ Jv . get n " mediaStream " end module Media_stream_track_source = struct type opts = Jv . t let opts ~ stream ( ) = let o = Jv . obj [ ] || in Jv . set o " mediaStreamTrack " ( Brr_io . Media . Track . to_jv stream ) ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create c ~ opts = Jv . new ' ( Jv . get Jv . global " MediaStreamTrackAudioSourceNode " ) [ | c ; opts ] | end module Oscillator = struct module Periodic_wave = struct type opts = Jv . t let opts ? disable_normalization ? real ? imag ( ) = let o = Jv . obj [ ] || in Jv . Bool . set_if_some o " disableNormalization " disable_normalization ; Jv . set o " real " ( Jv . of_option ~ none : Jv . undefined Tarray . to_jv real ) ; Jv . set o " imag " ( Jv . of_option ~ none : Jv . undefined Tarray . to_jv imag ) ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) let create ( ? opts = Jv . undefined ) c = Jv . new ' ( Jv . get Jv . global " PeriodicWave " ) [ | c ; opts ] | end module Type = struct type t = Jstr . t let sine = Jstr . v " sine " let square = Jstr . v " square " let sawtooth = Jstr . v " sawtooth " let triangle = Jstr . v " triangle " let custom = Jstr . v " custom " end type opts = Jv . t let opts ? channel_count ? channel_count_mode ? channel_interpretation ? type ' ? frequency ? detune ? periodic_wave ( ) = let o = Jv . obj [ ] || in Jv . Int . set_if_some o " channelCount " channel_count ; Jv . Jstr . set_if_some o " channelCountMode " channel_count_mode ; Jv . Jstr . set_if_some o " channelInterpretation " channel_interpretation ; Jv . Jstr . set_if_some o " type " type ' ; Jv . Float . set_if_some o " frequency " frequency ; Jv . Float . set_if_some o " detune " detune ; Jv . set_if_some o " periodicWave " periodic_wave ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create ( ? opts = Jv . undefined ) c = Jv . new ' ( Jv . get Jv . global " OscillatorNode " ) [ | c ; opts ] | let type ' n = Jv . Jstr . get n " type " let set_type n v = Jv . Jstr . set n " type " v let detune n = Param . of_jv @@ Jv . call n " detune " [ ] || let frequency n = Param . of_jv @@ Jv . call n " frequency " [ ] || let set_periodic_wave n w = ignore @@ Jv . call n " setPeriodicWave " [ | Periodic_wave . to_jv w ] | let start ? time : t n = let t = Jv . of_option ~ none : Jv . undefined Jv . of_float t in ignore @@ Jv . call n " start " [ | t ] | let stop ? time : t n = let t = Jv . of_option ~ none : Jv . undefined Jv . of_float t in ignore @@ Jv . call n " stop " [ | t ] | end module Panner = struct module Panning_model = struct type t = Jstr . t let equalpower = Jstr . v " equalpower " let hrtf = Jstr . v " HRTF " end module Distance_model = struct type t = Jstr . t let linear = Jstr . v " linear " let inverse = Jstr . v " inverse " let exponential = Jstr . v " exponential " end type opts = Jv . t let opts ? channel_count ? channel_count_mode ? channel_interpretation ? panning_model ? distance_model ? position_x ? position_y ? position_z ? orientation_x ? orientation_y ? orientation_z ? ref_distance ? max_distance ? rolloff_factor ? cone_inner_angle ? cone_outer_angle ? cone_outer_gain ( ) = let o = Jv . obj [ ] || in Jv . Int . set_if_some o " channelCount " channel_count ; Jv . Jstr . set_if_some o " channelCountMode " channel_count_mode ; Jv . Jstr . set_if_some o " channelInterpretation " channel_interpretation ; Jv . Jstr . set_if_some o " panningModel " panning_model ; Jv . Jstr . set_if_some o " distanceModel " distance_model ; Jv . Float . set_if_some o " positionX " position_x ; Jv . Float . set_if_some o " positionY " position_y ; Jv . Float . set_if_some o " positionZ " position_z ; Jv . Float . set_if_some o " orientationX " orientation_x ; Jv . Float . set_if_some o " orientationY " orientation_y ; Jv . Float . set_if_some o " orientationZ " orientation_z ; Jv . Float . set_if_some o " refDistance " ref_distance ; Jv . Float . set_if_some o " maxDistance " max_distance ; Jv . Float . set_if_some o " rolloff_factor " rolloff_factor ; Jv . Float . set_if_some o " cone_inner_angle " cone_inner_angle ; Jv . Float . set_if_some o " cone_outer_angle " cone_outer_angle ; Jv . Float . set_if_some o " cone_outer_gain " cone_outer_gain ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create ( ? opts = Jv . undefined ) c = Jv . new ' ( Jv . get Jv . global " PannerNode " ) [ | c ; opts ] | let panning_model n = Jv . Jstr . get n " panningModel " let set_panning_model n v = Jv . Jstr . set n " panningModel " v let distance_model n = Jv . Jstr . get n " distanceModel " let set_distance_model n v = Jv . Jstr . set n " distanceModel " v let position_x n = Param . of_jv @@ Jv . get n " positionX " let position_y n = Param . of_jv @@ Jv . get n " positionY " let position_z n = Param . of_jv @@ Jv . get n " positionZ " let orientation_x n = Param . of_jv @@ Jv . get n " orientationX " let orientation_y n = Param . of_jv @@ Jv . get n " orientationY " let orientation_z n = Param . of_jv @@ Jv . get n " orientationZ " let ref_distance n = Jv . Float . get n " refDistance " let set_ref_distance n v = Jv . Float . set n " refDistance " v let max_distance n = Jv . Float . get n " maxDistance " let set_max_distance n v = Jv . Float . set n " maxDistance " v let cone_inner_angle n = Jv . Float . get n " coneInnerAngle " let set_cone_inner_angle n v = Jv . Float . set n " coneInnerAngle " v let cone_outer_angle n = Jv . Float . get n " coneOuterAngle " let set_cone_outer_angle n v = Jv . Float . set n " coneOuterAngle " v let cone_outer_gain n = Jv . Float . get n " coneOuterGain " let set_cone_outer_gain n v = Jv . Float . set n " coneOuterGain " v end module Stereo_panner = struct type opts = Jv . t let opts ? channel_count ? channel_count_mode ? channel_interpretation ? pan ( ) = let o = Jv . obj [ ] || in Jv . Int . set_if_some o " channelCount " channel_count ; Jv . Jstr . set_if_some o " channelCountMode " channel_count_mode ; Jv . Jstr . set_if_some o " channelInterpretation " channel_interpretation ; Jv . Float . set_if_some o " pan " pan ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create ( ? opts = Jv . undefined ) c = Jv . new ' ( Jv . get Jv . global " StereoPannerNode " ) [ | c ; opts ] | let pan n = Param . of_jv @@ Jv . get n " pan " end module Wave_shaper = struct module Oversample = struct type t = Jstr . t let none = Jstr . v " none " let mul_2x = Jstr . v " 2x " let mul_4x = Jstr . v " 4x " end type opts = Jv . t let opts ? channel_count ? channel_count_mode ? channel_interpretation ? curve ? oversample ( ) = let o = Jv . obj [ ] || in Jv . Int . set_if_some o " channelCount " channel_count ; Jv . Jstr . set_if_some o " channelCountMode " channel_count_mode ; Jv . Jstr . set_if_some o " channelInterpretation " channel_interpretation ; Jv . set o " curve " ( Jv . of_option ~ none : Jv . undefined Tarray . to_jv curve ) ; Jv . Jstr . set_if_some o " oversample " oversample ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create ( ? opts = Jv . undefined ) c = Jv . new ' ( Jv . get Jv . global " WaveShaperNode " ) [ | c ; opts ] | let curve n = Jv . to_option Tarray . of_jv @@ Jv . get n " curve " let set_curve n v = Jv . set n " curve " ( Jv . of_option ~ none : Jv . null Tarray . to_jv v ) let oversample n = Jv . Jstr . get n " oversample " let set_oversample n v = Jv . Jstr . set n " oversample " v end module Worklet = struct type opts = Jv . t let opts ? channel_count ? channel_count_mode ? channel_interpretation ? input_count ? output_count ? output_channel_count ? parameters ? processor_options ( ) = let o = Jv . obj [ ] || in Jv . Int . set_if_some o " channelCount " channel_count ; Jv . Jstr . set_if_some o " channelCountMode " channel_count_mode ; Jv . Jstr . set_if_some o " channelInterpretation " channel_interpretation ; Jv . Int . set_if_some o " numberOfInputs " input_count ; Jv . Int . set_if_some o " numberOfOutputs " output_count ; Jv . set_if_some o " outputChannelCount " ( Option . map ( Jv . of_list Jv . of_int ) output_channel_count ) ; Jv . set_if_some o " parameterData " parameters ; Jv . set_if_some o " processorOptions " processor_options ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_node : t -> node = " % identity " let create ( ? opts = Jv . undefined ) c name = let args = Jv . [ | c ; of_jstr name ; opts ] | in Jv . new ' ( Jv . get Jv . global " AudioWorkletNode " ) args let parameter n k = let p = Jv . call ( Jv . get n " parameters " ) " get " Jv . [ | of_jstr k ] | in if Jv . is_none p then Jv . throw ( Jstr . ( v " no parameter named " + k ) ) else p let port n = Brr_io . Message . Port . of_jv @@ Jv . get n " port " end end module Timestamp = struct type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) let context_time t = Jv . Float . get t " contextTime " let performance_time t = Jv . Float . get t " performanceTime " end module Context = struct module State = struct type t = Jstr . t let suspended = Jstr . v " suspended " let running = Jstr . v " running " let closed = Jstr . v " closed " end module Base = struct type t = Node . context include ( Jv . Id : Jv . CONV with type t := t ) external as_target : t -> Ev . target = " % identity " let decode_audio_data c b = Fut . of_promise ~ ok : Buffer . of_jv @@ Jv . call c " decodeAudioData " Jv . [ | Buffer . to_jv b ] | let destination c = Node . Destination . of_jv @@ Jv . get c " destination " let sample_rate c = Jv . Float . get c " sampleRate " let current_time c = Jv . Float . get c " currentTime " let listener c = Listener . of_jv @@ Jv . get c " listener " let state c = Jv . Jstr . get c " state " let audio_worklet c = Worklet . of_jv @@ Jv . get c " audioWorklet " end module Latency_category = struct type t = Jstr . t let balanced = Jstr . v " balanced " let interactive = Jstr . v " interactive " let playback = Jstr . v " playback " end type opts = Jv . t let opts ? latency_hint ? sample_rate_hz ( ) = let o = Jv . obj [ ] || in let latency_hint = match latency_hint with | None -> None | Some ( ` Category c ) -> Some ( Jv . of_jstr c ) | Some ( ` Secs s ) -> Some ( Jv . of_float s ) in Jv . set_if_some o " latencyHint " latency_hint ; Jv . Float . set_if_some o " sampleRate " sample_rate_hz ; o type t = Jv . t external as_target : t -> Ev . target = " % identity " external as_base : t -> Base . t = " % identity " let create ( ? opts = Jv . undefined ) ( ) = Jv . new ' ( Jv . get Jv . global " AudioContext " ) [ | opts ] | let base_latency c = Jv . Float . get c " baseLatency " let output_latency c = Jv . Float . get c " outputLatency " let get_output_timestamp c = Timestamp . of_jv @@ Jv . call c " getOutputTimestamp " [ ] || let resume c = Fut . of_promise ~ ok : ignore @@ Jv . call c " resume " [ ] || let suspend c = Fut . of_promise ~ ok : ignore @@ Jv . call c " suspend " [ ] || let close c = Fut . of_promise ~ ok : ignore @@ Jv . call c " close " [ ] || module Offline = struct type opts = Jv . t let opts ~ channel_count : cc ~ length : l ~ sample_rate_hz : r ( ) = Jv . obj Jv . [ | " numberOfChannels " , of_int cc ; " length " , of_int l ; " sampleRate " , of_float r ] | type t = Jv . t external as_target : t -> Ev . target = " % identity " external as_base : t -> Base . t = " % identity " let length c = Jv . Int . get c " length " let create opts = Jv . new ' ( Jv . get Jv . global " OfflineAudioContext " ) [ | opts ] | let start_rendering c = Fut . of_promise ~ ok : Buffer . of_jv @@ Jv . call c " startRenderig " [ ] || let suspend c ~ secs = Fut . of_promise ~ ok : ignore @@ Jv . call c " suspend " Jv . [ | of_float secs ] | let resume c = Fut . of_promise ~ ok : ignore @@ Jv . call c " resume " [ ] || end end end
module Crypto_key = struct module Type = struct type t = Jstr . t let public = Jstr . v " public " let private ' = Jstr . v " private " let secret = Jstr . v " secret " end module Usage = struct type t = Jstr . t let encrypt = Jstr . v " encrypt " let decrypt = Jstr . v " decrypt " let sign = Jstr . v " sign " let verify = Jstr . v " verify " let derive_key = Jstr . v " deriveKey " let derive_bits = Jstr . v " deriveBits " let wrap_key = Jstr . v " wrapKey " let unwrap_key = Jstr . v " unwrapKey " end module Format = struct type t = Jstr . t let raw = Jstr . v " raw " let pkcs8 = Jstr . v " pkcs8 " let spki = Jstr . v " spki " let jwk = Jstr . v " jwk " end type algo = Jv . t type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) let type ' k = Jv . Jstr . get k " type " let extractable k = Jv . Bool . get k " extractable " let algorithm k = Jv . get k " algorithm " let usages k = Jv . to_jstr_list @@ Jv . get k " usages " type pair = Jv . t let public p = Jv . get p " publicKey " let private ' p = Jv . get p " privateKey " external pair_to_jv : pair -> Jv . t = " % identity " external pair_of_jv : Jv . t -> pair = " % identity " end
module Crypto_algo = struct type big_integer = Tarray . uint8 type t = Crypto_key . algo type algo = t include ( Jv . Id : Jv . CONV with type t := t ) let v n = Jv . obj [ | " name " , Jv . of_jstr n ] | let name a = Jv . Jstr . get a " name " let rsassa_pkcs1_v1_5 = Jstr . v " RSASSA - PKCS1 - v1_5 " module Rsa_hashed_key_gen_params = struct type t = Jv . t let v ~ name ~ modulus_length ~ public_exponent ~ hash ( ) = Jv . obj [ | " name " , Jv . of_jstr name ; " modulusLength " , Jv . of_int modulus_length ; " publicExponent " , Tarray . to_jv public_exponent ; " hash " , Jv . of_jstr hash ] | let of_algo = Fun . id let name a = Jv . Jstr . get a " name " let modulus_length a = Jv . Int . get a " modulusLength " let public_exponent a = Tarray . of_jv ( Jv . get a " publicExponent " ) let hash a = Jv . Jstr . get a " hash " end module Rsa_hashed_import_params = struct type t = Jv . t let v ~ name ~ hash ( ) = Jv . obj [ | " name " , Jv . of_jstr name ; " hash " , Jv . of_jstr hash ] | let of_algo = Fun . id let name a = Jv . Jstr . get a " name " let hash a = Jv . Jstr . get a " hash " end let rsa_pss = Jstr . v " RSA - PSS " module Rsa_pss_params = struct type t = Jv . t let v ( ? name = rsa_pss ) ~ salt_length ( ) = Jv . obj [ | " name " , Jv . of_jstr name ; " saltLength " , Jv . of_int salt_length ; ] | let of_algo = Fun . id let name a = Jv . Jstr . get a " name " let salt_length a = Jv . Int . get a " saltLength " end let rsa_oaep = Jstr . v " RSA - OAEP " module Rsa_oaep_params = struct type t = Jv . t let v ( ? name = rsa_oaep ) ? label ( ) = let label = match label with | None -> Jv . undefined | Some l -> Tarray . Buffer . to_jv l in Jv . obj [ | " name " , Jv . of_jstr name ; " label " , label ; ] | let of_algo = Fun . id let name a = Jv . Jstr . get a " name " let label a = Jv . to_option Tarray . Buffer . of_jv ( Jv . get a " label " ) end let ecdsa = Jstr . v " ECDSA " module Ec_key_gen_params = struct type t = Jv . t let v ~ name ~ named_curve ( ) = Jv . obj [ | " name " , Jv . of_jstr name ; " namedCurve " , Jv . of_jstr named_curve ] | let of_algo = Fun . id let name a = Jv . Jstr . get a " name " let named_curve a = Jv . Jstr . get a " namedCurve " end module Ec_key_import_params = struct type t = Jv . t let v ~ name ~ named_curve ( ) = Jv . obj [ | " name " , Jv . of_jstr name ; " namedCurve " , Jv . of_jstr named_curve ] | let of_algo = Fun . id let name a = Jv . Jstr . get a " name " let named_curve a = Jv . Jstr . get a " namedCurve " end module Ecdsa_params = struct type t = Jv . t let v ~ name ~ hash ( ) = Jv . obj [ | " name " , Jv . of_jstr name ; " hash " , Jv . of_jstr hash ] | let of_algo = Fun . id let name a = Jv . Jstr . get a " name " let hash a = Jv . Jstr . get a " hash " end let ecdh = Jstr . v " ECDH " module Ecdh_key_derive_params = struct type t = Jv . t let v ~ name ~ public ( ) = Jv . obj [ | " name " , Jv . of_jstr name ; " public " , public ] | let of_algo = Fun . id let name a = Jv . Jstr . get a " name " let public a = Jv . get a " public " end let aes_ctr = Jstr . v " AES - CTR " module Aes_key_gen_params = struct type t = Jv . t let v ~ name ~ length ( ) = Jv . obj [ | " name " , Jv . of_jstr name ; " length " , Jv . of_int length ] | let of_algo = Fun . id let name a = Jv . Jstr . get a " name " let length a = Jv . Int . get a " length " end module Aes_ctr_params = struct type t = Jv . t let v ( ? name = aes_ctr ) ~ counter ~ length ( ) = Jv . obj [ | " name " , Jv . of_jstr name ; " counter " , Tarray . Buffer . to_jv counter ; " length " , Jv . of_int length ] | let of_algo = Fun . id let name a = Jv . Jstr . get a " name " let counter a = Tarray . Buffer . of_jv @@ Jv . get a " counter " let length a = Jv . Int . get a " length " end let aes_cbc = Jstr . v " AES - CBC " module Aes_cbc_params = struct type t = Jv . t let v ( ? name = aes_cbc ) ~ iv ( ) = Jv . obj [ | " name " , Jv . of_jstr name ; " iv " , Tarray . Buffer . to_jv iv ] | let of_algo = Fun . id let name a = Jv . Jstr . get a " name " let iv a = Tarray . Buffer . of_jv @@ Jv . get a " iv " end let aes_gcm = Jstr . v " AES - GCM " module Aes_gcm_params = struct type t = Jv . t let v ( ? name = aes_cbc ) ~ iv ~ additional_data ~ tag_length ( ) = let add = match additional_data with | None -> Jv . undefined | Some a -> Tarray . Buffer . to_jv a in let tlen = match tag_length with | None -> Jv . undefined | Some l -> Jv . of_int l in Jv . obj [ | " name " , Jv . of_jstr name ; " iv " , Tarray . Buffer . to_jv iv ; " additionalData " , add ; " tagLength " , tlen ] | let of_algo = Fun . id let name a = Jv . Jstr . get a " name " let iv a = Tarray . Buffer . of_jv @@ Jv . get a " iv " let additional_data a = Jv . find_map Tarray . Buffer . of_jv a " additionalData " let tag_length a = Jv . find_map Jv . to_int a " tagLength " end let aes_kw = Jstr . v " AES - KW " let hmac = Jstr . v " HMAC " module Hmac_key_gen_params = struct type t = Jv . t let v ( ? name = hmac ) ? length ~ hash ( ) = let l = match length with None -> Jv . undefined | Some l -> Jv . of_int l in Jv . obj Jv . [ | " name " , of_jstr name ; " hash " , of_jstr hash ; " length " , l ] | let of_algo = Fun . id let name a = Jv . Jstr . get a " name " let hash a = Jv . Jstr . get a " hash " let length a = Jv . find_map Jv . to_int a " length " end module Hmac_import_params = Hmac_key_gen_params let sha_1 = Jstr . v " SHA - 1 " let sha_256 = Jstr . v " SHA - 256 " let sha_384 = Jstr . v " SHA - 384 " let sha_512 = Jstr . v " SHA - 512 " let hkdf = Jstr . v " HKDF " module Hkdf_params = struct type t = Jv . t let v ( ? name = hkdf ) ~ hash ~ salt ~ info ( ) = Jv . obj Jv . [ | " name " , of_jstr name ; " hash " , of_jstr hash ; " salt " , Tarray . Buffer . to_jv salt ; " info " , Tarray . Buffer . to_jv info ] | let of_algo = Fun . id let name a = Jv . Jstr . get a " name " let hash a = Jv . Jstr . get a " hash " let salt a = Tarray . Buffer . of_jv @@ Jv . get a " salt " let info a = Tarray . Buffer . of_jv @@ Jv . get a " info " end let pbkdf2 = Jstr . v " PBKDF2 " module Pbkdf2_params = struct type t = Jv . t let v ( ? name = pbkdf2 ) ~ hash ~ salt ~ iterations ( ) = Jv . obj Jv . [ | " name " , of_jstr name ; " hash " , of_jstr hash ; " salt " , Tarray . Buffer . to_jv salt ; " iterations " , of_int iterations ] | let of_algo = Fun . id let name a = Jv . Jstr . get a " name " let hash a = Jv . Jstr . get a " hash " let salt a = Tarray . Buffer . of_jv @@ Jv . get a " salt " let iterations a = Jv . Int . get a " iterations " end end
module Subtle_crypto = struct type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) let encrypt s a k d = Fut . of_promise ~ ok : Tarray . Buffer . of_jv @@ Jv . call s " encrypt " [ | Crypto_algo . to_jv a ; Crypto_key . to_jv k ; Tarray . to_jv d ] | let decrypt s a k d = Fut . of_promise ~ ok : Tarray . Buffer . of_jv @@ Jv . call s " decrypt " [ | Crypto_algo . to_jv a ; Crypto_key . to_jv k ; Tarray . to_jv d ] | let digest s a d = Fut . of_promise ~ ok : Tarray . Buffer . of_jv @@ Jv . call s " digest " [ | Crypto_algo . to_jv a ; Tarray . to_jv d ] | let sign s a k d = Fut . of_promise ~ ok : Tarray . Buffer . of_jv @@ Jv . call s " sign " [ | Crypto_algo . to_jv a ; Crypto_key . to_jv k ; Tarray . to_jv d ] | let verify s a k ~ sig ' d = Fut . of_promise ~ ok : Jv . to_bool @@ Jv . call s " verify " [ | Crypto_algo . to_jv a ; Crypto_key . to_jv k ; Tarray . to_jv sig ' ; Tarray . to_jv d ] | let generate_key s a ~ extractable ~ usages = Fut . of_promise ~ ok : Crypto_key . of_jv @@ Jv . call s " generateKey " [ | Crypto_algo . to_jv a ; Jv . of_bool extractable ; Jv . of_jstr_list usages ] | let generate_key_pair s a ~ extractable ~ usages = Fut . of_promise ~ ok : Crypto_key . pair_of_jv @@ Jv . call s " generateKey " [ | Crypto_algo . to_jv a ; Jv . of_bool extractable ; Jv . of_jstr_list usages ] | let derive_bits s a k l = Fut . of_promise ~ ok : Tarray . Buffer . of_jv @@ Jv . call s " deriveBits " [ | Crypto_algo . to_jv a ; Crypto_key . to_jv k ; Jv . of_int l ] | let derive_key s a k ~ derived ~ extractable ~ usages = Fut . of_promise ~ ok : Crypto_key . of_jv @@ Jv . call s " deriveKey " [ | Crypto_algo . to_jv a ; Crypto_key . to_jv k ; Crypto_algo . to_jv derived ; Jv . of_bool extractable ; Jv . of_jstr_list usages ] | let import_key s f k a ~ extractable ~ usages = let k = match k with | ` Buffer b -> Tarray . Buffer . to_jv b | ` Json_web_key k -> k in Fut . of_promise ~ ok : Crypto_key . of_jv @@ Jv . call s " importKey " [ | Jv . of_jstr f ; k ; Crypto_algo . to_jv a ; Jv . of_bool extractable ; Jv . of_jstr_list usages ] | let export_key s f k = let ok = match Jstr . equal Crypto_key . Format . jwk f with | true -> fun v -> ` Json_web_key v | false -> fun v -> ` Buffer ( Tarray . Buffer . of_jv v ) in Fut . of_promise ~ ok @@ Jv . call s " exportKey " [ | Jv . of_jstr f ; Crypto_key . to_jv k ] | let wrap_key s f k ~ wrap_key ~ wrapper = Fut . of_promise ~ ok : Tarray . Buffer . of_jv @@ Jv . call s " wrapKey " [ | Jv . of_jstr f ; Crypto_key . to_jv k ; Crypto_key . to_jv wrap_key ; Crypto_algo . to_jv wrapper ] | let unwrap_key s f k ~ wrap_key ~ wrapper ~ unwrapped ~ extractable ~ usages = Fut . of_promise ~ ok : Crypto_key . of_jv @@ Jv . call s " unwrapKey " [ | Jv . of_jstr f ; Tarray . to_jv k ; Crypto_key . to_jv wrap_key ; Crypto_algo . to_jv wrapper ; Crypto_algo . to_jv unwrapped ; Jv . of_bool extractable ; Jv . of_jstr_list usages ] | end
module Crypto = struct type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) let crypto = Jv . get Jv . global " crypto " let subtle c = Jv . get c " subtle " let set_random_values c a = ignore @@ Jv . call c " getRandomValues " [ | Tarray . to_jv a ] | end
module Midi = struct module Port = struct type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) let as_target = Ev . target_of_jv let open ' p = Fut . of_promise ~ ok : ignore @@ Jv . call p " open " [ ] || let close p = Fut . of_promise ~ ok : ignore @@ Jv . call p " close " [ ] || let [ @ inline ] get_nullable p prop = let v = Jv . get p prop in if Jv . is_none v then Jstr . empty else Jv . to_jstr v let id p = Jv . Jstr . get p " id " let name p = get_nullable p " name " let manufacturer p = get_nullable p " manufacturer " let version p = get_nullable p " version " let type ' p = Jv . Jstr . get p " type ' " let state p = Jv . Jstr . get p " state " let connection p = Jv . Jstr . get p " connection " let sub_of_port subp p = let t = type ' p in if Jstr . equal t subp then p else let exp = Jstr . ( v " Excepted " + subp + v " port but found : " + t ) in Jv . throw ( Jstr . append exp t ) end module Input = struct type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) let as_target = Ev . target_of_jv let as_port = Port . of_jv let of_port p = Port . sub_of_port ( Jstr . v " input " ) p end module Output = struct type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) let as_target = Ev . target_of_jv let as_port = Port . of_jv let of_port p = Port . sub_of_port ( Jstr . v " output " ) p let send ? timestamp_ms o msg = let args = match timestamp_ms with | None -> [ | Tarray . to_jv msg ] | | Some t -> [ | Tarray . to_jv msg ; Jv . of_float t ] | in match Jv . call o " send " args with | exception Jv . Error e -> Error e | s -> Ok ( ) let clear o = ignore @@ Jv . call o " clear " [ ] || end module Access = struct type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) let inputs a f acc = let it = Jv . It . iterator ( Jv . get a " inputs " ) in let f _ v acc = f v acc in Jv . It . fold_bindings ~ key : Jv . to_jstr ~ value : Output . of_jv f it acc let outputs a f acc = let it = Jv . It . iterator ( Jv . get a " outputs " ) in let f _ v acc = f v acc in Jv . It . fold_bindings ~ key : Jv . to_jstr ~ value : Output . of_jv f it acc type opts = Jv . t let opts ? sysex ? software ( ) = let o = Jv . obj [ ] || in Jv . Bool . set_if_some o " sysex " sysex ; Jv . Bool . set_if_some o " software " software ; o let of_navigator ? opts n = let args = match opts with None -> [ ] || | Some opts -> [ | opts ] | in Fut . of_promise ~ ok : of_jv @@ Jv . call ( Navigator . to_jv n ) " requestMIDIAccess " args end module Ev = struct module Message = struct type t = Jv . t let data e = Tarray . of_jv ( Jv . get e " data " ) end let midimessage = Ev . Type . create ( Jstr . v " midimessage " ) module Connection = struct type t = Jv . t let port e = Port . of_jv ( Jv . get e " port " ) end let statechange = Ev . Type . create ( Jstr . v " statechange " ) end end
module Worker = struct module Type = struct type t = Jstr . t let classic = Jstr . v " classic " let module ' = Jstr . v " module " end type opts = Jv . t let opts ? type ' ? credentials ? name ( ) = let o = Jv . obj ' [ ] || in Jv . Jstr . set_if_some o " type " type ' ; Jv . Jstr . set_if_some o " credentials " credentials ; Jv . Jstr . set_if_some o " name " name ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) let worker = Jv . get Jv . global " Worker " let create ( ? opts = Jv . undefined ) uri = Jv . new ' worker Jv . [ | of_jstr uri ; opts ] | external as_target : t -> Ev . target = " % identity " let terminate w = ignore @@ Jv . call w " terminate " [ ] || let post ? opts w v = let opts = match opts with None -> Jv . undefined | Some o -> Obj . magic o in ignore @@ Jv . call w " postMessage " [ | Jv . repr v ; opts ] | module Shared = struct type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) let shared = Jv . get Jv . global " SharedWorker " let create ( ? opts = Jv . undefined ) uri = Jv . new ' shared Jv . [ | of_jstr uri ; opts ] | external as_target : t -> Ev . target = " % identity " let port w = Message . Port . of_jv @@ Jv . get w " port " end let ami ( ) = Jv . has " WorkerGlobalScope " Jv . global module G = struct let import_scripts uris = ignore @@ Jv . call Jv . global " importScripts " [ | Jv . of_jstr_list uris ] | let post ? opts v = let opts = match opts with None -> Jv . undefined | Some o -> Obj . magic o in ignore @@ Jv . call Jv . global " postMessage " [ | Jv . repr v ; opts ] | let close ( ) = ignore @@ Jv . call Jv . global " close " [ ] || end end
module Service_worker = struct module Update_via_cache = struct type t = Jstr . t let imports = Jstr . v " imports " let all = Jstr . v " all " let none = Jstr . v " none " end module State = struct type t = Jstr . t let parsed = Jstr . v " parsed " let installing = Jstr . v " installing " let installed = Jstr . v " installed " let activating = Jstr . v " activating " let activated = Jstr . v " activated " let redundant = Jstr . v " redundant " end type t = Jv . t type service_worker = t include ( Jv . Id : Jv . CONV with type t := t ) external as_worker : t -> Worker . t = " % identity " external as_target : t -> Ev . target = " % identity " let script_url w = Jv . to_jstr @@ Jv . call w " scriptURL " [ ] || let state w = Jv . to_jstr @@ Jv . call w " state " [ ] || module Navigation_preload_manager = struct type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) let enable p = Fut . of_promise ~ ok : ignore @@ Jv . call p " enable " [ ] || let disable p = Fut . of_promise ~ ok : ignore @@ Jv . call p " disable " [ ] || let set_header_value p v = Fut . of_promise ~ ok : ignore @@ Jv . call p " setHeaderValue " Jv . [ | of_jstr v ] | let get_state p = let extract s = Jv . Bool . get s " enabled " , Jv . Jstr . get s " headerValue " in Fut . of_promise ~ ok : extract @@ Jv . call p " getState " [ ] || end module Registration = struct type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_target : t -> Ev . target = " % identity " let installing r = Jv . to_option Fun . id @@ Jv . get r " installing " let waiting r = Jv . to_option Fun . id @@ Jv . get r " waiting " let active r = Jv . to_option Fun . id @@ Jv . get r " active " let navigation_preload r = Navigation_preload_manager . of_jv @@ Jv . get r " navigationPreload " let scope r = Jv . Jstr . get r " scope " let update_via_cache r = Jv . Jstr . get r " updateViaCache " let update r = Fut . of_promise ~ ok : ignore @@ Jv . call r " update " [ ] || let unregister r = Fut . of_promise ~ ok : Jv . to_bool @@ Jv . call r " unregister " [ ] || let show_notification ? opts r title = let opts = Jv . of_option ~ none : Jv . undefined Jv . repr opts in Fut . of_promise ~ ok : ignore @@ Jv . call r " showNotification " Jv . [ | of_jstr title ; opts ] | let get_notifications ? tag r = let opts = match tag with | None -> Jv . undefined | Some tag -> Jv . obj [ " | tag " , Jv . of_jstr tag ] | in Fut . of_promise ~ ok ( : Jv . to_list Notification . of_jv ) @@ Jv . call r " getNotifications " [ | opts ] | end module Container = struct type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) let of_navigator n = Jv . get ( Navigator . to_jv n ) " serviceWorker " external as_target : t -> Ev . target = " % identity " let controller c = Jv . to_option Fun . id ( Jv . get c " controller " ) let ready c = Fut . of_promise ~ ok : Registration . of_jv @@ Jv . get c " ready " type register_opts = Jv . t let register_opts ? scope ? type ' ? update_via_cache ( ) = let o = Jv . obj [ ] || in Jv . Jstr . set_if_some o " scope " scope ; Jv . Jstr . set_if_some o " type " type ' ; Jv . Jstr . set_if_some o " updateViaCache " update_via_cache ; o let register ( ? register_opts = Jv . undefined ) c uri = Fut . of_promise ~ ok : Registration . of_jv @@ Jv . call c " register " Jv . [ | of_jstr uri ; register_opts ] | let get_registration c uri = let uri = Jv . of_option ~ none : Jv . undefined Jv . of_jstr uri in Fut . of_promise ~ ok ( : Jv . to_option Registration . of_jv ) @@ Jv . call c " getRegistration " Jv . [ | uri ] | let get_registrations c = Fut . of_promise ~ ok ( : Jv . to_list Registration . of_jv ) @@ Jv . call c " getRegistrations " Jv . [ ] || let start_messages c = ignore @@ Jv . call c " startMessages " [ ] || end module Client = struct module Visibility_state = struct type t = Jstr . t let hidden = Jstr . v " hidden " let visible = Jstr . v " visible " end module Type = struct type t = Jstr . t let window = Jstr . v " window " let worker = Jstr . v " worker " let sharedworker = Jstr . v " sharedworker " let all = Jstr . v " all " end module Frame_type = struct type t = Jstr . t let auxiliary = Jstr . v " auxiliary " let top_level = Jstr . v " top - level " let nested = Jstr . v " nested " let none = Jstr . v " none " end type t = Jv . t type client = t include ( Jv . Id : Jv . CONV with type t := t ) let url c = Jv . Jstr . get c " url " let frame_type c = Jv . Jstr . get c " frameType " let id c = Jv . Jstr . get c " id " let type ' c = Jv . Jstr . get c " type " let post ? opts c v = let opts = match opts with None -> Jv . undefined | Some o -> Obj . magic o in ignore @@ Jv . call c " postMessage " [ | Jv . repr v ; opts ] | module Window = struct type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) external as_client : t -> client = " % identity " let visibility_state w = Jv . Jstr . get w " visibilityState " let focused w = Jv . Bool . get w " focused " let ancestor_origins w = Jv . to_list Jv . to_jstr @@ Jv . get w " ancestorOrigins " let focus w = Fut . of_promise ~ ok : Fun . id @@ Jv . call w " focus " [ ] || let navigate w url = Fut . of_promise ~ ok : Fun . id @@ Jv . call w " focus " Jv . [ | of_jstr url ] | end end module Clients = struct type query_opts = Jv . t let query_opts ? include_uncontrolled ? type ' ( ) = let o = Jv . obj [ ] || in Jv . Bool . set_if_some o " includeUncontrolled " include_uncontrolled ; Jv . Jstr . set_if_some o " type " type ' ; o type t = Jv . t include ( Jv . Id : Jv . CONV with type t := t ) let get cs id = Fut . of_promise ~ ok ( : Jv . to_option Client . of_jv ) @@ Jv . call cs " get " [ | Jv . of_jstr id ] | let match_all ( ? query_opts = Jv . undefined ) cs = Fut . of_promise ~ ok ( : Jv . to_list Client . of_jv ) @@ Jv . call cs " matchAll " [ | query_opts ] | let open_window cs url = Fut . of_promise ~ ok ( : Jv . to_option Client . Window . of_jv ) @@ Jv . call cs " openWindow " [ | Jv . of_jstr url ] | let claim cs = Fut . of_promise ~ ok : ignore @@ Jv . call cs " claim " [ ] || end module G = struct let clients = Clients . of_jv @@ Jv . get Jv . global " clients " let registration = Registration . of_jv @@ Jv . get Jv . global " registration " let service_worker = of_jv @@ Jv . get Jv . global " serviceWorker " let skip_waiting ( ) = Fut . of_promise ~ ok : ignore @@ Jv . call Jv . global " skipWaiting " [ ] || end end
let pivot_level = 2 * lowest_level - 1
let new_id = ref ( - 1 )
let newty2 level desc = incr new_id ; { desc = desc ; level = level ; id = ! new_id }
let newgenty desc = newty2 generic_level desc
let newgenvar ( ) = newgenty Tvar
let rec field_kind_repr = function Fvar { contents = Some kind } -> field_kind_repr kind | kind -> kind
let rec repr = function { desc = Tlink t ' } -> repr t ' | { desc = Tfield ( _ , k , _ , t ' ) } when field_kind_repr k = Fabsent -> repr t ' | t -> t
let rec commu_repr = function Clink r when ! r <> Cunknown -> commu_repr ! r | c -> c
let rec row_field_repr_aux tl = function Reither ( _ , tl ' , _ , { contents = Some fi } ) -> row_field_repr_aux ( tl @ tl ' ) fi | Reither ( c , tl ' , m , r ) -> Reither ( c , tl @ tl ' , m , r ) | Rpresent ( Some _ ) when tl <> [ ] -> Rpresent ( Some ( List . hd tl ) ) | fi -> fi
let row_field_repr fi = row_field_repr_aux [ ] fi
let rec rev_concat l ll = match ll with [ ] -> l | l ' :: ll -> rev_concat ( l ' @ l ) ll
let rec row_repr_aux ll row = match ( repr row . row_more ) . desc with | Tvariant row ' -> let f = row . row_fields in row_repr_aux ( if f = [ ] then ll else f :: ll ) row ' | _ -> if ll = [ ] then row else { row with row_fields = rev_concat row . row_fields ll }
let row_repr row = row_repr_aux [ ] row
let rec row_field tag row = let rec find = function | ( tag ' , f ) :: fields -> if tag = tag ' then row_field_repr f else find fields | [ ] -> match repr row . row_more with | { desc = Tvariant row ' } -> row_field tag row ' | _ -> Rabsent in find row . row_fields
let rec row_more row = match repr row . row_more with | { desc = Tvariant row ' } -> row_more row ' | ty -> ty
let static_row row = let row = row_repr row in row . row_closed && List . for_all ( fun ( _ , f ) -> match row_field_repr f with Reither _ -> false | _ -> true ) row . row_fields
let hash_variant s = let accu = ref 0 in for i = 0 to String . length s - 1 do accu := 223 * ! accu + Char . code s . [ i ] done ; accu := ! accu land ( 1 lsl 31 - 1 ) ; if ! accu > 0x3FFFFFFF then ! accu - ( 1 lsl 31 ) else ! accu
let proxy ty = let ty0 = repr ty in match ty0 . desc with | Tvariant row when not ( static_row row ) -> row_more row | Tobject ( ty , _ ) -> let rec proxy_obj ty = match ty . desc with Tfield ( _ , _ , _ , ty ) | Tlink ty -> proxy_obj ty | Tvar | Tunivar | Tconstr _ -> ty | Tnil -> ty0 | _ -> assert false in proxy_obj ty | _ -> ty0
let has_constr_row t = match ( repr t ) . desc with Tobject ( t , _ ) -> let rec check_row t = match ( repr t ) . desc with Tfield ( _ , _ , _ , t ) -> check_row t | Tconstr _ -> true | _ -> false in check_row t | Tvariant row -> ( match row_more row with { desc = Tconstr _ } -> true | _ -> false ) | _ -> false
let is_row_name s = let l = String . length s in if l < 4 then false else String . sub s ( l - 4 ) 4 = " # row "
let rec iter_row f row = List . iter ( fun ( _ , fi ) -> match row_field_repr fi with | Rpresent ( Some ty ) -> f ty | Reither ( _ , tl , _ , _ ) -> List . iter f tl | _ -> ( ) ) row . row_fields ; match ( repr row . row_more ) . desc with Tvariant row -> iter_row f row | Tvar | Tunivar | Tsubst _ | Tconstr _ -> Misc . may ( fun ( _ , l ) -> List . iter f l ) row . row_name | _ -> assert false
let iter_type_expr f ty = match ty . desc with Tvar -> ( ) | Tarrow ( _ , ty1 , ty2 , _ ) -> f ty1 ; f ty2 | Ttuple l -> List . iter f l | Tconstr ( _ , l , _ ) -> List . iter f l | Tobject ( ty , { contents = Some ( _ , p ) } ) -> f ty ; List . iter f p | Tobject ( ty , _ ) -> f ty | Tvariant row -> iter_row f row ; f ( row_more row ) | Tfield ( _ , _ , ty1 , ty2 ) -> f ty1 ; f ty2 | Tnil -> ( ) | Tlink ty -> f ty | Tsubst ty -> f ty | Tunivar -> ( ) | Tpoly ( ty , tyl ) -> f ty ; List . iter f tyl | Tpackage ( _ , _ , l ) -> List . iter f l
let rec iter_abbrev f = function Mnil -> ( ) | Mcons ( _ , _ , ty , ty ' , rem ) -> f ty ; f ty ' ; iter_abbrev f rem | Mlink rem -> iter_abbrev f ! rem
let copy_row f fixed row keep more = let fields = List . map ( fun ( l , fi ) -> l , match row_field_repr fi with | Rpresent ( Some ty ) -> Rpresent ( Some ( f ty ) ) | Reither ( c , tl , m , e ) -> let e = if keep then e else ref None in let m = if row . row_fixed then fixed else m in let tl = List . map f tl in Reither ( c , tl , m , e ) | _ -> fi ) row . row_fields in let name = match row . row_name with None -> None | Some ( path , tl ) -> Some ( path , List . map f tl ) in { row_fields = fields ; row_more = more ; row_bound = ( ) ; row_fixed = row . row_fixed && fixed ; row_closed = row . row_closed ; row_name = name ; }
let rec copy_kind = function Fvar { contents = Some k } -> copy_kind k | Fvar _ -> Fvar ( ref None ) | Fpresent -> Fpresent | Fabsent -> assert false
let copy_commu c = if commu_repr c = Cok then Cok else Clink ( ref Cunknown )
let rec norm_univar ty = match ty . desc with Tunivar | Tsubst _ -> ty | Tlink ty -> norm_univar ty | Ttuple ( ty :: _ ) -> norm_univar ty | _ -> assert false
let rec copy_type_desc f = function Tvar -> Tvar | Tarrow ( p , ty1 , ty2 , c ) -> Tarrow ( p , f ty1 , f ty2 , copy_commu c ) | Ttuple l -> Ttuple ( List . map f l ) | Tconstr ( p , l , _ ) -> Tconstr ( p , List . map f l , ref Mnil ) | Tobject ( ty , { contents = Some ( p , tl ) } ) -> Tobject ( f ty , ref ( Some ( p , List . map f tl ) ) ) | Tobject ( ty , _ ) -> Tobject ( f ty , ref None ) | Tvariant row -> assert false | Tfield ( p , k , ty1 , ty2 ) -> Tfield ( p , field_kind_repr k , f ty1 , f ty2 ) | Tnil -> Tnil | Tlink ty -> copy_type_desc f ty . desc | Tsubst ty -> assert false | Tunivar -> Tunivar | Tpoly ( ty , tyl ) -> let tyl = List . map ( fun x -> norm_univar ( f x ) ) tyl in Tpoly ( f ty , tyl ) | Tpackage ( p , n , l ) -> Tpackage ( p , n , List . map f l )
let saved_desc = ref [ ]
let save_desc ty desc = saved_desc := ( ty , desc ) ::! saved_desc
let saved_kinds = ref [ ]
let new_kinds = ref [ ]
let dup_kind r = ( match ! r with None -> ( ) | Some _ -> assert false ) ; if not ( List . memq r ! new_kinds ) then begin saved_kinds := r :: ! saved_kinds ; let r ' = ref None in new_kinds := r ' :: ! new_kinds ; r := Some ( Fvar r ' ) end