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

wasm-tools parse support for .wast file. #1395

Closed
doehyunbaek opened this issue Jan 31, 2024 · 7 comments · Fixed by #1460
Closed

wasm-tools parse support for .wast file. #1395

doehyunbaek opened this issue Jan 31, 2024 · 7 comments · Fixed by #1460

Comments

@doehyunbaek
Copy link

From current README, I got the impression that both wat and wast crates are accessible from subcommand parse:

| `wasm-tools parse` | [wat] and [wast] | Translate the WebAssembly text format to binary |

But as I try out the command from the shell and also looking through the code, this doesn't seem currently possible:

wasm-tools/src/lib.rs

Lines 67 to 72 in 4975360

if let Some(path) = &self.input {
if path != Path::new("-") {
let bytes = wat::parse_file(path)?;
return Ok(bytes);
}
}

Is it intended? If not, may I contribute a feature addition?

@alexcrichton
Copy link
Member

Ah yes so *.wast support isn't surfaced in the CLI at this time. The wat crate is itself built on the wast crate (it's a very small shim) and the wast crate has support for both the text syntax and the *.wast assertion syntax. (sorry if that's confusingly named)

I'd be happy to see *.wast support in the CLI, but I'm not entirely sure what that might look like. Do you have an idea in mind though of what wasm-tools parse foo.wast might do?

@doehyunbaek
Copy link
Author

I did not have particular output format in mind, but I found that our team uses wast2json tool from wabt.

What do you think about implementing wast support as a drop-in replacement for wast2json? Pro would be that it would be really useful for the migration of the users.

@alexcrichton
Copy link
Member

Sounds reasonable to me! If you're up for making a PR that'd be most welcome as well, I'd be happy to review.

If I might throw out an initial idea to help shape this a bit:

  • I can't think of a better name than wasm-tools wast2json myself
  • Perhaps the output is JSON to stdout by default, -o outputs the JSON to a file, and --module-dir is a required argument of where to put all the modules?
  • I'd recommend using serde_json and serde::{Deserialize,Serialize} for JSON things in Rust
  • I'd prefer to keep the serde bits out of wast if possible (keeps its dependencies slim), so what I'd recommend is a src/bin/wasm-tools/wast2json.rs which has all the serde bits. That'd use Wast to convert to a structure which has #[derive(Serialize)] and then it could all be emitted as JSON.

Please feel free of course to change any details as well, just some initial suggestions!

@doehyunbaek
Copy link
Author

I'll try to follow up with a PR with respect to your suggestion. Thanks!

alexcrichton added a commit to alexcrichton/wasm-tools that referenced this issue Mar 19, 2024
This commit is an implementation of bytecodealliance#1395 which brings wabt's
`wast2json` functionality to `wasm-tools`. This is implemented entirely
in the CLI since if using the crate it's already got all the
functionality necessary for this. Additionally this PR attempts to be
pretty close to `wast2json` from wabt in terms of structure and syntax.
The main difference is that wabt fills out the expected return types of
an operation when the operation isn't supposed to return (e.g.
`assert_trap`), but `wasm-tools` won't do that as we don't have that
information so easily available.

This is tested by adding `*.json` files to the `snapshots` directory
with example outputs from all known `*.wast` tests. Everything should be
included except for the `component-model` tests at this time which don't
have support in printing the args/results.

Closes bytecodealliance#1395
github-merge-queue bot pushed a commit that referenced this issue Mar 19, 2024
* Implement a `wast2json` subcommand

This commit is an implementation of #1395 which brings wabt's
`wast2json` functionality to `wasm-tools`. This is implemented entirely
in the CLI since if using the crate it's already got all the
functionality necessary for this. Additionally this PR attempts to be
pretty close to `wast2json` from wabt in terms of structure and syntax.
The main difference is that wabt fills out the expected return types of
an operation when the operation isn't supposed to return (e.g.
`assert_trap`), but `wasm-tools` won't do that as we don't have that
information so easily available.

This is tested by adding `*.json` files to the `snapshots` directory
with example outputs from all known `*.wast` tests. Everything should be
included except for the `component-model` tests at this time which don't
have support in printing the args/results.

Closes #1395

* Fix tests on wasm

* Fix building just wast2json

* Generate the same test output on Windows

* Rename to `json-from-wast`

* Fix typo
@doehyunbaek
Copy link
Author

Oh @alexcrichton thanks for implementing this. I thought about doing thsis early April because I have some important due date in a few weeks. Maybe I should have communicated it.

Again thanks for your help!

@alexcrichton
Copy link
Member

Oh I'm so sorry! I was feeling inspired the other night, but I also should have checked in here first before going ahead.

@doehyunbaek
Copy link
Author

No I am really grateful that you did it! No need to be sorry!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants