|
| 1 | +//! Wasmtime test macro. |
| 2 | +//! |
| 3 | +//! This macro is a helper to define tests that exercise multiple configuration |
| 4 | +//! combinations for Wasmtime. Currently, only compiler strategies are |
| 5 | +//! supported. |
| 6 | +//! |
| 7 | +//! Usage |
| 8 | +//! |
| 9 | +//! #[wasmtime_test(strategies(Cranelift, Winch))] |
| 10 | +//! fn my_test(config: &mut Config) -> Result<()> { |
| 11 | +//! Ok(()) |
| 12 | +//! } |
| 13 | +use proc_macro::TokenStream; |
| 14 | +use quote::{quote, ToTokens, TokenStreamExt}; |
| 15 | +use syn::{ |
| 16 | + braced, |
| 17 | + parse::{Parse, ParseStream}, |
| 18 | + parse_macro_input, token, Attribute, Ident, Result, ReturnType, Signature, Visibility, |
| 19 | +}; |
| 20 | + |
| 21 | +/// Test configuration. |
| 22 | +struct TestConfig { |
| 23 | + /// Supported compiler strategies. |
| 24 | + strategies: Vec<(String, Ident)>, |
| 25 | +} |
| 26 | + |
| 27 | +impl TestConfig { |
| 28 | + /// Validate the test configuration. |
| 29 | + /// Only the number of strategies is validated, as this avoid expansions of |
| 30 | + /// empty strategies or more strategies than supported. |
| 31 | + /// |
| 32 | + /// The supported strategies are validated inline when parsing. |
| 33 | + fn validate(&self) -> anyhow::Result<()> { |
| 34 | + if self.strategies.len() > 2 { |
| 35 | + Err(anyhow::anyhow!("Expected at most 2 strategies")) |
| 36 | + } else if self.strategies.len() == 0 { |
| 37 | + Err(anyhow::anyhow!("Expected at least 1 strategy")) |
| 38 | + } else { |
| 39 | + Ok(()) |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl Default for TestConfig { |
| 45 | + fn default() -> Self { |
| 46 | + Self { strategies: vec![] } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/// A generic function body represented as a braced [`TokenStream`]. |
| 51 | +struct Block { |
| 52 | + brace: token::Brace, |
| 53 | + rest: proc_macro2::TokenStream, |
| 54 | +} |
| 55 | + |
| 56 | +impl Parse for Block { |
| 57 | + fn parse(input: ParseStream) -> Result<Self> { |
| 58 | + let content; |
| 59 | + Ok(Self { |
| 60 | + brace: braced!(content in input), |
| 61 | + rest: content.parse()?, |
| 62 | + }) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl ToTokens for Block { |
| 67 | + fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { |
| 68 | + self.brace.surround(tokens, |tokens| { |
| 69 | + tokens.append_all(self.rest.clone()); |
| 70 | + }); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/// Custom function parser. |
| 75 | +/// Parses the function's attributes, visibility and signature, leaving the |
| 76 | +/// block as an opaque [`TokenStream`]. |
| 77 | +struct Fn { |
| 78 | + attrs: Vec<Attribute>, |
| 79 | + visibility: Visibility, |
| 80 | + sig: Signature, |
| 81 | + body: Block, |
| 82 | +} |
| 83 | + |
| 84 | +impl Parse for Fn { |
| 85 | + fn parse(input: ParseStream) -> Result<Self> { |
| 86 | + let attrs = input.call(Attribute::parse_outer)?; |
| 87 | + let visibility: Visibility = input.parse()?; |
| 88 | + let sig: Signature = input.parse()?; |
| 89 | + let body: Block = input.parse()?; |
| 90 | + |
| 91 | + Ok(Self { |
| 92 | + attrs, |
| 93 | + visibility, |
| 94 | + sig, |
| 95 | + body, |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl ToTokens for Fn { |
| 101 | + fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { |
| 102 | + for attr in &self.attrs { |
| 103 | + attr.to_tokens(tokens); |
| 104 | + } |
| 105 | + self.visibility.to_tokens(tokens); |
| 106 | + self.sig.to_tokens(tokens); |
| 107 | + self.body.to_tokens(tokens); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +#[proc_macro_attribute] |
| 112 | +pub fn wasmtime_test(attrs: TokenStream, item: TokenStream) -> TokenStream { |
| 113 | + let mut test_config = TestConfig::default(); |
| 114 | + |
| 115 | + let config_parser = syn::meta::parser(|meta| { |
| 116 | + if meta.path.is_ident("strategies") { |
| 117 | + meta.parse_nested_meta(|meta| { |
| 118 | + if meta.path.is_ident("Winch") || meta.path.is_ident("Cranelift") { |
| 119 | + let id = meta.path.require_ident()?.clone(); |
| 120 | + test_config.strategies.push((id.to_string(), id)); |
| 121 | + Ok(()) |
| 122 | + } else { |
| 123 | + Err(meta.error("Unknown strategy")) |
| 124 | + } |
| 125 | + })?; |
| 126 | + |
| 127 | + test_config.validate().map_err(|e| meta.error(e)) |
| 128 | + } else { |
| 129 | + Err(meta.error("Unsupported attributes")) |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + parse_macro_input!(attrs with config_parser); |
| 134 | + |
| 135 | + match expand(&test_config, parse_macro_input!(item as Fn)) { |
| 136 | + Ok(tok) => tok, |
| 137 | + Err(e) => e.into_compile_error().into(), |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +fn expand(test_config: &TestConfig, func: Fn) -> Result<TokenStream> { |
| 142 | + let mut tests = vec![quote! { #func }]; |
| 143 | + let attrs = &func.attrs; |
| 144 | + |
| 145 | + for (strategy_name, ident) in &test_config.strategies { |
| 146 | + // Winch currently only offers support for x64. |
| 147 | + let target = if strategy_name == "Winch" { |
| 148 | + quote! { #[cfg(target_arch = "x86_64")] } |
| 149 | + } else { |
| 150 | + quote! {} |
| 151 | + }; |
| 152 | + let func_name = &func.sig.ident; |
| 153 | + let ret = match &func.sig.output { |
| 154 | + ReturnType::Default => quote! { () }, |
| 155 | + ReturnType::Type(_, ty) => quote! { -> #ty }, |
| 156 | + }; |
| 157 | + let test_name = Ident::new( |
| 158 | + &format!("{}_{}", strategy_name.to_lowercase(), func_name), |
| 159 | + func_name.span(), |
| 160 | + ); |
| 161 | + let tok = quote! { |
| 162 | + #[test] |
| 163 | + #target |
| 164 | + #(#attrs)* |
| 165 | + fn #test_name() #ret { |
| 166 | + let mut config = Config::new(); |
| 167 | + config.strategy(Strategy::#ident); |
| 168 | + #func_name(&mut config) |
| 169 | + } |
| 170 | + }; |
| 171 | + |
| 172 | + tests.push(tok); |
| 173 | + } |
| 174 | + Ok(quote! { |
| 175 | + #(#tests)* |
| 176 | + } |
| 177 | + .into()) |
| 178 | +} |
0 commit comments