Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/wasi-keyvalue/tests/main.rs
3082 views
1
use test_programs_artifacts::{KEYVALUE_MAIN_COMPONENT, foreach_keyvalue};
2
use wasmtime::{
3
Result, Store,
4
component::{Component, Linker, ResourceTable},
5
format_err,
6
};
7
use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView, p2::bindings::Command};
8
use wasmtime_wasi_keyvalue::{WasiKeyValue, WasiKeyValueCtx, WasiKeyValueCtxBuilder};
9
10
struct Ctx {
11
table: ResourceTable,
12
wasi_ctx: WasiCtx,
13
wasi_keyvalue_ctx: WasiKeyValueCtx,
14
}
15
16
impl WasiView for Ctx {
17
fn ctx(&mut self) -> WasiCtxView<'_> {
18
WasiCtxView {
19
ctx: &mut self.wasi_ctx,
20
table: &mut self.table,
21
}
22
}
23
}
24
25
async fn run_wasi(path: &str, ctx: Ctx) -> Result<()> {
26
let engine = test_programs_artifacts::engine(|_config| {});
27
let mut store = Store::new(&engine, ctx);
28
let component = Component::from_file(&engine, path)?;
29
30
let mut linker = Linker::new(&engine);
31
wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
32
wasmtime_wasi_keyvalue::add_to_linker(&mut linker, |h: &mut Ctx| {
33
WasiKeyValue::new(&h.wasi_keyvalue_ctx, &mut h.table)
34
})?;
35
36
let command = Command::instantiate_async(&mut store, &component, &linker).await?;
37
command
38
.wasi_cli_run()
39
.call_run(&mut store)
40
.await?
41
.map_err(|()| format_err!("command returned with failing exit status"))
42
}
43
44
macro_rules! assert_test_exists {
45
($name:ident) => {
46
#[expect(unused_imports, reason = "just here to assert it exists")]
47
use self::$name as _;
48
};
49
}
50
51
foreach_keyvalue!(assert_test_exists);
52
53
#[tokio::test(flavor = "multi_thread")]
54
async fn keyvalue_main() -> Result<()> {
55
run_wasi(
56
KEYVALUE_MAIN_COMPONENT,
57
Ctx {
58
table: ResourceTable::new(),
59
wasi_ctx: WasiCtx::builder().inherit_stderr().build(),
60
wasi_keyvalue_ctx: WasiKeyValueCtxBuilder::new()
61
.in_memory_data([("atomics_key", "5")])
62
.build(),
63
},
64
)
65
.await
66
}
67
68