| Copyright | (c) 2026 Tushar Adhatrao |
|---|---|
| License | MIT |
| Stability | experimental |
| Safe Haskell | None |
| Language | GHC2021 |
WasmEdge.Bindgen
Description
Inspired by wasmedge-bindgen in the Go and Rust SDKs, this module provides
high-level, type-safe abstractions for invoking WebAssembly guest functions
that accept and return complex data (JSON, UTF-8 strings, and raw ByteStrings)
without requiring manual pointer arithmetic or allocation management.
Calling Guest Functions
import WasmEdge
import WasmEdge.Bindgen
import Data.Aeson (FromJSON, ToJSON)
data MyRequest = MyRequest { query :: Text } deriving (Generic, ToJSON)
data MyResponse = MyResponse { hits :: [Text] } deriving (Generic, FromJSON)
-- Single-line guest invocation:
response <- callGuestJson vm "search_module" "execute_search" (MyRequest "example")
case response of
Left err -> putStrLn $ "Error: " ++ show err
Right res -> print (hits res)
Reading and Writing from Host Functions
When a host function is invoked by the guest, it receives a CallingFrame.
This module provides helpers to read and write strings and JSON directly:
myHostLog :: CallingFrame -> [WasmVal] -> IO (WasmResult [WasmVal])
myHostLog frame [WasmI32 ptr, WasmI32 len] = do
res <- callerReadText frame (fromIntegral ptr) (fromIntegral len)
case res of
Left err -> pure (Left err)
Right msg -> do
putStrLn $ "Guest log: " ++ show msg
pure (Right [])
Synopsis
- callGuestJson :: (ToJSON a, FromJSON b) => VM -> Text -> Text -> a -> IO (Either WasmEdgeError b)
- callGuestJson2 :: (ToJSON a, ToJSON b, FromJSON r) => VM -> Text -> Text -> a -> b -> IO (Either WasmEdgeError r)
- callGuestBytes :: VM -> Text -> Text -> ByteString -> IO (Either WasmEdgeError ByteString)
- callGuestText :: VM -> Text -> Text -> Text -> IO (Either WasmEdgeError Text)
- callGuest :: (WasmMemory a, WasmMemory b) => VM -> Text -> Text -> a -> IO (Either WasmEdgeError b)
- callerReadText :: CallingFrame -> Word32 -> Word32 -> IO (Either WasmEdgeError Text)
- callerWriteText :: CallingFrame -> Word32 -> Text -> IO (Either WasmEdgeError Word32)
- callerReadJson :: FromJSON a => CallingFrame -> Word32 -> Word32 -> IO (Either WasmEdgeError a)
- callerWriteJson :: ToJSON a => CallingFrame -> Word32 -> a -> IO (Either WasmEdgeError Word32)
High-Level Guest Invocation
callGuestJson :: (ToJSON a, FromJSON b) => VM -> Text -> Text -> a -> IO (Either WasmEdgeError b) #
Call a guest function with a JSON serializable value, returning a JSON decoded value.
The guest function must follow the standard allocation convention (alloc,
dealloc) and accept (ptr, len) and return (ptr, len) packed into an i64
or separate i32s.
callGuestJson2 :: (ToJSON a, ToJSON b, FromJSON r) => VM -> Text -> Text -> a -> b -> IO (Either WasmEdgeError r) #
Call a guest function with two JSON serializable values, returning a JSON decoded value.
callGuestBytes :: VM -> Text -> Text -> ByteString -> IO (Either WasmEdgeError ByteString) #
Call a guest function with a raw ByteString, returning a ByteString.
callGuestText :: VM -> Text -> Text -> Text -> IO (Either WasmEdgeError Text) #
callGuest :: (WasmMemory a, WasmMemory b) => VM -> Text -> Text -> a -> IO (Either WasmEdgeError b) #
Call a guest function with any type implementing WasmMemory.
Host Function CallingFrame Helpers
callerReadText :: CallingFrame -> Word32 -> Word32 -> IO (Either WasmEdgeError Text) #
Read a UTF-8 string from the calling guest's linear memory at the specified offset and length.
callerWriteText :: CallingFrame -> Word32 -> Text -> IO (Either WasmEdgeError Word32) #
Write a UTF-8 string into the calling guest's linear memory at the specified offset. Returns the number of bytes written.
callerReadJson :: FromJSON a => CallingFrame -> Word32 -> Word32 -> IO (Either WasmEdgeError a) #
Read and decode a JSON value from the calling guest's linear memory.
callerWriteJson :: ToJSON a => CallingFrame -> Word32 -> a -> IO (Either WasmEdgeError Word32) #
Encode and write a JSON value into the calling guest's linear memory at the specified offset. Returns the number of bytes written.