Skip to main content

wasi_virt_layer_cli/generator/
patch_component.rs

1use strum::VariantNames;
2
3use crate::{
4    abi::Wasip1ABIFunc,
5    generator::Generator,
6    unique_name::UniqueName,
7    util::{WalrusUtilExport as _, WalrusUtilImport as _, gen_component_name},
8};
9
10/// Apply the patch to use wit-bindgen.
11/// As it will be rejected otherwise,
12/// swap the import function generated by wit with the wasip1 import function.
13#[derive(Debug, Default)]
14pub struct PatchComponent;
15
16impl Generator for PatchComponent {
17    fn pre_vfs(
18        &mut self,
19        module: &mut walrus::Module,
20        _: &crate::generator::GeneratorCtx,
21    ) -> eyre::Result<()> {
22        for (name, (namespace, root)) in
23            <Wasip1ABIFunc as VariantNames>::VARIANTS
24                .iter()
25                .zip(core::iter::repeat((
26                    UniqueName::WASIP1_ABI_MODULE_ALT,
27                    UniqueName::CORE_MODULE_ROOT,
28                )))
29        {
30            let component_name = gen_component_name(namespace, name);
31
32            module.exports.erase(&format!("{name}_import_anchor"))?;
33
34            module.imports.may_swap_import(
35                (root, &component_name),
36                (UniqueName::WASIP1_ABI_MODULE, name),
37            )?;
38        }
39
40        Ok(())
41    }
42}