alexcrichton edited PR #175 from wasmtime-hostmodule
to main
:
This adds a crate defining a DSL for creating host modules. I debated whether to include this in
wasmtime
proper or whether to define an external crate, but I could definitely imagine that most people usingwasmtime
will want something like this in order to define host functions etc. I haven't added any code that reexports this from thewasmtime
root crate but I could imagine that that would be helpful (plus, it would mean we can remove the workspace glob that I added in 1a59141).The DSL looks like so:
use wasmtime_hostmodule::{ exports, BindArgType, Func, Global, Instantiate, Memory, Table, TableElementType }; fn hello_world() { println!("Hello, world!"); } fn print_and_return(val: i32) -> i32 { println!("{}", val); val } let mut counter = 100; let my_closure = move |inc: u32| -> u32 { counter += inc; counter }; let my_module = exports! { do_thing: Func(hello_world.bind::<()>()), print_and_return: Func(print_and_return.bind::<(i32,)>()), counting_func: Func(my_closure.bind::<(u32,)>()), my_glob: Global(100u64, Default::default()), memory: Memory { minimum: 1, maximum: Some(2), shared: false, }, table: Table { ty: TableElementType::Func, minimum: 10, maximum: Some(20), }, }; my_module.instantiate().unwrap();
Run
cargo doc
from the crate root for more info. This isn't a proc macro, the macro does almost nothing at all. All the magic is done in the type system, as God intended.
sunfishcode closed without merge PR #175.
Last updated: Nov 22 2024 at 16:03 UTC