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.
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, },};
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")?;
function, err := instance.Exports.GetFunction("guest_function")if err !=nil {panic(fmt.Sprintln("Failed to get the exported function:", err))}global, err := instance.Exports.GetGlobal("guest_global")if err !=nil {panic(fmt.Sprintln("Failed to get the exported global:", err))}table, err := instance.Exports.GetTable("guest_table")if err !=nil {panic(fmt.Sprintln("Failed to get the exported table:", err))}memory, err := instance.Exports.GetMemory("guest_memory")if err !=nil {panic(fmt.Sprintln("Failed to get the exported memory:", err))}
wasm_func_t* func =wasm_extern_as_func(exports.data[0]);if (func ==NULL) {printf("> Failed to get the exported function!\n");return1;}wasm_global_t* global =wasm_extern_as_global(exports.data[1]);if (global ==NULL) {printf("> Failed to get the exported global!\n");return1;}wasm_table_t* table =wasm_extern_as_table(exports.data[2]);if (table ==NULL) {printf("> Failed to get the exported table!\n");return1;}wasm_memory_t* memory =wasm_extern_as_memory(exports.data[3]);if (memory ==NULL) {printf("> Failed to get the exported memory!\n");return1;}
Again, we'll not cover how to use these entities here as this is the topic of other, more detailed, examples:
You should be able to run it using the make clean imports-exports && ./imports-exports command. The output should look like this:
Creating the store...
Compiling module...
Creating the imported function...
Creating the imported global...
Instantiating module...
Retrieving exports...
Retrieving the exported function...
Got the exported function: 0x7f9317e05e00
Retrieving the exported global...
Got the exported global: 0x7f9317e05e90
Retrieving the exported table...
Got the exported table: 0x7f9317e05ec0
Retrieving the exported memory...
Got the exported memory: 0x7f9317e05ef0
If you want to run the examples from the Wasmer repository codebase directly, you can also do: