wasmtime_cli/commands/
wast.rs

1//! The module that implements the `wasmtime wast` command.
2
3use anyhow::{Context as _, Result};
4use clap::Parser;
5use std::path::PathBuf;
6use wasmtime::{Engine, Store};
7use wasmtime_cli_flags::CommonOptions;
8use wasmtime_wast::{SpectestConfig, WastContext};
9
10/// Runs a WebAssembly test script file
11#[derive(Parser)]
12pub struct WastCommand {
13    #[command(flatten)]
14    common: CommonOptions,
15
16    /// The path of the WebAssembly test script to run
17    #[arg(required = true, value_name = "SCRIPT_FILE")]
18    scripts: Vec<PathBuf>,
19
20    /// Whether or not to generate DWARF debugging information in text-to-binary
21    /// transformations to show line numbers in backtraces.
22    #[arg(long, require_equals = true, value_name = "true|false")]
23    generate_dwarf: Option<Option<bool>>,
24}
25
26impl WastCommand {
27    /// Executes the command.
28    pub fn execute(mut self) -> Result<()> {
29        self.common.init_logging()?;
30
31        let mut config = self.common.config(None)?;
32        config.async_support(true);
33        let mut store = Store::new(&Engine::new(&config)?, ());
34        if let Some(fuel) = self.common.wasm.fuel {
35            store.set_fuel(fuel)?;
36        }
37        if let Some(true) = self.common.wasm.epoch_interruption {
38            store.epoch_deadline_trap();
39            store.set_epoch_deadline(1);
40        }
41        let mut wast_context = WastContext::new(store, wasmtime_wast::Async::Yes);
42
43        wast_context.generate_dwarf(optional_flag_with_default(self.generate_dwarf, true));
44        wast_context
45            .register_spectest(&SpectestConfig {
46                use_shared_memory: true,
47                suppress_prints: false,
48            })
49            .expect("error instantiating \"spectest\"");
50
51        for script in self.scripts.iter() {
52            wast_context
53                .run_file(script)
54                .with_context(|| format!("failed to run script file '{}'", script.display()))?
55        }
56
57        Ok(())
58    }
59}
60
61fn optional_flag_with_default(flag: Option<Option<bool>>, default: bool) -> bool {
62    match flag {
63        None => default,
64        Some(None) => true,
65        Some(Some(val)) => val,
66    }
67}