Files
wasmtime/misc/wasmtime-rust/macro/src/lib.rs
Josh Triplett 56ce6e9c9f Migrate from failure to thiserror and anyhow (#436)
* Migrate from failure to thiserror and anyhow

The failure crate invents its own traits that don't use
std::error::Error (because failure predates certain features added to
Error); this prevents using ? on an error from failure in a function
using Error. The thiserror and anyhow crates integrate with the standard
Error trait instead.

This change does not attempt to semantically change or refactor the
approach to error-handling in any portion of the code, to ensure that
the change remains straightforward to review. Modules using specific
differentiated error types move from failure_derive and derive(Fail) to
thiserror and derive(Error). Modules boxing all errors opaquely move
from failure::Error to anyhow. Modules using String as an error type
continue to do so. Code using unwrap or expect continues to do so.

Drop Display implementations when thiserror can easily derive an
identical instance.

Drop manual traversal of iter_causes; anyhow's Debug instance prints the
chain of causes by default.

Use anyhow's type alias anyhow::Result<T> in place of
std::result::Result<T, anyhow::Error> whenever possible.

* wasm2obj: Simplify error handling using existing messages

handle_module in wasm2obj manually maps
cranelift_codegen::isa::LookupError values to strings, but LookupError
values already have strings that say almost exactly the same thing.
Rely on the strings from cranelift.

* wasmtime: Rely on question-mark-in-main

The main() wrapper around rmain() completely matches the behavior of
question-mark-in-main (print error to stderr and return 1), so switch to
question-mark-in-main.

* Update to walrus 0.13 and wasm-webidl-bindings 0.6

Both crates switched from failure to anyhow; updating lets us avoid a
translation from failure to anyhow within wasmtime-interface-types.
2019-11-04 20:43:25 -08:00

166 lines
5.1 KiB
Rust

extern crate proc_macro;
use proc_macro2::TokenStream;
use quote::quote;
use syn::spanned::Spanned;
#[proc_macro_attribute]
pub fn wasmtime(
_attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let item = syn::parse_macro_input!(item as syn::ItemTrait);
expand(item).unwrap_or_else(|e| e.to_compile_error()).into()
}
fn expand(item: syn::ItemTrait) -> syn::Result<TokenStream> {
let definition = generate_struct(&item)?;
let load = generate_load(&item)?;
let methods = generate_methods(&item)?;
let name = &item.ident;
Ok(quote! {
#definition
impl #name {
#load
#methods
}
})
}
fn generate_struct(item: &syn::ItemTrait) -> syn::Result<TokenStream> {
let vis = &item.vis;
let name = &item.ident;
let root = root();
Ok(quote! {
#vis struct #name {
cx: #root::wasmtime_jit::Context,
handle: #root::wasmtime_jit::InstanceHandle,
data: #root::wasmtime_interface_types::ModuleData,
}
})
}
fn generate_load(item: &syn::ItemTrait) -> syn::Result<TokenStream> {
let vis = &item.vis;
let name = &item.ident;
let root = root();
Ok(quote! {
#vis fn load_file(path: impl AsRef<std::path::Path>) -> #root::anyhow::Result<#name> {
let bytes = std::fs::read(path)?;
let isa = {
let isa_builder = #root::cranelift_native::builder()
.map_err(|s| #root::anyhow::format_err!("{}", s))?;
let flag_builder = #root::cranelift_codegen::settings::builder();
isa_builder.finish(#root::cranelift_codegen::settings::Flags::new(flag_builder))
};
let mut cx = #root::wasmtime_jit::Context::with_isa(
isa,
#root::wasmtime_jit::CompilationStrategy::Auto
).with_features(#root::wasmtime_jit::Features {
multi_value: true,
..Default::default()
});
let data = #root::wasmtime_interface_types::ModuleData::new(&bytes)?;
let handle = cx.instantiate_module(None, &bytes)?;
Ok(#name { cx, handle, data })
}
})
}
fn generate_methods(item: &syn::ItemTrait) -> syn::Result<TokenStream> {
macro_rules! bail {
($e:expr, $($fmt:tt)*) => (
return Err(syn::Error::new($e.span(), format!($($fmt)*)));
)
}
let mut result = TokenStream::new();
let root = root();
for item in item.items.iter() {
let method = match item {
syn::TraitItem::Method(f) => f,
other => bail!(other, "only methods are allowed"),
};
if let Some(e) = &method.default {
bail!(e, "cannot specify an implementation of methods");
}
if let Some(t) = &method.sig.constness {
bail!(t, "cannot be `const`");
}
if let Some(t) = &method.sig.asyncness {
bail!(t, "cannot be `async`");
}
let mut args = Vec::new();
for arg in method.sig.inputs.iter() {
let arg = match arg {
syn::FnArg::Receiver(_) => continue,
syn::FnArg::Typed(arg) => arg,
};
let ident = match &*arg.pat {
syn::Pat::Ident(i) => i,
other => bail!(other, "must use bare idents for arguments"),
};
if let Some(t) = &ident.by_ref {
bail!(t, "arguments cannot bind by reference");
}
if let Some(t) = &ident.mutability {
bail!(t, "arguments cannot be mutable");
}
if let Some((_, t)) = &ident.subpat {
bail!(t, "arguments cannot have sub-bindings");
}
let ident = &ident.ident;
args.push(quote! {
#root::wasmtime_interface_types::Value::from(#ident)
});
}
let convert_ret = match &method.sig.output {
syn::ReturnType::Default => {
quote! {
<() as #root::FromVecValue>::from(results)
}
}
syn::ReturnType::Type(_, ty) => match &**ty {
syn::Type::Tuple(..) => {
quote! { <#ty as #root::FromVecValue>::from(results) }
}
_ => {
quote! { <(#ty,) as #root::FromVecValue>::from(results).map(|t| t.0) }
}
},
};
let sig = &method.sig;
let attrs = &method.attrs;
let name = &method.sig.ident;
result.extend(quote! {
#(#attrs)*
#sig {
let args = [
#(#args),*
];
let results = self.data.invoke(
&mut self.cx,
&mut self.handle,
stringify!(#name),
&args,
).expect("wasm execution failed");
#convert_ret.expect("failed to convert return type")
}
});
}
Ok(result)
}
fn root() -> TokenStream {
quote! { wasmtime_rust::__rt }
}