Skip to content

Commit

Permalink
Add Sys.override_argv, needed for toplevel_expect_tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
xclerc committed Sep 20, 2019
1 parent 0fe3689 commit 3c965c3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/core_sys.ml
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,18 @@ external opaque_identity : 'a -> 'a = "%opaque"
module Private = struct
let unix_quote = unix_quote
end

[%%if ocaml_version < (4, 09, 0)]
let override_argv args =
let len = Array.length args in
assert (len <= Array.length Sys.argv);
Array.blit ~src:args ~src_pos:0 ~dst:Sys.argv ~dst_pos:0 ~len;
(Caml.Obj.truncate [@ocaml.alert "-deprecated"]) (Obj.repr Sys.argv) len;
Arg.current := 0;
;;
[%%else]
external caml_sys_modify_argv : string array -> unit = "caml_sys_modify_argv";;
let override_argv new_argv =
caml_sys_modify_argv new_argv;
Arg.current := 0
[%%endif]
12 changes: 12 additions & 0 deletions src/core_sys.mli
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,18 @@ val home_directory : unit -> string
*)
external opaque_identity : 'a -> 'a = "%opaque"

(** [override_argv new_argv] makes subsequent calls to {!get_argv} return [new_argv].
Prior to OCaml version 4.09, this function has two noteworthy behaviors:
- it may raise if the length of [new_argv] is greater than the length of [argv] before
the call;
- it re-uses and mutates the previous [argv] value instead of using the new one; and
- it even mutates its length, which can be observed by inspecting the array returned
by an earlier call to {!get_argv}.
*)
val override_argv : string array -> unit


(**/**)

Expand Down

0 comments on commit 3c965c3

Please sign in to comment.