-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathreactor-hello.rs
64 lines (53 loc) · 2.03 KB
/
reactor-hello.rs
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
use dylibso_observe_sdk::adapter::stdout::StdoutAdapter;
#[tokio::main]
pub async fn main() -> anyhow::Result<()> {
env_logger::init_from_env(
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "warn"),
);
let args: Vec<_> = std::env::args().skip(1).collect();
let data = std::fs::read(&args[0])?;
let function_name = "hello";
let config = wasmtime::Config::new();
// Create instance
let engine = wasmtime::Engine::new(&config)?;
let module = wasmtime::Module::new(&engine, &data)?;
let adapter = StdoutAdapter::create();
// Setup WASI
let wasi_ctx = wasmtime_wasi::WasiCtxBuilder::new()
.inherit_env()?
.inherit_stdio()
.args(&args.clone())?
.build();
let mut store = wasmtime::Store::new(&engine, wasi_ctx);
let mut linker = wasmtime::Linker::new(&engine);
wasmtime_wasi::add_to_linker(&mut linker, |wasi| wasi)?;
// Provide the observability functions to the `Linker` to be made available
// to the instrumented guest code. These are safe to add and are a no-op
// if guest code is uninstrumented.
let trace_ctx = adapter.start(&mut linker, &data, Default::default())?;
let instance = linker.instantiate(&mut store, &module)?;
// call _initialize"
{
let f = instance
.get_func(&mut store, "_initialize")
.expect("function exists");
f.call(&mut store, &[], &mut []).unwrap();
}
// call hello
let f = instance
.get_func(&mut store, function_name)
.expect("function exists 2");
f.call(&mut store, &[], &mut []).unwrap();
trace_ctx.shutdown().await;
// call __wasm_call_dtors
// Normally reactors don't emit __wasm_call_dtors, but
// reactor-hello.c.instr.wasm includes it as an example of specifying
// a cleanup function in a reactor.
{
let f = instance
.get_func(&mut store, "__wasm_call_dtors")
.expect("function exists");
f.call(&mut store, &[], &mut []).unwrap();
}
Ok(())
}