wasm_bindgen_macro/
lib.rs

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