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;
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    /// Saves precompiled versions of modules to this path instead of running
26    /// tests.
27    #[arg(long)]
28    precompile_save: Option<PathBuf>,
29
30    /// Load precompiled modules from the specified directory instead of
31    /// compiling natively.
32    #[arg(long)]
33    precompile_load: Option<PathBuf>,
34
35    /// Whether or not to run wasm in async mode.
36    ///
37    /// This is enabled by default but disabling it may be useful when testing
38    /// Wasmtime itself.
39    #[arg(long = "async", require_equals = true, value_name = "true|false")]
40    async_: Option<Option<bool>>,
41}
42
43impl WastCommand {
44    /// Executes the command.
45    pub fn execute(mut self) -> Result<()> {
46        self.common.init_logging()?;
47
48        let async_ = optional_flag_with_default(self.async_, true);
49        let mut config = self.common.config(None)?;
50        config.async_support(async_);
51        let engine = Engine::new(&config)?;
52        let mut wast_context = WastContext::new(
53            &engine,
54            if async_ {
55                wasmtime_wast::Async::Yes
56            } else {
57                wasmtime_wast::Async::No
58            },
59            move |store| {
60                if let Some(fuel) = self.common.wasm.fuel {
61                    store.set_fuel(fuel).unwrap();
62                }
63                if let Some(true) = self.common.wasm.epoch_interruption {
64                    store.epoch_deadline_trap();
65                    store.set_epoch_deadline(1);
66                }
67            },
68        );
69
70        wast_context.generate_dwarf(optional_flag_with_default(self.generate_dwarf, true));
71        wast_context
72            .register_spectest(&SpectestConfig {
73                use_shared_memory: true,
74                suppress_prints: false,
75            })
76            .expect("error instantiating \"spectest\"");
77
78        if let Some(path) = &self.precompile_save {
79            wast_context.precompile_save(path);
80        }
81        if let Some(path) = &self.precompile_load {
82            wast_context.precompile_load(path);
83        }
84
85        for script in self.scripts.iter() {
86            wast_context
87                .run_file(script)
88                .with_context(|| format!("failed to run script file '{}'", script.display()))?;
89        }
90
91        Ok(())
92    }
93}
94
95fn optional_flag_with_default(flag: Option<Option<bool>>, default: bool) -> bool {
96    match flag {
97        None => default,
98        Some(None) => true,
99        Some(Some(val)) => val,
100    }
101}