Skip to main content

wasm_import

Attribute Macro wasm_import 

Source
#[wasm_import]
Expand description

Declares a block of JavaScript imports, in place of #[wasm_bindgen] on an extern "C" block.

Arguments are forwarded to #[wasm_bindgen], and the attributes inside the block are left for it to read. Each type the block declares also gets the wire traits, so it can appear in a #[wasm_export] signature. Unlike the other macros, #[wasm_bindgen] is applied unconditionally: an extern block cannot compile without it, so the block’s own cfg decides.

A catch import takes Rust types for its by-value parameters. The import itself is renamed to a hidden __wasm_raw_* function taking each parameter’s <T as ToWasm>::Wire, and a wrapper with the declared signature converts the arguments and calls it; a failed conversion is the wrapper’s Err. An Ok type other than () or JsValue is received as its <T as FromWasm>::Wire and converted the same way. An import without catch has nowhere to report a failed conversion, so it, and any getter, setter, constructor, or variadic import, is passed through unchanged.

ⓘ
#[wasm_import]
extern "C" {
    #[wasm_bindgen(js_name = SharedUnlockDriver)]
    pub type RawJsSharedUnlockDriver;

    #[wasm_bindgen(method, catch)]
    async fn lock_user(this: &RawJsSharedUnlockDriver, user_id: UserId) -> Result<(), JsValue>;
}