talira

wasm-bindgen

Generates the glue between Rust WebAssembly modules and JavaScript

open sourceMIT OR Apache-2.0free

wasm-bindgen lets Rust compiled to WebAssembly call JavaScript and the browser's APIs, and lets JavaScript call exported Rust functions and types. A #[wasm_bindgen] attribute marks what to import and export, and wasm-bindgen-cli generates the JavaScript glue and an ES module for the .wasm file, including only the bindings you use. Companion crates give typed bindings to JavaScript's built-ins (js-sys) and to Web APIs such as the DOM (web-sys), bridge Rust futures and JavaScript promises (wasm-bindgen-futures), and run tests in browsers or Node.js (wasm-bindgen-test).

Ways to install

Problems it solves

  • call the DOM from Rust compiled to WebAssembly
  • export a Rust function so my JavaScript app can import it from a .wasm file
  • run Rust code in the browser and talk to JavaScript

Features

  • Import JavaScript functions and export Rust functions and types with #[wasm_bindgen]
  • Generates JavaScript glue and an ES module, only for the bindings you use
  • Typed bindings to JavaScript built-ins (js-sys) and Web APIs (web-sys)
  • Rust futures and JavaScript promises interoperate (wasm-bindgen-futures)
  • Tests in headless browsers and Node.js (wasm-bindgen-test)

Limitations

  • The CLI needs a newer Rust (1.86) than the libraries (1.81), and the minimum can change in patch releases2

Alternatives

The same job

These do what wasm-bindgen does.

  1. napi-rs A framework for building compiled Node.js add-ons in Rust via Node-API

Compare wasm-bindgen with the top 3