MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / execute

Method execute

crates/environ/examples/factc.rs:54–103  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

52
53impl Factc {
54 fn execute(self) -> Result<()> {
55 env_logger::init();
56
57 let input = wat::parse_file(&self.input)?;
58
59 let tunables = Tunables::default_host();
60 let mut validator =
61 wasmparser::Validator::new_with_features(wasmparser::WasmFeatures::all());
62 let mut component_types = ComponentTypesBuilder::new(&validator);
63 let adapters = ScopeVec::new();
64
65 Translator::new(&tunables, &mut validator, &mut component_types, &adapters)
66 .translate(&input)?;
67
68 let (out_name, mut out_file): (_, Box<dyn std::io::Write>) = match &self.output {
69 Some(file) => (
70 file.as_path(),
71 Box::new(std::io::BufWriter::new(
72 std::fs::File::create(file)
73 .with_context(|| format!("failed to create {}", file.display()))?,
74 )),
75 ),
76 None => (Path::new("stdout"), Box::new(std::io::stdout())),
77 };
78
79 for wasm in adapters.into_iter() {
80 let output = if self.text {
81 wasmprinter::print_bytes(&wasm)
82 .to_wasmtime_result()
83 .context("failed to convert binary wasm to text")?
84 .into_bytes()
85 } else if self.output.is_none() && std::io::stdout().is_terminal() {
86 bail!("cannot print binary wasm output to a terminal unless `-t` flag is passed")
87 } else {
88 wasm.to_vec()
89 };
90
91 out_file
92 .write_all(&output)
93 .with_context(|| format!("failed to write to {}", out_name.display()))?;
94
95 if !self.skip_validate {
96 Validator::new_with_features(WasmFeatures::default() | WasmFeatures::MEMORY64)
97 .validate_all(&wasm)
98 .context("failed to validate generated module")?;
99 }
100 }
101
102 Ok(())
103 }
104}

Callers 1

mainFunction · 0.45

Calls 15

print_bytesFunction · 0.85
OkFunction · 0.85
with_contextMethod · 0.80
into_bytesMethod · 0.80
to_wasmtime_resultMethod · 0.80
write_allMethod · 0.80
initFunction · 0.50
newFunction · 0.50
createFunction · 0.50
stdoutFunction · 0.50
translateMethod · 0.45
into_iterMethod · 0.45

Tested by

no test coverage detected