Skip to content

Commit eb9ad9c

Browse files
authored
Support multiple -f flags in validate (#1882)
This commit adds support for multiple `--feature` or `-f` flags in the `wasm-tools validate` subcommand. This behaves the same way as supplying multiple values separated by commas, but it can additionally be used through passing multiple arguments now.
1 parent 89ff546 commit eb9ad9c

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/bin/wasm-tools/validate.rs

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,19 @@ pub struct Opts {
5454
/// Available feature options can be found in the wasmparser crate:
5555
/// <https://github.com/bytecodealliance/wasm-tools/blob/main/crates/wasmparser/src/features.rs>
5656
#[clap(long, short = 'f', value_parser = parse_features)]
57-
features: Option<WasmFeatures>,
57+
features: Vec<Vec<FeatureAction>>,
5858

5959
#[clap(flatten)]
6060
io: wasm_tools::InputOutput,
6161
}
6262

63+
#[derive(Clone)]
64+
enum FeatureAction {
65+
Reset(WasmFeatures),
66+
Enable(WasmFeatures),
67+
Disable(WasmFeatures),
68+
}
69+
6370
impl Opts {
6471
pub fn general_opts(&self) -> &wasm_tools::GeneralOpts {
6572
self.io.general_opts()
@@ -92,6 +99,26 @@ impl Opts {
9299
}
93100
}
94101

102+
fn features(&self) -> Result<WasmFeatures> {
103+
let mut ret = WasmFeatures::default();
104+
105+
for action in self.features.iter().flat_map(|v| v) {
106+
match action {
107+
FeatureAction::Enable(features) => {
108+
ret |= *features;
109+
}
110+
FeatureAction::Disable(features) => {
111+
ret &= !*features;
112+
}
113+
FeatureAction::Reset(features) => {
114+
ret = *features;
115+
}
116+
}
117+
}
118+
119+
Ok(ret)
120+
}
121+
95122
fn validate(&self, wasm: &[u8]) -> Result<()> {
96123
// Note that here we're copying the contents of
97124
// `Validator::validate_all`, but the end is followed up with a parallel
@@ -103,7 +130,7 @@ impl Opts {
103130
// `Validator` we're using as we navigate nested modules (the module
104131
// linking proposal) and any functions found are deferred to get
105132
// validated later.
106-
let mut validator = Validator::new_with_features(self.features.unwrap_or_default());
133+
let mut validator = Validator::new_with_features(self.features()?);
107134
let mut functions_to_validate = Vec::new();
108135

109136
let start = Instant::now();
@@ -182,8 +209,8 @@ impl Opts {
182209
}
183210
}
184211

185-
fn parse_features(arg: &str) -> Result<WasmFeatures> {
186-
let mut ret = WasmFeatures::default();
212+
fn parse_features(arg: &str) -> Result<Vec<FeatureAction>> {
213+
let mut ret = Vec::new();
187214

188215
const GROUPS: &[(&str, WasmFeatures)] = &[
189216
("mvp", WasmFeatures::WASM1),
@@ -226,18 +253,24 @@ fn parse_features(arg: &str) -> Result<WasmFeatures> {
226253
}
227254
match action {
228255
Action::ChangeAll => {
229-
for flag in WasmFeatures::FLAGS.iter() {
230-
ret.set(*flag.value(), enable);
231-
}
256+
ret.push(if enable {
257+
FeatureAction::Enable(WasmFeatures::all())
258+
} else {
259+
FeatureAction::Disable(WasmFeatures::all())
260+
});
232261
}
233262
Action::Modify(feature) => {
234-
ret.set(feature, enable);
263+
ret.push(if enable {
264+
FeatureAction::Enable(feature)
265+
} else {
266+
FeatureAction::Disable(feature)
267+
});
235268
}
236269
Action::Group(features) => {
237270
if !enable {
238271
bail!("cannot disable `{part}`, it can only be enabled");
239272
}
240-
ret = features;
273+
ret.push(FeatureAction::Reset(features));
241274
}
242275
}
243276
continue 'outer;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
;; RUN: validate % -f wasm1 -f simd
2+
3+
(module
4+
(import "" "" (global v128))
5+
)

0 commit comments

Comments
 (0)