Rust and JavaScript interoperability coming via WebAssembly

Mozillas’s goal is to make Rust a web language like JavaScript, for use alongside WebAssembly and JavaScript in web apps

Mozilla is working on a bridge between types in JavaScript and the Rust language, called wasm-bindgen, that builds on the WebAssembly portable code format.

The wasm-bindgen effort is meant to improve interoperability between JavaScript and Rust and to make Rust a web language like JavaScript, via WebAssembly. Wasm-bindgen provides high-level interactions between WebAssembly (wasm) modules and JavaScript. With wasm-bindgen, JavaScript and WebAssembly could communicate with strings, JavaScript objects, and classes as opposed to just integers and floats.

The high-level functions of wasm-bindgen include:

  • Import of JavaScript structs, functions, and objects to call in WebAssembly. Developers can call JavaScript methods on a struct and access properties, providing a native feel to Rust code once annotations are connected.
  • Export of Rust structures and functions to JavaScript. A Rust struct can be exported, which turns into a JavaScript class. Structs can then be passed around instead of just integers.

Currently, WebAssembly supports only four types: two integer types and two floating-point types. But JavaScript and Rust developers are often working with richer types, such as Rust developers working with Result for error handling. With wasm-bindgen, JavaScript could call a Rust API with a string or a Rust function to catch a JavaScript exception. By eliminating the mismatch between WebAssembly and JavaScript, JavaScript could invoke WebAssembly functions and WebAssembly could do the same with JavaScript functions.

Built on ECMAScript modules, wasm-bindgen is focused on Rust. But the intent is for it to also eventually be used for languages such as C and C++, too. But Mozilla says wasm-bindgen is not yet stableenough to expand to other languages.

A big goal for the Rust community is for the language to become a web language. By targeting WebAssembly, Rust could become a language that could run on the web just like JavaScript. But Mozilla notes it is not trying to replace JavaScript with Rust.  The company does not expect Rust WebAssembly apps to be completely written in Rust; instead, the bulk of application code would still be in JavaScript. Rust and WebAssembly would be used for specific parts of an application, such as parsing source maps or figuring out what changes to make to the DOM.

Where to download wasm-bindgen

You can download wasm-bindgen from GitHub.

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

Tags Rust

More about BuiltMozilla

Show Comments
[]