Embedding WebAssembly in your Rust application

Brandon Fish
Jan 24, 2019 · 2 min read

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!

Wasmer

Universal WebAssembly runtime

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store