30

Embedding WebAssembly in your Rust application Wasmer

 5 years ago
source link: https://www.tuicool.com/articles/hit/Q3iIziQ
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.

IZzEjiy.png!webRV3I32J.png!web

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.

VvAFNz7.jpg!web

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.

resize?s=400&v=4&width=40

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!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK