1 | #![doc (html_root_url = "https://docs.rs/wasm-bindgen-macro/0.2" )] |
2 | |
3 | extern crate proc_macro; |
4 | |
5 | use proc_macro::TokenStream; |
6 | use quote::quote; |
7 | |
8 | /// A list of all the attributes can be found here: https://rustwasm.github.io/docs/wasm-bindgen/reference/attributes/index.html |
9 | #[proc_macro_attribute ] |
10 | pub fn wasm_bindgen (attr: TokenStream, input: TokenStream) -> TokenStream { |
11 | match wasm_bindgen_macro_support::expand(attr.into(), input.into()) { |
12 | Ok(tokens: TokenStream) => { |
13 | if cfg!(feature = "xxx_debug_only_print_generated_code" ) { |
14 | println!(" {}" , tokens); |
15 | } |
16 | tokens.into() |
17 | } |
18 | Err(diagnostic: Diagnostic) => (quote! { #diagnostic }).into(), |
19 | } |
20 | } |
21 | |
22 | /// This macro takes a JS module as input and returns a URL that can be used to |
23 | /// access it at runtime. |
24 | /// |
25 | /// The module can be specified in a few ways: |
26 | /// - You can use `inline_js = "..."` to create an inline JS file. |
27 | /// - You can use `module = "/foo/bar"` to reference a file relative to the |
28 | /// root of the crate the macro is invoked in. |
29 | /// |
30 | /// The returned URL can be used for things like creating workers/worklets: |
31 | /// ```no_run |
32 | /// use web_sys::Worker; |
33 | /// let worker = Worker::new(&wasm_bindgen::link_to!(module = "/src/worker.js" )); |
34 | /// ``` |
35 | #[proc_macro ] |
36 | pub fn link_to(input: TokenStream) -> TokenStream { |
37 | match wasm_bindgen_macro_support::expand_link_to(input.into()) { |
38 | Ok(tokens: TokenStream) => { |
39 | if cfg!(feature = "xxx_debug_only_print_generated_code" ) { |
40 | println!(" {}" , tokens); |
41 | } |
42 | tokens.into() |
43 | } |
44 | // This `String::clone` is here so that IDEs know this is supposed to be a |
45 | // `String` and can keep type-checking the rest of the program even if the macro |
46 | // fails. |
47 | Err(diagnostic: Diagnostic) => (quote! { String::clone(#diagnostic) }).into(), |
48 | } |
49 | } |
50 | |
51 | #[proc_macro_attribute ] |
52 | pub fn __wasm_bindgen_class_marker (attr: TokenStream, input: TokenStream) -> TokenStream { |
53 | match wasm_bindgen_macro_support::expand_class_marker(attr.into(), input.into()) { |
54 | Ok(tokens: TokenStream) => { |
55 | if cfg!(feature = "xxx_debug_only_print_generated_code" ) { |
56 | println!(" {}" , tokens); |
57 | } |
58 | tokens.into() |
59 | } |
60 | Err(diagnostic: Diagnostic) => (quote! { #diagnostic }).into(), |
61 | } |
62 | } |
63 | |
64 | #[proc_macro_derive (BindgenedStruct, attributes(wasm_bindgen))] |
65 | pub fn __wasm_bindgen_struct_marker(item: TokenStream) -> TokenStream { |
66 | match wasm_bindgen_macro_support::expand_struct_marker(item.into()) { |
67 | Ok(tokens: TokenStream) => { |
68 | if cfg!(feature = "xxx_debug_only_print_generated_code" ) { |
69 | println!(" {}" , tokens); |
70 | } |
71 | tokens.into() |
72 | } |
73 | Err(diagnostic: Diagnostic) => (quote! { #diagnostic }).into(), |
74 | } |
75 | } |
76 | |