1use wasmtime::*;
2
3pub struct SpectestConfig {
5 pub use_shared_memory: bool,
7 pub suppress_prints: bool,
9}
10
11pub 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 = Memory::new(&mut *store, 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::atomic::{AtomicU32, Ordering::SeqCst};
93 use std::sync::Arc;
94 use wasmtime::component::{Resource, ResourceType};
95
96 let engine = linker.engine().clone();
97 linker
98 .root()
99 .func_wrap("host-echo-u32", |_, v: (u32,)| Ok(v))?;
100 linker
101 .root()
102 .func_wrap("host-return-two", |_, _: ()| Ok((2u32,)))?;
103 let mut i = linker.instance("host")?;
104 i.func_wrap("return-three", |_, _: ()| Ok((3u32,)))?;
105 i.instance("nested")?
106 .func_wrap("return-four", |_, _: ()| Ok((4u32,)))?;
107
108 let module = Module::new(
109 &engine,
110 r#"
111 (module
112 (global (export "g") i32 i32.const 100)
113 (func (export "f") (result i32) i32.const 101)
114 )
115 "#,
116 )?;
117 i.module("simple-module", &module)?;
118
119 struct Resource1;
120 struct Resource2;
121
122 #[derive(Default)]
123 struct ResourceState {
124 drops: AtomicU32,
125 last_drop: AtomicU32,
126 }
127
128 let state = Arc::new(ResourceState::default());
129
130 i.resource("resource1", ResourceType::host::<Resource1>(), {
131 let state = state.clone();
132 move |_, rep| {
133 state.drops.fetch_add(1, SeqCst);
134 state.last_drop.store(rep, SeqCst);
135
136 Ok(())
137 }
138 })?;
139 i.resource(
140 "resource2",
141 ResourceType::host::<Resource2>(),
142 |_, _| Ok(()),
143 )?;
144 i.resource(
148 "resource1-again",
149 ResourceType::host::<Resource1>(),
150 |_, _| {
151 panic!("shouldn't be destroyed");
152 },
153 )?;
154
155 i.func_wrap("[constructor]resource1", |_cx, (rep,): (u32,)| {
156 Ok((Resource::<Resource1>::new_own(rep),))
157 })?;
158 i.func_wrap(
159 "[static]resource1.assert",
160 |_cx, (resource, rep): (Resource<Resource1>, u32)| {
161 assert_eq!(resource.rep(), rep);
162 Ok(())
163 },
164 )?;
165 i.func_wrap("[static]resource1.last-drop", {
166 let state = state.clone();
167 move |_, (): ()| Ok((state.last_drop.load(SeqCst),))
168 })?;
169 i.func_wrap("[static]resource1.drops", {
170 let state = state.clone();
171 move |_, (): ()| Ok((state.drops.load(SeqCst),))
172 })?;
173 i.func_wrap(
174 "[method]resource1.simple",
175 |_cx, (resource, rep): (Resource<Resource1>, u32)| {
176 assert!(!resource.owned());
177 assert_eq!(resource.rep(), rep);
178 Ok(())
179 },
180 )?;
181
182 i.func_wrap(
183 "[method]resource1.take-borrow",
184 |_, (a, b): (Resource<Resource1>, Resource<Resource1>)| {
185 assert!(!a.owned());
186 assert!(!b.owned());
187 Ok(())
188 },
189 )?;
190 i.func_wrap(
191 "[method]resource1.take-own",
192 |_cx, (a, b): (Resource<Resource1>, Resource<Resource1>)| {
193 assert!(!a.owned());
194 assert!(b.owned());
195 Ok(())
196 },
197 )?;
198 Ok(())
199}