@@ -163,6 +163,9 @@ let highlight_location loc =
163163let append colorize output cl s =
164164 Dom. appendChild output (Tyxml_js.To_dom. of_element (colorize ~a_class: cl s))
165165
166+ let append_to_console s =
167+ Firebug. console##log (Js. string s)
168+
166169module History = struct
167170 let data = ref [| " " |]
168171
@@ -221,6 +224,12 @@ let run _ =
221224 let sharp_ppf = Format. formatter_of_out_channel sharp_chan in
222225 let caml_chan = open_out " /dev/null1" in
223226 let caml_ppf = Format. formatter_of_out_channel caml_chan in
227+ let binsharp_chan = open_out " /dev/null2" in
228+ let binsharp_ppf = Format. formatter_of_out_channel binsharp_chan in
229+ let bincaml_chan = open_out " /dev/null3" in
230+ let bincaml_ppf = Format. formatter_of_out_channel bincaml_chan in
231+ let consolecaml_chan = open_out " /dev/null3" in
232+ let consolecaml_ppf = Format. formatter_of_out_channel consolecaml_chan in
224233 let execute () =
225234 let content = Js. to_string textbox##.value##trim in
226235 let content' =
@@ -240,9 +249,28 @@ let run _ =
240249 resize ~container ~textbox ()
241250 >> = fun () ->
242251 container##.scrollTop := container##.scrollHeight;
243- textbox##focus ;
244252 Lwt. return_unit
245253 in
254+ let execute_callback mode content =
255+ let content' =
256+ let len = String. length content in
257+ if try content <> " " && content.[len - 1 ] <> ';' && content.[len - 2 ] <> ';'
258+ with _ -> true
259+ then content ^ " ;;"
260+ else if try content <> " " && content.[len - 1 ] = ';' && content.[len - 2 ] <> ';'
261+ with _ -> true
262+ then content ^ " ;"
263+ else content
264+ in match mode with
265+ | " internal" -> JsooTop. execute true ~pp_code: binsharp_ppf ~highlight_location bincaml_ppf content'
266+ | " console" -> JsooTop. execute true ~pp_code: binsharp_ppf ~highlight_location consolecaml_ppf content'
267+ | " toplevel" -> (
268+ current_position := output##.childNodes##.length;
269+ History. push content;
270+ JsooTop. execute true ~pp_code: sharp_ppf ~highlight_location caml_ppf content';
271+ )
272+ | _ -> ()
273+ in
246274 let history_down _e =
247275 let txt = Js. to_string textbox##.value in
248276 let pos = textbox##.selectionStart in
@@ -317,6 +345,7 @@ let run _ =
317345 Sys_js. set_channel_flusher sharp_chan (append Colorize. ocaml output " sharp" );
318346 Sys_js. set_channel_flusher stdout (append Colorize. text output " stdout" );
319347 Sys_js. set_channel_flusher stderr (append Colorize. text output " stderr" );
348+ Sys_js. set_channel_flusher consolecaml_chan append_to_console;
320349 let readline () =
321350 Js.Opt. case
322351 (Dom_html. window##prompt (Js. string " The toplevel expects inputs:" ) (Js. string " " ))
@@ -329,6 +358,11 @@ let run _ =
329358 setup_printers () ;
330359 History. setup () ;
331360 textbox##.value := Js. string " " ;
361+ (* Add callback*)
362+ Js.Unsafe. global##.executecallback := (object % js
363+ val execute = Js. wrap_meth_callback
364+ (fun _ mode content -> execute_callback (Js. to_string mode) (Js. to_string content))
365+ end);
332366 (* Run initial code if any *)
333367 try
334368 let code = List. assoc " code" (parse_hash () ) in
@@ -342,8 +376,16 @@ let run _ =
342376 (Js. string (Printexc. to_string exc))
343377 exc
344378
379+
380+
345381let _ =
346382 Dom_html. window##.onload :=
347383 Dom_html. handler (fun _ ->
348384 run () ;
349- Js. _false)
385+ Js. _false);
386+ Js.Unsafe. global##.toplevelcallback := (object % js
387+ val setup_toplevel = Js. wrap_meth_callback
388+ (fun () -> setup_toplevel () )
389+ val reset_toplevel = Js. wrap_meth_callback
390+ (fun () -> run () )
391+ end)
0 commit comments