wry_bindgen_macro/lib.rs
1//! wry-bindgen-macro - Proc-macro for wasm_bindgen-style bindings
2//!
3//! This crate provides the `#[wasm_bindgen]` attribute macro that generates
4//! code for Wry's WebView IPC protocol.
5
6use proc_macro::TokenStream;
7
8/// The main wasm_bindgen attribute macro.
9///
10/// This macro can be applied to `extern "C"` blocks to import JavaScript
11/// functions and types, using the same syntax as the original wasm-bindgen.
12///
13/// # Example
14///
15/// ```ignore
16/// use wry_bindgen::prelude::*;
17///
18/// #[wasm_bindgen]
19/// extern "C" {
20/// // Import a type
21/// #[wasm_bindgen(extends = Node)]
22/// pub type Element;
23///
24/// // Import a method
25/// #[wasm_bindgen(method, js_name = getAttribute)]
26/// pub fn get_attribute(this: &Element, name: &str) -> Option<String>;
27///
28/// // Import a getter
29/// #[wasm_bindgen(method, getter)]
30/// pub fn id(this: &Element) -> String;
31///
32/// // Import a constructor
33/// #[wasm_bindgen(constructor)]
34/// pub fn new() -> Element;
35/// }
36/// ```
37#[proc_macro_attribute]
38pub fn wasm_bindgen(attr: TokenStream, input: TokenStream) -> TokenStream {
39 match wry_bindgen_macro_support::expand(attr.into(), input.into()) {
40 Ok(tokens) => tokens.into(),
41 Err(err) => err.to_compile_error().into(),
42 }
43}
44
45/// Link to a JS file for use with workers/worklets.
46///
47/// This macro is only meaningful in WASM contexts. When running outside of WASM,
48/// it will panic at runtime.
49///
50/// # Example
51///
52/// ```ignore
53/// use web_sys::Worker;
54/// let worker = Worker::new(&wasm_bindgen::link_to!(module = "/src/worker.js"));
55/// ```
56#[proc_macro]
57pub fn link_to(_input: TokenStream) -> TokenStream {
58 quote::quote! {
59 panic!("link_to! cannot be used when running outside of wasm")
60 }
61 .into()
62}