Skip to main content

wasm_export

Attribute Macro wasm_export 

Source
#[wasm_export]
Expand description

Exports an impl block or a free function to JavaScript, in place of #[cfg_attr(feature = "wasm", wasm_bindgen(..))].

The item is left exactly as written, so Rust and UniFFI callers see its plain signature. A second #[wasm_bindgen] block of __wasm_ts_* shims is generated beside it, and only those name the wire types: each parameter is projected onto <T as FromWasm>::Wire and each return onto <T as ToWasm>::Wire, with the conversion in the shim body. References pass through as written.

Every pub method is shimmed, matching wasm_bindgen’s own rule, and every shim is fallible even where nothing can fail — wasm_bindgen renders Result<T, E> as T plus a throw, so the TypeScript is identical. A method’s own #[wasm_bindgen(..)] moves onto its shim.

Arguments are forwarded to #[wasm_bindgen], so #[wasm_export(js_class = Foo)] behaves as it would there. The item must not be generic; wasm_bindgen cannot export generic items.

§#[wasm_only]

Marks a method whose only intended caller is JavaScript, because Rust has a better API for the same thing. The method is renamed with a __wasm_only_ prefix, hidden from documentation, and marked #[deprecated] so it shows struck through in IDE autocomplete. Takes an optional note = "..." for that deprecation. The JS name is unaffected: it lives on the shim, which calls the renamed method.

§Example

ⓘ
#[wasm_export(js_class = IpcClient)]
impl JsIpcClient {
    pub async fn send(&self, message: OutgoingMessage) -> Result<(), SendError> { ... }

    // Exported to JavaScript, but struck through for Rust — use `IpcClient::start` instead.
    #[wasm_only(note = "Use `IpcClient::start`.")]
    pub async fn start(&self) -> Result<(), AlreadyRunningError> { ... }
}