wasi_virt_layer_cli/generator/
anonymous.rs1use smallvec::SmallVec;
6use strum::VariantNames;
7
8use crate::{
9 abi::Wasip1ABIFunc,
10 generator::{Generator, GeneratorCtx, memory::MemoryUniqueName},
11 unique_name::UniqueName,
12};
13
14#[derive(Debug, Default)]
16pub struct Anonymous;
17
18impl Generator for Anonymous {
19 fn pre_vfs(&mut self, module: &mut walrus::Module, ctx: &GeneratorCtx) -> eyre::Result<()> {
20 let anonymous_targets = module
25 .exports
26 .iter()
27 .filter_map(|e| {
28 e.name
29 .strip_prefix("__wasip1_vfs_")?
30 .strip_suffix("__start_anchor")
31 .map(|s| s.to_string())
32 })
33 .inspect(|targets| {
34 log::info!("Found target which VFS wasm required: {targets}");
35 })
36 .collect::<Vec<_>>();
37
38 let collected = ctx
39 .target_names
40 .iter()
41 .filter(|t| !anonymous_targets.iter().any(|at| at == t.as_ref()))
42 .collect::<SmallVec<[_; 1]>>();
43
44 const PREFIX: &str = UniqueName::PREFIX;
45
46 if collected.len() == 0 {
47 if module
52 .exports
53 .iter()
54 .any(|e| e.name == format!("{PREFIX}anonymous__start_anchor"))
55 {
56 eyre::bail!(
58 "The target which VFS wasm required from `<anonymous>` does not exist. Please add argument `<target_wasm>` or remove `<anonymous>` if you do not want to use VFS wasm.",
59 );
60 } else {
61 return Ok(());
62 }
63 }
64 if collected.len() > 1 {
65 eyre::bail!(
66 "There are multiple targets which VFS wasm required but not assigned: {:?}. Please specify the target explicitly instead of `<anonymous>`.",
67 collected
68 );
69 }
70
71 let only_target = &collected[0];
73
74 const EXPORT_POSTFIXS: &[&str] = &[
76 "__start_anchor",
77 "_memory_trap_anchor",
78 "_wasi_thread_start_anchor",
79 ];
80
81 for postfix in EXPORT_POSTFIXS {
82 let anonymous_export_name = format!("{PREFIX}anonymous{postfix}");
83 let target_export_name = format!("{PREFIX}{only_target}{postfix}");
84 if let Some(export) = module
85 .exports
86 .iter_mut()
87 .find(|e| e.name == anonymous_export_name)
88 {
89 export.name = target_export_name;
90 }
91 }
92
93 for export in module.exports.iter_mut() {
94 if let Some(anonymous_suffix) = export
95 .name
96 .strip_prefix(PREFIX)
97 .and_then(|s| s.strip_prefix("anonymous_"))
98 {
99 if let Some(f) = Wasip1ABIFunc::VARIANTS
100 .iter()
101 .find(|v| anonymous_suffix == **v)
102 {
103 export.name = format!("{PREFIX}{only_target}_{f}");
104 }
105 }
106 }
107
108 if let Some(special_anonymous_export) = module
109 .exports
110 .iter_mut()
111 .find(|e| e.name == format!("{PREFIX}wasi_thread_spawn_anonymous"))
112 {
113 special_anonymous_export.name = format!("{PREFIX}wasi_thread_spawn_{only_target}");
114 }
115
116 const NAMESPACE: &str = UniqueName::NAMESPACE;
117
118 const EXTRA_IMPORTS: &[&str] = &[
120 "_start",
121 "memory_trap",
122 "__main_void",
123 "reset",
124 "wasi_thread_start",
125 ];
126
127 for import in module
128 .imports
129 .iter_mut()
130 .filter(|import| import.module == NAMESPACE)
131 {
132 if let Some(anonymous_suffix) = import
133 .name
134 .strip_prefix(PREFIX)
135 .and_then(|s| s.strip_prefix("anonymous_"))
136 {
137 if let Some(f) = MemoryUniqueName::VARIANTS
138 .iter()
139 .chain(EXTRA_IMPORTS)
140 .find(|v| anonymous_suffix == **v)
141 {
142 import.name = format!("{PREFIX}{only_target}_{f}");
143 }
144 }
145 }
146
147 for export in module.exports.iter() {
149 log::info!("Export: {}", export.name);
150 }
151
152 for import in module.imports.iter() {
154 log::info!("Import: {}.{}", import.module, import.name);
155 }
156
157 Ok(())
158 }
159}