Skip to content

Commit 32e6b6b

Browse files
chore(deps): update upstream wasmtime dependencies (#142)
* chore(deps)!: update upstream wasmtime dependencies This commit updates the wasmtime dependencies in use, in preparation for the next release, as git dependencies cannot be used in a release. This change is breaking because the API to upstream `Wizer` has changed, and is now `async`. Since `wasmtime-wizer`'s `Wizer::run()` is re-exposed here we must introduce a breaking change here too. * refactor: use wasmtime::error::Context pervasively * chore: fix clippy lints * refactor: update regex test to use OnceLock, refresh binary
1 parent f29dd98 commit 32e6b6b

File tree

10 files changed

+532
-364
lines changed

10 files changed

+532
-364
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ name = "wizer"
1919
required-features = ["cli"]
2020

2121
[dependencies]
22-
anyhow = { version = "1.0.97", optional = true }
23-
env_logger = { version = "0.11.8", optional = true }
24-
clap = { version = "4.5", optional = true, features = ['derive'] }
25-
wasmtime = { workspace = true }
26-
wasmtime-wizer = { workspace = true }
22+
anyhow = { workspace = true, optional = true }
23+
clap = { workspace = true, optional = true, features = ['derive'] }
24+
env_logger = { workspace = true, optional = true }
25+
tokio = { workspace = true, features = ["macros"] }
26+
wasmtime = { workspace = true, features = ["async", "anyhow"] }
2727
wasmtime-wasi = { workspace = true, features = ["p1"] }
28+
wasmtime-wizer = { workspace = true, features = ["wasmtime"] }
2829

2930
[features]
3031
# Enable this dependency to get messages with WAT disassemblies when certain
@@ -34,25 +35,39 @@ wasmprinter = ['wasmtime-wizer/wasmprinter']
3435
cli = ['dep:clap', 'dep:env_logger', 'dep:anyhow', 'wasmtime-wizer/clap']
3536

3637
[workspace.dependencies]
37-
wasmprinter = "0.238.0"
38-
wasmtime = { git = 'https://github.com/bytecodealliance/wasmtime' }
39-
wasmtime-wasi = { git = 'https://github.com/bytecodealliance/wasmtime' }
40-
wasmtime-wizer = { git = 'https://github.com/bytecodealliance/wasmtime' }
38+
anyhow = { version = "1.0.97" }
39+
clap = { version = "4.5" }
40+
criterion = { version = "0.5.1" }
41+
env_logger = { version = "0.11.8" }
42+
log = { version = "0.4.27" }
43+
wasm-encoder = { version = "0.244.0" }
44+
wasmparser = { version = "0.244.0" }
45+
wasmprinter = { version = "0.244.0" }
46+
47+
# TODO: remove once wasmtime releases 42.0.0
48+
#wasmtime = { version = "42.0.0" }
49+
#wasmtime-wasi = { version = "42.0.0" }
50+
#wasmtime-wizer = { version = "42.0.0" }
51+
52+
wasmtime = { git = "https://github.com/bytecodealliance/wasmtime" }
53+
wasmtime-wasi = { git = "https://github.com/bytecodealliance/wasmtime" }
54+
wasmtime-wizer = { git = "https://github.com/bytecodealliance/wasmtime" }
55+
56+
wat = { version = "1.244.0" }
57+
tokio = { version = "1.49.0" }
4158

4259
[dev-dependencies]
43-
log = "0.4.27"
44-
anyhow = "1.0.97"
45-
criterion = "0.5.1"
46-
env_logger = "0.11.8"
60+
anyhow = { workspace = true }
61+
criterion = { workspace = true }
62+
env_logger = { workspace = true }
63+
log = { workspace = true }
64+
wasm-encoder = { workspace = true }
65+
wasmparser = { workspace = true }
4766
wasmprinter = { workspace = true }
48-
wat = "1.238.0"
49-
wasm-encoder = "0.238.0"
50-
wasmparser = "0.238.0"
67+
wat = { workspace = true }
5168

5269
[workspace]
53-
members = [
54-
"tests/regex-test",
55-
]
70+
members = ["tests/regex-test"]
5671

5772
[profile.bench]
5873
debug = true

rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
edition = "2024"

src/bin/wizer.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use anyhow::Context;
2-
use anyhow::Result;
3-
use clap::Parser;
41
use std::fs;
52
use std::io::{self, BufRead, Write};
63
use std::path::PathBuf;
4+
5+
use anyhow::Result;
6+
use clap::Parser;
7+
use wasmtime::error::Context;
78
use wizer::Wizer;
89

910
#[derive(Parser)]
@@ -168,7 +169,8 @@ fn optional_flag_with_default(flag: Option<Option<bool>>, default: bool) -> bool
168169
}
169170
}
170171

