diff --git a/derive/src/codegen.rs b/derive/src/codegen.rs index bdc4dbc..edbad4b 100644 --- a/derive/src/codegen.rs +++ b/derive/src/codegen.rs @@ -2,22 +2,21 @@ use crate::model::{ExtDefinition, ExternalFunc}; use proc_macro2::{Ident, Literal, Span, TokenStream}; use quote::{quote, quote_spanned, ToTokens}; - pub fn codegen(ext_def: &ExtDefinition, to: &mut TokenStream) { let mut externals = TokenStream::new(); let mut module_resolver = TokenStream::new(); // TODO: Come up with a name. let mut new_name = "_WASMI_IMPLS_".to_string(); - new_name.push_str("NAME".to_string().trim_start_matches("r#")); - let dummy_const = Ident::new(&new_name, Span::call_site()); + new_name.push_str("NAME".to_string().trim_start_matches("r#")); + let dummy_const = Ident::new(&new_name, Span::call_site()); derive_externals(ext_def, &mut externals); derive_module_resolver(ext_def, &mut module_resolver); (quote! { const #dummy_const: () = { - extern crate wasmi as _wasmi; + extern crate wasmi as _wasmi; use _wasmi::{ Trap, RuntimeValue, RuntimeArgs, Externals, ValueType, ModuleImportResolver, @@ -41,7 +40,8 @@ pub fn codegen(ext_def: &ExtDefinition, to: &mut TokenStream) { #externals #module_resolver }; - }).to_tokens(to); + }) + .to_tokens(to); } fn emit_dispatch_func_arm(func: &ExternalFunc) -> TokenStream { @@ -58,7 +58,8 @@ fn emit_dispatch_func_arm(func: &ExternalFunc) -> TokenStream { args.next() .and_then(|rt_val| rt_val.try_into()) .unwrap(); - }).to_tokens(&mut unmarshall_args); + }) + .to_tokens(&mut unmarshall_args); } let prologue = quote! { @@ -109,7 +110,8 @@ fn derive_externals(ext_def: &ExtDefinition, to: &mut TokenStream) { // ... } - }).to_tokens(to); + }) + .to_tokens(to); } fn emit_resolve_func_arm(func: &ExternalFunc) -> TokenStream { @@ -129,18 +131,26 @@ fn emit_resolve_func_arm(func: &ExternalFunc) -> TokenStream { } }; - let init = func.args.iter().map(|param| { - let ident = ¶m.ident; - quote! { - let #ident = None; - } - }).collect::>(); + let init = func + .args + .iter() + .map(|param| { + let ident = ¶m.ident; + quote! { + let #ident = None; + } + }) + .collect::>(); - let params_materialized_tys = func.args.iter().map(|param| { - let ident = ¶m.ident; - let span = param.ident.span(); - quote_spanned! {span=> materialize_arg_ty(#ident) } - }).collect::>(); + let params_materialized_tys = func + .args + .iter() + .map(|param| { + let ident = ¶m.ident; + let span = param.ident.span(); + quote_spanned! {span=> materialize_arg_ty(#ident) } + }) + .collect::>(); let materialized_return_ty = quote_spanned! { return_ty_span=> materialize_ret_type(return_val) @@ -162,8 +172,8 @@ fn emit_resolve_func_arm(func: &ExternalFunc) -> TokenStream { // at this point types of all variables and return_val are inferred. if signature.params() != &[#(#params_materialized_tys),*] || signature.return_type() != #materialized_return_ty { return Err(Error::Instantiation( - format!("Export {} has different signature {:?}", #string_ident, signature), - )); + format!("Export {} has different signature {:?}", #string_ident, signature), + )); } return Ok(FuncInstance::alloc_host(signature.clone(), #index)); diff --git a/derive/src/lib.rs b/derive/src/lib.rs index 7e8e5b3..22e94ea 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -1,10 +1,10 @@ -#![recursion_limit="128"] +#![recursion_limit = "128"] extern crate proc_macro; +mod codegen; mod model; mod parser; -mod codegen; use proc_macro::TokenStream; diff --git a/derive/src/parser.rs b/derive/src/parser.rs index 0ac0f03..6e19af2 100644 --- a/derive/src/parser.rs +++ b/derive/src/parser.rs @@ -1,6 +1,6 @@ use crate::model::{self, ExtDefinition, ExternalFunc, Param}; -use syn::{ItemImpl, ImplItem, ImplItemMethod, FnArg, ReturnType, Ident}; use syn::spanned::Spanned; +use syn::{FnArg, Ident, ImplItem, ImplItemMethod, ItemImpl, ReturnType}; /// Parse an incoming stream of tokens into externalities definition. pub fn parse(input: proc_macro2::TokenStream) -> Result { @@ -10,20 +10,22 @@ pub fn parse(input: proc_macro2::TokenStream) -> Result { for item in item_impl.items { match item { - ImplItem::Method(ImplItemMethod { - sig, - .. - }) => { + ImplItem::Method(ImplItemMethod { sig, .. }) => { let index = funcs.len() as u32; // self TODO: handle this properly - let args = sig.decl.inputs.iter().skip(1).enumerate().map(|(idx, input)| { - let param_name = format!("arg{}", idx); - let ident = Ident::new(¶m_name, input.span()); - Param { - ident, - } - }).collect::>(); + let args = sig + .decl + .inputs + .iter() + .skip(1) + .enumerate() + .map(|(idx, input)| { + let param_name = format!("arg{}", idx); + let ident = Ident::new(¶m_name, input.span()); + Param { ident } + }) + .collect::>(); let return_ty = match sig.decl.output { ReturnType::Default => None, @@ -36,8 +38,8 @@ pub fn parse(input: proc_macro2::TokenStream) -> Result { args, return_ty, }); - }, - _ => {}, + } + _ => {} } } diff --git a/src/derive_support.rs b/src/derive_support.rs index ce5f886..100b8ef 100644 --- a/src/derive_support.rs +++ b/src/derive_support.rs @@ -1,5 +1,5 @@ -use {ValueType, RuntimeValue, Trap}; use nan_preserving_float::{F32, F64}; +use {RuntimeValue, Trap, ValueType}; /// A trait that represents a value that can be directly coerced to one of /// wasm base value types. @@ -40,21 +40,17 @@ impl IntoWasmResult for () { } } -impl> IntoWasmResult for Result { +impl> IntoWasmResult for Result { const VALUE_TYPE: Option = Some(R::VALUE_TYPE); fn into_wasm_result(self) -> Result, Trap> { - self - .map(|v| Some(v.into_wasm_value())) - .map_err(Into::into) + self.map(|v| Some(v.into_wasm_value())).map_err(Into::into) } } -impl> IntoWasmResult for Result<(), E> { +impl> IntoWasmResult for Result<(), E> { const VALUE_TYPE: Option = None; fn into_wasm_result(self) -> Result, Trap> { - self - .map(|_| None) - .map_err(Into::into) + self.map(|_| None).map_err(Into::into) } } diff --git a/tests/derives.rs b/tests/derives.rs index 0db2aab..1d93fcd 100644 --- a/tests/derives.rs +++ b/tests/derives.rs @@ -1,17 +1,17 @@ -extern crate wasmi_derive; extern crate wasmi; +extern crate wasmi_derive; -use wasmi_derive::derive_externals; -use wasmi::HostError; use std::fmt; +use wasmi::HostError; +use wasmi_derive::derive_externals; #[derive(Debug)] struct NoInfoError; impl HostError for NoInfoError {} impl fmt::Display for NoInfoError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "NoInfoError") - } + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "NoInfoError") + } } struct NonStaticExternals<'a> {