| Copyright | (c) 2026 Tushar Adhatrao |
|---|---|
| License | MIT |
| Stability | stable |
| Safe Haskell | None |
| Language | GHC2021 |
WasmEdge.Caller
Description
Inspired by the Caller abstraction in wasmedge-sdk (Rust), this module
provides safe, idiomatic Haskell helpers for host functions to inspect and
manipulate the linear memory of the calling WebAssembly guest instance.
When a WebAssembly module invokes an imported host function, WasmEdge supplies
a CallingFrame. Using getCallerMemory, readCallerMemory, or writeCallerMemory,
host functions can easily exchange structured data, buffers, and strings with the guest.
Example
import WasmEdge.Caller
import qualified Data.ByteString as BS
-- Host function reading a string from guest memory and logging it
logMessageHostFunc :: CallingFrame -> [WasmVal] -> IO (WasmResult [WasmVal])
logMessageHostFunc frame [WasmI32 ptr, WasmI32 len] = do
mStr <- readCallerMemory frame 0 (fromIntegral ptr) (fromIntegral len)
case mStr of
Nothing -> pure (Left (WasmEdgeError ErrCategory_UserDefined 1 "Invalid memory access"))
Just bs -> do
putStrLn $ "Guest message: " ++ show bs
pure (Right [])
logMessageHostFunc _ _ = pure (Left (WasmEdgeError ErrCategory_UserDefined 2 "Invalid arguments"))
Synopsis
- getCallerMemory :: CallingFrame -> Word32 -> IO (Maybe MemoryInstance)
- readCallerMemory :: CallingFrame -> Word32 -> Word32 -> Word32 -> IO (Maybe ByteString)
- writeCallerMemory :: CallingFrame -> Word32 -> Word32 -> ByteString -> IO (WasmResult ())
- readCallerCString :: CallingFrame -> Word32 -> Word32 -> IO (Maybe ByteString)
- readCallerCStringMax :: CallingFrame -> Word32 -> Word32 -> Word32 -> IO (Maybe ByteString)
- getCallerModuleInstance :: CallingFrame -> IO (Ptr ModuleInstanceTag)
- getCallerExecutor :: CallingFrame -> IO (Ptr ExecutorTag)
Caller memory access
getCallerMemory :: CallingFrame -> Word32 -> IO (Maybe MemoryInstance) #
Borrow the MemoryInstance at the given index from the caller module.
In WebAssembly, index 0 is the default linear memory.
Returns Nothing if the caller module has no memory at that index or if the
calling frame is invalid.
Arguments
| :: CallingFrame | Calling frame passed to host function |
| -> Word32 | Memory index (typically 0) |
| -> Word32 | Byte offset in memory |
| -> Word32 | Number of bytes to read |
| -> IO (Maybe ByteString) |
Read length bytes from the caller's linear memory at index memIdx starting at offset.
Returns Nothing if the memory instance cannot be found or the range is out of bounds.
Arguments
| :: CallingFrame | Calling frame passed to host function |
| -> Word32 | Memory index (typically 0) |
| -> Word32 | Byte offset in memory |
| -> ByteString | Bytes to write |
| -> IO (WasmResult ()) |
Write a ByteString into the caller's linear memory at index memIdx starting at offset.
Arguments
| :: CallingFrame | Calling frame passed to host function |
| -> Word32 | Memory index (typically 0) |
| -> Word32 | Byte offset in memory |
| -> IO (Maybe ByteString) |
Read a null-terminated UTF-8 / ASCII string from caller memory starting at offset,
scanning up to 4096 bytes.
Arguments
| :: CallingFrame | Calling frame |
| -> Word32 | Memory index (typically 0) |
| -> Word32 | Byte offset in memory |
| -> Word32 | Maximum bytes to scan |
| -> IO (Maybe ByteString) |
Read a null-terminated string from caller memory with an explicit upper bound on bytes scanned.
Caller execution context
getCallerModuleInstance :: CallingFrame -> IO (Ptr ModuleInstanceTag) #
Retrieve the raw module instance pointer of the current calling frame.
getCallerExecutor :: CallingFrame -> IO (Ptr ExecutorTag) #
Retrieve the raw executor pointer from the current calling frame.