171-
fn main() -> anyhow::Result<()> {
172+
#[tokio::main]
173+
async fn main() -> Result<()> {
172174
env_logger::init();
173175
let options = Options::parse();
174176

@@ -262,10 +264,11 @@ fn main() -> anyhow::Result<()> {
262264

263265
let output_wasm = options
264266
.wizer
265-
.run(&mut store, &input_wasm, |store, module| {
267+
.run(&mut store, &input_wasm, async |store, module| {
266268
linker.define_unknown_imports_as_traps(module)?;
267269
linker.instantiate(store, module)
268-
})?;
270+
})
271+
.await?;
269272

270273
output
271274
.write_all(&output_wasm)

tests/make_linker.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
use anyhow::{anyhow, Context, Result};
1+
use anyhow::{anyhow, Result};
2+
use wasmtime::error::Context as _;
23
use wasmtime_wasi::WasiCtxBuilder;
34
use wat::parse_str as wat_to_wasm;
45
use wizer::Wizer;
56

67
fn get_wizer() -> Wizer {
7-
Wizer::new()
8+
let mut wizer = Wizer::new();
9+
wizer.init_func("wizer.initialize");
10+
wizer
811
}
912

10-
fn run_wasm(args: &[wasmtime::Val], expected: i32, wasm: &[u8]) -> Result<()> {
13+
async fn run_wasm(args: &[wasmtime::Val], expected: i32, wasm: &[u8]) -> Result<()> {
1114
let _ = env_logger::try_init();
1215

1316
let mut config = wasmtime::Config::new();
@@ -21,11 +24,15 @@ fn run_wasm(args: &[wasmtime::Val], expected: i32, wasm: &[u8]) -> Result<()> {
2124
let wasi_ctx = WasiCtxBuilder::new().build_p1();
2225
let mut store = wasmtime::Store::new(&engine, wasi_ctx);
2326

24-
let wasm = get_wizer().run(&mut store, &wasm, |store, module| {
25-
let mut linker = wasmtime::Linker::new(store.engine());
26-
linker.func_wrap("foo", "bar", |x: i32| x + 1)?;
27-
linker.instantiate(store, module)
28-
})?;
27+
let wasm = get_wizer()
28+
.run(&mut store, wasm, async |store, module| {
29+
let mut linker = wasmtime::Linker::new(store.engine());
30+
linker.func_wrap("foo", "bar", |x: i32| x + 1)?;
31+
linker.instantiate(store, module)
32+
})
33+
.await
34+
.map_err(|e| anyhow!(e))?;
35+
2936
log::debug!(
3037
"=== Wizened Wasm ==========================================================\n\
3138
{}\n\
@@ -40,8 +47,8 @@ fn run_wasm(args: &[wasmtime::Val], expected: i32, wasm: &[u8]) -> Result<()> {
4047
wasmtime::Module::new(store.engine(), wasm).context("Wasm test case failed to compile")?;
4148

4249
let mut linker = wasmtime::Linker::new(&engine);
43-
linker.func_wrap("foo", "bar", |_: i32| -> Result<i32> {
44-
Err(anyhow!("shouldn't be called"))
50+
linker.func_wrap("foo", "bar", |_: i32| -> wasmtime::error::Result<i32> {
51+
wasmtime::error::bail!("shouldn't be called")
4552
})?;
4653

4754
let instance = linker.instantiate(&mut store, &module)?;
@@ -51,7 +58,8 @@ fn run_wasm(args: &[wasmtime::Val], expected: i32, wasm: &[u8]) -> Result<()> {
5158
.ok_or_else(|| anyhow::anyhow!("the test Wasm module does not export a `run` function"))?;
5259

5360
let mut actual = vec![wasmtime::Val::I32(0)];
54-
run.call(&mut store, args, &mut actual)?;
61+
run.call(&mut store, args, &mut actual)
62+
.context("failed to call wasm run fn")?;
5563
anyhow::ensure!(actual.len() == 1, "expected one result");
5664
let actual = match actual[0] {
5765
wasmtime::Val::I32(x) => x,
@@ -67,14 +75,14 @@ fn run_wasm(args: &[wasmtime::Val], expected: i32, wasm: &[u8]) -> Result<()> {
6775
Ok(())
6876
}
6977

70-
fn run_wat(args: &[wasmtime::Val], expected: i32, wat: &str) -> Result<()> {
78+
async fn run_wat(args: &[wasmtime::Val], expected: i32, wat: &str) -> Result<()> {
7179
let _ = env_logger::try_init();
7280
let wasm = wat_to_wasm(wat)?;
73-
run_wasm(args, expected, &wasm)
81+
run_wasm(args, expected, &wasm).await
7482
}
7583

76-
#[test]
77-
fn custom_linker() -> Result<()> {
84+
#[tokio::test]
85+
async fn custom_linker() -> Result<()> {
7886
run_wat(
7987
&[],
8088
1,
@@ -93,4 +101,5 @@ fn custom_linker() -> Result<()> {
93101
)
94102
)"#,
95103
)
104+
.await
96105
}

tests/preloads.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,44 @@
11
use anyhow::Result;
2+
use wasmtime::error::Context as _;
23
use wasmtime::{Instance, Linker, Module};
34
use wasmtime_wizer::Wizer;
45
use wat::parse_str as wat_to_wasm;
56

6-
const PRELOAD1: &'static str = r#"
7+
const PRELOAD1: &str = r#"
78
(module
89
(func (export "f") (param i32) (result i32)
910
local.get 0
1011
i32.const 1
1112
i32.add))
1213
"#;
1314

14-
const PRELOAD2: &'static str = r#"
15+
const PRELOAD2: &str = r#"
1516
(module
1617
(func (export "f") (param i32) (result i32)
1718
local.get 0
1819
i32.const 2
1920
i32.add))
2021
"#;
2122

22-
fn run_with_preloads(args: &[wasmtime::Val], wat: &str) -> Result<wasmtime::Val> {
23+
async fn run_with_preloads(args: &[wasmtime::Val], wat: &str) -> Result<wasmtime::Val> {
2324
let wasm = wat_to_wasm(wat)?;
2425
let engine = wasmtime::Engine::default();
2526
let mut store = wasmtime::Store::new(&engine, ());
2627
let mod1 = Module::new(store.engine(), PRELOAD1)?;
2728
let mod2 = Module::new(store.engine(), PRELOAD2)?;
2829

29-
let processed = Wizer::new().run(&mut store, &wasm, |store, module| {
30-
let i1 = Instance::new(&mut *store, &mod1, &[])?;
31-
let i2 = Instance::new(&mut *store, &mod2, &[])?;
32-
let mut linker = Linker::new(store.engine());
33-
linker.instance(&mut *store, "mod1", i1)?;
34-
linker.instance(&mut *store, "mod2", i2)?;
35-
linker.instantiate(store, module)
36-
})?;
30+
let mut wizer = Wizer::new();
31+
wizer.init_func("wizer.initialize");
32+
let processed = wizer
33+
.run(&mut store, &wasm, async |mut store, module| {
34+
let i1 = Instance::new(&mut store, &mod1, &[]).context("failed to create instance")?;
35+
let i2 = Instance::new(&mut store, &mod2, &[]).context("failed to create instance")?;
36+
let mut linker = Linker::new(store.engine());
37+
linker.instance(&mut store, "mod1", i1)?;
38+
linker.instance(&mut store, "mod2", i2)?;
39+
linker.instantiate(store, module)
40+
})
41+
.await?;
3742

3843
let testmod = wasmtime::Module::new(&engine, &processed[..])?;
3944

@@ -48,13 +53,14 @@ fn run_with_preloads(args: &[wasmtime::Val], wat: &str) -> Result<wasmtime::Val>
4853
.get_func(&mut store, "run")
4954
.ok_or_else(|| anyhow::anyhow!("no `run` function on test module"))?;
5055
let mut returned = vec![wasmtime::Val::I32(0)];
51-
run.call(&mut store, args, &mut returned)?;
56+
run.call(&mut store, args, &mut returned)
57+
.context("call failed")?;
5258
Ok(returned[0])
5359
}
5460

55-
#[test]
56-
fn test_preloads() {
57-
const WAT: &'static str = r#"
61+
#[tokio::test]
62+
async fn test_preloads() -> Result<()> {
63+
const WAT: &str = r#"
5864
(module
5965
(import "mod1" "f" (func $mod1f (param i32) (result i32)))
6066
(import "mod2" "f" (func $mod2f (param i32) (result i32)))
@@ -80,6 +86,7 @@ fn test_preloads() {
8086
"#;
8187

8288
let result =
83-
run_with_preloads(&[wasmtime::Val::I32(200), wasmtime::Val::I32(201)], WAT).unwrap();
89+
run_with_preloads(&[wasmtime::Val::I32(200), wasmtime::Val::I32(201)], WAT).await?;
8490
assert!(matches!(result, wasmtime::Val::I32(607)));
91+
Ok(())
8592
}

tests/regex-test/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Source code used to create `/wizer/tests/regex_test.wasm`.
22

33
Rebuild with:
44

5-
```
6-
$ cargo build --release --target wasm32-wasi -p regex-test
7-
$ cp target/wasm32-wasi/release/regex_test.wasm tests/regex_test.wasm
5+
```console
6+
cargo build --release --target wasm32-wasip1 -p regex-test
7+
cp target/wasm32-wasip1/release/regex_test.wasm tests/regex_test.wasm
88
```

tests/regex-test/src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
use regex::Regex;
2+
use std::sync::OnceLock;
23

34
/// A regex that matches numbers that start with "1".
4-
static mut REGEX: Option<Regex> = None;
5+
static REGEX: OnceLock<Regex> = OnceLock::new();
56

67
#[export_name = "wizer.initialize"]
78
pub fn init() {
8-
unsafe {
9-
REGEX = Some(Regex::new(r"^1\d*$").unwrap());
10-
}
9+
REGEX.get_or_init(|| Regex::new(r"^1\d*$").expect("failed to compile regex"));
1110
}
1211

1312
#[no_mangle]
1413
pub fn run(n: i32) -> i32 {
1514
let s = format!("{}", n);
16-
let regex = unsafe { REGEX.as_ref().unwrap() };
15+
let regex = REGEX
16+
.get()
17+
.expect("regex should have been initialized at wizering");
1718
if regex.is_match(&s) {
1819
42
1920
} else {

tests/regex_test.wasm

-1.27 MB
Binary file not shown.

0 commit comments

Comments
 (0)