Stream: wasmtime

Topic: generating canonical abi exports in rust via proc macros


view this post on Zulip Carl Sverre (Sep 28 2021 at 18:02):

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!

view this post on Zulip fitzgen (he/him) (Sep 28 2021 at 18:19):

I guess you could generate a WITX snippet from your proc-macro and then internally pass that to witx-bindgen

view this post on Zulip fitzgen (he/him) (Sep 28 2021 at 18:20):

you'd have to make sure that the rust code shape matches what witx-bindgen is going to want tho

view this post on Zulip Alex Crichton (Sep 28 2021 at 18:28):

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...

view this post on Zulip Alex Crichton (Sep 28 2021 at 18:28):

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

view this post on Zulip Carl Sverre (Sep 28 2021 at 18:30):

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.

view this post on Zulip Carl Sverre (Sep 29 2021 at 00:18):

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 :).

view this post on Zulip Carl Sverre (Sep 29 2021 at 00:18):

https://github.com/carlsverre/wasi-witx-sandbox/pull/4

This PR creates a macro called wasi_interface which compiles WASM exports for a rust module without needing to define WITX. This makes it very easy to define WASM functions which take and return in...

Last updated: Nov 22 2024 at 16:03 UTC