Calling guest functions

A Wasm module can export entities, like functions, memories, globals and tables. This example illustrates how to call exported functions.

In this example we'll see how to use exported functions.

Exported function are the entities you will probably use the most: they will be your entrypoint to calling Wasm module logic.

Exported function come in two flavors:

  • Dynamic functions;

  • Native functions.

We'll cover both flavors in this example.

First we are going to want to initialize a new project. To do this we can navigate to our project folder, or create one. In this example, we will create a new project. Lets create it and navigate to it:

The final code for this example can be found on GitHub.

Please take a look at the setup steps for Rust.

cargo new exports-function
cd exports-function

This should generate two important files for us, Cargo.toml and src/main.rs. The Cargo.toml is a file that describes your project and its dependencies. The src/main.rs is the entry point for your project, and contains the fn main() { .. } that is run when the project is executed.

We then modify the Cargo.toml to add the Wasmer dependencies as shown below:

[package]
name = "exports-function"
version = "0.1.0"
authors = ["The Wasmer Engineering Team <engineering@wasmer.io>"]
edition = "2018"

[dependencies]
# The Wasmer API
wasmer = "1.0.0-alpha5"

Now that we have everything set up, let's go ahead and try it out!

Using the dynamic flavor

We'll start by fetching the guest function and see how to call it using the dynamic flavor. Our Wasm module exports a sum function, let's get and call it:

let sum = instance.exports.get_function("sum")?;
let args = [Value::I32(1), Value::I32(2)];
let result: Box<[Val]> = sum.call(&args)?;

Easy right?

Both example look nice but it does not seem like we are using standard functions. In fact, we are calling an external entity. With the native flavor we can get something that feels more like we are using functions as if they were provided by the host directly.

Let's have a look at this.

Using the native flavor

Let's continue with our previous sum function and see how we can make interacting with it better. To do so, we'll be using the native flavor. With this flavor, passing arguments and getting result will feel more natural.

To use this flavor, we have the choice of fetching the function again or transforming the one we already have into a native function:

let sum = sum.native::<(i32, i32), i32>()?;
let result: i32 = sum.call(3, 4)?;

Here we reused the previously fetched function and turned it into a native one. We could have directly fetched it as a native function:

let sum = instance
    .exports
    .get_native_function::<(i32, i32), i32>("sum")?;

Running

We now have everything we need to run the Wasm module, let's do it!

You should be able to run it using the cargo run command. The output should look like this:

Compiling module...
Instantiating module...
Calling `sum` function...
Results: [I32(3)]
Calling `sum` function (natively)...
Results: 7

If you want to run the examples from the Wasmer repository codebase directly, you can also do:

git clone https://github.com/wasmerio/wasmer.git
cd wasmer
cargo run --example exported-function --release --features "cranelift"

Last updated