Skip to content

Commit

Permalink
fix: rtop on OCaml 5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro committed Jul 8, 2024
1 parent 5828878 commit 4117b79
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
6 changes: 6 additions & 0 deletions rtop/dune
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
utop
reason.ocaml-migrate-parsetree))

(rule
(targets reason_toploop.ml)
(deps reason_toploop.cppo.ml)
(action
(run cppo -V OCAML:%{ocaml_version} %{deps} -o %{targets})))

(executable
(name rtop)
(public_name rtop)
Expand Down
16 changes: 14 additions & 2 deletions rtop/reason_toploop.ml → rtop/reason_toploop.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,20 @@ let main () =
print_endline "Reason is incompatible with camlp4!"
else begin
Toploop.parse_toplevel_phrase := Reason_util.correctly_catch_parse_errors
(fun x -> Reason_toolchain.To_current.copy_toplevel_phrase
(Reason_toolchain.RE.toplevel_phrase x));
(fun x ->
let r = Reason_toolchain.To_current.copy_toplevel_phrase
(Reason_toolchain.RE.toplevel_phrase x)
in
#if OCAML_VERSION >= (5,2,0)
(* NOTE(anmonteiro): after https://github.com/ocaml/ocaml/pull/12029, we get a
Fatal error: exception Invalid_argument("index out of bounds")
Raised by primitive operation at Toploop.ends_with_lf in file "toplevel/toploop.ml"
Setting `lex_eof_reached` seems to avoid whatever check upstream is doing. *)
x.lex_eof_reached <- true;
#endif
r);
Toploop.parse_use_file := Reason_util.correctly_catch_parse_errors
(fun x -> List.map Reason_toolchain.To_current.copy_toplevel_phrase
(Reason_toolchain.RE.use_file x));
Expand Down
23 changes: 9 additions & 14 deletions rtop/rtop.ml
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
let () = UTop.require ["reason.ocaml-migrate-parsetree"; "menhirLib";]

let () = try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH") with | Not_found -> ();;

let () = UTop.require ["reason.easy_format"; "reason";]

let () = Reason_toploop.main ()

let () = Reason_utop.init_reason ()

let () = print_string
let () =
UTop.require ["reason.ocaml-migrate-parsetree"; "menhirLib";];
try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH") with | Not_found -> ();
UTop.require ["reason.easy_format"; "reason";];
Reason_toploop.main ();
Reason_utop.init_reason ();
print_string
"
___ _______ ________ _ __
/ _ \\/ __/ _ | / __/ __ \\/ |/ /
Expand All @@ -20,6 +16,5 @@ let () = print_string
> let myVar = \"Hello Reason!\";
> let myList: list(string) = [\"first\", \"second\"];
> #use \"./src/myFile.re\"; /* loads the file into here */
"

let () = UTop_main.main ()
";
UTop_main.main ()
19 changes: 10 additions & 9 deletions test/rtopIntegration.t
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# Context: https://github.com/reasonml/reason/pull/674
Context: https://github.com/reasonml/reason/pull/674

# We can't directly call `rtop -stdin` because it circumvents what we're trying to
# test. See rtop.sh for the reason. We want to make sure utop's reason
# integration is legit,
We can't directly call `rtop -stdin` because it circumvents what we're trying to
test. See rtop.sh for the reason. We want to make sure utop's reason
integration is legit,

# `utop -stdin` wouldn't work because it somehow processes the code before
# invoking the reason plugin, so `echo someReasonCode | utop -stdin` would
# always error.
`utop -stdin` wouldn't work because it somehow processes the code before
invoking the reason plugin, so `echo someReasonCode | utop -stdin` would
always error.

Given the above, we're gonna test that utop integration works by piping code
into it and asserting the existence of some output.

# Given the above, we're gonna test that utop integration works by piping code
# into it and asserting the existence of some output.
$ echo "let f = a => a;" | rtop | grep -o "let f: 'a => 'a = <fun>;"
let f: 'a => 'a = <fun>;

Expand Down

0 comments on commit 4117b79

Please sign in to comment.