Hasty Briefsbeta

Bilingual

Notes on Writing WASM

3 days ago
  • #Rust
  • #wasm-bindgen
  • #WebAssembly
  • The author shares patterns for improving Rust-based Wasm development with wasm-bindgen.
  • Key recommendations include passing everything by reference, using Rc<RefCell<T>> or Arc<Mutex<T>>, and avoiding Copy on exported types.
  • Prefixing Rust-exported types with 'Wasm*' and JS-imported types with 'Js*' helps in distinguishing them.
  • Avoid deriving Copy on exported types to prevent accidental duplication of handles.
  • Passing by reference is preferred to avoid broken handles on the JS side.
  • Using wasm_refgen for types crossing the boundary in collections like Vec is recommended.
  • Convert Rust errors to JsValue using js_sys::Error for better error handling on the JS side.
  • Printing build info (version, Git hash) on startup aids in debugging and development.