diff --git a/src/bin/owi.ml b/src/bin/owi.ml index 054037c6d..ed145d89b 100644 --- a/src/bin/owi.ml +++ b/src/bin/owi.ml @@ -39,6 +39,10 @@ let files = let f = existing_non_dir_file in Cmdliner.Arg.(value & pos_all f [] (info [] ~doc)) +let emit_files = + let doc = "emit (.wat) files from corresponding (.wasm) files" in + Cmdliner.Arg.(value & flag & info [ "o" ] ~doc) + let no_exhaustion = let doc = "no exhaustion tests" in Cmdliner.Arg.(value & flag & info [ "no-exhaustion" ] ~doc) @@ -245,7 +249,7 @@ let wasm2wat_cmd = let man = [] @ shared_man in Cmd.info "wasm2wat" ~version ~doc ~sdocs ~man in - Cmd.v info Term.(const Cmd_wasm2wat.cmd $ files) + Cmd.v info Term.(const Cmd_wasm2wat.cmd $ files $ emit_files) let wat2wasm_cmd = let open Cmdliner in diff --git a/src/cmd/cmd_wasm2wat.ml b/src/cmd/cmd_wasm2wat.ml index 4559e79d5..c89c822f4 100644 --- a/src/cmd/cmd_wasm2wat.ml +++ b/src/cmd/cmd_wasm2wat.ml @@ -4,13 +4,19 @@ open Syntax -let cmd_one file = +(* Utility function to handle writing to a file or printing to stdout *) + +let cmd_one emitfile file = let ext = Fpath.get_ext file in + let wat_file = Fpath.set_ext "wat" file in + match ext with | ".wasm" -> let* m = Parse.Binary.Module.from_file file in let m = Binary_to_text.modul m in - Ok (Fmt.pr "%a@\n" Text.pp_modul m) + let wat_module = Fmt.str "%a@\n" Text.pp_modul m in + if emitfile then Bos.OS.File.write wat_file wat_module + else Ok (Fmt.pr "%s" wat_module) | ext -> Error (`Unsupported_file_extension ext) -let cmd files = list_iter cmd_one files +let cmd files emitfile = list_iter (cmd_one emitfile) files diff --git a/src/cmd/cmd_wasm2wat.mli b/src/cmd/cmd_wasm2wat.mli index 88ae4f0e4..08e7444ae 100644 --- a/src/cmd/cmd_wasm2wat.mli +++ b/src/cmd/cmd_wasm2wat.mli @@ -2,4 +2,4 @@ (* Copyright © 2021-2024 OCamlPro *) (* Written by the Owi programmers *) -val cmd : Fpath.t list -> unit Result.t +val cmd : Fpath.t list -> bool -> unit Result.t