pub mod abi_connect;
pub mod anonymous;
pub mod check;
pub mod debug;
pub mod memory;
pub mod patch_component;
pub mod producer;
pub mod shared_global;
pub mod special_func;
pub mod start_section;
pub mod threads;
use std::{any::Any, collections::HashMap, fs, io::Read as _, str::FromStr};
use camino::Utf8PathBuf;
use compact_str::{CompactString, ToCompactString as _};
use eyre::{Context as _, ContextCompat};
use itertools::Itertools;
use walrus::MemoryId;
use crate::{
args::{self, TargetMemoryType}, compile, config_checker::TomlRestorers, fallback_command, generator::start_section::StartSectionGenerator, unique_name::UniqueName, util::{
CaminoUtilModule as _, ResultUtil, WalrusFID as _, WalrusUtilExport as _, WalrusUtilModule,
WasmName, WasmNameHolder,
}
};
#[derive(Debug)]
pub struct GeneratorCtx {
pub vfs_name: WasmName,
pub target_names: Box<[WasmName]>,
pub target_names_with_self: Box<[WasmName]>,
pub vfs_used_memory_id: Option<MemoryId>,
pub vfs_used_global_id: Option<Box<[walrus::GlobalId]>>,
pub target_used_memory_id: Option<HashMap<WasmName, MemoryId>>,
pub target_used_global_id: Option<HashMap<WasmName, Box<[walrus::GlobalId]>>>,
pub start_func_id: Option<HashMap<WasmName, walrus::FunctionId>>,
pub target_memory_type: TargetMemoryType,
pub unstable_print_debug: bool,
pub dwarf: bool,
pub threads: bool,
pub adjust_abi: bool,
pub keep_build_artifacts: bool,
pub start_section_builder: Option<start_section::StartSectionBuilder>,
}
#[derive(Debug, Default)]
pub struct ComponentCtx {
vfs_name: Option<WasmName>,
target_names: Option<Box<[WasmName]>>,
target_memory_type: Option<TargetMemoryType>,
unstable_print_debug: Option<bool>,
dwarf: bool,
threads: Option<bool>,
adjust_abi: bool,
}
struct CompressNames {
names: Box<[String]>,
}
impl ToString for CompressNames {
fn to_string(&self) -> String {
self.names
.iter()
.map(|s| {
let len = s.len();
let len_len = len.to_string().len();
format!("{:09}{}{}", len_len, len, s)
})
.join("")
}
}
impl FromStr for CompressNames {
type Err = eyre::Report;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut names = Vec::new();
let mut rest = s;
while !rest.is_empty() {
let len_len = &rest[0..9];
let len_len: usize = len_len[len_len
.chars()
.position(|c| c != '0')
.unwrap_or(len_len.chars().count() - 1)..]
.parse()
.wrap_err_with(|| format!("Failed to parse length of length: {len_len}"))?;
rest = rest.get(9..).unwrap();
let len: usize = rest
.get(0..len_len)
.ok_or_else(|| eyre::eyre!("Failed to get length"))?
.parse()
.wrap_err_with(|| format!("Failed to parse length: {rest}"))?;
rest = rest.get(len_len..).unwrap();
let name = rest
.get(0..len)
.ok_or_else(|| eyre::eyre!("Failed to get name"))?;
names.push(name.to_string());
rest = rest.get(len..).unwrap();
}
Ok(Self {
names: names.into_boxed_slice(),
})
}
}
#[derive(Debug, Default)]
pub struct ComponentCtxVisitor {
vfs_name: Option<CompactString>,
target_names: Option<Box<[CompactString]>>,
target_memory_type: Option<TargetMemoryType>,
unstable_print_debug: Option<bool>,
dwarf: Option<bool>,
threads: Option<bool>,
adjust_abi: bool,
}
impl ComponentCtxVisitor {
pub fn new(
vfs_name: WasmName,
target_names: Box<[WasmName]>,
target_memory_type: TargetMemoryType,
unstable_print_debug: bool,
dwarf: bool,
threads: bool,
adjust_abi: bool,
) -> Self {
Self {
vfs_name: Some(vfs_name.to_compact_string()),
target_names: Some(
target_names
.into_iter()
.map(|s| s.to_compact_string())
.collect(),
),
target_memory_type: Some(target_memory_type),
unstable_print_debug: Some(unstable_print_debug),
dwarf: Some(dwarf),
threads: Some(threads),
adjust_abi,
}
}
}
impl Generator for ComponentCtxVisitor {
fn pre_vfs(&mut self, module: &mut walrus::Module, ctx: &GeneratorCtx) -> eyre::Result<()> {
let vfs_name = ctx.vfs_name.to_compact_string();
let target_names = CompressNames {
names: ctx
.target_names
.iter()
.map(|s| s.to_string())
.collect::<Box<_>>(),
};
let GeneratorCtx {
vfs_name: _,
target_names: _,
target_memory_type,
unstable_print_debug,
dwarf,
threads,
adjust_abi,
target_names_with_self: _,
vfs_used_memory_id: _,
vfs_used_global_id: _,
target_used_memory_id: _,
target_used_global_id: _,
start_func_id: _,
keep_build_artifacts: _,
start_section_builder: _,
} = ctx;
module.save_info("vfs_name", vfs_name.to_string())?;
module.save_info("target_names", target_names)?;
module.save_info("target_memory_type", *target_memory_type)?;
module.save_info("unstable_print_debug", *unstable_print_debug)?;
module.save_info("dwarf", *dwarf)?;
module.save_info("threads", *threads)?;
module.save_info("adjust_abi", *adjust_abi)?;
Ok(())
}
fn post_components(
&mut self,
module: &mut walrus::Module,
_: &ComponentCtx,
) -> eyre::Result<()> {
let vfs_name = module.load_info::<String>("vfs_name")?;
let target_names = module.load_info::<CompressNames>("target_names")?;
let target_memory_type = module.load_info::<TargetMemoryType>("target_memory_type")?;
let unstable_print_debug = module.load_info::<bool>("unstable_print_debug")?;
let dwarf = module.load_info::<bool>("dwarf")?;
let threads = module.load_info::<bool>("threads")?;
let adjust_abi = module.load_info::<bool>("adjust_abi")?;
self.vfs_name = Some(vfs_name.to_compact_string());
self.target_names = Some(
target_names
.names
.into_iter()
.map(|s| s.to_compact_string())
.collect(),
);
self.target_memory_type = Some(target_memory_type);
self.unstable_print_debug = Some(unstable_print_debug);
self.dwarf = Some(dwarf);
self.threads = Some(threads);
self.adjust_abi = adjust_abi;
Ok(())
}
}
impl ComponentCtx {
pub fn new(
vfs_name: WasmName,
target_names: Box<[WasmName]>,
target_memory_type: TargetMemoryType,
unstable_print_debug: bool,
dwarf: bool,
threads: bool,
adjust_abi: bool,
) -> Self {
Self {
vfs_name: Some(vfs_name),
target_names: Some(target_names),
target_memory_type: Some(target_memory_type),
unstable_print_debug: Some(unstable_print_debug),
dwarf,
threads: Some(threads),
adjust_abi,
}
}
pub fn vfs_name(&self) -> &WasmName {
self.vfs_name.as_ref().unwrap()
}
pub fn target_names(&self) -> &Box<[WasmName]> {
self.target_names.as_ref().unwrap()
}
pub fn target_memory_type(&self) -> TargetMemoryType {
self.target_memory_type.unwrap()
}
pub fn unstable_print_debug(&self) -> bool {
self.unstable_print_debug.unwrap()
}
pub fn dwarf(&self) -> bool {
self.dwarf
}
pub fn threads(&self) -> bool {
self.threads.unwrap()
}
}
pub trait Generator: std::fmt::Debug + Any {
#[allow(unused_variables)]
fn pre_vfs(&mut self, module: &mut walrus::Module, ctx: &GeneratorCtx) -> eyre::Result<()> {
Ok(())
}
#[allow(unused_variables)]
fn pre_target(
&mut self,
module: &mut walrus::Module,
ctx: &GeneratorCtx,
external: &ModuleExternal,
) -> eyre::Result<()> {
Ok(())
}
#[allow(unused_variables)]
fn post_combine(
&mut self,
module: &mut walrus::Module,
ctx: &GeneratorCtx,
) -> eyre::Result<()> {
Ok(())
}
#[allow(unused_variables)]
fn post_lower_memory(
&mut self,
module: &mut walrus::Module,
ctx: &GeneratorCtx,
) -> eyre::Result<()> {
Ok(())
}
#[allow(unused_variables)]
fn post_components(
&mut self,
module: &mut walrus::Module,
ctx: &ComponentCtx,
) -> eyre::Result<()> {
Ok(())
}
#[allow(unused_variables)]
fn post_all_optimize(
&mut self,
module: &mut walrus::Module,
ctx: &ComponentCtx,
) -> eyre::Result<bool> {
Ok(false)
}
}
impl<T: std::fmt::Debug + Any + Generator> Generator for [T] {
fn pre_vfs(&mut self, module: &mut walrus::Module, ctx: &GeneratorCtx) -> eyre::Result<()> {
for generator in self {
generator.pre_vfs(module, ctx).wrap_err_with(|| {
eyre::eyre!(format!("Failed to run pre_vfs for {generator:?}"))
})?;
}
Ok(())
}
fn pre_target(
&mut self,
module: &mut walrus::Module,
ctx: &GeneratorCtx,
external: &ModuleExternal,
) -> eyre::Result<()> {
for generator in self {
generator
.pre_target(module, ctx, external)
.wrap_err_with(|| {
eyre::eyre!(format!("Failed to run pre_target for {generator:?}"))
})?;
}
Ok(())
}
fn post_combine(
&mut self,
module: &mut walrus::Module,
ctx: &GeneratorCtx,
) -> eyre::Result<()> {
for generator in self {
generator.post_combine(module, ctx).wrap_err_with(|| {
eyre::eyre!(format!("Failed to run post_combine for {generator:?}"))
})?;
}
Ok(())
}
fn post_lower_memory(
&mut self,
module: &mut walrus::Module,
ctx: &GeneratorCtx,
) -> eyre::Result<()> {
for generator in self {
generator.post_lower_memory(module, ctx).wrap_err_with(|| {
eyre::eyre!(format!("Failed to run post_lower_memory for {generator:?}"))
})?;
}
Ok(())
}
fn post_components(
&mut self,
module: &mut walrus::Module,
ctx: &ComponentCtx,
) -> eyre::Result<()> {
for generator in self {
generator.post_components(module, ctx).wrap_err_with(|| {
eyre::eyre!(format!("Failed to run post_components for {generator:?}"))
})?;
}
Ok(())
}
fn post_all_optimize(
&mut self,
module: &mut walrus::Module,
ctx: &ComponentCtx,
) -> eyre::Result<bool> {
let mut changed = false;
for generator in self {
changed |= generator.post_all_optimize(module, ctx).wrap_err_with(|| {
eyre::eyre!(format!("Failed to run post_all_optimize for {generator:?}"))
})?;
}
Ok(changed)
}
}
impl Generator for Box<dyn Generator + 'static> {
fn pre_vfs(&mut self, module: &mut walrus::Module, ctx: &GeneratorCtx) -> eyre::Result<()> {
(**self).pre_vfs(module, ctx)
}
fn pre_target(
&mut self,
module: &mut walrus::Module,
ctx: &GeneratorCtx,
external: &ModuleExternal,
) -> eyre::Result<()> {
(**self).pre_target(module, ctx, external)
}
fn post_combine(
&mut self,
module: &mut walrus::Module,
ctx: &GeneratorCtx,
) -> eyre::Result<()> {
(**self).post_combine(module, ctx)
}
fn post_lower_memory(
&mut self,
module: &mut walrus::Module,
ctx: &GeneratorCtx,
) -> eyre::Result<()> {
(**self).post_lower_memory(module, ctx)
}
fn post_components(
&mut self,
module: &mut walrus::Module,
ctx: &ComponentCtx,
) -> eyre::Result<()> {
(**self).post_components(module, ctx)
}
fn post_all_optimize(
&mut self,
module: &mut walrus::Module,
ctx: &ComponentCtx,
) -> eyre::Result<bool> {
(**self).post_all_optimize(module, ctx)
}
}
impl<'a> Generator for &'a mut (dyn Generator + 'a) {
fn pre_vfs(&mut self, module: &mut walrus::Module, ctx: &GeneratorCtx) -> eyre::Result<()> {
(**self).pre_vfs(module, ctx)
}
fn pre_target(
&mut self,
module: &mut walrus::Module,
ctx: &GeneratorCtx,
external: &ModuleExternal,
) -> eyre::Result<()> {
(**self).pre_target(module, ctx, external)
}
fn post_combine(
&mut self,
module: &mut walrus::Module,
ctx: &GeneratorCtx,
) -> eyre::Result<()> {
(**self).post_combine(module, ctx)
}
fn post_lower_memory(
&mut self,
module: &mut walrus::Module,
ctx: &GeneratorCtx,
) -> eyre::Result<()> {
(**self).post_lower_memory(module, ctx)
}
fn post_components(
&mut self,
module: &mut walrus::Module,
ctx: &ComponentCtx,
) -> eyre::Result<()> {
(**self).post_components(module, ctx)
}
fn post_all_optimize(
&mut self,
module: &mut walrus::Module,
ctx: &ComponentCtx,
) -> eyre::Result<bool> {
(**self).post_all_optimize(module, ctx)
}
}
#[derive(Debug)]
pub struct ModuleExternal {
pub name: WasmName,
}
impl ModuleExternal {
pub fn new(name: &WasmName) -> Self {
Self { name: name.clone() }
}
}
#[derive(Debug)]
pub struct GeneratorRunner {
pub checkers: Vec<Box<dyn Generator + 'static>>,
pub generators: Vec<Box<dyn Generator + 'static>>,
pub ctx: GeneratorCtx,
pub path: WasmPath,
pub targets: Box<[WasmPath]>,
pub toml_restorers: Option<TomlRestorers>,
pub memory_hint: HashMap<WasmName, usize>,
pub wasm_name_holder: WasmNameHolder,
}
#[derive(Debug)]
pub struct ComponentRunner {
pub generators: Vec<Box<dyn Generator + 'static>>,
pub ctx: Option<ComponentCtx>,
pub path: WasmPath,
pub wasm_name_holder: Option<WasmNameHolder>,
}
pub(crate) trait WrapRunner<T> {
#[allow(unused_variables)]
fn wrap_run(
self,
path: &mut WasmPath,
dwarf: bool,
keep_build_artifacts: bool,
) -> eyre::Result<T>
where
Self: Sized;
}
impl<T, F: FnOnce(&mut walrus::Module) -> eyre::Result<T>> WrapRunner<T> for F {
fn wrap_run(
self,
path: &mut WasmPath,
dwarf: bool,
keep_build_artifacts: bool,
) -> eyre::Result<T> {
let old_path = path.path()?;
let module =
&mut walrus::Module::load(old_path, dwarf).wrap_err("Failed to load Wasm module")?;
let result = (self)(module)?;
let new_path = old_path.with_extension("adjusted.wasm");
if fs::metadata(&new_path).is_ok() {
fs::remove_file(&new_path)
.wrap_err_with(|| format!("Failed to remove existing file {new_path}"))?;
}
module
.emit_wasm_file(&new_path)
.to_eyre()
.wrap_err_with(|| format!("Failed to write adjusted Wasm to {new_path}"))?;
if !keep_build_artifacts {
std::fs::remove_file(old_path)
.wrap_err_with(|| format!("Failed to remove existing file {old_path}"))?;
}
path.set_path(new_path)?;
Ok(result)
}
}
pub(crate) trait EndWithOpt<T> {
#[allow(unused_variables)]
fn with_opt(
self,
path: &mut WasmPath,
dwarf: bool,
keep_build_artifacts: bool,
) -> eyre::Result<T>
where
Self: Sized;
#[allow(dead_code)]
fn with_opt_args(
self,
path: &mut WasmPath,
args: &[&str],
require_update: bool,
dwarf: bool,
keep_build_artifacts: bool,
) -> eyre::Result<T>
where
Self: Sized;
}
impl<T, F: FnOnce(&mut WasmPath) -> eyre::Result<T>> EndWithOpt<T> for F {
fn with_opt(
self,
path: &mut WasmPath,
dwarf: bool,
keep_build_artifacts: bool,
) -> eyre::Result<T>
where
Self: Sized,
{
let result = (self)(path).wrap_err("Failed to run with with_opt")?;
println!("Optimizing Wasm...");
let old_path = path.path()?.clone();
let new_path = compile::optimize_wasm(&old_path, &[], false, dwarf)
.wrap_err("Failed to optimize Wasm")?;
if !keep_build_artifacts && old_path != new_path {
std::fs::remove_file(&old_path)
.wrap_err_with(|| format!("Failed to remove existing file {old_path}"))?;
}
path.set_path(new_path)?;
Ok(result)
}
fn with_opt_args(
self,
path: &mut WasmPath,
args: &[&str],
require_update: bool,
dwarf: bool,
keep_build_artifacts: bool,
) -> eyre::Result<T>
where
Self: Sized,
{
let result = (self)(path).wrap_err("Failed to run with with_opt_args")?;
println!("Optimizing Wasm... with args: {}", args.iter().join(" "));
let old_path = path.path()?.clone();
let new_path = compile::optimize_wasm(&old_path, args, require_update, dwarf)
.wrap_err("Failed to optimize Wasm")?;
if !keep_build_artifacts && old_path != new_path {
std::fs::remove_file(&old_path)
.wrap_err_with(|| format!("Failed to remove existing file {old_path}"))?;
}
path.set_path(new_path)?;
Ok(result)
}
}
impl GeneratorRunner {
pub fn new(
path: WasmPath,
targets: Box<[WasmPath]>,
threads: bool,
dwarf: bool,
unstable_print_debug: bool,
adjust_abi: bool,
keep_build_artifacts: bool,
memory_type: TargetMemoryType,
toml_restorers: TomlRestorers,
memory_hint: Box<[Option<usize>]>,
) -> eyre::Result<Self> {
let target_names_with_self = core::iter::once(Ok(path.name()?.to_compact_string()))
.chain(targets.iter().map(|t| Ok(t.name()?.to_compact_string())))
.collect::<eyre::Result<Box<_>>>()?;
let wasm_name_holder = WasmNameHolder::new(target_names_with_self);
let mut wasm_name_holder_iter = wasm_name_holder.iter();
let vfs_name = wasm_name_holder_iter
.next()
.ok_or_else(|| eyre::eyre!("Failed to get VFS name"))?;
let target_names = wasm_name_holder_iter.collect::<Box<_>>();
for name in target_names.iter() {
log::info!("Targeting module: {name}");
}
log::info!("VFS module: {vfs_name}");
let target_names_with_self = target_names
.iter()
.cloned()
.chain(core::iter::once(vfs_name.clone()))
.collect::<Box<_>>();
let memory_hint = memory_hint
.into_iter()
.zip(target_names.iter().cloned())
.filter_map(|(hint, name)| hint.map(|h| (name, h)))
.collect::<HashMap<_, _>>();
Ok(Self {
checkers: Vec::new(),
generators: Vec::new(),
ctx: GeneratorCtx {
vfs_name,
target_names,
target_names_with_self,
target_memory_type: memory_type,
unstable_print_debug,
dwarf,
threads,
adjust_abi,
keep_build_artifacts,
vfs_used_memory_id: None,
vfs_used_global_id: None,
target_used_memory_id: None,
target_used_global_id: None,
start_func_id: None,
start_section_builder: None,
},
path,
targets,
toml_restorers: Some(toml_restorers),
memory_hint,
wasm_name_holder,
})
}
pub fn add_generator<G: Generator + 'static>(&mut self, generator: G) {
self.generators.push(Box::new(generator));
}
pub fn checker(&mut self, checker: impl Generator + 'static) {
self.checkers.push(Box::new(checker));
}
pub fn get_generator_ref<T: Generator + 'static>(&self) -> eyre::Result<&T> {
fn downcast_ref<T: 'static>(b: &dyn Any) -> Option<&'_ T> {
if b.is::<T>() {
Some(b.downcast_ref::<T>().unwrap())
} else {
None
}
}
self.generators
.iter()
.map(|g| g.as_ref())
.find_map(|g| downcast_ref::<T>(g))
.wrap_err_with(|| {
eyre::eyre!("Failed to get generator: {}", core::any::type_name::<T>())
})
.wrap_err_with(|| eyre::eyre!("Available generators: {:?}", self.generators))
}
#[deprecated(
note = "Ensure this function is self-contained. This is a temporary measure for debugging purposes."
)]
pub const fn path(&self) -> &WasmPath {
&self.path
}
#[deprecated(
note = "Ensure this function is self-contained. This is a temporary measure for debugging purposes."
)]
pub const fn targets(&self) -> &Box<[WasmPath]> {
&self.targets
}
pub const fn ctx(&self) -> &GeneratorCtx {
&self.ctx
}
pub fn definitely(&mut self) -> eyre::Result<()> {
self.path.definitely(self.ctx.threads)?;
for target in &mut self.targets {
target.definitely(self.ctx.threads)?;
}
Ok(())
}
pub fn run_layers_to_component(
mut self,
out_dir: &Utf8PathBuf,
keep_build_artifacts: bool,
) -> eyre::Result<ComponentRunner> {
self.definitely()?;
let toml_restorers = self
.toml_restorers
.take()
.ok_or_else(|| eyre::eyre!("TomlRestorers already taken"))?;
toml_restorers
.restore()
.wrap_err("Failed to restore toml files")?;
let mut mem_id_visitor = MemoryIDVisitor {
memory_hint: self.memory_hint.clone(),
used_vfs_memory_id: None,
used_target_memory_id: None,
};
let mut global_id_visitor = GlobalIdVisitor {
vfs_global_id: None,
global_id: None,
};
let mut start_section_generator = StartSectionGenerator::default();
let dwarf = self.ctx.dwarf;
println!("Remove existing output directory...");
if std::fs::metadata(&out_dir).is_ok() {
std::fs::remove_dir_all(&out_dir).expect("Failed to remove existing directory");
}
std::fs::create_dir_all(&out_dir).expect("Failed to create output directory");
let mut component_ctx_visitor = ComponentCtxVisitor::new(
self.ctx.vfs_name.clone(),
self.ctx.target_names.clone(),
self.ctx.target_memory_type,
self.ctx.unstable_print_debug,
self.ctx.dwarf,
self.ctx.threads,
self.ctx.adjust_abi,
);
println!("Adjusting VFS Wasm...");
(|path: &mut WasmPath| {
(|module: &mut walrus::Module| {
self.checkers
.pre_vfs(module, &self.ctx)
.wrap_err("Failed to run checkers in pre_vfs")?;
mem_id_visitor
.pre_vfs(module, &self.ctx)
.wrap_err("Failed in pre_vfs")?;
global_id_visitor
.pre_vfs(module, &self.ctx)
.wrap_err("Failed in pre_vfs")?;
self.ctx.vfs_used_memory_id = mem_id_visitor.used_vfs_memory_id;
component_ctx_visitor
.pre_vfs(module, &self.ctx)
.wrap_err("Failed in pre_vfs")?;
start_section_generator.init(
module,
self.ctx.vfs_name.clone(),
&self.ctx.target_names,
);
self.ctx.start_section_builder = Some(start_section_generator.builder());
self.generators
.pre_vfs(module, &self.ctx)
.wrap_err("Failed in run_pre_vfs")
})
.wrap_run(path, dwarf, keep_build_artifacts)
})
.with_opt(&mut self.path, dwarf, keep_build_artifacts)?;
let mut start_section_generator = Some(start_section_generator);
println!("Adjusting target Wasm...");
self.ctx.vfs_used_memory_id = None;
for (target, target_name) in self.targets.iter_mut().zip(self.ctx.target_names.clone()) {
(|path: &mut WasmPath| {
(|module: &mut walrus::Module| {
let external = ModuleExternal::new(&target_name);
mem_id_visitor
.pre_target(module, &self.ctx, &external)
.wrap_err("Failed in pre_target")?;
global_id_visitor
.pre_target(module, &self.ctx, &external)
.wrap_err("Failed in pre_target")?;
self.ctx.target_used_memory_id = mem_id_visitor.used_target_memory_id.clone();
self.generators
.pre_target(module, &self.ctx, &external)
.wrap_err("Failed in run_pre_target")
})
.wrap_run(path, dwarf, keep_build_artifacts)
})
.with_opt(target, dwarf, keep_build_artifacts)?;
}
println!("Combining Wasm modules...");
self.ctx.vfs_used_memory_id = None;
self.ctx.target_used_memory_id = None;
let output = format!("{out_dir}/merged.wasm");
(|path: &mut WasmPath| {
let old_path = path.path()?.clone();
merge(
&old_path,
&self
.targets
.iter()
.map(|t| t.path())
.collect::<eyre::Result<Vec<_>>>()?,
&output,
self.ctx.threads,
dwarf,
)
.wrap_err("Failed to combine Wasm modules")?;
if !keep_build_artifacts {
std::fs::remove_file(&old_path)
.wrap_err_with(|| format!("Failed to remove existing file {old_path}"))?;
}
path.set_path(output.into())
})
.with_opt(&mut self.path, dwarf, keep_build_artifacts)?;
println!("Adjusting Merged Wasm...");
(|path: &mut WasmPath| {
(|module: &mut walrus::Module| {
mem_id_visitor
.post_combine(module, &self.ctx)
.wrap_err("Failed in post_combine")?;
global_id_visitor
.post_combine(module, &self.ctx)
.wrap_err("Failed in post_combine")?;
let mut start_func_id_visitor = StartFuncIdVisitor::default();
start_func_id_visitor
.post_combine(module, &self.ctx)
.wrap_err("Failed in post_combine")?;
self.ctx.vfs_used_memory_id = mem_id_visitor.used_vfs_memory_id.take();
self.ctx.target_used_memory_id = mem_id_visitor.used_target_memory_id.take();
self.ctx.vfs_used_global_id = global_id_visitor.vfs_global_id.take();
self.ctx.target_used_global_id = global_id_visitor.global_id.take();
self.ctx.start_func_id = start_func_id_visitor.start_func_id.take();
self.generators.post_combine(module, &self.ctx)?;
if self.ctx.target_memory_type == TargetMemoryType::Multi {
start_section_generator
.take()
.unwrap()
.build(module, &self.ctx)?;
}
Ok(())
})
.wrap_run(path, dwarf, keep_build_artifacts)
})
.with_opt(&mut self.path, dwarf, keep_build_artifacts)?;
self.ctx.vfs_used_memory_id = None;
self.ctx.target_used_memory_id = None;
self.ctx.vfs_used_global_id = None;
self.ctx.target_used_global_id = None;
self.ctx.start_func_id = None;
if self.ctx.target_memory_type == TargetMemoryType::Single {
let old_path = self.path.path()?.clone();
println!("Generating single memory Merged Wasm...");
let optimized_path =
compile::optimize_wasm(&old_path, &["--multi-memory-lowering"], true, dwarf)?;
if !keep_build_artifacts {
std::fs::remove_file(&old_path)
.wrap_err_with(|| format!("Failed to remove existing file {old_path}"))?;
}
self.path.set_path(optimized_path)?;
(|path: &mut WasmPath| {
(|module: &mut walrus::Module| {
mem_id_visitor
.post_lower_memory(module, &self.ctx)
.wrap_err("Failed in post_lower_memory")?;
self.ctx.vfs_used_memory_id = mem_id_visitor.used_vfs_memory_id.take();
self.ctx.target_used_memory_id = mem_id_visitor.used_target_memory_id.take();
self.generators
.post_lower_memory(module, &self.ctx)
.wrap_err("Failed in run_post_lower_memory")?;
start_section_generator
.take()
.unwrap()
.build(module, &self.ctx)?;
Ok(())
})
.wrap_run(path, dwarf, keep_build_artifacts)
})
.with_opt(&mut self.path, dwarf, keep_build_artifacts)?;
}
println!("Translating Wasm to Component...");
let old_path = self.path.path()?.clone();
let component = compile::wasm_to_component(&old_path, &self.ctx.target_names)
.wrap_err("Failed to translate Wasm to Component")?;
if !keep_build_artifacts {
std::fs::remove_file(&old_path)
.wrap_err_with(|| format!("Failed to remove existing file {old_path}"))?;
}
let new_component = format!("{out_dir}/{}.component.wasm", self.ctx.vfs_name);
std::fs::rename(&component, &new_component)
.wrap_err_with(|| format!("Failed to rename file {component} to {new_component}"))?;
self.path.set_path(Utf8PathBuf::from(new_component))?;
Ok(ComponentRunner::with_generators(self.path, self.generators))
}
}
impl ComponentRunner {
pub fn new(path: WasmPath) -> Self {
Self {
generators: Vec::new(),
ctx: None,
path,
wasm_name_holder: None,
}
}
pub fn checker(&mut self, checker: impl Generator + 'static) {
self.generators.push(Box::new(checker));
}
pub fn with_generators(path: WasmPath, generators: Vec<Box<dyn Generator + 'static>>) -> Self {
Self {
generators,
ctx: None,
path,
wasm_name_holder: None,
}
}
pub fn add_generator<G: Generator + 'static>(&mut self, generator: G) {
self.generators.push(Box::new(generator));
}
pub fn get_generator_ref<T: Generator + 'static>(&self) -> eyre::Result<&T> {
fn downcast_ref<T: 'static>(b: &dyn Any) -> Option<&'_ T> {
if b.is::<T>() {
Some(b.downcast_ref::<T>().unwrap())
} else {
None
}
}
self.generators
.iter()
.map(|g| g.as_ref())
.find_map(|g| downcast_ref::<T>(g))
.wrap_err_with(|| {
eyre::eyre!("Failed to get generator: {}", core::any::type_name::<T>())
})
.wrap_err_with(|| eyre::eyre!("Available generators: {:?}", self.generators))
}
pub fn component_to_files(
&mut self,
parsed_args: &(impl args::PostBuildContext + ?Sized),
dwarf: bool,
only_core: bool,
) -> eyre::Result<(bool, CompactString, HashMap<CompactString, (u64, u64)>)> {
let out_dir = parsed_args.out_dir();
let name = self.path.name()?;
println!("Translating Component to JS...");
let core_wasm_path = (|path: &mut WasmPath| {
let old_path = path.path()?.clone();
let binary = std::fs::read(&old_path).wrap_err("Failed to read component")?;
let transpiled = parsed_args
.transpile_to_js(&binary, &name)
.wrap_err("Failed to transpile to JS")?;
let mut core_wasm = None;
for (name, data) in transpiled.files.iter() {
let name = camino::Utf8PathBuf::from(name);
let file_name = out_dir.join(&name);
if std::fs::metadata(&file_name).is_ok() {
std::fs::remove_file(&file_name).wrap_err_with(|| {
eyre::eyre!("Failed to remove existing file: {file_name}")
})?;
}
if name.as_str().ends_with(".core.wasm") {
let file_name = camino::Utf8PathBuf::from(file_name);
std::fs::write(&file_name, &data).wrap_err_with(|| {
eyre::eyre!("Failed to write core wasm file: {file_name}")
})?;
core_wasm = Some(file_name);
} else {
if let Some(parent) = name.parent() {
if !parent.as_str().is_empty() {
let dir = name.ancestors().nth(1).wrap_err_with(|| {
eyre::eyre!("Failed to get parent directory: {}", name)
})?;
let joined_dir = out_dir.join(dir);
if !std::fs::metadata(&joined_dir).is_ok() {
if dir.as_str() != "interfaces" {
log::warn!("Creating directory: {joined_dir}");
}
if only_core {
continue;
}
std::fs::create_dir_all(&joined_dir).wrap_err_with(|| {
eyre::eyre!("Failed to create directory: {joined_dir}")
})?;
}
}
}
if only_core {
continue;
}
std::fs::write(&file_name, &data).wrap_err_with(|| {
eyre::eyre!("Failed to write transpiled file: {file_name}")
})?;
}
}
let core_wasm = core_wasm
.as_ref()
.ok_or_else(|| eyre::eyre!("Failed to find core wasm"))?;
if !parsed_args.keep_build_artifacts() {
std::fs::remove_file(&old_path)
.wrap_err_with(|| format!("Failed to remove existing file {old_path}"))?;
}
path.set_path(core_wasm.clone())?;
Ok(core_wasm.clone())
})
.with_opt(&mut self.path, dwarf, parsed_args.keep_build_artifacts())?;
let mem_size_visitor = MemorySizeVisitor::default();
self.generators.push(Box::new(mem_size_visitor));
println!("Adjusting component Merged Wasm...");
(|path: &mut WasmPath| {
(|module: &mut walrus::Module| {
let mut visitor = ComponentCtxVisitor::default();
visitor
.post_components(
module,
&ComponentCtx {
dwarf,
..Default::default()
},
)
.wrap_err("Failed in post_components")?;
let wasm_name_holder = WasmNameHolder::new(
visitor
.target_names
.unwrap()
.into_iter()
.chain(core::iter::once(visitor.vfs_name.unwrap()))
.collect::<Box<_>>(),
);
self.wasm_name_holder = Some(wasm_name_holder);
let mut wasm_name_holder_iter = self.wasm_name_holder.as_ref().unwrap().iter();
self.ctx = Some(ComponentCtx {
vfs_name: Some(wasm_name_holder_iter.next().unwrap()),
target_names: Some(wasm_name_holder_iter.collect::<Box<_>>()),
target_memory_type: Some(visitor.target_memory_type.unwrap()),
unstable_print_debug: Some(visitor.unstable_print_debug.unwrap()),
dwarf: visitor.dwarf.unwrap(),
threads: Some(visitor.threads.unwrap()),
adjust_abi: visitor.adjust_abi,
});
self.generators
.post_components(module, self.ctx.as_ref().unwrap())
.wrap_err("Failed in run_post_components")
})
.wrap_run(path, dwarf, parsed_args.keep_build_artifacts())
})
.with_opt(&mut self.path, dwarf, parsed_args.keep_build_artifacts())?;
let dwarf = {
let new_dwarf = self.ctx.as_ref().unwrap().dwarf;
if dwarf && !new_dwarf {
log::warn!(
"Dwarf was disabled in component processing, you should re-run with --dwarf"
);
}
new_dwarf
};
println!("Final optimizing Merged Wasm...");
let mut i = 0;
while i < self.generators.len() {
(|module: &mut walrus::Module| {
loop {
if self.generators[i]
.post_all_optimize(module, self.ctx.as_ref().unwrap())
.wrap_err("Failed in post_all_optimize")?
{
i += 1;
return Ok(());
}
i += 1;
if i >= self.generators.len() {
return Ok(());
}
}
})
.wrap_run(&mut self.path, dwarf, parsed_args.keep_build_artifacts())?;
}
std::fs::rename(self.path.path()?, &core_wasm_path).wrap_err_with(|| {
eyre::eyre!(
"Failed to rename final wasm from {} to {}",
self.path.path().unwrap(),
core_wasm_path
)
})?;
Ok((
self.ctx.as_ref().unwrap().threads(),
core_wasm_path
.get_file_main_name()
.ok_or_else(|| eyre::eyre!("Failed to get file name"))?,
self.get_generator_ref::<MemorySizeVisitor>()?
.mem_size
.clone()
.unwrap(),
))
}
}
#[derive(Debug, Default, Clone)]
struct MemoryIDVisitor {
pub memory_hint: HashMap<WasmName, usize>,
pub used_vfs_memory_id: Option<MemoryId>,
pub used_target_memory_id: Option<HashMap<WasmName, MemoryId>>,
}
impl Generator for MemoryIDVisitor {
fn pre_vfs(
&mut self,
module: &mut walrus::Module,
_: &crate::generator::GeneratorCtx,
) -> eyre::Result<()> {
println!("Finding VFS memory id...");
let id = module
.get_memory_anchor("vfs", false)
.wrap_err("Failed to get target memory id")?;
self.used_vfs_memory_id = Some(id);
Ok(())
}
fn pre_target(
&mut self,
module: &mut walrus::Module,
_: &crate::generator::GeneratorCtx,
external: &ModuleExternal,
) -> eyre::Result<()> {
let id = module
.find_used_memory_id(self.memory_hint.get(&external.name).copied())
.wrap_err("Failed to find used memory id")?;
module
.create_memory_anchor(&external.name, id)
.wrap_err("Failed to create memory anchor")?;
self.used_target_memory_id
.get_or_insert_default()
.insert(external.name.clone(), id);
Ok(())
}
fn post_combine(
&mut self,
module: &mut walrus::Module,
ctx: &GeneratorCtx,
) -> eyre::Result<()> {
let id = module
.get_memory_anchor("vfs", true)
.wrap_err("Failed to find used memory id after combine")?;
self.used_vfs_memory_id = Some(id);
self.used_target_memory_id.get_or_insert_default().clear();
for wasm in &ctx.target_names {
let id = module
.get_memory_anchor(wasm, true)
.wrap_err("Failed to find used memory id after combine")?;
self.used_target_memory_id
.as_mut()
.unwrap()
.insert(wasm.clone(), id);
}
Ok(())
}
fn post_lower_memory(
&mut self,
module: &mut walrus::Module,
_: &GeneratorCtx,
) -> eyre::Result<()> {
let id = module
.get_memory_id()
.to_eyre()
.wrap_err("Failed to get single memory id after lowering")?;
self.used_vfs_memory_id = Some(id);
self.used_target_memory_id = None;
Ok(())
}
}
#[derive(Debug, Default, Clone)]
struct MemorySizeVisitor {
mem_size: Option<HashMap<CompactString, (u64, u64)>>,
}
impl Generator for MemorySizeVisitor {
fn post_components(
&mut self,
module: &mut walrus::Module,
_: &ComponentCtx,
) -> eyre::Result<()> {
let mem_size = module
.memories
.iter()
.filter(|mem| mem.import.is_some())
.map(|mem| {
(
module
.imports
.get(mem.import.unwrap())
.name
.to_compact_string(),
(
mem.initial as u64,
mem.maximum.unwrap_or(mem.initial) as u64,
),
)
})
.collect::<HashMap<_, _>>();
self.mem_size = Some(mem_size);
Ok(())
}
}
#[derive(Debug, Default)]
struct GlobalIdVisitor {
vfs_global_id: Option<Box<[walrus::GlobalId]>>,
global_id: Option<HashMap<WasmName, Box<[walrus::GlobalId]>>>,
}
impl Generator for GlobalIdVisitor {
fn pre_vfs(&mut self, module: &mut walrus::Module, _: &GeneratorCtx) -> eyre::Result<()> {
module
.create_global_anchor("vfs")
.wrap_err("Failed to create global anchor")?;
Ok(())
}
fn pre_target(
&mut self,
module: &mut walrus::Module,
_: &GeneratorCtx,
external: &ModuleExternal,
) -> eyre::Result<()> {
module
.create_global_anchor(&external.name)
.wrap_err("Failed to create global anchor")?;
Ok(())
}
fn post_combine(
&mut self,
module: &mut walrus::Module,
ctx: &GeneratorCtx,
) -> eyre::Result<()> {
self.global_id = Some(HashMap::new());
let vfs_globals = module
.get_global_anchor("vfs")
.wrap_err("Failed to get global anchor for vfs")?;
self.vfs_global_id = Some(vfs_globals);
for wasm in &ctx.target_names {
let globals = module
.get_global_anchor(wasm)
.wrap_err_with(|| format!("Failed to get global anchor for {wasm}"))?;
self.global_id
.as_mut()
.unwrap()
.insert(wasm.clone(), globals);
}
Ok(())
}
}
#[derive(Debug, Default)]
struct StartFuncIdVisitor {
start_func_id: Option<HashMap<WasmName, walrus::FunctionId>>,
}
impl Generator for StartFuncIdVisitor {
fn post_combine(
&mut self,
module: &mut walrus::Module,
ctx: &GeneratorCtx,
) -> eyre::Result<()> {
for wasm in &ctx.target_names {
let export = format!("__wasip1_vfs_{wasm}__start").get_fid(&module.exports)?;
self.start_func_id
.get_or_insert_default()
.insert(wasm.clone(), export);
module
.exports
.erase_with(export, ctx.unstable_print_debug)?;
module.exports.erase_with(
&format!("__wasip1_vfs_{wasm}__start_anchor"),
ctx.unstable_print_debug,
)?;
}
Ok(())
}
}
#[derive(Debug, Clone)]
pub enum WasmPath {
Maybe {
manifest_path: Utf8PathBuf,
package: String,
},
Definitely(Utf8PathBuf),
Component(Utf8PathBuf),
}
impl FromStr for WasmPath {
type Err = eyre::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
fn parse_path_and_package(s: &str) -> eyre::Result<Option<WasmPath>> {
let split = s.split("::").collect::<Vec<_>>();
if split.len() == 2 {
let manifest_path = Utf8PathBuf::from_str(split[0])
.wrap_err_with(|| format!("Failed to parse manifest path: {}", split[0]))?;
let package = split[1].to_string();
return Ok(Some(WasmPath::Maybe {
manifest_path,
package,
}));
}
Ok(None)
}
if let Some(wasm_path) =
parse_path_and_package(s).wrap_err("Failed to parse path and package")?
{
return Ok(wasm_path);
}
if s.ends_with(".wasm") {
let path = Utf8PathBuf::from_str(s)
.wrap_err_with(|| format!("Failed to parse wasm path: {s}"))?;
return WasmPath::with_wasm(path);
}
if s.ends_with("Cargo.toml") {
let manifest_path = Utf8PathBuf::from_str(s)
.wrap_err_with(|| format!("Failed to parse manifest path: {s}"))?;
return WasmPath::with_maybe_only_manifest(manifest_path);
}
WasmPath::with_maybe_only_package(s.to_string())
.wrap_err_with(|| format!("Failed to parse package name: {s}"))
}
}
impl WasmPath {
pub fn name(&self) -> eyre::Result<CompactString> {
match self {
WasmPath::Maybe { package, .. } => Ok(package.to_compact_string()),
WasmPath::Definitely(path) | WasmPath::Component(path) => path
.get_file_main_name()
.ok_or_else(|| eyre::eyre!("Failed to get file name from {path}")),
}
}
pub fn manifest_path(&self) -> Option<&Utf8PathBuf> {
match self {
WasmPath::Maybe { manifest_path, .. } => Some(manifest_path),
WasmPath::Definitely(_) | WasmPath::Component(_) => None,
}
}
pub fn root_manifest_path(&self) -> Option<Utf8PathBuf> {
match self {
WasmPath::Maybe { manifest_path, .. } => {
let cargo_metadata = {
let mut metadata_command = cargo_metadata::MetadataCommand::new();
metadata_command.manifest_path(&manifest_path);
metadata_command.exec().unwrap()
};
Some(cargo_metadata.workspace_root.join("Cargo.toml"))
}
WasmPath::Definitely(_) | WasmPath::Component(_) => None,
}
}
pub const fn with_maybe(manifest_path: Utf8PathBuf, package: String) -> Self {
Self::Maybe {
manifest_path,
package,
}
}
pub fn with_maybe_only_manifest(manifest_path: Utf8PathBuf) -> eyre::Result<Self> {
let cargo_metadata = {
let mut metadata_command = cargo_metadata::MetadataCommand::new();
metadata_command.manifest_path(&manifest_path);
metadata_command.exec().unwrap()
};
let building_crate = compile::get_building_crate(&cargo_metadata, &None)?;
Ok(Self::Maybe {
manifest_path,
package: building_crate.name.to_string(),
})
}
pub fn with_maybe_only_package(package: String) -> eyre::Result<Self> {
let cargo_metadata = {
let metadata_command = cargo_metadata::MetadataCommand::new();
metadata_command.exec().unwrap()
};
let building_crate = compile::get_building_crate(&cargo_metadata, &Some(package.clone()))?;
Ok(Self::Maybe {
manifest_path: building_crate.manifest_path,
package: building_crate.name.to_string(),
})
}
pub fn with_maybe_none() -> eyre::Result<Self> {
let cargo_metadata = {
let metadata_command = cargo_metadata::MetadataCommand::new();
metadata_command.exec().unwrap()
};
let building_crate = compile::get_building_crate(&cargo_metadata, &None)?;
Ok(Self::Maybe {
manifest_path: building_crate.manifest_path,
package: building_crate.name.to_string(),
})
}
pub fn with_wasm(path: Utf8PathBuf) -> eyre::Result<Self> {
if path.extension() != Some("wasm") {
eyre::bail!("Wasm file does not have .wasm extension: {path}");
}
if !fs::metadata(&path).is_ok() {
eyre::bail!("Wasm file does not exist: {path}");
}
let mut file =
fs::File::open(&path).wrap_err_with(|| format!("Failed to open wasm file: {path}"))?;
let mut magic = [0u8; 8];
file.read_exact(&mut magic)
.wrap_err_with(|| format!("Failed to read magic number from wasm file: {path}"))?;
if !magic.starts_with(b"\0asm") {
eyre::bail!("Wasm file does not have valid magic number: {path}");
}
let version = &magic[4..8];
if version == [0x0D, 0x00, 0x01, 0x00] {
return Ok(Self::Component(path));
}
if version != [0x01, 0x00, 0x00, 0x00] {
eyre::bail!("Wasm file does not have valid version: {path}");
}
Ok(Self::Definitely(path))
}
pub fn definitely(&mut self, threads: bool) -> eyre::Result<()> {
if let WasmPath::Maybe {
manifest_path,
package,
} = self
{
let cargo_metadata = {
let mut metadata_command = cargo_metadata::MetadataCommand::new();
metadata_command.manifest_path(&manifest_path);
metadata_command.exec().unwrap()
};
let building_crate =
compile::get_building_crate(&cargo_metadata, &Some(package.clone()))?;
let vfs_name = building_crate.name.to_string();
let path =
compile::build_vfs(Some(&manifest_path.to_string()), &building_crate, threads)
.wrap_err_with(|| eyre::eyre!("Failed to build VFS: {vfs_name}"))?;
*self = WasmPath::Definitely(path);
}
Ok(())
}
pub fn path(&self) -> eyre::Result<&Utf8PathBuf> {
match self {
WasmPath::Maybe { .. } => {
eyre::bail!("WasmPath is not definitely set: {self:?}")
}
WasmPath::Definitely(p) | WasmPath::Component(p) => Ok(p),
}
}
pub fn set_path(&mut self, path: Utf8PathBuf) -> eyre::Result<()> {
if matches!(self, WasmPath::Maybe { .. }) {
eyre::bail!("WasmPath is not definitely set: {path}")
}
*self = WasmPath::Definitely(path);
Ok(())
}
}
pub fn merge(
vfs: &Utf8PathBuf,
wasm: &[impl AsRef<std::path::Path>],
output: impl AsRef<std::path::Path>,
_threads: bool,
dwarf: bool,
) -> eyre::Result<()> {
let custom_section = {
let mut vfs_module = walrus::Module::load(vfs, dwarf)?;
let custom_section_names = vfs_module
.customs
.iter()
.map(|(_, section)| section.name().to_string())
.filter(|name| name.starts_with("component-type:"))
.collect::<Vec<_>>();
let custom_section = custom_section_names
.iter()
.map(|id| {
let section = vfs_module.customs.remove_raw(id);
section.unwrap()
})
.collect::<Vec<_>>();
custom_section
};
let mut merge_cmd = fallback_command::get_fallback_command("wasm-merge");
if dwarf {
merge_cmd.arg("--debuginfo");
}
merge_cmd.arg(vfs).arg(UniqueName::WASIP1_ABI_MODULE);
for wasm in wasm {
merge_cmd.arg(wasm.as_ref().as_os_str().to_str().unwrap()).arg(format!(
"wasip1_vfs_{}",
wasm.as_ref().get_file_main_name().unwrap()
));
}
merge_cmd
.arg("--output")
.arg(output.as_ref().as_os_str().to_str().unwrap())
.arg("--enable-multimemory")
.arg("--enable-threads");
let result = merge_cmd
.spawn()
.map_err(|e| match e.kind() {
std::io::ErrorKind::NotFound => eyre::eyre!(
"wasm-merge command not found. Please install wasm-merge from https://github.com/WebAssembly/binaryen/releases/latest"
),
_ => e.into(),
})?
.wait_with_output()
.wrap_err("Failed to wait for wasm-merge process")?;
if !result.success {
return Err(eyre::eyre!("wasm-merge command failed"));
}
let mut module = walrus::Module::load(output.as_ref(), dwarf)?;
for section in custom_section {
module.customs.add(section);
}
fs::remove_file(output.as_ref()).expect("Failed to remove existing file");
module
.emit_wasm_file(output.as_ref())
.expect("Failed to emit wasm file");
Ok(())
}
macro_rules! _add_generators_by_type {
($runner:expr, $($ty:ty),* $(,)?) => {
$(
if let Some(_) = $runner.get_generator_ref::<$ty>().ok() {
panic!("Generator of type {} already exists", std::any::type_name::<$ty>());
}
$runner.add_generator(<$ty>::default());
)*
};
}
pub(crate) use _add_generators_by_type as add_generators_by_type;