Skip to content

Commit

Permalink
add -o option to emit .wat files
Browse files Browse the repository at this point in the history
  • Loading branch information
vasucp1207 committed Aug 21, 2024
1 parent 7101e12 commit 0e7af4f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/bin/owi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions src/cmd/cmd_wasm2wat.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/cmd/cmd_wasm2wat.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0e7af4f

Please sign in to comment.