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;
7use quote::ToTokens;
8
9/// The main wasm_bindgen attribute macro.
10///
11/// This macro can be applied to `extern "C"` blocks to import JavaScript
12/// functions and types, using the same syntax as the original wasm-bindgen.
13///
14/// # Example
15///
16/// ```ignore
17/// use wry_bindgen::prelude::*;
18///
19/// #[wasm_bindgen]
20/// extern "C" {
21/// // Import a type
22/// #[wasm_bindgen(extends = Node)]
23/// pub type Element;
24///
25/// // Import a method
26/// #[wasm_bindgen(method, js_name = getAttribute)]
27/// pub fn get_attribute(this: &Element, name: &str) -> Option<String>;
28///
29/// // Import a getter
30/// #[wasm_bindgen(method, getter)]
31/// pub fn id(this: &Element) -> String;
32///
33/// // Import a constructor
34/// #[wasm_bindgen(constructor)]
35/// pub fn new() -> Element;
36/// }
37/// ```
38#[proc_macro_attribute]
39pub fn wasm_bindgen(attr: TokenStream, input: TokenStream) -> TokenStream {
40 match wry_bindgen_macro_support::expand(attr.into(), input.into()) {
41 Ok(tokens) => tokens.into(),
42 Err(err) => err.to_token_stream().into(),
43 }
44}
45
46/// Internal class marker macro used by wasm-bindgen impl-method expansion.
47#[proc_macro_attribute]
48pub fn __wasm_bindgen_class_marker(attr: TokenStream, input: TokenStream) -> TokenStream {
49 match wry_bindgen_macro_support::expand_class_marker(attr.into(), input.into()) {
50 Ok(tokens) => tokens.into(),
51 Err(err) => err.to_token_stream().into(),
52 }
53}
54
55/// Link to a JS file for use with workers/worklets.
56///
57/// Registers the referenced JS with the runtime and returns the URL the WebView
58/// serves it from, so it can be handed to APIs like `Worker::new`.
59///
60/// # Example
61///
62/// ```ignore
63/// use web_sys::Worker;
64/// let worker = Worker::new(&wasm_bindgen::link_to!(module = "/src/worker.js"));
65/// ```
66#[proc_macro]
67pub fn link_to(input: TokenStream) -> TokenStream {
68 match wry_bindgen_macro_support::expand_link_to(input.into()) {
69 Ok(tokens) => tokens.into(),
70 Err(err) => err.to_token_stream().into(),
71 }
72}