| Copyright | (c) 2026 Tushar Adhatrao |
|---|---|
| License | MIT |
| Stability | experimental |
| Safe Haskell | None |
| Language | GHC2021 |
WasmEdge.Memory.Alloc
Description
Provides a high-level API for calling WebAssembly functions that accept or return heap-allocated data. This module implements the standard __guest-side allocation__ convention:
- The Wasm guest module exports:
*
alloc(size: i32) -> i32— allocatesizebytes, return pointer *dealloc(ptr: i32, len: i32)— free a previously allocated buffer - The Haskell host serialises the input with
wasmPoke, calls the Wasm function with(ptr, len), and deserialises the output.
This convention is compatible with the allocator produced by Rust's
#[no_mangle] pub fn alloc(size: usize) -> *mut u8 pattern and with
the wasmedge-sdk Rust examples in this repository.
Quick start
-- Register the module first: registerModuleFromFile vm "my_module" "my_module.wasm" >>= throwOnFail -- Then call a function that takes Text (ptr, len) and returns Text (ptr, len): result <- withWasmBuffer vm "my_module" "greet" defaultConvention (Hello :: Text) case result of Left err -> print err Right txt -> putStrLn txt
See withWasmBuffer2 for two-argument variants.
Return value encoding
The Wasm function must return a (ptr: i32, len: i32) pair. We support two
common encodings:
PackedI64— packed into a singlei64:ptr << 32 | len. This is allocation-free on the guest side.SeparateI32s— two separatei32return values.
Synopsis
- withWasmBuffer :: (WasmMemory a, WasmMemory b) => VM -> ModuleName -> FunctionName -> AllocConvention -> a -> IO (WasmResult b)
- withWasmBuffer2 :: (WasmMemory a, WasmMemory b, WasmMemory c) => VM -> ModuleName -> FunctionName -> AllocConvention -> a -> b -> IO (WasmResult c)
- wasmAlloc :: VM -> ModuleName -> AllocConvention -> Word32 -> IO (WasmResult Word32)
- wasmDealloc :: VM -> ModuleName -> AllocConvention -> Word32 -> Word32 -> IO (WasmResult ())
- data AllocConvention = AllocConvention {}
- data ResultEncoding
- defaultConvention :: AllocConvention
- type ModuleName = Text
- type FunctionName = Text
High-level buffer call (single argument)
Arguments
| :: (WasmMemory a, WasmMemory b) | |
| => VM | |
| -> ModuleName | |
| -> FunctionName | |
| -> AllocConvention | |
| -> a | Input value to serialise |
| -> IO (WasmResult b) |
The primary high-level API for calling a Wasm function that takes a serialised buffer and returns a serialised buffer.
The Wasm function must have the signature (depending on ResultEncoding):
PackedI64:(ptr: i32, len: i32) -> i64SeparateI32s:(ptr: i32, len: i32) -> (i32, i32)
Internally, this bracket:
- Allocates a scratch buffer in Wasm linear memory via the
allocexport. - Serialises the input
ainto that buffer usingwasmPoke. - Calls the Wasm function with
(inputPtr, inputLen). - Decodes the return value as
(retPtr, retLen). - Reads and deserialises
bfrom Wasm memory at(retPtr, retLen). - Deallocates both the input and output buffers via
dealloc.
Returns Left if any step fails. Deallocation is attempted on all error
paths.
High-level buffer call (two arguments)
withWasmBuffer2 :: (WasmMemory a, WasmMemory b, WasmMemory c) => VM -> ModuleName -> FunctionName -> AllocConvention -> a -> b -> IO (WasmResult c) #
Like withWasmBuffer but for Wasm functions that take two buffer
arguments.
Expected Wasm signature ():PackedI64
(aPtr: i32, aLen: i32, bPtr: i32, bLen: i32) -> i64
Manual allocation primitives
Arguments
| :: VM | |
| -> ModuleName | |
| -> AllocConvention | |
| -> Word32 | Number of bytes to allocate |
| -> IO (WasmResult Word32) |
Arguments
| :: VM | |
| -> ModuleName | |
| -> AllocConvention | |
| -> Word32 | Pointer returned by |
| -> Word32 | Length in bytes |
| -> IO (WasmResult ()) |
Free a previously-allocated Wasm buffer by calling the module-exported
dealloc function.
Convention configuration
data AllocConvention #
Configuration for the guest allocator naming convention.
Constructors
| AllocConvention | |
Fields
| |
data ResultEncoding #
How a Wasm function encodes its buffer result.
Most idiomatic Rust guest code uses PackedI64. wasm-bindgen uses
SeparateI32s.
Constructors
| PackedI64 |
|
| SeparateI32s | Two separate |
defaultConvention :: AllocConvention #
The default convention: alloc, dealloc, packed i64 result, 64 KiB scratch.
Type aliases
type ModuleName = Text #
Name of the registered Wasm module.
type FunctionName = Text #
Name of the exported Wasm function.