wasi_virt_layer_cli/generator/
abi_connect.rs1use itertools::Itertools;
2use strum::VariantNames;
3
4use crate::{
5 abi::{Wasip1ABIFunc, Wasip1ThreadsABIExportFunc, Wasip1ThreadsABIFunc},
6 generator::{Generator, threads::ThreadsSpawnName},
7 unique_name::UniqueName,
8 util::{
9 WalrusFID, WalrusUtilExport, WalrusUtilImport, WalrusUtilModule, WasmName,
10 gen_component_name,
11 },
12};
13
14#[derive(Debug, strum::AsRefStr, strum::EnumCount, Hash, PartialEq, Eq)]
15#[strum(serialize_all = "snake_case")]
16pub enum Wasip1ABIName<'a> {
17 #[strum(serialize = "__self")]
18 SelfDefault { import: &'a str },
19 #[strum(serialize = "")]
20 TargetTemporal { wasm: &'a WasmName, import: &'a str },
21}
22
23#[derive(Debug, Default)]
28pub struct ConnectWasip1ABI;
29
30impl Generator for ConnectWasip1ABI {
31 fn pre_vfs(
34 &mut self,
35 module: &mut walrus::Module,
36 ctx: &super::GeneratorCtx,
37 ) -> eyre::Result<()> {
38 for import in <Wasip1ABIFunc as strum::VariantNames>::VARIANTS {
39 let export = UniqueName::Wasip1ABI(&Wasip1ABIName::SelfDefault { import })
40 .get_fid(&module.exports)
41 .ok();
42
43 if let Some(import_id) = (
44 UniqueName::WASIP1_ABI_MODULE,
47 import,
48 )
49 .get_fid(&module.imports)
50 .ok()
51 {
52 if let Some(_) = export {
53 module.connect_func_alt_with_remove_export(
54 import_id,
55 &UniqueName::Wasip1ABI(&Wasip1ABIName::SelfDefault { import }).to_string(),
56 ctx.unstable_print_debug,
57 )?;
58 } else {
59 log::warn!("No plug found for Wasip1 ABI import self: {import}");
60 }
61 } else {
62 if let Some(_) = export {
63 module
64 .exports
65 .remove(
66 &UniqueName::Wasip1ABI(&Wasip1ABIName::SelfDefault { import })
67 .to_string(),
68 )
69 .unwrap();
70 }
71 }
72 }
73
74 Ok(())
75 }
76
77 fn pre_target(
78 &mut self,
79 module: &mut walrus::Module,
80 _: &crate::generator::GeneratorCtx,
81 external: &crate::generator::ModuleExternal,
82 ) -> eyre::Result<()> {
83 module
84 .imports
85 .iter_mut()
86 .filter(|import| {
87 <Wasip1ABIFunc as strum::VariantNames>::VARIANTS.contains(&import.name.as_str())
88 && import.module == UniqueName::WASIP1_ABI_MODULE
89 })
90 .for_each(|import| {
91 import.name =
92 crate::unique_name::UniqueName::Wasip1ABI(&Wasip1ABIName::TargetTemporal {
93 wasm: &external.name,
94 import: import.name.as_str(),
95 })
96 .to_string();
97 });
98
99 Ok(())
100 }
101
102 fn post_combine(
103 &mut self,
104 module: &mut walrus::Module,
105 ctx: &crate::generator::GeneratorCtx,
106 ) -> eyre::Result<()> {
107 for wasm in &ctx.target_names {
108 for import in <Wasip1ABIFunc as strum::VariantNames>::VARIANTS {
109 if let Some(import_id) = (UniqueName::WASIP1_ABI_MODULE, import)
110 .get_fid(&module.imports)
111 .ok()
112 {
113 unreachable!();
114 } else {
116 module
117 .exports
118 .erase_with(
119 &UniqueName::Wasip1ABI(&Wasip1ABIName::TargetTemporal { wasm, import }),
120 ctx.unstable_print_debug,
121 )
122 .ok();
123 }
124 }
125 }
126 Ok(())
127 }
128}
129
130#[derive(Debug, Default)]
131pub struct ConnectWasip1ThreadsABI;
132
133impl Generator for ConnectWasip1ThreadsABI {
134 fn post_combine(
135 &mut self,
136 module: &mut walrus::Module,
137 ctx: &crate::generator::GeneratorCtx,
138 ) -> eyre::Result<()> {
139 if ctx.threads {
140 for wasm in &ctx.target_names {
141 if UniqueName::ThreadsSpawn(&ThreadsSpawnName::WasiThreadStartDestination(wasm))
142 .get_fid(&module.exports)
143 .ok()
144 .is_some()
145 {
146 module.connect_func_alt_with_remove_export(
147 (
148 UniqueName::NAMESPACE,
149 &UniqueName::ThreadsSpawn(&ThreadsSpawnName::WasiThreadStart(wasm)),
150 ),
151 &UniqueName::ThreadsSpawn(&ThreadsSpawnName::WasiThreadStartDestination(
152 wasm,
153 ))
154 .to_string(),
155 ctx.unstable_print_debug,
156 )?;
157
158 module.exports.erase_with(
159 &UniqueName::ThreadsSpawn(&ThreadsSpawnName::WasiThreadStartAnchor(wasm)),
160 ctx.unstable_print_debug,
161 )?;
162
163 module.connect_func_alt_with_remove_export(
164 (
165 UniqueName::WASIP1_THREADS_ABI_MODULE,
166 &UniqueName::ThreadsSpawn(&&ThreadsSpawnName::WasiThreadSpawn(wasm)),
167 ),
168 &UniqueName::ThreadsSpawn(&ThreadsSpawnName::WasiThreadSpawn(wasm))
169 .to_string(),
170 ctx.unstable_print_debug,
171 )?;
172 }
173 }
174 }
175 Ok(())
176 }
177}
178
179#[derive(Debug, Default)]
182pub struct NonRecursiveWasiABI;
183
184impl Generator for NonRecursiveWasiABI {
185 fn pre_vfs(
186 &mut self,
187 module: &mut walrus::Module,
188 _: &super::GeneratorCtx,
189 ) -> eyre::Result<()> {
190 for import in <Wasip1ABIFunc as strum::VariantNames>::VARIANTS {
191 if let Some(fid) = (UniqueName::CORE_NON_RECURSIVE_MODULE_ROOT, import)
192 .get_fid(&module.imports)
193 .ok()
194 {
195 if let Some(import_id) = (UniqueName::WASIP1_ABI_MODULE, import)
197 .get_fid(&module.imports)
198 .ok()
199 {
200 module.renew_call_fn(fid, import_id)?;
201 } else {
202 let import_id = module.imports.get_imported_func(fid).unwrap().id();
203 let import = module.imports.get_mut(import_id);
204 import.module = UniqueName::WASIP1_ABI_MODULE.to_string();
205 }
206 }
207 }
208
209 module
210 .imports
211 .iter()
212 .filter(|import| import.module == UniqueName::CORE_NON_RECURSIVE_MODULE_ROOT)
213 .map(|import| &import.name)
214 .for_each(|name| {
215 log::warn!(
216 "Non-recursive Wasip1 ABI import exists: {name}, but this is not verified."
217 );
218 });
219
220 Ok(())
221 }
222}
223
224#[derive(Debug, Clone, Default)]
226pub struct AdjustABI;
227
228impl Generator for AdjustABI {
229 fn post_components(
230 &mut self,
231 module: &mut walrus::Module,
232 ctx: &super::ComponentCtx,
233 ) -> eyre::Result<()> {
234 if !ctx.adjust_abi {
235 return Ok(());
236 }
237
238 for import_name in <Wasip1ABIFunc as strum::VariantNames>::VARIANTS {
239 if let Ok(import_id) = (
240 UniqueName::CORE_MODULE_ROOT,
241 &gen_component_name(UniqueName::WASIP1_ABI_MODULE_ALT, import_name),
242 )
243 .get_fid(&module.imports)
244 {
245 if let Ok(import_id) =
246 (UniqueName::WASIP1_ABI_MODULE, import_name).get_fid(&module.imports)
247 {
248 module.imports.erase(import_id).unwrap();
249 }
250
251 let import_id = module.imports.get_imported_func(import_id).unwrap().id();
252 let import = module.imports.get_mut(import_id);
253 import.module = UniqueName::WASIP1_ABI_MODULE.to_string();
254 import.name = import_name.to_string();
255 }
256 }
257
258 let import_name = <Wasip1ThreadsABIFunc as strum::VariantNames>::VARIANTS
259 .iter()
260 .exactly_one()
261 .unwrap();
262
263 if let Ok(import_id) = (
264 UniqueName::THREADS_MODULE_ROOT,
265 &gen_component_name(UniqueName::WASIP1_THREADS_ABI_MODULE_ALT, import_name),
266 )
267 .get_fid(&module.imports)
268 {
269 if let Ok(import_id) =
270 (UniqueName::WASIP1_THREADS_ABI_MODULE, import_name).get_fid(&module.imports)
271 {
272 module.imports.erase(import_id).unwrap();
273 }
274
275 let import_id = module.imports.get_imported_func(import_id).unwrap().id();
276 let import = module.imports.get_mut(import_id);
277 import.module = UniqueName::WASIP1_THREADS_ABI_MODULE.to_string();
278 import.name = import_name.to_string();
279
280 let export_name = Wasip1ThreadsABIExportFunc::VARIANTS
281 .iter()
282 .exactly_one()
283 .unwrap();
284
285 if let Ok(export_id) = export_name.get_fid(&module.exports) {
286 module.exports.erase(export_id)?;
287 }
288
289 let id = UniqueName::THREADS_EXPORT_MODULE_ROOT.get_fid(&module.exports)?;
291 let eid = module.exports.get_exported_func(id).unwrap().id();
292 let export = module.exports.get_mut(eid);
293 export.name = UniqueName::THREADS_EXPORT_MODULE_ROOT.to_string();
294 }
295
296 Ok(())
297 }
298}