wain_exec/
lib.rs

1#![forbid(unsafe_code)]
2#![warn(clippy::dbg_macro)]
3
4extern crate wain_ast;
5
6pub mod trap;
7
8mod cast;
9mod globals;
10mod import;
11mod memory;
12mod runtime;
13mod stack;
14mod table;
15mod value;
16
17pub use import::{check_func_signature, DefaultImporter, ImportInvalidError, ImportInvokeError, Importer};
18pub use memory::Memory;
19pub use runtime::Runtime;
20pub use stack::Stack;
21pub use value::Value;
22
23use std::io;
24use trap::Result;
25use wain_ast::Module;
26
27/// A convenient function to execute a WebAssembly module.
28///
29/// This function takes parsed and validated WebAssembly module and it invokes a start function if
30/// presents. Otherwise it invokes a function exported as '_start' with no argument.
31///
32/// For standard I/O speed, this function locks io::Stdin and io::Stdout objects because currently
33/// getchar() and putchar() don't buffer its input/output. This behavior may change in the future.
34///
35/// If the behavior is not acceptable, please make an abstract machine runtime with
36/// Runtime::instantiate.
37///
38/// You will need importer for initializing Runtime struct. Please use DefaultImporter::with_stdio()
39/// or make your own importer struct which implements Importer trait.
40pub fn execute(module: &Module<'_>) -> Result<()> {
41    let stdin = io::stdin();
42    let stdout = io::stdout();
43    let importer = DefaultImporter::with_stdio(stdin.lock(), stdout.lock());
44    let mut runtime = Runtime::instantiate(module, importer)?;
45    if runtime.module().entrypoint.is_none() {
46        runtime.invoke("_start", &[])?;
47    }
48    Ok(())
49}