wasm_bindgen_macro/
lib.rs

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