2018-07-29 17:59:46 +02:00
|
|
|
//! This crate contains the part of the implementation of the `#[wasm_bindgen]` optsibute that is
|
|
|
|
//! not in the shared backend crate.
|
|
|
|
|
|
|
|
#![doc(html_root_url = "https://docs.rs/wasm-bindgen-macro-support/0.2")]
|
|
|
|
|
|
|
|
extern crate proc_macro2;
|
|
|
|
extern crate quote;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate syn;
|
2018-08-01 18:59:59 -05:00
|
|
|
#[macro_use]
|
2018-07-29 17:59:46 +02:00
|
|
|
extern crate wasm_bindgen_backend as backend;
|
|
|
|
extern crate wasm_bindgen_shared as shared;
|
|
|
|
|
2018-09-26 08:26:00 -07:00
|
|
|
use backend::{Diagnostic, TryToTokens};
|
2018-07-29 17:59:46 +02:00
|
|
|
pub use parser::BindgenAttrs;
|
|
|
|
use parser::MacroParse;
|
2018-08-01 17:15:27 -05:00
|
|
|
use proc_macro2::TokenStream;
|
Migrate `wasm-bindgen` to using `walrus`
This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.
The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.
Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)
It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.
Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!
Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
2019-01-31 09:54:23 -08:00
|
|
|
use quote::ToTokens;
|
2019-01-25 14:31:50 -08:00
|
|
|
use quote::TokenStreamExt;
|
Migrate `wasm-bindgen` to using `walrus`
This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.
The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.
Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)
It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.
Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!
Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
2019-01-31 09:54:23 -08:00
|
|
|
use syn::parse::{Parse, ParseStream, Result as SynResult};
|
2018-07-29 17:59:46 +02:00
|
|
|
|
|
|
|
mod parser;
|
|
|
|
|
|
|
|
/// Takes the parsed input from a `#[wasm_bindgen]` macro and returns the generated bindings
|
2018-08-01 17:15:27 -05:00
|
|
|
pub fn expand(attr: TokenStream, input: TokenStream) -> Result<TokenStream, Diagnostic> {
|
2018-11-27 15:14:59 -08:00
|
|
|
parser::reset_attrs_used();
|
2018-09-04 11:32:09 -07:00
|
|
|
let item = syn::parse2::<syn::Item>(input)?;
|
|
|
|
let opts = syn::parse2(attr)?;
|
2018-08-01 17:15:27 -05:00
|
|
|
|
2018-07-29 17:59:46 +02:00
|
|
|
let mut tokens = proc_macro2::TokenStream::new();
|
|
|
|
let mut program = backend::ast::Program::default();
|
2018-08-01 17:15:27 -05:00
|
|
|
item.macro_parse(&mut program, (Some(opts), &mut tokens))?;
|
|
|
|
program.try_to_tokens(&mut tokens)?;
|
2018-11-27 15:14:59 -08:00
|
|
|
|
|
|
|
// If we successfully got here then we should have used up all attributes
|
|
|
|
// and considered all of them to see if they were used. If one was forgotten
|
|
|
|
// that's a bug on our end, so sanity check here.
|
|
|
|
parser::assert_all_attrs_checked();
|
|
|
|
|
2018-08-01 17:15:27 -05:00
|
|
|
Ok(tokens)
|
|
|
|
}
|
2019-01-25 14:31:50 -08:00
|
|
|
|
|
|
|
/// Takes the parsed input from a `#[wasm_bindgen]` macro and returns the generated bindings
|
Migrate `wasm-bindgen` to using `walrus`
This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.
The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.
Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)
It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.
Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!
Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
2019-01-31 09:54:23 -08:00
|
|
|
pub fn expand_class_marker(
|
|
|
|
attr: TokenStream,
|
|
|
|
input: TokenStream,
|
|
|
|
) -> Result<TokenStream, Diagnostic> {
|
2019-01-25 14:31:50 -08:00
|
|
|
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();
|
Migrate `wasm-bindgen` to using `walrus`
This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.
The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.
Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)
It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.
Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!
Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
2019-01-31 09:54:23 -08:00
|
|
|
tokens.append_all(item.attrs.iter().filter(|attr| match attr.style {
|
|
|
|
syn::AttrStyle::Outer => true,
|
|
|
|
_ => false,
|
2019-01-25 14:31:50 -08:00
|
|
|
}));
|
|
|
|
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);
|
|
|
|
}
|
Migrate `wasm-bindgen` to using `walrus`
This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.
The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.
Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)
It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.
Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!
Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
2019-01-31 09:54:23 -08:00
|
|
|
tokens.append_all(item.attrs.iter().filter(|attr| match attr.style {
|
|
|
|
syn::AttrStyle::Inner(_) => true,
|
|
|
|
_ => false,
|
2019-01-25 14:31:50 -08:00
|
|
|
}));
|
|
|
|
tokens.append_all(&item.block.stmts);
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(err) = err {
|
Migrate `wasm-bindgen` to using `walrus`
This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.
The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.
Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)
It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.
Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!
Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
2019-01-31 09:54:23 -08:00
|
|
|
return Err(err);
|
2019-01-25 14:31:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 })
|
|
|
|
}
|
|
|
|
}
|