#wasm #run-wasm #cargo #wasm32-wasi #run-time #runner #target

app wasm-runner

A simple wrapper for WASM Runtimes to use with Cargo

5 unstable releases

0.3.1 Jan 4, 2022
0.3.0 Jan 1, 2022
0.2.1 Jan 1, 2022
0.2.0 Dec 31, 2021
0.1.0 Dec 31, 2021

#1579 in Development tools

Download history 153/week @ 2024-06-18 157/week @ 2024-06-25 148/week @ 2024-07-02 152/week @ 2024-07-09 152/week @ 2024-07-16 151/week @ 2024-07-23 216/week @ 2024-07-30 153/week @ 2024-08-06 148/week @ 2024-08-13 150/week @ 2024-08-20 152/week @ 2024-08-27 148/week @ 2024-09-03 138/week @ 2024-09-10 1070/week @ 2024-09-17 1150/week @ 2024-09-24 261/week @ 2024-10-01

2,641 downloads per month

Apache-2.0

11KB
118 lines

Simple WASM Runner

Crate CI Build License

This is a simple wrapper program to run a WASM runtime (currently one of wasmer or wasmtime) as a runner for things like cargo run. This is useful when wanting to use a target such as wasm32-wasi without the trappings of wasm-pack for example, just running cargo run or cargo test in a package for the wasm32-wasi target. Cargo expects a runner to take the program executable followed by its arguments, and most WASM runtimes have a particular structure to their arguments (e.g., requiring a -- before program arguments).

Something like cargo-wasi is a great, more integrated solution, but if you want or need control over how the WASM runtime is executed (e.g., passing runtime options down to the runner), this simple wrapper may meet your needs.

Usage

Install the runner using cargo:

$ cargo install wasm-runner

You can install directly from the source tree as:

$ cargo install --path=.

After this you can now have a .cargo/config.toml that uses this runner to execute your binaries for WASM targets.

For example:

[target.'cfg(target_arch="wasm32")']
runner = ["wasm-runner", "wasmer"]

This assumes wasmer is in your PATH, but you can also specify the full path if you like.

At this point assuming wasm32-wasi is installed as a target in your toolchain (e.g., rustup target add wasm32-wasi), you can run unit tests or binaries with:

$ cargo --target wasm32-wasi test
$ cargo --target wasm32-wasi run -- some arguments

There are some environment variables that can add additional configurability (e.g., the [env] Cargo configuration key):

  • The WASM_RUNNER_VERBOSE variable can be set (to any value such as 1)to get diagnostic output as to what the runner is doing.
  • The WASM_RUNNER_RT_ARGS environment variable can take a JSON array of strings to pass runtime arguments.

You can run everything without a configuration file if you so choose:

$ WASM_RUNNER_RT_ARGS='["--enable-all", "--llvm"]' \
    WASM_RUNNER_VERBOSE=1 \
    CARGO_TARGET_WASM32_WASI_RUNNER="wasm-runner wasmer" \
    cargo run --target=wasm32-wasi -- hello world

Sample Application

The test-wasm folder has a simple crate that is configured to run the wrapper with wasmer.

$ (cd test-wasm && cargo run --verbose -- hello world)

TODO

  • CI testing for wasmtime.
  • Ability to support other runtimes such as wavm.

License

This project is licensed under the Apache-2.0 License.

Dependencies

~2–11MB
~133K SLoC