This commit uses the `*.witx` files describing the current wasi API to reduce the boilerplate used to define implementations in the `wasmtime-wasi` crate. Eventually I'd like to remove lots of boilerplate in the `wasi-common` crate too, but this should at least be a good start! The boilerplate removed here is: * No need to list each function to add it to the `wasmtime_runtime::Module` being created * No need to list the signature of the function in a separate `syscalls.rs` file. Instead the `*.witx` file is processed in a single-use macro inside the `wasmtime-wasi` crate. This macro uses the signatures known from `*.witx` to automatically register with the right type in the wasm module as well as define a wrapper that the wasm module will call into. Functionally this is all the same as before, it's just defined in a different way now! The shim generated by this macro which wasmtime calls into only uses `i32`/`i64`/etc wasm types, and it internally uses `as` casts to convert to the right wasi types when delegating into the `wasi-common` crate. One change was necessary to get this implemented, however. The functions in `wasi-common` sometimes took `WasiCtx` and sometimes took a slice of memory. After this PR they uniformly all require both `WasiCtx` and memory so the wrappers can be auto-generated. The arguments are ignored if they weren't previously required.
39 lines
896 B
Rust
39 lines
896 B
Rust
extern crate proc_macro;
|
|
|
|
mod raw_types;
|
|
mod utils;
|
|
mod wasi;
|
|
|
|
use proc_macro::TokenStream;
|
|
use proc_macro2::TokenStream as TokenStream2;
|
|
|
|
#[proc_macro]
|
|
pub fn witx_host_types(args: TokenStream) -> TokenStream {
|
|
TokenStream::from(raw_types::gen(
|
|
TokenStream2::from(args),
|
|
raw_types::Mode::Host,
|
|
))
|
|
}
|
|
|
|
#[proc_macro]
|
|
pub fn witx_wasi_types(args: TokenStream) -> TokenStream {
|
|
TokenStream::from(raw_types::gen(
|
|
TokenStream2::from(args),
|
|
raw_types::Mode::Wasi,
|
|
))
|
|
}
|
|
|
|
#[proc_macro]
|
|
pub fn witx_wasi32_types(args: TokenStream) -> TokenStream {
|
|
TokenStream::from(raw_types::gen(
|
|
TokenStream2::from(args),
|
|
raw_types::Mode::Wasi32,
|
|
))
|
|
}
|
|
|
|
/// A single-use macro in the `wasmtime-wasi` crate.
|
|
#[proc_macro]
|
|
pub fn define_add_wrappers_to_module(args: TokenStream) -> TokenStream {
|
|
wasi::add_wrappers_to_module(args.into()).into()
|
|
}
|