wasmedge-hs-0.17.1.0: Haskell bindings for the WasmEdge WebAssembly runtime
Copyright(c) 2026 Tushar Adhatrao
LicenseMIT
Stabilitystable
Safe HaskellNone
LanguageGHC2021

WasmEdge.Function

Description

Provides types and functions for WebAssembly function types and host function instances:

  • FunctionType — parameter and return type signatures
  • FunctionInstance — a callable function in WebAssembly or the host
  • HostFunc — raw low-level callback interface (CallingFrame -> [WasmVal] -> IO (WasmResult [WasmVal]))
  • ToHostFunc — high-level typeclass allowing normal Haskell functions (e.g. Int32 -> Int32 -> IO Int32 or CallingFrame -> 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

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

Instances details
WasmReturn r => ToHostFunc (IO r) # 
Instance details

Defined in WasmEdge.Function

Methods

toHostFunc :: IO r -> HostFunc #

hostFuncSignature :: Proxy (IO r) -> ([ValType], [ValType]) #

WasmReturn r => ToHostFunc (CallingFrame -> IO r) # 
Instance details

Defined in WasmEdge.Function

(WasmValType a, WasmReturn r) => ToHostFunc (CallingFrame -> a -> IO r) # 
Instance details

Defined in WasmEdge.Function

Methods

toHostFunc :: (CallingFrame -> a -> IO r) -> HostFunc #

hostFuncSignature :: Proxy (CallingFrame -> a -> IO r) -> ([ValType], [ValType]) #

(WasmValType a, WasmValType b, WasmReturn r) => ToHostFunc (CallingFrame -> a -> b -> IO r) # 
Instance details

Defined in WasmEdge.Function

Methods

toHostFunc :: (CallingFrame -> a -> b -> IO r) -> HostFunc #

hostFuncSignature :: Proxy (CallingFrame -> a -> b -> IO r) -> ([ValType], [ValType]) #

(WasmValType a, WasmValType b, WasmValType c, WasmReturn r) => ToHostFunc (CallingFrame -> a -> b -> c -> IO r) # 
Instance details

Defined in WasmEdge.Function

Methods

toHostFunc :: (CallingFrame -> a -> b -> c -> IO r) -> HostFunc #

hostFuncSignature :: Proxy (CallingFrame -> a -> b -> c -> IO r) -> ([ValType], [ValType]) #

(WasmValType a, WasmValType b, WasmValType c, WasmValType d, WasmReturn r) => ToHostFunc (CallingFrame -> a -> b -> c -> d -> IO r) # 
Instance details

Defined in WasmEdge.Function

Methods

toHostFunc :: (CallingFrame -> a -> b -> c -> d -> IO r) -> HostFunc #

hostFuncSignature :: Proxy (CallingFrame -> a -> b -> c -> d -> IO r) -> ([ValType], [ValType]) #

(WasmValType a, WasmReturn r) => ToHostFunc (a -> IO r) # 
Instance details

Defined in WasmEdge.Function

Methods

toHostFunc :: (a -> IO r) -> HostFunc #

hostFuncSignature :: Proxy (a -> IO r) -> ([ValType], [ValType]) #

(WasmValType a, WasmValType b, WasmReturn r) => ToHostFunc (a -> b -> IO r) # 
Instance details

Defined in WasmEdge.Function

Methods

toHostFunc :: (a -> b -> IO r) -> HostFunc #

hostFuncSignature :: Proxy (a -> b -> IO r) -> ([ValType], [ValType]) #

(WasmValType a, WasmValType b, WasmValType c, WasmReturn r) => ToHostFunc (a -> b -> c -> IO r) # 
Instance details

Defined in WasmEdge.Function

Methods

toHostFunc :: (a -> b -> c -> IO r) -> HostFunc #

hostFuncSignature :: Proxy (a -> b -> c -> IO r) -> ([ValType], [ValType]) #

(WasmValType a, WasmValType b, WasmValType c, WasmValType d, WasmReturn r) => ToHostFunc (a -> b -> c -> d -> IO r) # 
Instance details

Defined in WasmEdge.Function

Methods

toHostFunc :: (a -> b -> c -> d -> IO r) -> HostFunc #

hostFuncSignature :: Proxy (a -> b -> c -> d -> IO r) -> ([ValType], [ValType]) #

class WasmValType a where #

Typeclass for Haskell values that can be converted to and from a single WebAssembly value.

Instances

Instances details
WasmValType Int16 #

Int16 is coerced to an i32 (Wasm has no i16).

Instance details

Defined in WasmEdge.Function

WasmValType Int32 # 
Instance details

Defined in WasmEdge.Function

WasmValType Int64 # 
Instance details

Defined in WasmEdge.Function

WasmValType Int8 #

Int8 is coerced to an i32 (Wasm has no i8).

Instance details

Defined in WasmEdge.Function

WasmValType Word16 #

Word16 is coerced to an i32 (Wasm has no u16).

Instance details

Defined in WasmEdge.Function

WasmValType Word32 # 
Instance details

Defined in WasmEdge.Function

WasmValType Word64 # 
Instance details

Defined in WasmEdge.Function

WasmValType Word8 #

Word8 is coerced to an i32 (Wasm has no u8).

Instance details

Defined in WasmEdge.Function

WasmValType WasmVal # 
Instance details

Defined in WasmEdge.Function

WasmValType Bool #

Bool is encoded as i32: 0 = False, 1 = True.

Instance details

Defined in WasmEdge.Function

WasmValType Double # 
Instance details

Defined in WasmEdge.Function

WasmValType Float # 
Instance details

Defined in WasmEdge.Function

class WasmReturn a where #

Typeclass for host function return values.

Methods

toWasmReturn :: a -> [WasmVal] #

wasmReturnTypes :: Proxy a -> [ValType] #

Instances

Instances details
WasmReturn Int32 # 
Instance details

Defined in WasmEdge.Function

WasmReturn Int64 # 
Instance details

Defined in WasmEdge.Function

WasmReturn Word32 # 
Instance details

Defined in WasmEdge.Function

WasmReturn Word64 # 
Instance details

Defined in WasmEdge.Function

WasmReturn () # 
Instance details

Defined in WasmEdge.Function

Methods

toWasmReturn :: () -> [WasmVal] #

wasmReturnTypes :: Proxy () -> [ValType] #

WasmReturn Double # 
Instance details

Defined in WasmEdge.Function

WasmReturn Float # 
Instance details

Defined in WasmEdge.Function

(WasmValType a, WasmValType b) => WasmReturn (a, b) # 
Instance details

Defined in WasmEdge.Function

Methods

toWasmReturn :: (a, b) -> [WasmVal] #

wasmReturnTypes :: Proxy (a, b) -> [ValType] #

(WasmValType a, WasmValType b, WasmValType c) => WasmReturn (a, b, c) # 
Instance details

Defined in WasmEdge.Function

Methods

toWasmReturn :: (a, b, c) -> [WasmVal] #

wasmReturnTypes :: Proxy (a, b, c) -> [ValType] #

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.

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.

Instances

Instances details
WasmReturn r => ToHostFunc (CallingFrame -> IO r) # 
Instance details

Defined in WasmEdge.Function

(WasmValType a, WasmReturn r) => ToHostFunc (CallingFrame -> a -> IO r) # 
Instance details

Defined in WasmEdge.Function

Methods

toHostFunc :: (CallingFrame -> a -> IO r) -> HostFunc #

hostFuncSignature :: Proxy (CallingFrame -> a -> IO r) -> ([ValType], [ValType]) #

(WasmValType a, WasmValType b, WasmReturn r) => ToHostFunc (CallingFrame -> a -> b -> IO r) # 
Instance details

Defined in WasmEdge.Function

Methods

toHostFunc :: (CallingFrame -> a -> b -> IO r) -> HostFunc #

hostFuncSignature :: Proxy (CallingFrame -> a -> b -> IO r) -> ([ValType], [ValType]) #

(WasmValType a, WasmValType b, WasmValType c, WasmReturn r) => ToHostFunc (CallingFrame -> a -> b -> c -> IO r) # 
Instance details

Defined in WasmEdge.Function

Methods

toHostFunc :: (CallingFrame -> a -> b -> c -> IO r) -> HostFunc #

hostFuncSignature :: Proxy (CallingFrame -> a -> b -> c -> IO r) -> ([ValType], [ValType]) #

(WasmValType a, WasmValType b, WasmValType c, WasmValType d, WasmReturn r) => ToHostFunc (CallingFrame -> a -> b -> c -> d -> IO r) # 
Instance details

Defined in WasmEdge.Function

Methods

toHostFunc :: (CallingFrame -> a -> b -> c -> d -> IO r) -> HostFunc #

hostFuncSignature :: Proxy (CallingFrame -> a -> b -> c -> d -> IO r) -> ([ValType], [ValType]) #

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.

Internal / borrowed pointers

borrowFunction :: Ptr FunctionInstanceTag -> IO FunctionInstance #

Create a borrowed FunctionInstance from a raw pointer (does not finalize).

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.