Skip to main content

wasmtime_wast/
spectest.rs

1use wasmtime::*;
2
3/// Configuration of how spectest primitives work.
4pub struct SpectestConfig {
5    /// Whether or not to have a `shared_memory` definition.
6    pub use_shared_memory: bool,
7    /// Whether or not spectest functions that print things actually print things.
8    pub suppress_prints: bool,
9}
10
11/// Return an instance implementing the "spectest" interface used in the
12/// spec testsuite.
13pub fn link_spectest<T>(
14    linker: &mut Linker<T>,
15    store: &mut Store<T>,
16    config: &SpectestConfig,
17) -> Result<()> {
18    let suppress = config.suppress_prints;
19    linker.func_wrap("spectest", "print", || {})?;
20    linker.func_wrap("spectest", "print_i32", move |val: i32| {
21        if !suppress {
22            println!("{val}: i32")
23        }
24    })?;
25    linker.func_wrap("spectest", "print_i64", move |val: i64| {
26        if !suppress {
27            println!("{val}: i64")
28        }
29    })?;
30    linker.func_wrap("spectest", "print_f32", move |val: f32| {
31        if !suppress {
32            println!("{val}: f32")
33        }
34    })?;
35    linker.func_wrap("spectest", "print_f64", move |val: f64| {
36        if !suppress {
37            println!("{val}: f64")
38        }
39    })?;
40    linker.func_wrap("spectest", "print_i32_f32", move |i: i32, f: f32| {
41        if !suppress {
42            println!("{i}: i32");
43            println!("{f}: f32");
44        }
45    })?;
46    linker.func_wrap("spectest", "print_f64_f64", move |f1: f64, f2: f64| {
47        if !suppress {
48            println!("{f1}: f64");
49            println!("{f2}: f64");
50        }
51    })?;
52
53    let ty = GlobalType::new(ValType::I32, Mutability::Const);
54    let g = Global::new(&mut *store, ty, Val::I32(666))?;
55    linker.define(&mut *store, "spectest", "global_i32", g)?;
56
57    let ty = GlobalType::new(ValType::I64, Mutability::Const);
58    let g = Global::new(&mut *store, ty, Val::I64(666))?;
59    linker.define(&mut *store, "spectest", "global_i64", g)?;
60
61    let ty = GlobalType::new(ValType::F32, Mutability::Const);
62    let g = Global::new(&mut *store, ty, Val::F32(0x4426_a666))?;
63    linker.define(&mut *store, "spectest", "global_f32", g)?;
64
65    let ty = GlobalType::new(ValType::F64, Mutability::Const);
66    let g = Global::new(&mut *store, ty, Val::F64(0x4084_d4cc_cccc_cccd))?;
67    linker.define(&mut *store, "spectest", "global_f64", g)?;
68
69    let ty = TableType::new(RefType::FUNCREF, 10, Some(20));
70    let table = Table::new(&mut *store, ty, Ref::Func(None))?;
71    linker.define(&mut *store, "spectest", "table", table)?;
72
73    let ty = TableType::new64(RefType::FUNCREF, 10, Some(20));
74    let table = Table::new(&mut *store, ty, Ref::Func(None))?;
75    linker.define(&mut *store, "spectest", "table64", table)?;
76
77    let ty = MemoryType::new(1, Some(2));
78    let memory = Memory::new(&mut *store, ty)?;
79    linker.define(&mut *store, "spectest", "memory", memory)?;
80
81    if config.use_shared_memory {
82        let ty = MemoryType::shared(1, 1);
83        let memory = SharedMemory::new(store.engine(), ty)?;
84        linker.define(&mut *store, "spectest", "shared_memory", memory)?;
85    }
86
87    Ok(())
88}
89
90#[cfg(feature = "component-model")]
91pub fn link_component_spectest<T>(linker: &mut component::Linker<T>) -> Result<()> {
92    use std::sync::Arc;
93    use std::sync::atomic::{AtomicU32, Ordering::SeqCst};
94    use wasmtime::component::{Resource, ResourceType};
95
96    let engine = linker.engine().clone();
97    linker
98        .root()
99        .func_wrap_concurrent("host-echo-u32", |_, (v,): (u32,)| {
100            Box::pin(async move { Ok((v,)) })
101        })?;
102    linker
103        .root()
104        .func_wrap("host-return-two", |_, _: ()| Ok((2u32,)))?;
105    let mut i = linker.instance("host")?;
106    i.func_wrap("return-three", |_, _: ()| Ok((3u32,)))?;
107    i.instance("nested")?
108        .func_wrap("return-four", |_, _: ()| Ok((4u32,)))?;
109
110    if !cfg!(miri) {
111        let module = Module::new(
112            &engine,
113            r#"
114                (module
115                    (global (export "g") i32 i32.const 100)
116                    (func (export "f") (result i32) i32.const 101)
117                )
118            "#,
119        )?;
120        i.module("simple-module", &module)?;
121    }
122
123    struct Resource1;
124    struct Resource2;
125
126    #[derive(Default)]
127    struct ResourceState {
128        drops: AtomicU32,
129        last_drop: AtomicU32,
130    }
131
132    let state = Arc::new(ResourceState::default());
133
134    i.resource("resource1", ResourceType::host::<Resource1>(), {
135        let state = state.clone();
136        move |_, rep| {
137            state.drops.fetch_add(1, SeqCst);
138            state.last_drop.store(rep, SeqCst);
139
140            Ok(())
141        }
142    })?;
143    i.resource(
144        "resource2",
145        ResourceType::host::<Resource2>(),
146        |_, _| Ok(()),
147    )?;
148    // Currently the embedder API requires redefining the resource destructor
149    // here despite this being the same type as before, and fixing that is left
150    // for a future refactoring.
151    i.resource(
152        "resource1-again",
153        ResourceType::host::<Resource1>(),
154        |_, _| {
155            panic!("shouldn't be destroyed");
156        },
157    )?;
158
159    i.func_wrap("[constructor]resource1", |_cx, (rep,): (u32,)| {
160        Ok((Resource::<Resource1>::new_own(rep),))
161    })?;
162    i.func_wrap(
163        "[static]resource1.assert",
164        |_cx, (resource, rep): (Resource<Resource1>, u32)| {
165            assert_eq!(resource.rep(), rep);
166            Ok(())
167        },
168    )?;
169    i.func_wrap("[static]resource1.last-drop", {
170        let state = state.clone();
171        move |_, (): ()| Ok((state.last_drop.load(SeqCst),))
172    })?;
173    i.func_wrap("[static]resource1.drops", {
174        let state = state.clone();
175        move |_, (): ()| Ok((state.drops.load(SeqCst),))
176    })?;
177    i.func_wrap(
178        "[method]resource1.simple",
179        |_cx, (resource, rep): (Resource<Resource1>, u32)| {
180            assert!(!resource.owned());
181            assert_eq!(resource.rep(), rep);
182            Ok(())
183        },
184    )?;
185
186    i.func_wrap(
187        "[method]resource1.take-borrow",
188        |_, (a, b): (Resource<Resource1>, Resource<Resource1>)| {
189            assert!(!a.owned());
190            assert!(!b.owned());
191            Ok(())
192        },
193    )?;
194    i.func_wrap(
195        "[method]resource1.take-own",
196        |_cx, (a, b): (Resource<Resource1>, Resource<Resource1>)| {
197            assert!(!a.owned());
198            assert!(b.owned());
199            Ok(())
200        },
201    )?;
202    i.func_wrap_concurrent("never-return", |_, _: ()| {
203        Box::pin(async move { std::future::pending::<Result<()>>().await })
204    })?;
205    i.func_wrap_concurrent("return-two-slowly", |_, _: ()| {
206        Box::pin(async move {
207            tokio::task::yield_now().await;
208            Ok((2,))
209        })
210    })?;
211    i.func_wrap_concurrent("echo-slowly", |_, (a,): (u32,)| {
212        Box::pin(async move {
213            tokio::task::yield_now().await;
214            Ok((a,))
215        })
216    })?;
217    i.func_wrap_concurrent(
218        "[method]resource1.never-return",
219        |_, (_,): (Resource<Resource1>,)| {
220            Box::pin(async move { std::future::pending::<Result<()>>().await })
221        },
222    )?;
223    i.func_wrap("return-hi", |_cx, (): ()| Ok(("hi".to_string(),)))?;
224    Ok(())
225}