|
| 1 | +use tokio::prelude::*; |
| 2 | +use wasi_process::WasiProcess; |
| 3 | +use wasmer_wasi::{state::WasiState, WasiVersion}; |
| 4 | + |
| 5 | +#[tokio::main] |
| 6 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 7 | + let mut args = std::env::args().skip(1); |
| 8 | + let wasm = std::fs::read(args.next().expect("must pass wasm file"))?; |
| 9 | + let module = wasmer_runtime::compile(&wasm)?; |
| 10 | + let mut state = WasiState::new("progg"); |
| 11 | + wasi_process::add_stdio(&mut state); |
| 12 | + state.args(args).preopen_dir(".")?; |
| 13 | + let imports = wasmer_wasi::generate_import_object_from_state( |
| 14 | + state.build()?, |
| 15 | + wasmer_wasi::get_wasi_version(&module, false).unwrap_or(WasiVersion::Latest), |
| 16 | + ); |
| 17 | + let mut wasi = WasiProcess::new(module.instantiate(&imports)?); |
| 18 | + let mut proc_stdin = wasi.stdin.take().unwrap(); |
| 19 | + let mut stdin = io::stdin(); |
| 20 | + let mut proc_stdout = wasi.stdout.take().unwrap(); |
| 21 | + let mut stdout = io::stdout(); |
| 22 | + let mut proc_stderr = wasi.stderr.take().unwrap(); |
| 23 | + let mut stderr = io::stderr(); |
| 24 | + let proc_fut = wasi.spawn(); |
| 25 | + tokio::try_join!( |
| 26 | + io::copy(&mut stdin, &mut proc_stdin), |
| 27 | + io::copy(&mut proc_stdout, &mut stdout), |
| 28 | + io::copy(&mut proc_stderr, &mut stderr), |
| 29 | + )?; |
| 30 | + proc_fut.await?; |
| 31 | + Ok(()) |
| 32 | +} |
0 commit comments