| Copyright | (c) 2026 Tushar Adhatrao |
|---|---|
| License | MIT |
| Stability | stable |
| Safe Haskell | None |
| Language | GHC2021 |
WasmEdge.Function
Description
Provides types and functions for WebAssembly function types and host function instances:
FunctionType— parameter and return type signaturesFunctionInstance— a callable function in WebAssembly or the hostHostFunc— raw low-level callback interface (CallingFrame -> [WasmVal] -> IO (WasmResult [WasmVal]))ToHostFunc— high-level typeclass allowing normal Haskell functions (e.g.Int32 -> Int32 -> IO Int32orCallingFrame -> Word32 -> IO ()) to be registered directly as host functions without manual value unpacking.CallingFrame— execution frame passed to host functions providing access to caller memory and module instances.
Synopsis
- class ToHostFunc f where
- toHostFunc :: f -> HostFunc
- hostFuncSignature :: Proxy f -> ([ValType], [ValType])
- class WasmValType a where
- toWasmVal :: a -> WasmVal
- fromWasmVal :: WasmVal -> Maybe a
- wasmValType :: Proxy a -> ValType
- class WasmReturn a where
- toWasmReturn :: a -> [WasmVal]
- wasmReturnTypes :: Proxy a -> [ValType]
- createHostFunction :: ToHostFunc f => f -> IO FunctionInstance
- withHostFunction :: ToHostFunc f => f -> (FunctionInstance -> IO a) -> IO a
- data FunctionType
- createFunctionType :: [ValType] -> [ValType] -> IO FunctionType
- deleteFunctionType :: FunctionType -> IO ()
- withFunctionType :: [ValType] -> [ValType] -> (FunctionType -> IO a) -> IO a
- getFunctionTypeParameters :: FunctionType -> IO [ValType]
- getFunctionTypeReturns :: FunctionType -> IO [ValType]
- newtype CallingFrame = CallingFrame (BorrowedCtx CallingFrameTag)
- callingFrameGetExecutor :: CallingFrame -> IO (Ptr ExecutorTag)
- callingFrameGetModuleInstance :: CallingFrame -> IO (Ptr ModuleInstanceTag)
- callingFrameGetMemoryInstance :: CallingFrame -> Word32 -> IO (Ptr MemoryInstanceTag)
- type HostFunc = CallingFrame -> [WasmVal] -> IO (WasmResult [WasmVal])
- data FunctionInstance
- createFunction :: FunctionType -> HostFunc -> IO FunctionInstance
- deleteFunction :: FunctionInstance -> IO ()
- withFunction :: FunctionType -> HostFunc -> (FunctionInstance -> IO a) -> IO a
- getFunctionType :: FunctionInstance -> IO FunctionType
- unsafeFunctionPtr :: FunctionInstance -> IO (Ptr FunctionInstanceTag)
- borrowFunction :: Ptr FunctionInstanceTag -> IO FunctionInstance
- unsafeFunctionTypePtr :: FunctionType -> IO (Ptr FunctionTypeTag)
- borrowFunctionType :: Ptr FunctionTypeTag -> IO FunctionType
- disarmFunction :: FunctionInstance -> IO ()
High-level host functions
class ToHostFunc f where #
Typeclass for functions that can be converted into a WasmEdge host function.
Automatically converts Haskell typed arguments and results, with optional CallingFrame.
Instances
class WasmValType a where #
Typeclass for Haskell values that can be converted to and from a single WebAssembly value.
Instances
| WasmValType Int16 # |
|
Defined in WasmEdge.Function | |
| WasmValType Int32 # | |
Defined in WasmEdge.Function | |
| WasmValType Int64 # | |
Defined in WasmEdge.Function | |
| WasmValType Int8 # |
|
Defined in WasmEdge.Function | |
| WasmValType Word16 # |
|
Defined in WasmEdge.Function | |
| WasmValType Word32 # | |
Defined in WasmEdge.Function | |
| WasmValType Word64 # | |
Defined in WasmEdge.Function | |
| WasmValType Word8 # |
|
Defined in WasmEdge.Function | |
| WasmValType WasmVal # | |
Defined in WasmEdge.Function | |
| WasmValType Bool # | |
Defined in WasmEdge.Function | |
| WasmValType Double # | |
Defined in WasmEdge.Function | |
| WasmValType Float # | |
Defined in WasmEdge.Function | |
class WasmReturn a where #
Typeclass for host function return values.
Instances
| WasmReturn Int32 # | |
Defined in WasmEdge.Function | |
| WasmReturn Int64 # | |
Defined in WasmEdge.Function | |
| WasmReturn Word32 # | |
Defined in WasmEdge.Function | |
| WasmReturn Word64 # | |
Defined in WasmEdge.Function | |
| WasmReturn () # | |
Defined in WasmEdge.Function | |
| WasmReturn Double # | |
Defined in WasmEdge.Function | |
| WasmReturn Float # | |
Defined in WasmEdge.Function | |
| (WasmValType a, WasmValType b) => WasmReturn (a, b) # | |
Defined in WasmEdge.Function | |
| (WasmValType a, WasmValType b, WasmValType c) => WasmReturn (a, b, c) # | |
Defined in WasmEdge.Function | |
createHostFunction :: ToHostFunc f => f -> IO FunctionInstance #
Create a host FunctionInstance automatically inferring its FunctionType from Haskell types.
withHostFunction :: ToHostFunc f => f -> (FunctionInstance -> IO a) -> IO a #
Scoped FunctionInstance with automatic signature inference.
Function type
data FunctionType #
A WebAssembly function type describing parameter and return types.
createFunctionType :: [ValType] -> [ValType] -> IO FunctionType #
Create a FunctionType from lists of parameter and return types.
deleteFunctionType :: FunctionType -> IO () #
Delete a FunctionType.
withFunctionType :: [ValType] -> [ValType] -> (FunctionType -> IO a) -> IO a #
Scoped FunctionType.
getFunctionTypeParameters :: FunctionType -> IO [ValType] #
Get the parameter types of a FunctionType.
getFunctionTypeReturns :: FunctionType -> IO [ValType] #
Get the return types of a FunctionType.
Calling frame
newtype CallingFrame #
Context passed to a host function callback representing the caller's frame.
Constructors
| CallingFrame (BorrowedCtx CallingFrameTag) |
Instances
callingFrameGetExecutor :: CallingFrame -> IO (Ptr ExecutorTag) #
Get the executor from a CallingFrame.
callingFrameGetModuleInstance :: CallingFrame -> IO (Ptr ModuleInstanceTag) #
Get the module instance from a CallingFrame.
callingFrameGetMemoryInstance :: CallingFrame -> Word32 -> IO (Ptr MemoryInstanceTag) #
Get the memory instance by index (typically 0) from a CallingFrame.
Raw host function callback
type HostFunc = CallingFrame -> [WasmVal] -> IO (WasmResult [WasmVal]) #
Raw low-level Haskell implementation of a host function callable from WebAssembly.
Function instance
data FunctionInstance #
A WebAssembly function instance.
createFunction :: FunctionType -> HostFunc -> IO FunctionInstance #
Create a host FunctionInstance from a FunctionType and raw Haskell HostFunc.
deleteFunction :: FunctionInstance -> IO () #
Delete a FunctionInstance.
withFunction :: FunctionType -> HostFunc -> (FunctionInstance -> IO a) -> IO a #
Scoped FunctionInstance with raw HostFunc.
getFunctionType :: FunctionInstance -> IO FunctionType #
Get the FunctionType of a FunctionInstance.
Internal / borrowed pointers
unsafeFunctionPtr :: FunctionInstance -> IO (Ptr FunctionInstanceTag) #
Extract the raw pointer of a FunctionInstance.
borrowFunction :: Ptr FunctionInstanceTag -> IO FunctionInstance #
Create a borrowed FunctionInstance from a raw pointer (does not finalize).
unsafeFunctionTypePtr :: FunctionType -> IO (Ptr FunctionTypeTag) #
Extract the raw pointer of a FunctionType.
borrowFunctionType :: Ptr FunctionTypeTag -> IO FunctionType #
Create a borrowed FunctionType from a raw pointer (does not finalize).
disarmFunction :: FunctionInstance -> IO () #
Disarm finalizer when ownership is transferred to a module instance.