-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rs
46 lines (40 loc) · 1.33 KB
/
main.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
use wasmtime::{
self,
component::{Component, Linker},
Config, Engine, Store,
};
wasmtime::component::bindgen!("markdown" in "crates/markdown/wit/markdown.wit");
struct Imports {
stdin: std::io::Stdin,
}
impl MarkdownImports for Imports {
fn readline(&mut self) -> anyhow::Result<String> {
let mut buf = String::new();
self.stdin.read_line(&mut buf)?;
Ok(buf)
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let wasm = std::env::args().skip(1).next().expect("USAGE: demo WASM");
// Enable component model (which isn't supported by default)
let mut config = Config::new();
config.wasm_component_model(true);
// Create a wasmtime execution context
let engine = Engine::new(&config)?;
let mut store = Store::new(
&engine,
Imports {
stdin: std::io::stdin(),
},
);
let mut linker = Linker::new(&engine);
Markdown::add_to_linker(&mut linker, |state: &mut Imports| state)?;
// Read and compile the wasm component
let component = Component::from_file(&engine, wasm)?;
// Instantiate a markdown instance
let (markdown, _) = Markdown::instantiate(&mut store, &component, &linker)?;
println!("Enter some markdown:");
let res = markdown.call_render(&mut store)?;
println!("Html:\n{res}");
Ok(())
}