Files
wasmtime/crates/wasi-common/wig/src/hostcalls.rs
Alex Crichton 5953215bac Auto-generate the hostcalls module of wasi-common (#846)
* Auto-generate shims for old `wasi_unstable` module

This commit is effectively just doing what #707 already did, but
applying it to the `snapshot_0` module as well. The end result is the
same, where we cut down on all the boilerplate in `snapshot_0` and bring
it in line with the main `wasi_snapshot_preview1` implementation. The
goal here is to make it easier to change the two in tandem since they're
both doing the same thing.

* Migrate `wasi_common::hostcalls` to a macro

This commit migrates the `hostcalls` module to being auto-generated by a
macro rather than duplicating a handwritten signature for each wasi
syscall.

* Auto-generate snapshot_0's `hostcalls` module

Similar to the previous commit, but for `snapshot_0`

* Delete the `wasi-common-cbindgen` crate

This is no longer needed with the hostcalls macro now, we can easily
fold the definition of the cbindgen macro into the same crate.

* Rustfmt

* Fix windows build errors

* Rustfmt

* Remove now no-longer-necessary code

* rustfmt
2020-01-22 14:54:39 -06:00

132 lines
3.9 KiB
Rust

use crate::utils;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
pub fn define(args: TokenStream) -> TokenStream {
let (path, phase) = utils::witx_path_from_args(args);
let doc = match witx::load(&[&path]) {
Ok(doc) => doc,
Err(e) => {
panic!("error opening file {}: {}", path, e);
}
};
let mut ret = TokenStream::new();
let old = match phase.as_str() {
"snapshot" => false,
"old/snapshot_0" => true,
s => panic!("unsupported phase: {}", s),
};
for module in doc.modules() {
for func in module.funcs() {
ret.extend(generate_wrappers(&func, old));
}
}
return ret;
}
fn generate_wrappers(func: &witx::InterfaceFunc, old: bool) -> TokenStream {
let name = format_ident!("{}", func.name.as_str());
let mut arg_declarations = Vec::new();
let mut arg_names = Vec::new();
for param in func.params.iter() {
let name = utils::param_name(param);
if let witx::TypePassedBy::PointerLengthPair = param.tref.type_().passed_by() {
let ptr = format_ident!("{}_ptr", name);
let len = format_ident!("{}_len", name);
arg_declarations.push(quote! { #ptr: super::wasi32::uintptr_t });
arg_declarations.push(quote! { #len: super::wasi32::size_t });
arg_names.push(ptr);
arg_names.push(len);
continue;
}
match &param.tref {
witx::TypeRef::Name(n) => {
if n.name.as_str() == "size" {
arg_declarations.push(quote! { #name: super::wasi32::size_t });
} else {
let ty_name = format_ident!("__wasi_{}_t", n.name.as_str());
arg_declarations.push(quote! { #name: super::wasi::#ty_name });
}
}
witx::TypeRef::Value(v) => match &**v {
witx::Type::ConstPointer(_) | witx::Type::Pointer(_) => {
arg_declarations.push(quote! { #name: super::wasi32::uintptr_t });
}
_ => panic!("unexpected value type"),
},
}
arg_names.push(name);
}
let mut ret = quote!(());
for (i, result) in func.results.iter().enumerate() {
if i == 0 {
match &result.tref {
witx::TypeRef::Name(n) => {
let ty_name = format_ident!("__wasi_{}_t", n.name.as_str());
ret = quote! { super::wasi::#ty_name };
}
witx::TypeRef::Value(_) => panic!("unexpected value type"),
}
continue;
}
let name = utils::param_name(result);
arg_declarations.push(quote! { #name: super::wasi32::uintptr_t });
arg_names.push(name);
}
let call = quote! {
super::hostcalls_impl::#name(wasi_ctx, memory, #(#arg_names,)*)
};
let body = if func.results.len() == 0 {
call
} else {
quote! {
let ret = #call
.err()
.unwrap_or(super::Error::ESUCCESS)
.as_wasi_error();
log::trace!(" | errno={}", ret);
ret.as_raw_errno()
}
};
let c_abi_name = if old {
format_ident!("old_wasi_common_{}", name)
} else {
format_ident!("wasi_common_{}", name)
};
quote! {
pub unsafe fn #name(
wasi_ctx: &mut super::WasiCtx,
memory: &mut [u8],
#(#arg_declarations,)*
) -> #ret {
#body
}
#[no_mangle]
pub unsafe fn #c_abi_name(
wasi_ctx: *mut super::WasiCtx,
memory: *mut u8,
memory_len: usize,
#(#arg_declarations,)*
) -> #ret {
#name(
&mut *wasi_ctx,
std::slice::from_raw_parts_mut(memory, memory_len),
#(#arg_names,)*
)
}
}
}