Skip to content

Commit 60664a7

Browse files
Merge pull request #12 from arnaudgolfouse/stub-wasi-examples
Stub wasi examples
2 parents 2523b60 + 1657f01 commit 60664a7

File tree

7 files changed

+216
-170
lines changed

7 files changed

+216
-170
lines changed

Cargo.lock

Lines changed: 48 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,15 @@ The example can run using [`wasmi`](https://github.com/paritytech/wasmi).
3939
The command to run examples (from the top-level directory) is:
4040

4141
```sh
42-
cargo run -- <lang>
42+
cargo run [--features wasi] -- <lang>
4343
# or
44-
cargo run --no-default-features --features <abi> -- <lang>
45-
# or
46-
cargo run -- -i <PATH> <func> <args>
47-
# or
48-
cargo run --no-default-features --features <abi> -- -i <PATH> <func> <args>
44+
cargo run [--features wasi] -- -i <PATH> <func> <args>
4945
```
5046

5147
Where:
5248

5349
- `<lang>` is `rust`, `zig` or `c`
54-
- `<abi>` is `abi_unknown` or `abi_wasi` (defaults to `abi_unknown`)
50+
- add `wasi` to the list of features to compile the example with WASI (required on the C example) and stub all the resulting WASI function if the runner is `host-wasmi`.
5551
- `<PATH>` is the path to a wasm file
5652
- `<func>` is the exported function to call in the wasm file, with `<args>` as arguments
5753

@@ -65,16 +61,16 @@ Where:
6561

6662
```sh
6763
cargo run -- rust # compile and run the Rust example
64+
cargo run --features wasi -- rust # compile and run the Rust example with WASI (stubbed)
6865
cargo run -- zig # compile and run the Zig example
69-
# TODO irrelevant until next PR is merged
70-
# NOTE: this needs the abi_wasi feature, because the wasi functions are not stubbed. See the 'Tips' section to learn more.
71-
cargo run -- c # compile and run the C example
66+
# NOTE: this needs the wasi feature, as `emcc` only compiles in WASI.
67+
cargo run --features wasi -- c # compile and run the C example
7268
cargo run -- -i MY_WASM_FILE.wasm MY_FUNCTION arg1 arg2
7369
```
7470

7571
### Tips
7672

77-
- If the runner complains about missing definition for `wasi_snapshot_preview1` functions, try running your `.wasm` through [wasi-stub](./wasi-stub/). It stubs all wasi function in your wasm, so don't expect print or read_file to work anymore.
73+
- `host-wasmi` does not support running with WASI, and will stub all WASI functions instead.
7874
- host-wasmi compiles fastest 😉
7975

8076
## You need to stub a WebAssembly plugin

examples/test-runner/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ edition = "2021"
55

66
[dependencies]
77
anyhow = "1.0.71"
8-
host-wasmi = { path = "../host-wasmi" }
98
cfg-if = "1.0.0"
9+
host-wasmi = { path = "../host-wasmi" }
10+
wasi-stub = { path = "../../wasi-stub", optional = true }
1011

1112

1213
[features]
13-
default = ["abi_unknown"]
14-
abi_unknown = []
15-
abi_wasi = []
14+
default = []
15+
wasi = ["dep:wasi-stub"]

examples/test-runner/src/main.rs

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ use std::process::Command;
66

77
use host_wasmi::PluginInstance;
88

9-
#[cfg(feature = "abi_unknown")]
9+
#[cfg(not(feature = "wasi"))]
1010
mod consts {
1111
pub const RUST_TARGET: &str = "wasm32-unknown-unknown";
1212
pub const RUST_PATH: &str =
1313
"examples/hello_rust/target/wasm32-unknown-unknown/debug/hello.wasm";
1414
pub const ZIG_TARGET: &str = "wasm32-freestanding";
1515
}
1616

17-
#[cfg(feature = "abi_wasi")]
17+
#[cfg(feature = "wasi")]
1818
mod consts {
1919
pub const RUST_TARGET: &str = "wasm32-wasi";
2020
pub const RUST_PATH: &str = "examples/hello_rust/target/wasm32-wasi/debug/hello.wasm";
@@ -27,8 +27,9 @@ fn main() -> Result<()> {
2727
if args.is_empty() {
2828
anyhow::bail!("1 argument required: 'rust', 'zig' or 'c'")
2929
}
30+
#[cfg(feature = "wasi")]
31+
println!("[INFO] The WASI functions will be stubbed (by `wasi-stub`) for this run");
3032
let plugin_binary = match args[0].as_str() {
31-
#[cfg(any(feature = "abi_unknown", feature = "abi_wasi"))]
3233
"rust" => {
3334
println!("=== compiling the Rust plugin");
3435
Command::new("cargo")
@@ -39,10 +40,9 @@ fn main() -> Result<()> {
3940
.spawn()?
4041
.wait()?;
4142
println!("===");
42-
println!("getting wasm from: {}", consts::RUST_PATH);
43+
println!("[INFO] getting wasm from: {}", consts::RUST_PATH);
4344
std::fs::read(consts::RUST_PATH)?
4445
}
45-
#[cfg(any(feature = "abi_unknown", feature = "abi_wasi"))]
4646
"zig" => {
4747
println!("=== compiling the Zig plugin");
4848
Command::new("zig")
@@ -57,15 +57,13 @@ fn main() -> Result<()> {
5757
.expect("do you have zig installed and in the path?")
5858
.wait()?;
5959
println!("===");
60-
println!("getting wasm from: examples/hello_zig/hello.wasm");
60+
println!("[INFO] getting wasm from: examples/hello_zig/hello.wasm");
6161
std::fs::read("examples/hello_zig/hello.wasm")?
6262
}
6363
"c" => {
6464
println!("=== compiling the C plugin");
65-
#[cfg(feature = "abi_unknown")]
66-
println!("cfg(abi_unknown) has no effect for C example");
67-
#[cfg(feature = "abi_wasi")]
68-
println!("cfg(abi_wasi) has no effect for C example");
65+
#[cfg(not(feature = "wasi"))]
66+
eprintln!("WARNING: the C example should be compiled with `--features wasi`");
6967

7068
println!("{}", std::env::current_dir().unwrap().display());
7169
Command::new("emcc")
@@ -80,22 +78,12 @@ fn main() -> Result<()> {
8078
.expect("do you have emcc installed and in the path?")
8179
.wait()?;
8280
println!("===");
83-
println!("getting wasm from: examples/hello_c/hello.wasm");
81+
println!("[INFO] getting wasm from: examples/hello_c/hello.wasm");
8482
std::fs::read("examples/hello_c/hello.wasm")?
8583
}
86-
87-
#[cfg(not(any(feature = "abi_unknown", feature = "abi_wasi")))]
88-
"rust" | "zig" => {
89-
panic!(
90-
"for testing rust or zig, you must enable one feature in [abi_unknown, abi_wasi]"
91-
)
92-
}
9384
"-i" | "--input" => {
94-
#[cfg(feature = "abi_wasi")]
95-
println!("The feature abi_wasi is enabled but the file tested is provided by you.\nThis feature influence how the examples are built, they have no effect here.");
9685
custom_run = true;
97-
println!("===");
98-
println!("getting wasm from: {}", args[1].as_str());
86+
println!("[INFO] getting wasm from: {}", args[1].as_str());
9987
println!(
10088
"running func: {}",
10189
args.get(2)
@@ -107,19 +95,33 @@ fn main() -> Result<()> {
10795
_ => anyhow::bail!("unknown argument '{}'", args[0].as_str()),
10896
};
10997

98+
#[cfg(feature = "wasi")]
99+
let plugin_binary = {
100+
println!("[INFO] Using wasi-stub");
101+
let res = wasi_stub::stub_wasi_functions(&plugin_binary)?;
102+
println!("[INFO] WASI functions have been stubbed");
103+
res
104+
};
105+
110106
let mut plugin_instance = PluginInstance::new_from_bytes(plugin_binary).unwrap();
111107
if custom_run {
112-
println!(
113-
"{:?}",
114-
plugin_instance.call(
115-
args[2].as_str(),
116-
args.iter()
117-
.skip(3)
118-
.map(|x| x.as_bytes())
119-
.collect::<Vec<_>>()
120-
.as_slice()
121-
)
122-
);
108+
let function = args[2].as_str();
109+
let args = args
110+
.iter()
111+
.skip(3)
112+
.map(|x| x.as_bytes())
113+
.collect::<Vec<_>>();
114+
let result = match plugin_instance.call(function, &args) {
115+
Ok(res) => res,
116+
Err(err) => {
117+
eprintln!("Error: {err}");
118+
return Ok(());
119+
}
120+
};
121+
match String::from_utf8(result) {
122+
Ok(s) => println!("{s}"),
123+
Err(_) => panic!("Error: function call '{function}' did not return UTF-8"),
124+
}
123125
return Ok(());
124126
}
125127

@@ -135,7 +137,7 @@ fn main() -> Result<()> {
135137
let result = match plugin_instance.call(function, args) {
136138
Ok(res) => res,
137139
Err(err) => {
138-
println!("Error: {err}");
140+
eprintln!("Error: {err}");
139141
continue;
140142
}
141143
};

wasi-stub/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ version = "0.1.0"
55
authors = ["Arnaud Golfouse <[email protected]>"]
66

77
[dependencies]
8-
wasm-encoder = "0.29.0"
9-
wasmparser = "0.107.0"
8+
wasm-encoder = "0.31.0"
9+
wasmparser = "0.109.0"
10+
thiserror = "1.0"

0 commit comments

Comments
 (0)