Skip to content

Commit 60c2b5c

Browse files
authored
Merge pull request #48 from jbdoderlein/dev
New toplevel access
2 parents 435d5f9 + e80b6e2 commit 60c2b5c

4 files changed

Lines changed: 56 additions & 23 deletions

File tree

src/js/editor_change.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,17 @@ const buttons =document.getElementById("menu-button");
1212
var MOBILE = false;
1313

1414

15-
function parse(str, editor) {
16-
let textarea = document.getElementById('userinput');
15+
function parse(str) {
1716
let cmd = str.split(';;\n');
18-
const ke = new KeyboardEvent("keydown", {bubbles: true, cancelable: true, keyCode: 13});
1917
for (let i = 0; i < cmd.length; i++) {
2018
if (!cmd[i].endsWith(';;')) cmd[i] += ';;';
21-
textarea.value = cmd[i];
22-
textarea.dispatchEvent(ke);
23-
}
24-
if (editor != null) {
25-
setTimeout(function () {
26-
editor.focus();
27-
}, 5);
19+
executecallback.execute("toplevel", cmd[i]);
2820
}
2921
}
3022

3123
function reset_ocaml() {
32-
document.getElementById('output').innerHTML = ''
33-
let textarea = document.getElementById('userinput');
34-
const ke = new KeyboardEvent("keydown", {bubbles: true, cancelable: true, ctrlKey: true, keyCode: 75});
35-
textarea.dispatchEvent(ke);
24+
document.getElementById('output').innerHTML = '';
25+
toplevelcallback.setup();
3626
}
3727

3828
function changefontsize(id, a) {

toplevel_build/compile.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,38 +49,38 @@
4949

5050
Build_Toplevel () {
5151
BUILD_VERSION=$1
52-
52+
5353
cd js_of_ocaml/toplevel/examples/lwt_toplevel
54-
54+
5555
# Check if OCaml version $BUILD_VERSION is installed
5656
if [[ -d ~/.opam/$BUILD_VERSION ]]; then
5757
VERSION_ALREADY_INSTALLED=true
5858
else
5959
VERSION_ALREADY_INSTALLED=false
6060
fi
61-
61+
6262
# Switch to OCaml version $BUILD_VERSION
6363
if [[ $VERSION_ALREADY_INSTALLED = true ]]; then
6464
opam switch $BUILD_VERSION
6565
else
6666
echo "Installing OCaml version $BUILD_VERSION ..."
6767
opam switch create $BUILD_VERSION
6868
fi
69-
69+
7070
# Install dependencies
7171
echo "Installing dependencies ..."
7272
eval $(opam env)
7373
opam install --yes tyxml ocp-indent higlo cohttp lwt tyxml reactiveData yojson graphics menhirLib cmdliner ppxlib react menhir dune
7474
eval $(opam env)
75-
75+
7676
# Build
7777
echo "Building toplevel-$BUILD_VERSION.js ..."
7878
dune clean
7979
dune build
80-
80+
8181
# Remove OCaml version $BUILD_VERSION if it wasn't previously installed
8282
[[ $VERSION_ALREADY_INSTALLED = false && $KEEP = false ]] && opam switch remove --yes $BUILD_VERSION
83-
83+
8484
# Save build
8585
cd ../../../..
8686
[[ ! -d "$OUTPUT_DIR" ]] && mkdir -p "$OUTPUT_DIR"

toplevel_build/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
--file %{dep:test_dynlink.js}
8181
--export %{dep:export.txt}
8282
--toplevel
83+
--opt 3
8384
--disable shortvar
8485
--disable genprim
8586
+base/runtime.js

toplevel_build/toplevel.ml

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ let highlight_location loc =
163163
let 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+
166169
module 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+
345381
let _ =
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

Comments
 (0)