use smallvec::SmallVec;
use strum::VariantNames;
use crate::{
abi::Wasip1ABIFunc,
generator::{Generator, GeneratorCtx, memory::MemoryUniqueName},
unique_name::UniqueName,
};
#[derive(Debug, Default)]
pub struct Anonymous;
impl Generator for Anonymous {
fn pre_vfs(&mut self, module: &mut walrus::Module, ctx: &GeneratorCtx) -> eyre::Result<()> {
let anonymous_targets = module
.exports
.iter()
.filter_map(|e| {
e.name
.strip_prefix("__wasip1_vfs_")?
.strip_suffix("__start_anchor")
.map(|s| s.to_string())
})
.inspect(|targets| {
log::info!("Found target which VFS wasm required: {targets}");
})
.collect::<Vec<_>>();
let collected = ctx
.target_names
.iter()
.filter(|t| !anonymous_targets.iter().any(|at| at == t.as_ref()))
.collect::<SmallVec<[_; 1]>>();
const PREFIX: &str = UniqueName::PREFIX;
if collected.len() == 0 {
if module
.exports
.iter()
.any(|e| e.name == format!("{PREFIX}anonymous__start_anchor"))
{
eyre::bail!(
"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.",
);
} else {
return Ok(());
}
}
if collected.len() > 1 {
eyre::bail!(
"There are multiple targets which VFS wasm required but not assigned: {:?}. Please specify the target explicitly instead of `<anonymous>`.",
collected
);
}
let only_target = &collected[0];
const EXPORT_POSTFIXS: &[&str] = &[
"__start_anchor",
"_memory_trap_anchor",
"_wasi_thread_start_anchor",
];
for postfix in EXPORT_POSTFIXS {
let anonymous_export_name = format!("{PREFIX}anonymous{postfix}");
let target_export_name = format!("{PREFIX}{only_target}{postfix}");
if let Some(export) = module
.exports
.iter_mut()
.find(|e| e.name == anonymous_export_name)
{
export.name = target_export_name;
}
}
for export in module.exports.iter_mut() {
if let Some(anonymous_suffix) = export
.name
.strip_prefix(PREFIX)
.and_then(|s| s.strip_prefix("anonymous_"))
{
if let Some(f) = Wasip1ABIFunc::VARIANTS
.iter()
.find(|v| anonymous_suffix == **v)
{
export.name = format!("{PREFIX}{only_target}_{f}");
}
}
}
if let Some(special_anonymous_export) = module
.exports
.iter_mut()
.find(|e| e.name == format!("{PREFIX}wasi_thread_spawn_anonymous"))
{
special_anonymous_export.name = format!("{PREFIX}wasi_thread_spawn_{only_target}");
}
const NAMESPACE: &str = UniqueName::NAMESPACE;
const EXTRA_IMPORTS: &[&str] = &[
"_start",
"memory_trap",
"__main_void",
"reset",
"wasi_thread_start",
];
for import in module
.imports
.iter_mut()
.filter(|import| import.module == NAMESPACE)
{
if let Some(anonymous_suffix) = import
.name
.strip_prefix(PREFIX)
.and_then(|s| s.strip_prefix("anonymous_"))
{
if let Some(f) = MemoryUniqueName::VARIANTS
.iter()
.chain(EXTRA_IMPORTS)
.find(|v| anonymous_suffix == **v)
{
import.name = format!("{PREFIX}{only_target}_{f}");
}
}
}
for export in module.exports.iter() {
log::info!("Export: {}", export.name);
}
for import in module.imports.iter() {
log::info!("Import: {}.{}", import.module, import.name);
}
Ok(())
}
}