1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
mod world;
use crate::types::NnErrno;
use anyhow::{Context, Error};
use env_logger::Env;
use std::path::PathBuf;
use structopt::StructOpt;
use wasmtime::{Linker, Module, Store, TypedFunc};
use wiggle::GuestErrorType;
use world::World;
const DEFAULT_FILTER: &str = "info,regalloc=warn";
fn main() -> Result<(), Error> {
env_logger::init_from_env(Env::default().default_filter_or(DEFAULT_FILTER));
let Args { module } = Args::from_args();
log::info!("Started");
let wasm = std::fs::read(&module)
.with_context(|| format!("Unable to read \"{}\"", module.display()))?;
log::info!("Loaded {} bytes from \"{}\"", wasm.len(), module.display());
let mut store = Store::default();
let m = Module::from_binary(store.engine(), &wasm)
.context("Unable to load the WebAssembly binary")?;
log::info!("Loaded the WebAssembly module");
let mut linker: Linker<World> = Linker::new(store.engine());
wasi_ephemeral_nn::add_to_linker(&mut linker, |world| {
&mut world.wasi_nn_ctx
})
.context("Unable to add")?;
let instance = linker
.instantiate(&mut store, &m)
.context("Unable to instantiate the WebAssembly module")?;
log::info!("Instantiated the WebAssembly module");
let start: TypedFunc<(), ()> = instance
.get_typed_func(&mut store, "start")
.context("Unable to get the start() function")?;
log::info!("Calling start()");
start
.call(&mut store, ())
.context("An error occurred while calling start()")?;
log::info!("Completed successfully!");
Ok(())
}
wiggle::from_witx!({
witx: ["$CARGO_MANIFEST_DIR/../vendor/wasi-nn/phases/ephemeral/witx/wasi_ephemeral_nn.witx"]
});
impl GuestErrorType for NnErrno {
fn success() -> Self { NnErrno::Success }
}
#[derive(Debug, StructOpt)]
struct Args {
#[structopt(
help = "The WebAssembly module to execute",
parse(from_os_str)
)]
module: PathBuf,
}