text
stringlengths
0
601k
type ' msg t = | CommentNode of string | Text of string | Node of string * string * string * string * ' msg properties * ' msg t list | LazyGen of string * ( unit -> ' msg t ) * ' msg t ref | Tagger of ( ' msg applicationCallbacks ref -> ' msg applicationCallbacks ref ) * ' msg t
let noNode = CommentNode " "
let comment s = CommentNode s
let text s = Text s
let fullnode namespace tagName key unique props vdoms = Node ( namespace , tagName , key , unique , props , vdoms )
let node ( ? namespace " " ) = tagName ( ? key " " ) = ( ? unique " " ) = props vdoms = fullnode namespace tagName key unique props vdoms
let lazyGen key fn = LazyGen ( key , fn , ref noNode )
let prop key value = RawProp ( key , value )
let onCB name key cb = Event ( name , EventHandlerCallback ( key , cb ) , ref None )
let onMsg name msg = Event ( name , EventHandlerMsg msg , ref None )
let attribute namespace key value = Attribute ( namespace , key , value )
let data key value = Data ( key , value )
let style key value = Style [ ( key , value ) ]
let styles s = Style s
let rec renderToHtmlString = function | CommentNode s -> " <!-- " ^ s ^ " " --> | Text s -> s | Node ( namespace , tagName , _key , _unique , props , vdoms ) -> let renderProp = function | NoProp -> " " | RawProp ( k , v ) -> String . concat " " [ " " ; k ; " " " ; =\ v ; " " " ] \ | Attribute ( _namespace , k , v ) -> String . concat " " [ " " ; k ; " " " ; =\ v ; " " " ] \ | Data ( k , v ) -> String . concat " " [ " data " ; - k ; " " " ; =\ v ; " " " ] \ | Event ( _ , _ , _ ) -> " " | Style s -> String . concat " " [ " style " " ; =\ String . concat " ; " ( List . map ( fun ( k , v ) -> String . concat " " [ k ; " " ; : v ; " ; " ] ) s ) ; " " " ] \ in String . concat " " [ " " < ; namespace ; if namespace = " " then " " else " " : ; tagName ; String . concat " " ( List . map ( fun p -> renderProp p ) props ) ; " " > ; String . concat " " ( List . map ( fun v -> renderToHtmlString v ) vdoms ) ; " " </ ; tagName ; " " > ] | LazyGen ( _key , gen , _cache ) -> let vdom = gen ( ) in renderToHtmlString vdom | Tagger ( _tagger , vdom ) -> renderToHtmlString vdom
let emptyEventHandler : Web . Node . event_cb = fun [ @ bs ] _ev -> ( )
let emptyEventCB = fun _ev -> None
let eventHandler callbacks cb : Web . Node . event_cb = fun [ @ bs ] ev -> match ! cb ev with | None -> ( ) | Some msg -> ! callbacks . enqueue msg
let eventHandler_GetCB = function | EventHandlerCallback ( _ , cb ) -> cb | EventHandlerMsg msg -> fun _ev -> Some msg
let compareEventHandlerTypes left = function | EventHandlerCallback ( cb , _ ) -> ( match left with | EventHandlerCallback ( lcb , _ ) when cb = lcb -> true | _ -> false ) | EventHandlerMsg msg -> ( match left with | EventHandlerMsg lmsg when msg = lmsg -> true | _ -> false )
let eventHandler_Register callbacks elem name handlerType = let cb = ref ( eventHandler_GetCB handlerType ) in let handler = eventHandler callbacks cb in let ( ) = Web . Node . addEventListener elem name handler false in Some { handler ; cb }
let eventHandler_Unregister elem name = function | None -> None | Some cache -> let ( ) = Web . Node . removeEventListener elem name cache . handler false in None
let eventHandler_Mutate callbacks elem ( oldName : string ) ( newName : string ) oldHandlerType newHandlerType oldCache newCache = match ! oldCache with | None -> newCache := eventHandler_Register callbacks elem newName newHandlerType | Some oldcache -> if oldName = newName then let ( ) = newCache := ! oldCache in if compareEventHandlerTypes oldHandlerType newHandlerType then ( ) else let cb = eventHandler_GetCB newHandlerType in let ( ) = oldcache . cb := cb in ( ) else let ( ) = oldCache := eventHandler_Unregister elem oldName ! oldCache in let ( ) = newCache := eventHandler_Register callbacks elem newName newHandlerType in ( )
let patchVNodesOnElems_PropertiesApply_Add callbacks elem _idx = function | NoProp -> ( ) | RawProp ( k , v ) -> Web . Node . setProp elem k v | Attribute ( namespace , k , v ) -> Web . Node . setAttributeNsOptional elem namespace k v | Data ( k , v ) -> Js . log ( " TODO : Add Data Unhandled " , k , v ) ; failwith " TODO : Add Data Unhandled " | Event ( name , handlerType , cache ) -> cache := eventHandler_Register callbacks elem name handlerType | Style s -> List . fold_left ( fun ( ) ( k , v ) -> Web . Node . setStyleProperty elem k ( Js . Null . return v ) ) ( ) s
let patchVNodesOnElems_PropertiesApply_Remove _callbacks elem _idx = function | NoProp -> ( ) | RawProp ( k , _v ) -> Web . Node . setProp elem k Js . Undefined . empty | Attribute ( namespace , k , _v ) -> Web . Node . removeAttributeNsOptional elem namespace k | Data ( k , v ) -> Js . log ( " TODO : Remove Data Unhandled " , k , v ) ; failwith " TODO : Remove Data Unhandled " | Event ( name , _ , cache ) -> cache := eventHandler_Unregister elem name ! cache | Style s -> List . fold_left ( fun ( ) ( k , _v ) -> Web . Node . setStyleProperty elem k Js . Null . empty ) ( ) s
let patchVNodesOnElems_PropertiesApply_RemoveAdd callbacks elem idx oldProp newProp = let ( ) = patchVNodesOnElems_PropertiesApply_Remove callbacks elem idx oldProp in let ( ) = patchVNodesOnElems_PropertiesApply_Add callbacks elem idx newProp in ( )
let patchVNodesOnElems_PropertiesApply_Mutate _callbacks elem _idx oldProp = function | NoProp as _newProp -> failwith " This should never be called as all entries through NoProp are gated . " | RawProp ( k , v ) as _newProp -> Web . Node . setProp elem k v | Attribute ( namespace , k , v ) as _newProp -> Web . Node . setAttributeNsOptional elem namespace k v | Data ( k , v ) as _newProp -> Js . log ( " TODO : Mutate Data Unhandled " , k , v ) ; failwith " TODO : Mutate Data Unhandled " | Event ( _newName , _newHandlerType , _newCache ) as _newProp -> failwith " This will never be called because it is gated " | Style s as _newProp -> match [ @ ocaml . warning " - 4 " ] oldProp with | Style oldS -> List . fold_left2 ( fun ( ) ( ok , ov ) ( nk , nv ) -> if ok = nk then if ov = nv then ( ) else Web . Node . setStyleProperty elem nk ( Js . Null . return nv ) else let ( ) = Web . Node . setStyleProperty elem ok Js . Null . empty in Web . Node . setStyleProperty elem nk ( Js . Null . return nv ) ) ( ) oldS s | _ -> failwith " Passed a non - Style to a new Style as a Mutations while the old Style is not actually a style " !
let rec patchVNodesOnElems_PropertiesApply callbacks elem idx oldProperties newProperties = match [ @ ocaml . warning " - 4 " ] oldProperties , newProperties with | [ ] , [ ] -> true | [ ] , _newProp :: _newRest -> false | _oldProp :: _oldRest , [ ] -> false | NoProp :: oldRest , NoProp :: newRest -> patchVNodesOnElems_PropertiesApply callbacks elem ( idx + 1 ) oldRest newRest | ( RawProp ( oldK , oldV ) as oldProp ) :: oldRest , ( RawProp ( newK , newV ) as newProp ) :: newRest -> let ( ) = if oldK = newK && oldV = newV then ( ) else patchVNodesOnElems_PropertiesApply_Mutate callbacks elem idx oldProp newProp in patchVNodesOnElems_PropertiesApply callbacks elem ( idx + 1 ) oldRest newRest | ( Attribute ( oldNS , oldK , oldV ) as oldProp ) :: oldRest , ( Attribute ( newNS , newK , newV ) as newProp ) :: newRest -> let ( ) = if oldNS = newNS && oldK = newK && oldV = newV then ( ) else patchVNodesOnElems_PropertiesApply_Mutate callbacks elem idx oldProp newProp in patchVNodesOnElems_PropertiesApply callbacks elem ( idx + 1 ) oldRest newRest | ( Data ( oldK , oldV ) as oldProp ) :: oldRest , ( Data ( newK , newV ) as newProp ) :: newRest -> let ( ) = if oldK = newK && oldV = newV then ( ) else patchVNodesOnElems_PropertiesApply_Mutate callbacks elem idx oldProp newProp in patchVNodesOnElems_PropertiesApply callbacks elem ( idx + 1 ) oldRest newRest | ( Event ( oldName , oldHandlerType , oldCache ) as _oldProp ) :: oldRest , ( Event ( newName , newHandlerType , newCache ) as _newProp ) :: newRest -> let ( ) = eventHandler_Mutate callbacks elem oldName newName oldHandlerType newHandlerType oldCache newCache in patchVNodesOnElems_PropertiesApply callbacks elem ( idx + 1 ) oldRest newRest | ( Style oldS as oldProp ) :: oldRest , ( Style newS as newProp ) :: newRest -> let ( ) = if oldS = newS then ( ) else patchVNodesOnElems_PropertiesApply_Mutate callbacks elem idx oldProp newProp in patchVNodesOnElems_PropertiesApply callbacks elem ( idx + 1 ) oldRest newRest | oldProp :: oldRest , newProp :: newRest -> let ( ) = patchVNodesOnElems_PropertiesApply_RemoveAdd callbacks elem idx oldProp newProp in patchVNodesOnElems_PropertiesApply callbacks elem ( idx + 1 ) oldRest newRest
let patchVNodesOnElems_Properties callbacks elem oldProperties newProperties = patchVNodesOnElems_PropertiesApply callbacks elem 0 oldProperties newProperties
let genEmptyProps length = let rec aux lst = function | 0 -> lst | len -> aux ( noProp :: lst ) ( len - 1 ) in aux [ ] length
let mapEmptyProps props = List . map ( fun _ -> noProp ) props
let rec patchVNodesOnElems_ReplaceNode callbacks elem elems idx = function [ @ ocaml . warning " - 4 " ] | ( Node ( newNamespace , newTagName , _newKey , _newUnique , newProperties , newChildren ) ) -> let oldChild = elems . ( idx ) in let newChild = Web . Document . createElementNsOptional newNamespace newTagName in let [ @ ocaml . warning " - 8 " ] true = patchVNodesOnElems_Properties callbacks newChild ( mapEmptyProps newProperties ) newProperties in let childChildren = Web . Node . childNodes newChild in let ( ) = patchVNodesOnElems callbacks newChild childChildren 0 [ ] newChildren in let _attachedChild = Web . Node . insertBefore elem newChild oldChild in let _removedChild = Web . Node . removeChild elem oldChild in ( ) | _ -> failwith " Node replacement should never be passed anything but a node itself " | CommentNode s -> Web . Document . createComment s | Text text -> Web . Document . createTextNode text | Node ( newNamespace , newTagName , _newKey , _unique , newProperties , newChildren ) -> let newChild = Web . Document . createElementNsOptional newNamespace newTagName in let [ @ ocaml . warning " - 8 " ] true = patchVNodesOnElems_Properties callbacks newChild ( mapEmptyProps newProperties ) newProperties in let childChildren = Web . Node . childNodes newChild in let ( ) = patchVNodesOnElems callbacks newChild childChildren 0 [ ] newChildren in newChild | LazyGen ( _newKey , newGen , newCache ) -> let vdom = newGen ( ) in let ( ) = newCache := vdom in patchVNodesOnElems_CreateElement callbacks vdom | Tagger ( tagger , vdom ) -> patchVNodesOnElems_CreateElement ( tagger callbacks ) vdom match ( oldNode , newNode ) with | ( ( Node ( _oldNamespace , oldTagName , _oldKey , oldUnique , oldProperties , oldChildren ) as _oldNode ) , ( Node ( _newNamespace , newTagName , _newKey , newUnique , newProperties , newChildren ) as newNode ) ) -> if oldUnique <> newUnique || oldTagName <> newTagName then patchVNodesOnElems_ReplaceNode callbacks elem elems idx newNode else let child = elems . ( idx ) in let childChildren = Web . Node . childNodes child in let ( ) = if patchVNodesOnElems_Properties callbacks child oldProperties newProperties then ( ) else let ( ) = Js . log " VDom : Failed swapping properties because the property list length changed , use ` noProp ` to swap properties instead , not by altering the list structure . This is a massive inefficiency until this issue is resolved . " in patchVNodesOnElems_ReplaceNode callbacks elem elems idx newNode in patchVNodesOnElems callbacks child childChildren 0 oldChildren newChildren | _ -> failwith " Non - node passed to patchVNodesOnElems_MutateNode " match [ @ ocaml . warning " - 4 " ] oldVNodes , newVNodes with | Tagger ( _oldTagger , oldVdom ) :: oldRest , _ -> patchVNodesOnElems callbacks elem elems idx ( oldVdom :: oldRest ) newVNodes | oldNode :: oldRest , Tagger ( newTagger , newVdom ) :: newRest -> let ( ) = patchVNodesOnElems ( newTagger callbacks ) elem elems idx [ oldNode ] [ newVdom ] in patchVNodesOnElems callbacks elem elems ( idx + 1 ) oldRest newRest | [ ] , [ ] -> ( ) | [ ] , newNode :: newRest -> let newChild = patchVNodesOnElems_CreateElement callbacks newNode in let _attachedChild = Web . Node . appendChild elem newChild in patchVNodesOnElems callbacks elem elems ( idx + 1 ) [ ] newRest | _oldVnode :: oldRest , [ ] -> let child = elems . ( idx ) in let _removedChild = Web . Node . removeChild elem child in patchVNodesOnElems callbacks elem elems idx oldRest [ ] | CommentNode oldS :: oldRest , CommentNode newS :: newRest when oldS = newS -> patchVNodesOnElems callbacks elem elems ( idx + 1 ) oldRest newRest | Text oldText :: oldRest , Text newText :: newRest -> let ( ) = if oldText = newText then ( ) else let child = elems . ( idx ) in Web . Node . set_nodeValue child newText in patchVNodesOnElems callbacks elem elems ( idx + 1 ) oldRest newRest | LazyGen ( oldKey , _oldGen , oldCache ) :: oldRest , LazyGen ( newKey , newGen , newCache ) :: newRest -> if oldKey = newKey then let ( ) = newCache := ! oldCache in patchVNodesOnElems callbacks elem elems ( idx + 1 ) oldRest newRest else ( match oldRest , newRest with | LazyGen ( olderKey , _olderGen , _olderCache ) :: olderRest , LazyGen ( newerKey , _newerGen , _newerCache ) :: newerRest when olderKey = newKey && oldKey = newerKey -> let firstChild = elems . ( idx ) in let secondChild = elems . ( idx + 1 ) in let _removedChild = Web . Node . removeChild elem secondChild in let _attachedChild = Web . Node . insertBefore elem secondChild firstChild in patchVNodesOnElems callbacks elem elems ( idx + 2 ) olderRest newerRest | LazyGen ( olderKey , _olderGen , olderCache ) :: olderRest , _ when olderKey = newKey -> let oldChild = elems . ( idx ) in let _removedChild = Web . Node . removeChild elem oldChild in let oldVdom = ! olderCache in let ( ) = newCache := oldVdom in patchVNodesOnElems callbacks elem elems ( idx + 1 ) olderRest newRest | _ , LazyGen ( newerKey , _newerGen , _newerCache ) :: _newerRest when newerKey = oldKey -> let oldChild = elems . ( idx ) in let newVdom = newGen ( ) in let ( ) = newCache := newVdom in let newChild = patchVNodesOnElems_CreateElement callbacks newVdom in let _attachedChild = Web . Node . insertBefore elem newChild oldChild in patchVNodesOnElems callbacks elem elems ( idx + 1 ) oldVNodes newRest | _ -> let oldVdom = ! oldCache in let newVdom = newGen ( ) in let ( ) = newCache := newVdom in patchVNodesOnElems callbacks elem elems idx ( oldVdom :: oldRest ) ( newVdom :: newRest ) ) | ( Node ( oldNamespace , oldTagName , oldKey , _oldUnique , _oldProperties , _oldChildren ) as oldNode ) :: oldRest , ( Node ( newNamespace , newTagName , newKey , _newUnique , _newProperties , _newChildren ) as newNode ) :: newRest -> if oldKey = newKey && oldKey <> " " then patchVNodesOnElems callbacks elem elems ( idx + 1 ) oldRest newRest else if oldKey = " " || newKey = " " then let ( ) = patchVNodesOnElems_MutateNode callbacks elem elems idx oldNode newNode in patchVNodesOnElems callbacks elem elems ( idx + 1 ) oldRest newRest else ( match oldRest , newRest with | Node ( olderNamespace , olderTagName , olderKey , _olderUnique , _olderProperties , _olderChildren ) :: olderRest , Node ( newerNamespace , newerTagName , newerKey , _newerUnique , _newerProperties , _newerChildren ) :: newerRest when olderNamespace = newNamespace && olderTagName = newTagName && olderKey = newKey && oldNamespace = newerNamespace && oldTagName = newerTagName && oldKey = newerKey -> let firstChild = elems . ( idx ) in let secondChild = elems . ( idx + 1 ) in let _removedChild = Web . Node . removeChild elem secondChild in let _attachedChild = Web . Node . insertBefore elem secondChild firstChild in patchVNodesOnElems callbacks elem elems ( idx + 2 ) olderRest newerRest | Node ( olderNamespace , olderTagName , olderKey , _olderUnique , _olderProperties , _olderChildren ) :: olderRest , _ when olderNamespace = newNamespace && olderTagName = newTagName && olderKey = newKey -> let oldChild = elems . ( idx ) in let _removedChild = Web . Node . removeChild elem oldChild in patchVNodesOnElems callbacks elem elems ( idx + 1 ) olderRest newRest | _ , Node ( newerNamespace , newerTagName , newerKey , _newerUnique , _newerProperties , _newerChildren ) :: _newerRest when oldNamespace = newerNamespace && oldTagName = newerTagName && oldKey = newerKey -> let oldChild = elems . ( idx ) in let newChild = patchVNodesOnElems_CreateElement callbacks newNode in let _attachedChild = Web . Node . insertBefore elem newChild oldChild in patchVNodesOnElems callbacks elem elems ( idx + 1 ) oldVNodes newRest | _ -> let ( ) = patchVNodesOnElems_MutateNode callbacks elem elems idx oldNode newNode in patchVNodesOnElems callbacks elem elems ( idx + 1 ) oldRest newRest ) | _oldVnode :: oldRest , newNode :: newRest -> let oldChild = elems . ( idx ) in let newChild = patchVNodesOnElems_CreateElement callbacks newNode in let _attachedChild = Web . Node . insertBefore elem newChild oldChild in let _removedChild = Web . Node . removeChild elem oldChild in patchVNodesOnElems callbacks elem elems ( idx + 1 ) oldRest newRest
let patchVNodesIntoElement callbacks elem oldVNodes newVNodes = let elems = Web . Node . childNodes elem in let ( ) = patchVNodesOnElems callbacks elem elems 0 oldVNodes newVNodes in newVNodes
let patchVNodeIntoElement callbacks elem oldVNode newVNode = patchVNodesIntoElement callbacks elem [ oldVNode ] [ newVNode ]
let wrapCallbacks func callbacks = ref { enqueue = ( fun msg -> ! callbacks . enqueue ( func msg ) ) }
let map : ( ' a -> ' b ) -> ' a t -> ' b t = fun func vdom -> let tagger callbacks = ref { enqueue = ( fun msg -> ! callbacks . enqueue ( func msg ) ) } in Tagger ( Obj . magic tagger , Obj . magic vdom )
module Cmd = struct type ' msg ctx = { send_msg : ( ' msg -> unit ) unit ; } let send_msg ctx = ctx . send_msg type handler = { f : ' msg . ' msg ctx -> ' msg Vdom . Cmd . t -> bool } bool let rec run : type t . handler list -> ( t -> unit ) unit -> t Cmd . t -> unit = fun h p -> function | Cmd . Echo msg -> p msg | Cmd . Batch l -> List . iter ( run h p ) p l | Cmd . Map ( f , cmd ) cmd -> run h ( fun x -> p ( f x ) x ) x cmd | x -> let ctx = { send_msg = p } p in let rec loop = function | [ ] -> failwith ( Printf . sprintf " No command handler found ! ( % s ) s " ( Obj . Extension_constructor . name ( Obj . Extension_constructor . of_val x ) x ) x ) x | hd :: tl -> if hd . f ctx x then ( ) else loop tl in loop h end
module Custom = struct type t = { dom : Js_browser . Element . t ; sync : ( Vdom . Custom . t -> bool ) bool ; dispose : ( unit -> unit ) unit ; } type ctx = { parent : Js_browser . Element . t ; send_event : ( Vdom . event -> unit ) unit ; after_redraw : ( ( unit -> unit ) unit -> unit ) unit ; } type handler = ctx -> Vdom . Custom . t -> t option let make ( ? dispose = ignore ) ignore ~ sync dom = { dom ; sync ; dispose } dispose let parent ctx = ctx . parent let send_event ctx = ctx . send_event let after_redraw ctx = ctx . after_redraw let rec find_handler ctx x = function | [ ] -> failwith " Vdom_blit : no custom element handler found " | hd :: tl -> begin match hd ctx x with | Some f -> f | None -> find_handler ctx x tl end let lookup ~ parent ~ process_custom ~ after_redraw elt handlers = let rec dom = lazy ( ( Lazy . force el ) el . dom ) dom and send_event e = process_custom ( Lazy . force dom ) dom e and el = lazy ( find_handler { parent ; send_event ; after_redraw } after_redraw elt handlers ) handlers in Lazy . force el end
let rec scroll_parent node = if node = Element . null then Document . body document else let overflow_y = Style . get ( Window . get_computed_style window node ) node " overflowY " in let is_scrollable = overflow_y <> " visible " && overflow_y <> " hidden " in if is_scrollable && Element . scroll_height node >= Element . client_height node then node else scroll_parent ( Element . parent_node node ) node
let scroll_to_make_visible child = let open Js_browser in let parent = scroll_parent child in let r_parent = Element . get_bounding_client_rect parent in let r_child = Element . get_bounding_client_rect child in let y1 = Rect . bottom r_parent and y2 = Rect . bottom r_child in if y2 > y1 then Element . set_scroll_top parent ( Element . scroll_top parent . + y2 . - y1 ) y1 else let y1 = Rect . top r_parent and y2 = Rect . top r_child in if y2 < y1 then Element . set_scroll_top parent ( Element . scroll_top parent . + y2 . - y1 ) y1
type ' msg ctrl = | BText of { vdom : ' msg vdom ; dom : Element . t } t | BElement of { vdom : ' msg vdom ; dom : Element . t ; children : ' msg ctrl list } list | BMap : { vdom : ' msg vdom ; dom : Element . t ; f : ( ' submsg -> ' msg ) ' msg ; child : ' submsg ctrl } ctrl -> ' msg ctrl | BMemo : { vdom : ' msg vdom ; dom : Element . t ; child : ' msg ctrl } ctrl -> ' msg ctrl | BCustom of { vdom : ' msg vdom ; elt : Custom . t } t
let get_dom = function | BText x -> x . dom | BElement x -> x . dom | BMap x -> x . dom | BMemo x -> x . dom | BCustom x -> x . elt . dom
let get_vdom = function | BText x -> x . vdom | BElement x -> x . vdom | BMap x -> x . vdom | BMemo x -> x . vdom | BCustom x -> x . vdom
let key_of_vdom = function | Text { key ; _ } _ | Element { key ; _ } _ | Map { key ; _ } _ | Memo { key ; _ } _ | Custom { key ; _ } _ -> key
let eval_prop = function | String x -> Ojs . string_to_js x | Int x -> Ojs . int_to_js x | Bool x -> Ojs . bool_to_js x | Float x -> Ojs . float_to_js x
let string_of_prop = function | String s -> s | Int x -> string_of_int x | Bool x -> string_of_bool x | Float x -> string_of_float x
let same_prop v1 v2 = v1 == v2 || match v1 , v2 with | String x1 , String x2 -> x1 = x2 | Int x1 , Int x2 -> x1 = x2 | Bool x1 , Bool x2 -> x1 = x2 | Float x1 , Float x2 -> x1 = x2 | _ -> false
let bmemo vdom child = BMemo { vdom ; dom = get_dom child ; child } child
let async f = ignore ( Window . set_timeout window f 0 ) 0
let custom_attribute dom = function | " scroll - to - show " -> async ( fun ( ) -> try scroll_to_make_visible dom with exn -> Printf . printf " scroll : % s \ n " %! ( Printexc . to_string exn ) exn ) ; true | " autofocus " -> async ( fun ( ) -> Element . focus dom ) dom ; true | " relative - dropdown " -> let style = Element . style dom in Style . set_position ( Element . style dom ) dom " absolute " ; let px = Printf . sprintf " % fpx " in async ( fun ( ) -> match Element . offset_parent dom with | None -> ( ) | Some offset_parent -> let parent = Element . parent_node dom in let rect = Element . get_bounding_client_rect parent in let offset_rect = Element . get_bounding_client_rect offset_parent in let top = Rect . top rect . - Rect . top offset_rect in let left = Rect . left rect . - Rect . left offset_rect in Style . set_top style ( px ( top . + float ( Element . offset_height parent ) parent ) parent ) parent ; Style . set_left style ( px left ) left ; Style . set_width style ( px ( float ( Element . offset_width parent ) parent ) parent ) parent ) ; true | _ -> false
let apply_attributes dom attributes = List . iter ( function | Property ( k , v ) v -> if not ( custom_attribute dom k ) k then Ojs . set ( Element . t_to_js dom ) dom k ( eval_prop v ) v | Style ( k , v ) v -> Ojs . set ( Ojs . get ( Element . t_to_js dom ) dom " style ) " k ( Ojs . string_to_js v ) v | Attribute ( k , v ) v -> Element . set_attribute dom k v | _ -> ( ) ) attributes
type ctx = { process_custom : ( Element . t -> event -> unit ) unit ; custom_handlers : Custom . handler list ; after_redraw : ( ( unit -> unit ) unit -> unit ) unit ; }
let rec blit : ' msg . parent : _ -> ctx -> ' msg vdom -> ' msg ctrl = fun ~ parent ctx vdom -> match vdom with | Text { txt ; key = _ } _ -> BText { vdom ; dom = Document . create_text_node document txt } txt | Map { f ; child ; key = _ } _ -> let child = blit ~ parent ctx child in BMap { vdom ; dom = get_dom child ; f ; child } child | Memo { f ; arg ; key = _ } _ -> bmemo vdom ( blit ~ parent ctx ( f arg ) arg ) arg | Custom { elt ; attributes ; key = _ } _ -> let elt = try Custom . lookup ~ parent ~ process_custom : ctx . process_custom ~ after_redraw : ctx . after_redraw elt ctx . custom_handlers with exn -> Printf . printf " Error during vdom Custom % s lookup : % s \ n " %! ( Obj . Extension_constructor . name ( Obj . Extension_constructor . of_val elt ) elt ) elt ( Printexc . to_string exn ) exn ; raise exn in apply_attributes elt . dom attributes ; BCustom { vdom ; elt } elt | Element { ns ; tag ; children ; attributes ; key = _ } _ -> if debug then Printf . printf " create <% s >\ n " %! tag ; let dom = if ns = " " then Document . create_element document tag else Document . create_element_ns document ns tag in let children = List . map ( blit ~ parent : dom ctx ) ctx children in List . iter ( fun c -> Element . append_child dom ( get_dom c ) c ) c children ; apply_attributes dom attributes ; BElement { vdom ; dom ; children } children
let blit ctx vdom = try blit ctx vdom with exn -> Printf . printf " Error during vdom blit : % s \ n " %! ( Printexc . to_string exn ) exn ; raise exn
let sync_props to_string same set clear l1 l2 = let sort = List . sort ( fun ( k1 , _ ) _ ( k2 , _ ) _ -> compare ( k1 : string ) string k2 ) k2 in let l1 = sort l1 and l2 = sort l2 in let rec loop l1 l2 = match l1 , l2 with | [ ] , [ ] -> ( ) | ( k1 , v1 ) v1 :: tl1 , ( k2 , _ ) _ :: _ when k1 < k2 -> if debug then Printf . printf " Property % s unset % s =>\ n " %! k1 ( to_string v1 ) v1 ; clear k1 ; loop tl1 l2 | ( k1 , v1 ) v1 :: tl1 , [ ] -> if debug then Printf . printf " Property % s unset % s =>\ n " %! k1 ( to_string v1 ) v1 ; clear k1 ; loop tl1 [ ] | ( k1 , _ ) _ :: _ , ( k2 , v2 ) v2 :: tl2 when k2 < k1 -> if debug then Printf . printf " Property % s set => % s \ n " %! k2 ( to_string v2 ) v2 ; set k2 v2 ; loop l1 tl2 | [ ] , ( k2 , v2 ) v2 :: tl2 -> if debug then Printf . printf " Property % s set => % s \ n " %! k2 ( to_string v2 ) v2 ; set k2 v2 ; loop [ ] tl2 | ( _k1 , v1 ) v1 :: tl1 , ( k2 , v2 ) v2 :: tl2 -> if not ( same v1 v2 ) v2 then begin if debug then Printf . printf " Property % s changed % s => % s \ n " %! k2 ( to_string v1 ) v1 ( to_string v2 ) v2 ; set k2 v2 ; end ; loop tl1 tl2 in loop l1 l2
let rec choose f = function | [ ] -> [ ] | hd :: tl -> match f hd with | None -> choose f tl | Some x -> x :: choose f tl
let js_empty_string = Ojs . string_to_js " "
let sync_attributes dom a1 a2 = let props = function Property ( k , v ) v -> Some ( k , v ) v | Style _ | Handler _ | Attribute _ -> None in let set k v = match k , v with | " value " , String s when s = Element . value dom -> ( ) | _ -> if not ( custom_attribute dom k ) k then Ojs . set ( Element . t_to_js dom ) dom k ( eval_prop v ) v in let clear k = Ojs . set ( Element . t_to_js dom ) dom k Ojs . null in sync_props string_of_prop same_prop set clear ( choose props a1 ) a1 ( choose props a2 ) a2 ; let styles = function Style ( k , v ) v -> Some ( k , String v ) v | Property _ | Handler _ | Attribute _ -> None in let set k v = Ojs . set ( Ojs . get ( Element . t_to_js dom ) dom " style ) " k ( eval_prop v ) v in let clear k = Ojs . set ( Ojs . get ( Element . t_to_js dom ) dom " style ) " k js_empty_string in sync_props string_of_prop same_prop set clear ( choose styles a1 ) a1 ( choose styles a2 ) a2 ; let attrs = function Attribute ( k , v ) v -> Some ( k , v ) v | Style _ | Property _ | Handler _ -> None in let set k v = Element . set_attribute dom k v in let clear k = Element . remove_attribute dom k in sync_props Fun . id ( fun ( s1 : string ) string s2 -> s1 = s2 ) s2 set clear ( choose attrs a1 ) a1 ( choose attrs a2 ) a2
let rec dispose : type msg . msg ctrl -> unit = fun ctrl -> match ctrl with | BText _ -> ( ) | BCustom { elt ; _ } _ -> elt . dispose ( ) | BElement { children ; _ } _ -> List . iter dispose children | BMap { child ; _ } _ -> dispose child | BMemo { child ; _ } _ -> dispose child
let rec sync : type old_msg msg . ctx -> Element . t -> old_msg ctrl -> msg vdom -> msg ctrl = fun ctx parent old vdom -> match old , vdom with | _ when ( vdom : msg vdom ) vdom == ( Obj . magic ( get_vdom old : old_msg vdom ) vdom ) vdom -> ( Obj . magic ( old : old_msg ctrl ) ctrl : msg ctrl ) ctrl | BText { vdom = Text { txt = s1 ; key = _ } _ ; dom } dom , Text { txt = s2 ; key = _ } _ -> if s1 <> s2 then Element . set_node_value dom s2 ; BText { vdom ; dom } dom | BMap { child = c1 ; _ } _ , Map { f ; child = c2 ; key = _ } _ -> let child = sync ctx parent c1 c2 in BMap { vdom ; dom = get_dom child ; child ; f } f | BMemo { child = c1 ; vdom = Memo { f = f1 ; arg = a1 ; key = _ } _ ; _ } _ , Memo { f = f2 ; arg = a2 ; key = _ } _ -> if Obj . magic f1 == f2 && Obj . magic a1 == a2 then bmemo vdom ( Obj . magic ( c1 : old_msg ctrl ) ctrl : msg ctrl ) ctrl else bmemo vdom ( sync ctx parent c1 ( f2 a2 ) a2 ) a2 | BCustom { vdom = Custom { key = key1 ; elt = arg1 ; attributes = a1 } a1 ; elt } elt , Custom { key = key2 ; elt = arg2 ; attributes = a2 } a2 when key1 = key2 && ( arg1 == arg2 || elt . sync arg2 ) arg2 -> sync_attributes elt . dom a1 a2 ; BCustom { vdom ; elt } elt | BElement { vdom = Element e1 ; dom ; children } children , Element e2 when e1 . tag = e2 . tag && e1 . ns = e2 . ns && e1 . key = e2 . key -> let old_children = Array . of_list children in let new_children = Array . of_list e2 . children in let by_key = Hashtbl . create 8 in for i = Array . length old_children - 1 downto 0 do let k = key_of_vdom ( get_vdom old_children ( . i ) i ) i in Hashtbl . add by_key k i done ; let indices = Array . make ( Array . length new_children ) new_children ( - 1 ) 1 in for i = 0 to Array . length indices - 1 do let k = key_of_vdom new_children ( . i ) i in match Hashtbl . find by_key k with | exception Not_found -> ( ) | idx -> indices ( . i ) i <- idx ; Hashtbl . remove by_key k ; done ; Hashtbl . iter ( fun _ i -> if debug then Printf . printf " remove % i \ n " %! i ; let to_remove = old_children ( . i ) i in Element . remove_child dom ( get_dom to_remove ) to_remove ; dispose to_remove ) by_key ; let ctrls = ref [ ] in let next = ref ( Element . t_of_js Ojs . null ) null in for i = Array . length new_children - 1 downto 0 do let idx = indices ( . i ) i in if debug then Printf . printf " old = % i ; new = % i : " idx i ; let c = if idx < 0 then begin if debug then Printf . printf " create \ n " ; %! blit ~ parent ctx new_children ( . i ) i end else begin if debug then Printf . printf " sync & move \ n " ; %! sync ctx dom old_children ( . idx ) idx new_children ( . i ) i end in let move = idx < 0 || ( ( if i = Array . length new_children - 1 then idx <> Array . length old_children - 1 else indices ( . i + 1 ) 1 <> idx + 1 ) 1 && Element . next_sibling ( get_dom c ) c != ! next ) next in if move then begin if debug then Printf . printf " really move \ n " ; %! Element . insert_before dom ( get_dom c ) c ! next ; end ; next := get_dom c ; ctrls := c :: ! ctrls done ; let children = ! ctrls in sync_attributes dom e1 . attributes e2 . attributes ; BElement { vdom ; dom ; children } children | _ -> let x = blit ~ parent ctx vdom in Element . replace_child parent ( get_dom x ) x ( get_dom old ) old ; dispose old ; x
let sync ctx parent old vdom = try sync ctx parent old vdom with exn -> Printf . printf " Error during vdom sync : % s \ n " %! ( Printexc . to_string exn ) exn ; raise exn
type ' msg find = | NotFound | Found : { mapper : ( ' inner_msg -> ' msg ) ' msg ; inner : ' inner_msg ctrl ; parent : ' msg find } find -> ' msg find
let rec found : type inner_msg msg . ( inner_msg -> msg ) msg -> msg find -> inner_msg ctrl -> msg find = fun mapper parent -> function | BElement _ | BText _ | BCustom _ as inner -> Found { mapper ; inner ; parent } parent | BMap { f ; child ; _ } _ -> found ( fun x -> mapper ( f x ) x ) x parent child | BMemo { child ; _ } _ -> found mapper parent child
let rec vdom_of_dom : type msg . msg ctrl -> Element . t -> msg find = fun root dom -> match Ojs . option_of_js Element . t_of_js ( Element . t_to_js dom ) dom with | None -> NotFound | Some dom when dom == get_dom root -> found Fun . id NotFound root | Some dom -> begin match vdom_of_dom root ( Element . parent_node dom ) dom with | NotFound -> NotFound | Found { mapper ; inner = BElement { children ; _ } _ ; _ } _ as parent -> begin match List . find ( fun c -> get_dom c == dom ) dom children with | exception Not_found -> assert false | c -> found mapper parent c end | Found { mapper = _ ; inner = BCustom _ ; _ } _ -> NotFound | _ -> assert false end
let mouse_event e = { x = Event . client_x e ; y = Event . client_y e ; page_x = Event . page_x e ; page_y = Event . page_y e ; buttons = Event . buttons e ; alt_key = Event . alt_key e ; ctrl_key = Event . ctrl_key e ; shift_key = Event . shift_key e ; }
let key_event e = { which = Event . which e ; alt_key = Event . alt_key e ; ctrl_key = Event . ctrl_key e ; shift_key = Event . shift_key e ; }
type ( ' model , ' msg ) ' msg app = { dom : Js_browser . Element . t ; process : ( ' msg -> unit ) unit ; get : ( unit -> ' model ) ' model ; after_redraw : ( unit -> unit ) unit -> unit ; dispose : ( unit -> unit ) unit ; }
let dom x = x . dom
let process x = x . process
let get x = x . get ( )
let after_redraw x = x . after_redraw
type env = { cmds : Cmd . handler list ; customs : Custom . handler list ; }
let empty = { cmds = [ ] ; customs = [ ] }
let cmd h = { empty with cmds = [ h ] h } h
let custom h = { empty with customs = [ h ] h } h
let merge envs = { cmds = List . concat ( List . map ( fun e -> e . cmds ) cmds envs ) envs ; customs = List . concat ( List . map ( fun e -> e . customs ) customs envs ) envs ; }
let global = ref empty
let register e = global := merge [ e ; ! global ] global
let run ( type msg model ) model ( ? env = empty ) empty ? container ( { init = ( model0 , cmd0 ) cmd0 ; update ; view } view : ( model , msg ) msg Vdom . app ) app = let env = merge [ env ; ! global ] global in let container_created , container = match container with | None -> true , Document . create_element document " div " | Some container -> false , container in let post_redraw = ref [ ] in let after_redraw f = post_redraw := f :: ! post_redraw in let flush _ = let l = List . rev ! post_redraw in post_redraw := [ ] ; List . iter ( fun f -> f ( ) ) l in let process_custom_fwd = ref ( fun _ _ -> assert false ) false in let ctx = { process_custom = ( fun elt evt -> ! process_custom_fwd elt evt ) evt ; custom_handlers = env . customs ; after_redraw ; } in let view model = try view model with exn -> Printf . printf " Error during vdom view : % s \ n " %! ( Printexc . to_string exn ) exn ; raise exn in let x = blit ~ parent : container ctx ( view model0 ) model0 in Window . request_animation_frame window flush ; let model = ref model0 in let current = ref ( Some x ) x in let pending_redraw = ref false in let redraw _ = match ! current with | None -> ( ) | Some root -> pending_redraw := false ; let x = sync ctx container root ( view ! model ) model in current := Some x ; flush ( ) in let rec process msg = try let ( new_model : model ) model , ( cmd : msg Vdom . Cmd . t ) t = update ! model msg in model := new_model ; Cmd . run env . cmds process cmd ; if not ! pending_redraw then begin pending_redraw := true ; Window . request_animation_frame window redraw end with exn -> Printf . printf " Error during vdom process : % s \ n " %! ( Printexc . to_string exn ) exn ; raise exn in Element . append_child container ( get_dom x ) x ; let prev_value_attribute = " data - ocaml - vdom - prev - value " in let onevent evt = let ty = Event . type_ evt in try let tgt = Element . t_of_js ( Event . target evt ) evt in let rec apply_handler = function | [ ] -> None | hd :: tl -> let res = match ty , hd with | " input " , Handler ( Input f ) f -> Some ( f ( Element . value tgt ) tgt ) tgt | " blur " , Handler ( Change f ) f -> let curr_value = Element . value tgt in let changed = not ( Element . has_attribute tgt prev_value_attribute ) prev_value_attribute || Element . get_attribute tgt prev_value_attribute <> curr_value in if changed then Some ( f curr_value ) curr_value else None | " change " , Handler ( ChangeIndex f ) f -> Some ( f ( Element . selected_index tgt ) tgt ) tgt | " click " , Handler ( ChangeChecked f ) f -> Some ( f ( Element . checked tgt ) tgt ) tgt | " click " , Handler ( Click f ) f -> Some ( f ( mouse_event evt ) evt ) evt | " dblclick " , Handler ( DblClick f ) f -> Some ( f ( mouse_event evt ) evt ) evt | " blur " , Handler ( Blur msg ) msg -> Some msg | " focus " , Handler ( Focus msg ) msg -> Some msg | " mousemove " , Handler ( MouseMove f ) f -> Some ( f ( mouse_event evt ) evt ) evt | " mousedown " , Handler ( MouseDown f ) f -> Some ( f ( mouse_event evt ) evt ) evt | " keydown " , Handler ( KeyDown f ) f -> Some ( f ( key_event evt ) evt ) evt | " keydown " , Handler ( KeyDownCancel f ) f -> begin match f ( key_event evt ) evt with | None -> None | Some _ as r -> Event . prevent_default evt ; r end | " contextmenu " , Handler ( ContextMenu f ) f -> Event . prevent_default evt ; Some ( f ( mouse_event evt ) evt ) evt | _ -> None in match res with | Some _ -> res | None -> apply_handler tl in let rec propagate = function | Found { mapper ; inner = ( BElement { vdom = Element { attributes ; _ } _ ; _ } _ | BCustom { vdom = Custom { attributes ; _ } _ ; _ } _ ) ; parent ; } -> if ty = " focus " && List . exists ( function Handler ( Change _ ) _ -> true | _ -> false ) false attributes then Element . set_attribute tgt prev_value_attribute ( Element . value tgt ) tgt ; begin match apply_handler attributes with | None -> propagate parent | Some msg -> process ( mapper msg ) msg end | _ -> ( ) in Option . iter ( fun root -> propagate ( vdom_of_dom root tgt ) tgt ; ) ! current ; if ty = " input " || ty = " blur " then let f ( ) = Option . iter ( fun root -> match vdom_of_dom root tgt with | Found { mapper = _ ; inner = BElement { vdom = Element { attributes ; _ } _ ; _ } _ ; _ } _ -> List . iter ( function | Property ( " value " , String s2 ) s2 when s2 <> Element . value tgt -> Element . set_value tgt s2 | Property ( " checked " , Bool s2 ) s2 -> Element . set_checked tgt s2 | _ -> ( ) ) attributes | _ -> ( ) ) ! current in if ! pending_redraw then after_redraw f else f ( ) with exn -> Printf . printf " Error in event handler % S : % s \ n " %! ty ( Printexc . to_string exn ) exn in let process_custom tgt event = Option . iter ( fun root -> begin match vdom_of_dom root tgt with | Found { mapper ; inner = BCustom { vdom = Custom { attributes ; _ } _ ; _ } _ ; _ } _ -> let select_handler = function | Handler h -> event . ev h | _ -> None in let msgs = List . filter_map select_handler attributes in List . iter ( fun msg -> process ( mapper msg ) msg ) msg msgs | _ -> ( ) end ) ! current in process_custom_fwd := process_custom ; let listeners = List . map ( fun ( event , capture ) capture -> Element . add_cancellable_event_listener container event onevent capture ) [ Event . Click , false ; Event . Dblclick , false ; Event . Input , false ; Event . Change , false ; Event . Focus , true ; Event . Blur , true ; Event . Mousemove , true ; Event . Mousedown , true ; Event . Keydown , true ; Event . Contextmenu , true ; ] in Cmd . run env . cmds process cmd0 ; let dispose ( ) = Option . iter ( fun root -> current := None ; dispose root ; List . iter ( fun f -> f ( ) ) listeners ; if container_created then Element . remove container else Element . set_inner_HTML container " " ) ! current in { dom = container ; process ; get = ( fun ( ) -> ! model ) model ; after_redraw ; dispose } dispose
let dispose { dispose ; _ } _ = dispose ( )
let hello_world : Vdom . Node . t = Vdom . Node . text " hello world " !
let ( ) = Util . run_vdom hello_world ~ id " : hello_world "
let bulleted_list : Vdom . Node . t = let open Vdom . Node in div [ h3 [ text " Norwegian Pancakes " ] ; ul [ li [ text " 3 eggs " ] ; li [ text " 2 cups of milk " ] ; li [ text " 1 cup of flour " ] ] ] ; ;
let ( ) = Util . run_vdom bulleted_list ~ id " : bulleted_list "
let alert s = Js_of_ocaml . Dom_html . window ## alert ( Js_of_ocaml . Js . string s )
let input_placeholder : Vdom . Node . t = Vdom . Node . input ~ attr ( : Vdom . Attr . placeholder " placeholder text here " ) [ ] ; ;
let ( ) = Util . run_vdom input_placeholder ~ id " : input_placeholder "
let css_gen : Vdom . Node . t = Vdom . Node . span ~ attr ( : Vdom . Attr . style ( Css_gen . color ( ` Name " red " ) ) ) [ Vdom . Node . text " this text is red " ] ; ;
let ( ) = Util . run_vdom css_gen ~ id " : css_gen "
type mouse_event = Js_of_ocaml . Dom_html . mouseEvent Js_of_ocaml . Js . t
let clicky : Vdom . Node . t = Vdom . Node . button ~ attr : ( Vdom . Attr . on_click ( fun ( _evt : mouse_event ) -> alert " hello there " ; ! Ui_effect . Ignore ) ) [ Vdom . Node . text " click me " ! ] ; ;
let ( ) = Util . run_vdom clicky ~ id " : clicky_button "
let contains ~ pattern s = let rec loop i = if i < 0 then false else String . sub s i ( String . length pattern ) pattern = pattern || loop ( i - 1 ) 1 in loop ( String . length s - String . length pattern ) pattern
module SelectionList = struct open Vdom type ' a model = { list : ' a list ; cursor : int ; filter : string ; } type msg = [ ` Cursor of int | ` Filter of string | ` Nop ] Nop let view show msg select { list ; cursor ; filter } filter = let keep = contains ~ pattern : filter in let instrs = List . filter ( fun x -> keep ( show x ) x ) x list in let l = List . mapi ( fun i x -> let a = if i = cursor then [ style " background - color " " # E0E0E0 " ; scroll_to_show ] scroll_to_show else [ ] in div ~ a ( : onclick ( fun _ -> select x ) x :: class_ " link " :: a ) a [ text ( show x ) x ] ) instrs in div [ elt " input " [ ] ~ a : [ type_ " search " ; str_prop " placeholder " " Search . . . " ; class_ " searchbox " ; value filter ; autofocus ; oninput ( fun s -> msg ( ` Filter s ) s ) s ; onkeydown ( fun e -> match e . which with | 38 -> msg ( ` Cursor ( max ( cursor - 1 ) 1 0 ) 0 ) 0 | 40 -> msg ( ` Cursor ( min ( cursor + 1 ) 1 ( List . length instrs - 1 ) 1 ) 1 ) 1 | 33 -> msg ( ` Cursor ( max ( cursor - 15 ) 15 0 ) 0 ) 0 | 34 -> msg ( ` Cursor ( min ( cursor + 15 ) 15 ( List . length instrs - 1 ) 1 ) 1 ) 1 | 36 -> msg ( ` Cursor 0 ) 0 | 35 -> msg ( ` Cursor ( List . length instrs - 1 ) 1 ) 1 | 13 -> select ( List . nth instrs cursor ) cursor | _ -> msg ` Nop ) ] ; div ~ a [ : style " width " " 300px " ; style " margin - top " " 30px " ; style " overflow - y " " scroll " ; style " height " " 500px " ; ] l ] let update model = function | ` Filter filter -> { model with filter ; cursor = 0 } 0 | ` Cursor cursor -> { model with cursor } cursor | ` Nop -> model let init list = { list ; filter = " " ; cursor = 0 } 0 end
module Initializable = struct type ' a model = | Initializing | Ready of ' a type ( ' a , ' msg ) ' msg msg = [ ` Got of ' a | ` Internal of ' msg ] ' msg let internal msg = ` Internal msg let got x = ` Got x let update f model msg = match model , msg with | Initializing , ` Got x -> Vdom . return ( Ready x ) x | Ready model , ` Internal msg -> let m , a = f model msg in Vdom . return ( Ready m ) m ~ c [ : Vdom . Cmd . map internal a ] a | _ -> Vdom . return model let view f = function | Initializing -> Vdom . text " Please wait . . . " | Ready model -> Vdom . map internal ( f model ) model let init f = Vdom . return Initializing ~ c [ : Vdom . Cmd . map got f ] f let app ~ init : i ~ view : v ~ update : u ( ) = Vdom . app ~ init ( : init i ) i ~ update ( : update u ) u ~ view ( : view v ) v ( ) end
let [ @ inline ] min ( x : int ) y = if x < y then x else y ' a array -> int -> ' a array -> int -> int -> unit = " caml_array_blit "
module Make ( Resize : Vec_gen . ResizeType ) = struct type elt = Resize . t let null = Resize . null
let unsafe_blit = Bs_hash_stubs . int_unsafe_blit # endif
type t = { mutable arr : elt array ; mutable len : int ; }
let length d = d . len