Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/wizer/tests/all/make_linker.rs
2459 views
1
use anyhow::{Context, Result, anyhow};
2
use wasmtime_wasi::WasiCtxBuilder;
3
use wasmtime_wizer::Wizer;
4
use wat::parse_str as wat_to_wasm;
5
6
async fn run_wasm(args: &[wasmtime::Val], expected: i32, wasm: &[u8]) -> Result<()> {
7
let _ = env_logger::try_init();
8
9
let mut config = wasmtime::Config::new();
10
config.async_support(true);
11
wasmtime::Cache::from_file(None)
12
.map(|cache| config.cache(Some(cache)))
13
.unwrap();
14
config.wasm_multi_memory(true);
15
config.wasm_multi_value(true);
16
17
let engine = wasmtime::Engine::new(&config)?;
18
let wasi_ctx = WasiCtxBuilder::new().build_p1();
19
let mut store = wasmtime::Store::new(&engine, wasi_ctx);
20
let wasm = Wizer::new()
21
.run(&mut store, &wasm, async |store, module| {
22
let mut linker = wasmtime::Linker::new(module.engine());
23
linker.func_wrap("foo", "bar", |x: i32| x + 1)?;
24
linker.instantiate_async(store, module).await
25
})
26
.await?;
27
log::debug!(
28
"=== Wizened Wasm ==========================================================\n\
29
{}\n\
30
===========================================================================",
31
wasmprinter::print_bytes(&wasm).unwrap()
32
);
33
if log::log_enabled!(log::Level::Debug) {
34
std::fs::write("test.wasm", &wasm).unwrap();
35
}
36
37
let wasi_ctx = WasiCtxBuilder::new().build_p1();
38
let mut store = wasmtime::Store::new(&engine, wasi_ctx);
39
let module =
40
wasmtime::Module::new(store.engine(), wasm).context("Wasm test case failed to compile")?;
41
42
let mut linker = wasmtime::Linker::new(&engine);
43
linker.func_wrap("foo", "bar", |_: i32| -> Result<i32> {
44
Err(anyhow!("shouldn't be called"))
45
})?;
46
47
let instance = linker.instantiate_async(&mut store, &module).await?;
48
49
let run = instance
50
.get_func(&mut store, "run")
51
.ok_or_else(|| anyhow::anyhow!("the test Wasm module does not export a `run` function"))?;
52
53
let mut actual = vec![wasmtime::Val::I32(0)];
54
run.call_async(&mut store, args, &mut actual).await?;
55
anyhow::ensure!(actual.len() == 1, "expected one result");
56
let actual = match actual[0] {
57
wasmtime::Val::I32(x) => x,
58
_ => anyhow::bail!("expected an i32 result"),
59
};
60
anyhow::ensure!(
61
expected == actual,
62
"expected `{expected}`, found `{actual}`",
63
);
64
65
Ok(())
66
}
67
68
async fn run_wat(args: &[wasmtime::Val], expected: i32, wat: &str) -> Result<()> {
69
let _ = env_logger::try_init();
70
let wasm = wat_to_wasm(wat)?;
71
run_wasm(args, expected, &wasm).await
72
}
73
74
#[tokio::test]
75
async fn custom_linker() -> Result<()> {
76
run_wat(
77
&[],
78
1,
79
r#"
80
(module
81
(type (func (param i32) (result i32)))
82
(import "foo" "bar" (func (type 0)))
83
(global $g (mut i32) (i32.const 0))
84
(func (export "wizer-initialize")
85
global.get $g
86
call 0
87
global.set $g
88
)
89
(func (export "run") (result i32)
90
(global.get $g)
91
)
92
)"#,
93
)
94
.await
95
}
96
97