Commit fcdbd679 authored by Kim Nguyễn's avatar Kim Nguyễn
Browse files

Minor tweaks and add the file driver/cduceeditor_js_runtime.ml that was...

Minor tweaks and add the file driver/cduceeditor_js_runtime.ml that was previously not added to the repository because of a buggy .gitignore entry.
parent acbd4cda
configure.log
cduce
build_flags
cduce*_js_runtime*
cduce*_js_runtime
cduce*_js_runtime.js
cduce*_js_runtime.bytecode
tests/js/ace-builds/**
tests/js/*.js
!tests/js/cducetop_js_runtime.js
......
let log s =
Firebug.console ## log (Js.string s)
let output buff c =
match c with
'<' -> Buffer.add_string buff "&lt;"
| '>' -> Buffer.add_string buff "&gt;"
| '\'' -> Buffer.add_string buff "&apos;"
| '"' -> Buffer.add_string buff "&quot;"
| '&' -> Buffer.add_string buff "&apos;"
| _ -> Buffer.add_char buff c
let output_str buff str idx len =
for i = idx to idx + len - 1 do
output buff str.[i]
done
let make_ppf_fun elem style =
let otag = ref false in
let buff = Buffer.create 256 in
let output str idx len =
if not !otag then begin
Buffer.add_string buff "<span class='";
Buffer.add_string buff style;
Buffer.add_string buff "'>";
otag:=true
end;
output_str buff str idx len
in
let flush () =
if !otag then begin
Buffer.add_string buff "</span>";
otag := false
end;
let s = Buffer.contents buff in
Buffer.clear buff;
elem ## innerHTML <- ((elem ## innerHTML) ## concat (Js.string s))
in
(output, flush)
let make_ppf elem style =
let out, flush = make_ppf_fun elem style in
Format.make_formatter out flush
let make_out_channel oc elem style =
let out, flush = make_ppf_fun elem style in
let cb str =
out str 0 (String.length str);
flush ()
in
Sys_js.set_channel_flusher oc cb
let get_opt o =
Js.Opt.get o (fun () -> assert false)
let coord_index str pos =
let rec loop i row col =
if i == pos then (row, col) else
if str.[i] == '\n' then
loop (i+1) (row + 1) 0
else
loop (i+1) row (col+1)
in
loop 0 0 0
(* Architecture of the page:
1) Main editor window (ace.io)
2) A compile & run button, with id "compile_button"
3) A div emulating the console for standard output, with id "output_area"
4) A iframe pointing to a static "sandbox page" that the user can break if (s)he wants, with
id "sandbox_iframe". The inner doccument is set at the begining of the program.
TODO: resolve names earlier.
*)
let setup_editor outfmt errfmt button_id =
let button =
get_opt (Dom_html.document ## getElementById (Js.string button_id))
in
let handler = Dom.handler
(fun ev ->
let global = Js.Unsafe.global in
let editor = Js.Unsafe.get global (Js.string "editor") in
let code = Js.to_string (Js.Unsafe.meth_call editor "getValue" [| |]) in
try
Format.printf "Compiling program ...%!";
Librarian.compile_run ~unload:true false (Ident.U.mk "Main") (`String code);
Format.printf " ok@\n%!";
Format.pp_print_flush errfmt ();
Format.pp_print_flush outfmt ();
flush stdout;
flush stderr;
Js._false
with
(Cduce_loc.Location ((_,i,j), _, _) |
Ulexer.Error (i,j,_) ) as e ->
let xi, yi = coord_index code i in
let xj, yj = coord_index code j in
ignore (Js.Unsafe.meth_call global "highlightError"
(Array.map Js.Unsafe.inject [| xi; yi; xj; yj |]));
Format.printf " failed@\n%!";
Cduce.print_exn errfmt e;
Format.pp_print_flush errfmt ();
Js._false
| Failure s -> log(s); Js._false
)
in
button ## onclick <- handler
let () =
Cduce_config.init_all ();
Cduce_js.use ();
let outputdiv = get_opt (Dom_html.document ## getElementById (Js.string "output_area")) in
let fmt = make_ppf outputdiv "stdout" in
let fmt_err = make_ppf outputdiv "stderr" in
make_out_channel stdout outputdiv "stdout";
make_out_channel stderr outputdiv "stderr";
Cduce.toplevel := false;
Librarian.run_loaded := true;
(*Cduce_js.main_document := begin
match Dom_html.tagged (get_opt
(Dom_html.document ## getElementById (Js.string "sandbox_iframe")))
with
Dom_html.Iframe iframe -> Some (get_opt (iframe ## contentDocument))
| _ -> None
end;*)
setup_editor fmt fmt_err "compile_button"
......@@ -73,7 +73,7 @@
height: 46vh;
width:100%;
margin:0;
padding:0;
padding:0.5em;
}
.error {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment