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