wiggle: new error configuration for generating a "trappable error" (#5276)

* Add a new "trappable" mode for wiggle to make an error type

start refactoring how errors are generated and configured

put a pin in this - you can now configure a generated error

but i need to go fix Names

Names is no longer a struct, rt is hardcoded to wiggle

rest of fixes to pass tests

its called a trappable error now

don't generate UserErrorConversion trait if empty

mention in macro docs

* undo omitting the user error conversion trait when empty
This commit is contained in:
Pat Hickey
2022-11-16 08:54:41 -08:00
committed by GitHub
parent 1bd78f1a35
commit 22433ed726
15 changed files with 561 additions and 464 deletions

View File

@@ -1,17 +1,17 @@
use crate::config::Asyncness;
use crate::funcs::func_bounds;
use crate::{CodegenSettings, Names};
use crate::names;
use crate::CodegenSettings;
use proc_macro2::{Ident, Span, TokenStream};
use quote::{format_ident, quote};
use std::collections::HashSet;
pub fn link_module(
module: &witx::Module,
names: &Names,
target_path: Option<&syn::Path>,
settings: &CodegenSettings,
) -> TokenStream {
let module_ident = names.module(&module.name);
let module_ident = names::module(&module.name);
let send_bound = if settings.async_.contains_async(module) {
quote! { + Send, T: Send }
@@ -23,8 +23,8 @@ pub fn link_module(
let mut bounds = HashSet::new();
for f in module.funcs() {
let asyncness = settings.async_.get(module.name.as_str(), f.name.as_str());
bodies.push(generate_func(&module, &f, names, target_path, asyncness));
let bound = func_bounds(names, module, &f, settings);
bodies.push(generate_func(&module, &f, target_path, asyncness));
let bound = func_bounds(module, &f, settings);
for b in bound {
bounds.insert(b);
}
@@ -46,14 +46,12 @@ pub fn link_module(
format_ident!("add_{}_to_linker", module_ident)
};
let rt = names.runtime_mod();
quote! {
/// Adds all instance items to the specified `Linker`.
pub fn #func_name<T, U>(
linker: &mut #rt::wasmtime_crate::Linker<T>,
linker: &mut wiggle::wasmtime_crate::Linker<T>,
get_cx: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
) -> #rt::anyhow::Result<()>
) -> wiggle::anyhow::Result<()>
where
U: #ctx_bound #send_bound
{
@@ -66,17 +64,14 @@ pub fn link_module(
fn generate_func(
module: &witx::Module,
func: &witx::InterfaceFunc,
names: &Names,
target_path: Option<&syn::Path>,
asyncness: Asyncness,
) -> TokenStream {
let rt = names.runtime_mod();
let module_str = module.name.as_str();
let module_ident = names.module(&module.name);
let module_ident = names::module(&module.name);
let field_str = func.name.as_str();
let field_ident = names.func(&func.name);
let field_ident = names::func(&func.name);
let (params, results) = func.wasm_signature();
@@ -88,14 +83,14 @@ fn generate_func(
.enumerate()
.map(|(i, ty)| {
let name = &arg_names[i];
let wasm = names.wasm_type(*ty);
let wasm = names::wasm_type(*ty);
quote! { #name: #wasm }
})
.collect::<Vec<_>>();
let ret_ty = match results.len() {
0 => quote!(()),
1 => names.wasm_type(results[0]),
1 => names::wasm_type(results[0]),
_ => unimplemented!(),
};
@@ -114,16 +109,16 @@ fn generate_func(
let body = quote! {
let export = caller.get_export("memory");
let (mem, ctx) = match &export {
Some(#rt::wasmtime_crate::Extern::Memory(m)) => {
Some(wiggle::wasmtime_crate::Extern::Memory(m)) => {
let (mem, ctx) = m.data_and_store_mut(&mut caller);
let ctx = get_cx(ctx);
(#rt::wasmtime::WasmtimeGuestMemory::new(mem), ctx)
(wiggle::wasmtime::WasmtimeGuestMemory::new(mem), ctx)
}
Some(#rt::wasmtime_crate::Extern::SharedMemory(m)) => {
Some(wiggle::wasmtime_crate::Extern::SharedMemory(m)) => {
let ctx = get_cx(caller.data_mut());
(#rt::wasmtime::WasmtimeGuestMemory::shared(m.data()), ctx)
(wiggle::wasmtime::WasmtimeGuestMemory::shared(m.data()), ctx)
}
_ => #rt::anyhow::bail!("missing required memory export"),
_ => wiggle::anyhow::bail!("missing required memory export"),
};
Ok(<#ret_ty>::from(#abi_func(ctx, &mem #(, #arg_names)*) #await_ ?))
};
@@ -135,7 +130,7 @@ fn generate_func(
linker.#wrapper(
#module_str,
#field_str,
move |mut caller: #rt::wasmtime_crate::Caller<'_, T> #(, #arg_decls)*| {
move |mut caller: wiggle::wasmtime_crate::Caller<'_, T> #(, #arg_decls)*| {
Box::new(async move { #body })
},
)?;
@@ -147,9 +142,9 @@ fn generate_func(
linker.func_wrap(
#module_str,
#field_str,
move |mut caller: #rt::wasmtime_crate::Caller<'_, T> #(, #arg_decls)*| -> #rt::anyhow::Result<#ret_ty> {
move |mut caller: wiggle::wasmtime_crate::Caller<'_, T> #(, #arg_decls)*| -> wiggle::anyhow::Result<#ret_ty> {
let result = async { #body };
#rt::run_in_dummy_executor(result)?
wiggle::run_in_dummy_executor(result)?
},
)?;
}
@@ -160,7 +155,7 @@ fn generate_func(
linker.func_wrap(
#module_str,
#field_str,
move |mut caller: #rt::wasmtime_crate::Caller<'_, T> #(, #arg_decls)*| -> #rt::anyhow::Result<#ret_ty> {
move |mut caller: wiggle::wasmtime_crate::Caller<'_, T> #(, #arg_decls)*| -> wiggle::anyhow::Result<#ret_ty> {
#body
},
)?;