generate: now we have a way to do errors, i guess

This commit is contained in:
Pat Hickey
2020-01-22 20:47:40 -08:00
parent 97077954f8
commit c05475b806
8 changed files with 158 additions and 96 deletions

View File

@@ -3,44 +3,45 @@ use quote::quote;
use crate::names::Names;
// FIXME need to template what arguments are required to an import function - some context
// FIXME need to template what argument is required to an import function - some context
// struct (e.g. WasiCtx) should be provided at the invocation of the `gen` proc macro.
// Rather than pass in memory as a `&mut [u8]` as today, require a `GuestMemory<'a>` be
// passed in.
//
// Additionally - need to template how to transform GuestValueError and MemoryError into
// the error type returned! From impl is a good start, but what if we want to log
// a more informative error? Maybe the conversion should be a (generated, for each by-value first
// return type) trait impled by WasiCtx?
pub fn define_func(names: &Names, func: &witx::InterfaceFunc) -> TokenStream {
let ident = names.func(&func.name);
let mut args = quote!(wasi_ctx: &mut WasiCtx, memory: GuestMemory,);
let arg_signature = |param: &witx::InterfaceFuncParam| -> TokenStream {
let name = names.func_param(&param.name);
match param.tref.type_().passed_by() {
witx::TypePassedBy::Value(atom) => {
let atom = names.atom_type(atom);
quote!(#name: #atom,)
quote!(#name: #atom)
}
witx::TypePassedBy::Pointer => {
let atom = names.atom_type(witx::AtomType::I32);
quote!(#name: #atom,)
quote!(#name: #atom)
}
witx::TypePassedBy::PointerLengthPair => {
let atom = names.atom_type(witx::AtomType::I32);
let len_name = names.func_len_param(&param.name);
quote!(#name: #atom, #len_name, #atom,)
quote!(#name: #atom, #len_name: #atom)
}
}
};
for param in func.params.iter() {
args.extend(arg_signature(param));
}
if let Some(arg_results) = func.results.get(1..) {
for result in arg_results {
args.extend(arg_signature(result))
}
}
let ret = if let Some(first_result) = func.results.get(0) {
let params = func
.params
.iter()
.chain(func.results.iter().skip(1))
.map(arg_signature);
let abi_args = quote!(
wasi_ctx: &mut WasiCtx, memory: GuestMemory,
#(#params),*
);
let abi_ret = if let Some(first_result) = func.results.get(0) {
match first_result.tref.type_().passed_by() {
witx::TypePassedBy::Value(atom) => names.atom_type(atom),
_ => unreachable!("first result should always be passed by value"),
@@ -51,5 +52,5 @@ pub fn define_func(names: &Names, func: &witx::InterfaceFunc) -> TokenStream {
quote!(())
};
quote!(pub fn #ident(#args) -> #ret { unimplemented!() })
quote!(pub fn #ident(#abi_args) -> #abi_ret { unimplemented!() })
}