Paul Krill
Editor at Large

Rust and JavaScript interoperability coming via WebAssembly

news
Apr 9, 20182 mins

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

Computerworld - Scary Tech [Slide-12] - Language translation systems run by big brother
Credit: Thinkstock

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.

Paul Krill

Paul Krill is editor at large at InfoWorld. Paul has been covering computer technology as a news and feature reporter for more than 35 years, including 30 years at InfoWorld. He has specialized in coverage of software development tools and technologies since the 1990s, and he continues to lead InfoWorld’s news coverage of software development platforms including Java and .NET and programming languages including JavaScript, TypeScript, PHP, Python, Ruby, Rust, and Go. Long trusted as a reporter who prioritizes accuracy, integrity, and the best interests of readers, Paul is sought out by technology companies and industry organizations who want to reach InfoWorld’s audience of software developers and other information technology professionals. Paul has won a “Best Technology News Coverage” award from IDG.

More from this author