In this example we'll be using a simple Wasm module which exports some globals.
Globals are probably the simplest entity we'll encounter in Wasm modules but there is still some interesting things to talk about. For example, globals come in two flavors:
Immutable globals (const)
Mutable globals (var)
We will cover both 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 .
Please take a look at the.
cargo new exports-global
cd exports-global
We have to modify Cargo.toml to add the Wasmer dependencies as shown below:
[dependencies]
# The Wasmer API
wasmer = "1.0"
The final code for this example can be found on .
Please take a look at the.
mkdir wasmer-example-imports-exports
cd wasmer-example-imports-exports
go mod init github.com/$USER/wasmer-example-imports-exports
The final code for this example can be found on .
Please take a look at the.
mkdir wasmer-example-imports-exports
cd wasmer-example-imports-exports
pip install wasmer
pip install wasmer_compiler_cranelift
mkdir wasmer-example-exports-global
cd wasmer-example-exports-global
vim Makefile
Now that we have everything set up, let's go ahead and try it out!
Querying types information
The first interesting thing to do is to query their type information in order to know if they are mutable or not. Our module exports two globals, one and some. Which one is mutable and which one is not?
let one = instance.exports.get_global("one")?;
let some = instance.exports.get_global("some")?;
let one_type = one.ty();
let some_type = some.ty();
println!("one type: {:?} {:?}", one_type.mutability, one_type.ty);
println!("some type: {:?} {:?}", some_type.mutability, some_type.ty);
one, err := instance.Exports.GetGlobal("one")
if err != nil {
panic(fmt.Sprintln("Failed to retrieve the `one` global:", err))
}
some, err := instance.Exports.GetGlobal("some")
if err != nil {
panic(fmt.Sprintln("Failed to retrieve the `some` global:", err))
}
oneType := one.Type()
someType := some.Type()
fmt.Printf(
"`one` type: %s %s\n",
oneType.Mutability(),
oneType.ValueType().Kind().String()
)
fmt.Printf(
"`some` type: %s %s\n",
someType.Mutability(),
someType.ValueType().Kind().String()
)
The global API is straightforward: it provides a dedicated method to get the value of a given global. Look how easy it is:
let some_value = some.get();
println!("`some` value: {:?}", some_value);
someValue, err := some.Get()
if err != nil {
panic(fmt.Sprintln("Failed to get the `some` global value:", err))
}
fmt.Printf("`some` value: %.1f\n", someValue)
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...
Getting globals types information...
`one` type: Const F32
`some` type: Var F32
Getting global values...
`one` value: 1.0
`some` value: F32(0.0)
Setting global values...
`one` value after `set`: F32(1.0)
`some` value after `set_some`: F32(21.0)
`some` value after `set`: F32(42.0)
git clone https://github.com/wasmerio/wasmer.git
cd wasmer
cargo run --example exported-function --release --features "cranelift"
You should be able to run it using the go run main.go command. The output should look like this:
Compiling module...
Instantiating module...
Getting globals types information...
`one` type: const f32
`some` type: var f32
Getting global values...
`one` value: 1.0
`some` value: 0.0
Setting global values...
`one` value: 1.0
`some` value after `set_some`: 21.0
`some` value after `set`: 42.0
git clone https://github.com/wasmerio/wasmer-go.git
cd wasmer-go
go test examples/example_exports_global_test.go
You should be able to run it using the python main.py command.
git clone https://github.com/wasmerio/wasmer-python.git
cd wasmer-python
just prelude
source .env/bin/activate
just build-all $target
python examples/exports_global.py
You should be able to run it using the make clean exports-global && ./exports-global command. The output should look like this:
Creating the store...
Compiling module...
Creating imports...
Instantiating module...
Retrieving exports...
Getting globals types information...
`one` type: const 2
`some` type: 2
Getting global values...`one` value: 1.0
`some` value: 0.0
Setting global values...
Attempted to set an immutable global: `RuntimeError: Attempted to set an immutable global`
`some` value: 0.0
git clone https://github.com/wasmerio/wasmer.git
cd wasmer/lib/c-api/examples/exports-global.c
make clean exports-global
./exports-global
The final code for this example can be found on .
Please take a look at the.
If you want to run the examples from the Wasmer codebase directly, you can also do:
If you want to run the examples from the Wasmer codebase directly, you can also do:
If you want to run the examples from the Wasmer codebase directly, you can also do:
If you want to run the examples from the Wasmer codebase directly, you can also do: