use clap::Parser;
use std::fs::File;
use std::io::prelude::*;
use std::sync::Arc;
use std::sync::Mutex;
type BoxError = Box<dyn std::error::Error>;
#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
#[clap(propagate_version = true)]
struct Args {
source: String,
out: String,
}
fn main() -> Result<(), BoxError> {
let args = Args::parse();
let mut input = Vec::new();
{
let mut file = File::open(&args.source).expect("File not found");
file.read_to_end(&mut input)
.expect("Error while reading file");
}
let input = wasm_parser::parse(&input)
.map_err(|err| format!("failed to parse Wasm module: {}", err))?;
let wasm = core_wasm_ast::traverse::WasmModule::new(Arc::new(input));
let mut name_section = None;
let mut custom_sections_to_remove = vec![];
for section in wasm.get_custom_sections() {
match section {
core_wasm_ast::CustomSection::Unknown(name, _) => {
custom_sections_to_remove.push(name.clone());
}
core_wasm_ast::CustomSection::Name(n) => {
name_section = Some(n.clone());
custom_sections_to_remove.push("name".to_owned());
}
_ => {}
}
}
println!("removing custom sections: {custom_sections_to_remove:?}");
for section in &custom_sections_to_remove {
wasm.remove_custom_section(section);
}
{
let bytes = wasm_printer::wasm::print(&wasm.into_inner()).unwrap();
let mut file = File::create(&args.source)?;
file.write_all(&bytes)?;
}
{
let mut bytes = vec![];
wasm_printer::wasm::write_section_custom_name(&mut bytes, &name_section.unwrap()).unwrap();
let mut file = File::create(&args.out)?;
file.write_all(&bytes)?;
}
Ok(())
}