wasmtime_cli/commands/
wast.rs1use 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#[derive(Parser)]
12pub struct WastCommand {
13 #[command(flatten)]
14 common: CommonOptions,
15
16 #[arg(required = true, value_name = "SCRIPT_FILE")]
18 scripts: Vec<PathBuf>,
19
20 #[arg(long, require_equals = true, value_name = "true|false")]
23 generate_dwarf: Option<Option<bool>>,
24
25 #[arg(long)]
28 precompile_save: Option<PathBuf>,
29
30 #[arg(long)]
33 precompile_load: Option<PathBuf>,
34}
35
36impl WastCommand {
37 pub fn execute(mut self) -> Result<()> {
39 self.common.init_logging()?;
40
41 let mut config = self.common.config(None)?;
42 config.async_support(true);
43 let mut store = Store::new(&Engine::new(&config)?, ());
44 if let Some(fuel) = self.common.wasm.fuel {
45 store.set_fuel(fuel)?;
46 }
47 if let Some(true) = self.common.wasm.epoch_interruption {
48 store.epoch_deadline_trap();
49 store.set_epoch_deadline(1);
50 }
51 let mut wast_context = WastContext::new(store, wasmtime_wast::Async::Yes);
52
53 wast_context.generate_dwarf(optional_flag_with_default(self.generate_dwarf, true));
54 wast_context
55 .register_spectest(&SpectestConfig {
56 use_shared_memory: true,
57 suppress_prints: false,
58 })
59 .expect("error instantiating \"spectest\"");
60
61 if let Some(path) = &self.precompile_save {
62 wast_context.precompile_save(path);
63 }
64 if let Some(path) = &self.precompile_load {
65 wast_context.precompile_load(path);
66 }
67
68 for script in self.scripts.iter() {
69 wast_context
70 .run_file(script)
71 .with_context(|| format!("failed to run script file '{}'", script.display()))?;
72 }
73
74 Ok(())
75 }
76}
77
78fn optional_flag_with_default(flag: Option<Option<bool>>, default: bool) -> bool {
79 match flag {
80 None => default,
81 Some(None) => true,
82 Some(Some(val)) => val,
83 }
84}