mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-26 19:21:35 +00:00
Fix conditional #[wasm_bindgen] in impls
Reported in #1191 the fix requires us to get a bit creative I think. The general gist is that a block like this: #[wasm_bindgen] impl Foo { pub fn foo() {} } was previously expanded all in one go. Now, however, it's expanded into: impl Foo { #[__wasm_bindgen_class_marker(Foo = "Foo")] pub fn foo() {} } // goop generated by orginal #[wasm_bindgen] This method of expansion takes advantage of rustc's recursive expansion feature. It also allows us to expand `impl` blocks and allow inner items to not be fully expanded yet, such as still having `#[cfg]` attributes (like in the original bug report). We use theinternal `__wasm_bindgen_class_marker` to indicate that we're parsing an `ImplItemMethod` unconditionally, and then generation proceeds as usual. The only final catch is that when we're expanding in an `impl` block we have to generate tokens for the `Program` (wasm-bindgen injected goop like the custom section) inside the body of the function itself instead of next to it. Otherwise we'd get syntax errors inside of impl blocks! Closes #1191
This commit is contained in:
@ -13,8 +13,11 @@ extern crate wasm_bindgen_shared as shared;
|
||||
|
||||
use backend::{Diagnostic, TryToTokens};
|
||||
pub use parser::BindgenAttrs;
|
||||
use quote::ToTokens;
|
||||
use parser::MacroParse;
|
||||
use proc_macro2::TokenStream;
|
||||
use syn::parse::{Parse, ParseStream, Result as SynResult};
|
||||
use quote::TokenStreamExt;
|
||||
|
||||
mod parser;
|
||||
|
||||
@ -36,3 +39,68 @@ pub fn expand(attr: TokenStream, input: TokenStream) -> Result<TokenStream, Diag
|
||||
|
||||
Ok(tokens)
|
||||
}
|
||||
|
||||
/// Takes the parsed input from a `#[wasm_bindgen]` macro and returns the generated bindings
|
||||
pub fn expand_class_marker(attr: TokenStream, input: TokenStream) -> Result<TokenStream, Diagnostic> {
|
||||
parser::reset_attrs_used();
|
||||
let mut item = syn::parse2::<syn::ImplItemMethod>(input)?;
|
||||
let opts: ClassMarker = syn::parse2(attr)?;
|
||||
|
||||
let mut program = backend::ast::Program::default();
|
||||
item.macro_parse(&mut program, (&opts.class, &opts.js_class))?;
|
||||
parser::assert_all_attrs_checked(); // same as above
|
||||
|
||||
// This is where things are slightly different, we are being expanded in the
|
||||
// context of an impl so we can't inject arbitrary item-like tokens into the
|
||||
// output stream. If we were to do that then it wouldn't parse!
|
||||
//
|
||||
// Instead what we want to do is to generate the tokens for `program` into
|
||||
// the header of the function. This'll inject some no_mangle functions and
|
||||
// statics and such, and they should all be valid in the context of the
|
||||
// start of a function.
|
||||
//
|
||||
// We manually implement `ToTokens for ImplItemMethod` here, injecting our
|
||||
// program's tokens before the actual method's inner body tokens.
|
||||
let mut tokens = proc_macro2::TokenStream::new();
|
||||
tokens.append_all(item.attrs.iter().filter(|attr| {
|
||||
match attr.style {
|
||||
syn::AttrStyle::Outer => true,
|
||||
_ => false,
|
||||
}
|
||||
}));
|
||||
item.vis.to_tokens(&mut tokens);
|
||||
item.sig.to_tokens(&mut tokens);
|
||||
let mut err = None;
|
||||
item.block.brace_token.surround(&mut tokens, |tokens| {
|
||||
if let Err(e) = program.try_to_tokens(tokens) {
|
||||
err = Some(e);
|
||||
}
|
||||
tokens.append_all(item.attrs.iter().filter(|attr| {
|
||||
match attr.style {
|
||||
syn::AttrStyle::Inner(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}));
|
||||
tokens.append_all(&item.block.stmts);
|
||||
});
|
||||
|
||||
if let Some(err) = err {
|
||||
return Err(err)
|
||||
}
|
||||
|
||||
Ok(tokens)
|
||||
}
|
||||
|
||||
struct ClassMarker {
|
||||
class: syn::Ident,
|
||||
js_class: String,
|
||||
}
|
||||
|
||||
impl Parse for ClassMarker {
|
||||
fn parse(input: ParseStream) -> SynResult<Self> {
|
||||
let class = input.parse::<syn::Ident>()?;
|
||||
input.parse::<Token![=]>()?;
|
||||
let js_class = input.parse::<syn::LitStr>()?.value();
|
||||
Ok(ClassMarker { class, js_class })
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user