Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/wizer/tests/all/preloads.rs
2459 views
1
use anyhow::Result;
2
use wasmtime::{Instance, Linker, Module};
3
use wasmtime_wizer::Wizer;
4
use wat::parse_str as wat_to_wasm;
5
6
const PRELOAD1: &'static str = r#"
7
(module
8
(func (export "f") (param i32) (result i32)
9
local.get 0
10
i32.const 1
11
i32.add))
12
"#;
13
14
const PRELOAD2: &'static str = r#"
15
(module
16
(func (export "f") (param i32) (result i32)
17
local.get 0
18
i32.const 2
19
i32.add))
20
"#;
21
22
async fn run_with_preloads(args: &[wasmtime::Val], wat: &str) -> Result<wasmtime::Val> {
23
let wasm = wat_to_wasm(wat)?;
24
let mut config = wasmtime::Config::new();
25
config.async_support(true);
26
let engine = wasmtime::Engine::new(&config)?;
27
let mut store = wasmtime::Store::new(&engine, ());
28
let mod1 = Module::new(store.engine(), PRELOAD1)?;
29
let mod2 = Module::new(store.engine(), PRELOAD2)?;
30
31
let processed = Wizer::new()
32
.run(&mut store, &wasm, async |store, module| {
33
let i1 = Instance::new_async(&mut *store, &mod1, &[]).await?;
34
let i2 = Instance::new_async(&mut *store, &mod2, &[]).await?;
35
let mut linker = Linker::new(store.engine());
36
linker.instance(&mut *store, "mod1", i1)?;
37
linker.instance(&mut *store, "mod2", i2)?;
38
linker.instantiate_async(store, module).await
39
})
40
.await?;
41
42
let testmod = wasmtime::Module::new(&engine, &processed[..])?;
43
44
let mod1_inst = wasmtime::Instance::new_async(&mut store, &mod1, &[]).await?;
45
let mod2_inst = wasmtime::Instance::new_async(&mut store, &mod2, &[]).await?;
46
let mut linker = wasmtime::Linker::new(&engine);
47
linker.instance(&mut store, "mod1", mod1_inst)?;
48
linker.instance(&mut store, "mod2", mod2_inst)?;
49
50
let inst = linker.instantiate_async(&mut store, &testmod).await?;
51
let run = inst
52
.get_func(&mut store, "run")
53
.ok_or_else(|| anyhow::anyhow!("no `run` function on test module"))?;
54
let mut returned = vec![wasmtime::Val::I32(0)];
55
run.call_async(&mut store, args, &mut returned).await?;
56
Ok(returned[0])
57
}
58
59
#[tokio::test]
60
async fn test_preloads() {
61
const WAT: &'static str = r#"
62
(module
63
(import "mod1" "f" (func $mod1f (param i32) (result i32)))
64
(import "mod2" "f" (func $mod2f (param i32) (result i32)))
65
(global $g1 (mut i32) (i32.const 0))
66
(global $g2 (mut i32) (i32.const 0))
67
(func (export "wizer-initialize")
68
i32.const 100
69
call $mod1f
70
global.set $g1
71
i32.const 100
72
call $mod2f
73
global.set $g2)
74
(func (export "run") (param i32 i32) (result i32)
75
local.get 0
76
call $mod1f
77
local.get 1
78
call $mod2f
79
i32.add
80
global.get $g1
81
global.get $g2
82
i32.add
83
i32.add))
84
"#;
85
86
let result = run_with_preloads(&[wasmtime::Val::I32(200), wasmtime::Val::I32(201)], WAT)
87
.await
88
.unwrap();
89
assert!(matches!(result, wasmtime::Val::I32(607)));
90
}
91
92