🔁Imports & exports

A Wasm module can import and export entities, like functions, memories, globals and tables. This example illustrates the basics of using these entities.

In this example we'll be using a sample Wasm module which exports some entities and requires us to also import some of them.

The goal here is to give you an idea of how to work with imports and exports. We won't go into the details of each entities, they'll be covered in more details in the other examples.

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 imports-exports
cd imports-exports

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

[dependencies]
# The Wasmer API
wasmer = "1.0"

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

Declaring imports

When a Wasm modules declares imports you will have to make them available before you can instantiate the module. Our first task will be to create the required entities.

The module we are using needs two imports:

  • A function named host_function in a namespace with an empty name;

  • A global named host_global in the env namespace.

Let's create the import object:

let import_object = imports! {
    "" => {
        "host_function" => host_function,
    },
    "env" => {
        "host_global" => host_global,
    },
};

Now that we have our import object ready, we'll need to use it when instantiating the module:

let instance = Instance::new(&module, &import_object)?;

That's it! Easy right?

We did not go into the details of how to create the imported entities, we encourage you to read other examples to know more about this:

↩ī¸ Exposing host (imported) functions

Fetching exports

Let's have a look at our module again: it exports some entities for us in our host program:

  • A function named guest_function;

  • A global name guest_global;

  • A table name guest_table;

  • A memory named guest_memory.

To get these entities we'll use the exports API:

let function = instance.exports.get::<Function>("guest_function")?;

let global = instance.exports.get::<Global>("guest_global")?;

let memory = instance.exports.get::<Memory>("guest_memory")?;

let table = instance.exports.get::<Table>("guest_table")?;

Again, we'll not cover how to use these entities here as this is the topic of other, more detailed, examples:

â†Ēī¸ Calling guest (exported) functions↩ī¸ Exposing host (imported) functions✏ī¸ Using guest (exported) globals💾Interacting with memory

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...
Creating the imported function...
Creating the imported global...
Instantiating module...
Getting the exported function...
Got exported function of type: FunctionType { params: [], results: [I32] }
Getting the exported global...
Got exported global of type: GlobalType { ty: I32, mutability: Const }
Getting the exported memory...
Got exported memory of type: MemoryType { minimum: 1 pages, maximum: None, shared: false }
Getting the exported table...
Got exported table of type: TableType { ty: FuncRef, minimum: 1, maximum: Some(1) }

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 imports-exports --release --features "cranelift"

Last updated