Embedding WebAssembly in your Rust application

Brandon Fish
Wasmer
Published in
2 min readJan 24, 2019

Co-authored by Lachlan Sneff

Wasmer is a WebAssembly runtime designed to run both standalone and embedded.

The crate wasmer-runtime exposes an easy to use and safe api for compiling, creating imports, and calling WebAssembly from your own library.

This tutorial goes over how to make a simple wasm application and run it using the wasmer-runtime!

Creating a Wasm Application

Our sample application is just a basic “Hello, World!” program. Because WebAssembly doesn’t inherently have any way to print to the command line, we have to import such a function from our environment.

In this case, we define an imported print_str function.

When the hello_wasm function is called by the embedder, it will proceed to call the imported print_str function with a pointer to the “Hello, World!” string and its length as arguments.

Embedding WebAssembly using Wasmer!

This example shows to use use the wasmer-runtime crate to compile and run WebAssembly!

Since our Wasm application imported a function print_str, we must create an ImportObject containing the implementation of that function.

While you can manually (and unsafely) fill in the import object yourself, we’ve defined a useful macro that does function signature checking automatically to make it easier and safer.

This example is hosted on Github. Check out the Readme for instructions on how to try it out for yourself!

If you have feedback, issues, or feature requests please open an issue in our Github repository.

We look forward to what cool things you build with WebAssembly and our new embeddable runtime!

--

--