Memories, tables and globals
editThe bytes of a memory are a .NET array: read and write them with GetSpan or Data.
using var runtime = new WasmRuntime();
var module = runtime.Compile(WasmRuntime.WatToWasm("""
(module
(memory (export "memory") 1 4)
(data (i32.const 0) "Hello")
(func (export "grow") (result i32) (memory.grow (i32.const 1))))
"""));
using var store = runtime.CreateStore();
var instance = store.Instantiate(module);
var memory = instance.GetMemory("memory")!;
Assert.Equal("Hello", Encoding.ASCII.GetString(memory.GetSpan()[..5]));
// The bytes of a memory are a .NET array, replaced when the memory grows
var changes = 0;
memory.DataChanged += (sender, e) => changes++;
instance.GetFunction("grow")!.Call();
Assert.Equal(2UL, memory.Size); // pages of 64 KiB
Assert.HasCount(2 * 65536, memory.Data);
Assert.Equal(1, changes);- The sizes are in pages of 64 KiB (PageSize).
- The array is replaced when the memory grows, by Grow or by a
memory.growinstruction: do not keep it, or a span of it, across calls into WebAssembly. DataChanged is raised after it is replaced. - A memory cannot grow beyond its maximum size or MaxMemorySize:
memory.growreturns -1, and Grow throws a WasmAllocationException.
The bytes of a shared memory never move: see Shared memories.
Tables and globals
using var runtime = new WasmRuntime();
var module = runtime.Compile(WasmRuntime.WatToWasm("""
(module
(global (export "counter") (mut i32) (i32.const 0))
(table (export "callbacks") 1 funcref)
(func (export "increment")
(global.set 0 (i32.add (global.get 0) (i32.const 1)))))
"""));
using var store = runtime.CreateStore();
var instance = store.Instantiate(module);
var counter = instance.GetGlobal("counter")!;
var callbacks = instance.GetTable("callbacks")!;
// A table contains references: put the exported function in it
callbacks.Set(0, WasmValue.FromReference(instance.GetFunction("increment")));
var increment = (WasmFunction)callbacks.Get(0).Reference!;
increment.Call();
increment.Call();
Assert.Equal(2, counter.Get().I32);
counter.Set(WasmValue.FromI32(40));
Assert.Equal(40, counter.Get().I32);- A WasmTable contains references of its element type. An index out of its bounds throws a WasmAllocationException, and so does a growth beyond its maximum size or MaxTableElements.
- Setting an immutable WasmGlobal, or a value of another type, throws a WasmTypeMismatchException.