Memory Manipulation
In this example, we'll present how to manipulate the linear memory with the APIs defined in wasmedge_sdk::Memory.
The code in the following example is verified on
- wasmedge-sdk v0.5.0
- wasmedge-sys v0.10.0
- wasmedge-types v0.3.0
Wasm module
Before talking about the code, let's first see the wasm module we use in this example. In the wasm module, a linear memory of 1-page (64KiB) size is defined; in addition, three functions are exported from this module: get_at
, set_at
, and mem_size
.
(module
(type $mem_size_t (func (result i32)))
(type $get_at_t (func (param i32) (result i32)))
(type $set_at_t (func (param i32) (param i32)))
# A memory with initial size of 1 page
(memory $mem 1)
(func $get_at (type $get_at_t) (param $idx i32) (result i32)
(i32.load (local.get $idx)))
(func $set_at (type $set_at_t) (param $idx i32) (param $val i32)
(i32.store (local.get $idx) (local.get $val)))
(func $mem_size (type $mem_size_t) (result i32)
(memory.size))
# Exported functions
(export "get_at" (func $get_at))
(export "set_at" (func $set_at))
(export "mem_size" (func $mem_size))
(export "memory" (memory $mem)))
Next, we'll demonstrate how to manipulate the linear memory by calling the exported functions.
Load and Register Module
Let's start off by getting all imports right away so you can follow along
To load a Module
, wasmedge-sdk
defines two methods:
-
from_file loads a wasm module from a file, and meanwhile, validates the loaded wasm module.
-
from_bytes loads a wasm module from an array of in-memory bytes, and meanwhile, validates the loaded wasm module.
Here we use Module::from_bytes
method to load our wasm module from an array of in-memory bytes.
The module returned by Module::from_bytes
is a compiled module, also called AST Module in WasmEdge terminology. To use it in WasmEdge runtime environment, we need to instantiate the AST module. We use Store::register_named_module API to achieve the goal.
In the code above, we register the AST module into a Store
, in which the module is instantiated, and as a result, a module instance named extern
is returned.
Memory
In the previous section, we get an instance by registering a compiled module into the runtime environment. Now we retrieve the memory instance from the module instance, and make use of the APIs defined in Memory to manipulate the linear memory.
The comments in the code explain the meaning of the code sample above, so we don't describe more.
The complete code of this example can be found in memory.rs.