Hello! I want to write a proc attribute macro in Rust which compiles the corresponding wasm export (with interface types) for a given Rust function. For example, the user might enter something like:
struct Input {
s: String,
i: i64,
}
struct Output {
a: i64,
b: f64,
c: String,
}
#[wasi_fn]
fn mapper(input: Input) -> Output {
Output {
a: input.i * input.i,
b: input.i * 123.234,
c: format!("hello {}", input.s),
}
}
That should generate the correct export along with the interface type lifting/lowering semantics corresponding to the canonical abi.
My hope is that I could reuse some of the existing canonical binding generation code in the witx-bindgen project. If anyone is very comfortable with that code and believes it's possible to reuse, I would greatly appreciate any pointers you might have.
Thanks!
I guess you could generate a WITX snippet from your proc-macro and then internally pass that to witx-bindgen
you'd have to make sure that the rust code shape matches what witx-bindgen is going to want tho
Unfortunately rust procedural macros don't have the capability to do what you want since they only have the AST, not type information (such as the definition of Input
and Output
). This is why witx-bindgen
works off the *.witx
document, which does have type information, as opposed to the Rust syntax tree. One day I want #[witx_bindgen]
to exist but there's so much else to otherwise get done with witx-bindgen
it may take awhile...
Otherwise though it should be somewhat easy-ish to use the witx_bindgen_gen_rust_wasm
crate directly to generate Rust code if you can massage the input into a witx2::Interface
-lookalike thing
good feedback - I am fine with the user needing to annotate the other types they are using as well... I'm surprised I can't get that information at compile time about the shapes of Input and Output.
So I have a somewhat working first pass. Will keep cleaning it up. Note - this is just to demo an idea more than be a final solution. Obviously going directly from rust -> rust is better than going through witx :).
https://github.com/carlsverre/wasi-witx-sandbox/pull/4
Last updated: Dec 23 2024 at 14:03 UTC