Update bench.
[why3.git] / examples / gcd / jsmain.ml
blob304259473783086db9794a2d37b58c79941ab839
3 (* computation part *)
5 let compute_result text =
6 try
7 let a,b = Scanf.sscanf text "%d %d" (fun x y -> x,y) in
8 string_of_int (EuclideanAlgorithm63.euclid a b)
9 with _ -> "exception"
13 (* HTML rendering *)
15 module Html = Js_of_ocaml.Dom_html
16 module Js = Js_of_ocaml.Js
17 module Dom = Js_of_ocaml.Dom
19 let node x = (x : #Dom.node Js.t :> Dom.node Js.t)
21 let (<|) e l = List.iter (fun c -> Dom.appendChild e c) l; node e
23 let html_of_string (d : Html.document Js.t) (s:string) =
24 d##createElement (Js.string "p") <|
25 [node (d##createTextNode (Js.string s))]
27 let replace_child p n =
28 Js.Opt.iter (p##.firstChild) (fun c -> Dom.removeChild p c);
29 Dom.appendChild p n
31 let onload (_event : #Html.event Js.t) : bool Js.t =
32 let d = Html.document in
33 let body =
34 Js.Opt.get (d##getElementById(Js.string "test"))
35 (fun () -> assert false) in
36 let textbox = Html.createTextarea d in
37 textbox##.rows := 20; textbox##.cols := 100;
38 let preview = Html.createDiv d in
39 preview##.style##.border := Js.string "1px black";
40 preview##.style##.padding := Js.string "5px";
41 Dom.appendChild body textbox;
42 Dom.appendChild body (Html.createBr d);
43 Dom.appendChild body preview;
44 let rec dyn_preview old_text n =
45 let text = Js.to_string (textbox##.value) in
46 let n =
47 if text <> old_text then
48 begin
49 begin try
50 let rendered =
51 html_of_string d (compute_result text)
53 replace_child preview rendered
54 with _ -> ()
55 end;
57 end
58 else
59 max 0 (n - 1)
61 Lwt.bind
62 (Js_of_ocaml_lwt.Lwt_js.sleep (if n = 0 then 0.5 else 0.1))
63 (fun () -> dyn_preview text n)
65 let (_ : 'a Lwt.t) = dyn_preview "" 0 in Js._false
67 let (_ : unit) = Html.window##.onload := Html.handler onload