use anyhow::{Context as _, Result};
use clap::Parser;
use std::path::PathBuf;
use wasmtime::{Engine, Store};
use wasmtime_cli_flags::CommonOptions;
use wasmtime_wast::{SpectestConfig, WastContext};
#[derive(Parser)]
pub struct WastCommand {
#[command(flatten)]
common: CommonOptions,
#[arg(required = true, value_name = "SCRIPT_FILE")]
scripts: Vec<PathBuf>,
#[arg(long, require_equals = true, value_name = "true|false")]
generate_dwarf: Option<Option<bool>>,
#[arg(long)]
precompile_save: Option<PathBuf>,
#[arg(long)]
precompile_load: Option<PathBuf>,
}
impl WastCommand {
pub fn execute(mut self) -> Result<()> {
self.common.init_logging()?;
let mut config = self.common.config(None)?;
config.async_support(true);
let mut store = Store::new(&Engine::new(&config)?, ());
if let Some(fuel) = self.common.wasm.fuel {
store.set_fuel(fuel)?;
}
if let Some(true) = self.common.wasm.epoch_interruption {
store.epoch_deadline_trap();
store.set_epoch_deadline(1);
}
let mut wast_context = WastContext::new(store, wasmtime_wast::Async::Yes);
wast_context.generate_dwarf(optional_flag_with_default(self.generate_dwarf, true));
wast_context
.register_spectest(&SpectestConfig {
use_shared_memory: true,
suppress_prints: false,
})
.expect("error instantiating \"spectest\"");
if let Some(path) = &self.precompile_save {
wast_context.precompile_save(path);
}
if let Some(path) = &self.precompile_load {
wast_context.precompile_load(path);
}
for script in self.scripts.iter() {
wast_context
.run_file(script)
.with_context(|| format!("failed to run script file '{}'", script.display()))?;
}
Ok(())
}
}
fn optional_flag_with_default(flag: Option<Option<bool>>, default: bool) -> bool {
match flag {
None => default,
Some(None) => true,
Some(Some(val)) => val,
}
}