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... |
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 . ... |
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 ? n... |
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... |
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 matc... |
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 ( ... |
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 [ ... |
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 va... |
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 . ... |
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_... |
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 ( ... |
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... |
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 . ... |
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 ... |
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 ' " # u... |
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_... |
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 ; ... |
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_foc... |
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 . Js... |
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 ; stdo... |
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 ( ) ; ... |
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 ... |
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 ... |
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 " dec... |
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 .... |
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 decryp... |
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... |
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... |
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 " ... |
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 ( So... |
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 | Tn... |
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 = Tconst... |
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_r... |
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... |
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 fixe... |
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 , { conte... |
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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.