Rust WebAssembly Hello World — Deep Dive with wasm-bindgen and wasm2wat

nicole
nicole
Jul 2 · 8 min read

This post first appeared on WebAssemblyMan.com: Rust WebAssembly Hello World.

In this article, we are going to create a WebAssembly Hello World program with Rust. You might be thinking that a WebAssembly Hello World tutorial could be found almost everywhere, including one on the Mozilla website and rustwasm.github.io. So why would you need to read this article?

This article is different as we are going beyond just getting the Hello World program to work. We will dig deeper to understand the codes generated by wasm-bindgen and how they all come together to help us with our development. The WebAssembly Binary Toolkit will also be used to explore the generated wasm code. This will give us a good understanding of Rust WebAssembly and a good foundation to start with. It also quickly becomes clear why Rust WebAssembly applications are smaller and more optimized as compared to other frameworks with Garbage Collector, and why it is a winner for developers with existing JavaScript web applications or websites. Things like web-sys (Raw API bindings for Web APIS) and WASI (WebAssembly System Interface) also become easier to appreciate.

Prerequisites

Hello World Tutorial in Rust WebAssembly

1. Install wasm-bindgen. There are other ways to create a Hello World program without wasm-bindgen. But in this tutorial, we are going to use it as it is essential in Rust WebAssembly development.

cargo install wasm-bindgen-cli

Rust WebAssembly enables you to surgically insert WebAssembly modules into your existing JavaScript applications, especially into your performance critical code paths. You can think of wasm-bindgen as a tool that helps you do that by generating the glue and bindings for the efficient interactions between JavaScript and WebAssembly.

2. Next, we will like to create a new Rust WebAssembly project. Enter the following command

Please note the double dash before “lib”.

You should see the following message. A new helloworld package with your project files has been created.

Created library `helloworld` package

3. Using a text editor, open Cargo.toml. You should see the following

Change it to the following:

The “cdylib” indicator makes this Rust project expose a C-style dynamic library that is used by linkers to produce our WebAssembly module. As described above, wasm-bindgen enables or facilitates high-level interactions between wasm modules and JavaScript.

4. Using a text editor, open “src/lib.rs”. Change it to the following:

In the above code, wasm_bindgen allows us to import JavaScript things (‘alert’) into Rust and export Rust things (‘helloworld’) to JavaScript.

5. Compile the code. In your command line enter the following:

cargo build — target wasm32-unknown-unknown

Please note the double dash before “target”. If “wasm32-unknown-unknown target” is not installed, enter the following to install it.

rustup target add wasm32-unknown-unknown

6. Setup a web server to run our program.

We need a web server to test our WebAssembly “Hello World” program. We are going to use Webpack but you can also use another tool or web server that you are comfortable with. We need to create 3 files: index.js, package.json, and webpack.config.js. However, we will not be going into the details of these files as we are going to focus on the codes generated by wasm-bindgen.

index.js

package.json

webpack.config.js

You may require the following commands if some of the packages are not available on your system.

  • npm install webpack — save-dev
  • npm install webpack-cli — save-dev
  • npm install webpack-dev-server — save-dev
  • npm install html-webpack-plugin — save-dev
  • npm install @wasm-tool/wasm-pack-plugin — save-dev
  • npm install text-encoding — save-dev

Please note the double dash before “save-dev”.

7. Build the program by running the following command:

npm run build

8. Run the Hello World program

npm run serve

Navigate your browser to “localhost:8080” and see a “Hello World!” alert is displayed.

8. Now that we got the Hello World program running, we want to dig deeper. Go to the “pkg” folder and see that the following files are automatically generated for you during the build process.

  • “helloworld_bg.wasm”
  • “helloworld.js”
  • “helloworld.d.ts”
  • “package.json”

The files can also be generated manually by using the following wasm-bindgen cli command.

wasm-bindgen target/wasm32-unknown-unknown/debug/helloworld.wasm — out-dir ./pkg

Please note the double dash before “out-dir”.

9. The first thing we should do is to understand what happens when we run the Hello World program in a browser. The following shows the sequence of function calls (or more specifically the files containing the functions) that occur when we navigate point to “localhost:8080” in the browser.

index.js -> helloworld.js -> helloworld_bg.wasm

index.js

“index.js” imports a “helloworld.js” package and calls the “helloworld” function in the package.

helloworld.js

“helloworld.js” file is automatically generated by wasm-bindgen and contains the JavaScript glue for importing DOM and JavaScript functions into Rust. It also exposes an API on the generated WebAssembly functions to JavaScript.

