Search code examples
rustwebassemblywasm-bindgen

How does wasm-bindgen determine which bindings to generate?


When I run wasm-bindgen --out-dir out some_binary.wasm wasm-bindgen is able to figure out which functions have been annotated with #[wasm_bindgen] and to generate appropriate bindings for them so that they can be called from Javascript. How does this work? Is a special section of the WASM binary being used to hold this auxiliary information?


Solution

  • The answer to this question is in the Communicating types to wasm-bindgen section of the wasm-bindgen guide.

    In a nutshell, the #[wasm_bindgen] macro generates executable functions that describe the necessary bindings in Javascript inside some_binary.wasm. These functions are then executed by the wasm-bindgen CLI program to generate the Javascript bindings and a stripped WebAssembly module, i.e., some_binary_bg.wasm.