2 min read

Yesterday, the Rust and WebAssembly team introduced a new crate called web-sys, which provides raw bindings to all the Web APIs. The web-sys crate enables the interaction with all the standard web platform methods.

Why use web-sys crate?

Web includes many APIs and adding support for all of them manually can prove to be a very hectic task. These APIs are standardized with the same interface definition language (Web IDL). The developers came up with a new Web IDL frontend to wasm-bindgen.

When the frontend was ready, they took the interface definitions for all the Web APIs from all their standards to automatically generate a -sys crate from them, known as web-sys. Bringing all these APIs at one place will make it easy for people to write neat libraries and other utility crates for the Web.

The wasm-bindgen architecture

The new wasm-bindgen architecture is shown below:

Source: GitHub

A frontend named wasm-bindgen-webidl is added to wasm-bindgen, which takes in Web IDL interface definitions and emits the internal Abstract Syntax Tree (AST) of wasm-bindgen.

Once we have the AST, emitting zero-overhead Rust and JavaScript glue code to do type conversion and shepherding of function parameters is the same as normal #[wasm_bindgen] annotations.

Using this architecture makes wasm-bindgen future-compatible with the host bindings proposal. This proposal focuses on making WebAssembly to directly call into native DOM methods without going through a JavaScript shim. This will provide better performance since calls from wasm can be statically validated to type check once at compilation time, rather than dynamically on every single call.

To know more about the web-sys crate, check out the official announcement at GitHub.

Read Next

Introducing Wasmjit: A kernel mode WebAssembly runtime for Linux

Why is everyone going crazy over WebAssembly?

Next.js 7, a framework for server-rendered React applications, releases with support for React context API and Webassembly


Subscribe to the weekly Packt Hub newsletter. We'll send you the results of our AI Now Survey, featuring data and insights from across the tech landscape.

* indicates required