-
Notifications
You must be signed in to change notification settings - Fork 220
/
strip.rs
75 lines (63 loc) · 2.17 KB
/
strip.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use anyhow::{bail, Result};
use wasm_encoder::RawSection;
use wasmparser::{Encoding, Parser, Payload::*};
/// Removes custom sections from an input WebAssembly file.
///
/// This command will by default strip all custom sections such as DWARF
/// debugging information from a wasm file. It will not strip the `name` section
/// by default unless the `--all` flag is passed.
#[derive(clap::Parser)]
pub struct Opts {
#[clap(flatten)]
io: wasm_tools::InputOutput,
/// Remove all custom sections, regardless of name.
#[clap(long, short)]
all: bool,
/// Remove custom sections matching the specified regex.
#[clap(long, short, value_name = "REGEX")]
delete: Vec<String>,
/// Output the text format of WebAssembly instead of the binary format.
#[clap(short = 't', long)]
wat: bool,
}
impl Opts {
pub fn run(&self) -> Result<()> {
let input = self.io.parse_input_wasm()?;
let to_delete = regex::RegexSet::new(self.delete.iter())?;
let strip_custom_section = |name: &str| {
// If explicitly specified, strip everything.
if self.all {
return true;
}
// If any section was called out by name only delete those sections.
if !to_delete.is_empty() {
return to_delete.is_match(name);
}
// Finally default strip everything but the `name` section.
name != "name"
};
let mut module = wasm_encoder::Module::new();
for payload in Parser::new(0).parse_all(&input) {
let payload = payload?;
match &payload {
CustomSection(c) => {
if strip_custom_section(c.name()) {
continue;
}
}
_ => {}
}
if let Some((id, range)) = payload.as_section() {
module.section(&RawSection {
id,
data: &input[range],
});
}
}
self.io.output(wasm_tools::Output::Wasm {
bytes: module.as_slice(),
wat: self.wat,
})?;
Ok(())
}
}