Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --emit-file option to emit .wat files #413

Merged
merged 5 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## unreleased

- add `--emit-file` option to `wasm2wat` to emit .wat files
- adds flags `--fail-on-trap-only` and `fail-on-assertion-only`
- parameterize the `Thread` module on the symbolic memory and the `Choice_monad` module on a Thread
- adds a `owi_char` function to create char symbolic value
Expand Down
2 changes: 1 addition & 1 deletion example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ COMMANDS
validate [--debug] [OPTION]… [ARG]…
Validate a module

wasm2wat [OPTION]… [ARG]…
wasm2wat [--emit-file] [OPTION]… [ARG]…
Generate a text format file (.wat) from a binary format file
(.wasm)

Expand Down
6 changes: 5 additions & 1 deletion example/wasm2wat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ NAME
file (.wasm)

SYNOPSIS
owi wasm2wat [OPTION]… [ARG]…
owi wasm2wat [--emit-file] [OPTION]… [ARG]…

OPTIONS
--emit-file
emit (.wat) files from corresponding (.wasm) files

COMMON OPTIONS
--help[=FMT] (default=auto)
Expand Down
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 [ "emit-file" ] ~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
10 changes: 7 additions & 3 deletions src/cmd/cmd_wasm2wat.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

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)
if emitfile then Bos.OS.File.writef wat_file "%a@\n" Text.pp_modul m
else Ok (Fmt.pr "%a@\n" Text.pp_modul m)
| 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
2 changes: 1 addition & 1 deletion test/help/help.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ no subcommand should print help
validate [--debug] [OPTION]… [ARG]…
Validate a module

wasm2wat [OPTION]… [ARG]…
wasm2wat [--emit-file] [OPTION]… [ARG]…
Generate a text format file (.wat) from a binary format file
(.wasm)

Expand Down
21 changes: 21 additions & 0 deletions test/wasm2wat/emit.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
test that we can emit to a file:
$ owi wasm2wat m.wasm --emit-file
$ cat m.wat
(module

(type (sub final (func (param i32) (param i32) (result i32))))

(type (sub final (func)))
(func (param i32) (param i32) (result i32)
local.get 0
local.get 1
i32.add
)
(func
i32.const 22
i32.const 20
call 0
drop
)
(start 1)
)
2 changes: 1 addition & 1 deletion test/wasm2wat/not_exists.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$ owi wasm2wat idontexist.wat
owi: no file 'idontexist.wat'
Usage: owi wasm2wat [OPTION]… [ARG]…
Usage: owi wasm2wat [--emit-file] [OPTION]… [ARG]…
Try 'owi wasm2wat --help' or 'owi --help' for more information.
[124]
$ owi wasm2wat bad.ext
Expand Down