Rust WebAssembly focuses on integrating WebAssembly with existing JavaScript applications. To do this, we need to pass different values, objects or structs from JavaScript in and out of WebAssembly functions. This is not easy as the different object types of two different systems need to be reconciled. To make matters worst, WebAssembly currently only supports integer and floating point numbers, not strings. This means you cannot simply pass a string into a WebAssembly function.

To pass a string into WebAssembly, you need to convert the string into numbers (note the TextEncoderAPI specified in webpack.config.js), put the numbers into the memory space of WebAssembly and finally return a pointer of the string to the WebAssembly function so that you can use it in JavaScript. At the end of it, you will need to free up the WebAssembly memory space used by the string.

If you look at the above JavaScript codes, this is exactly what has been executed automatically. The “helloworld” function first calls “passStringToWasm”. This function creates some memory space in WebAssembly, converts your string to numbers, writes the numbers to the memory space, and returns a pointer to the string. The pointer is then passed to “wasm.helloworld” to perform your JavaScript “alert”. Finally, “wasm.__wbindgen_free” frees up the memory. If you are just passing a simple string, you may be able to do this by yourself, but think of when you have more complex objects and structs, the effort quickly becomes non-trivial. This illustrates the importance of wasm-bindgen in Rust WebAssembly development.

10. In our previous step, we noted that a “helloworld.js” file is generated by wasm-bindgen, and the functions generated in this file call into our generated WebAssembly codes in “helloworld_bg.wasm”. Basically, “helloworld.js” acts as a bridge between other JavaScripts such as index.js to the generated “helloworld_bg.wasm” WebAssembly.

We can explore “helloworld_bg.wasm” further by entering the following command:

wasm2wat helloworld_bg.wasm > helloworld.txt

This command uses the WebAssemblyBinary Toolkit to translate WebAssembly to WebAssembly text format and saves it to a “helloworld.txt” file. Open “helloworld.txt” in a text editor and look for the “$helloworld” function. This is the generated WebAssembly function of our “helloworld” function that we have defined in “src/lib.rs”.

Look for the following line in “helloworld.txt”:

This line exports the “helloworld” wasm function for the host to call. We call this wasm function through “wasm.helloworld” in “helloworld.js”.
Next, look for the following line:

This corresponds to the following JavaScript function generated in “helloworld.js”

This is the part where wasm-bindgen provides a glue to help you use JavaScript functions or the DOM in WebAssembly. With this, we now understand how wasm-bindgen helps us facilitate interaction by importing JavaScript things and exporting WebAssembly things.

11. Finally, let’s take a look at the other files generated by wasm-bindgen.

“helloworld.d.ts”

This .d.ts file contains TypeScript type declarations for the JavaScript glue and is useful if your existing JavaScript application is using TypeScript. You can have your calls into WebAssembly functions type checked or have your IDE provide auto-completion. If you aren’t using TypeScript, you can safely ignore this file.

“package.json”

The package.json file contains metadata about the generated JavaScript and WebAssembly package. It automatically fills in all the npm dependencies from your Rust code and enables you to publish to npm.

What further understanding can we draw from the above?

Take a look at the following code again:

helloworld.js

The code to allocate and deallocate the memory is carried out for you. There is no need for a Garbage Collector or a full framework engine making WebAssembly applications or modules written in Rust small and optimized. Other languages that require a Garbage Collector will need to include wasm code for their underlying framework engine. So, no matter how optimized they are, their size will not be smaller than what Rust provides. This makes Rust WebAssembly a good choice if you need to integrate or inject small WebAssembly modules into JavaScript web applications.

Other things to note beyond the Hello World

web-sys

With wasm-bindgen, we can call a JavaScript function in Rust WebAssembly by using an “extern”. Remember the following from “src/lib.rs”:

The Web has a huge number of APIs, from DOM manipulation to WebGL to Web Audio. So if our Rust WebAssembly program grows, and we need to make many different calls to Web APIs, we will need to spend time writing a lot of “extern” code. web-sys serves to act as a frontend to wasm-bindgen to provide the raw bindings to all the Web’s APIs. This means if you use web-sys, you can save time by not having to write the “extern” code.

WASI (WebAssembly System Interface)

This is a standardization effort to push WebAssembly beyond the browser. To run WebAssembly on a machine or OS (Operating System) outside the browser, we may need to access system resources. This standardization specifies how you can access these system resources in a secure and yet portable way across different machine architectures. Currently, there are already two implementations, Wasmtime by Mozilla, and Lucet by Fastly. And we need to ask ourselves, will WASM + WASI become the next Docker?

Brought to you by WebAssemblyMan.com : Man page of WebAssembly.

nicole

Written by

nicole

Progressive Web Apps, Webassembly, React, Angular, Vue and very bad and horrible stories.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade