| Copyright | (c) 2026 Tushar Adhatrao |
|---|---|
| License | MIT |
| Stability | stable |
| Safe Haskell | None |
| Language | GHC2021 |
WasmEdge
Contents
- Core types (re-exported from WasmEdge.Types)
- Error types (re-exported from WasmEdge.Error)
- Version
- VM (re-exported from WasmEdge.VM)
- Host Functions (re-exported from WasmEdge.Function)
- Complex Data & Linear Memory (re-exported from WasmEdge.Memory.Codec and WasmEdge.Memory.Alloc)
- High-Level Guest Calling & Bindgen (re-exported from WasmEdge.Bindgen)
- Lifted Host Functions (re-exported from WasmEdge.Function.Memory)
- Caller memory helpers (re-exported from WasmEdge.Caller)
Description
Import this module for access to the most commonly used functions:
import WasmEdge main :: IO () main = withVM Nothing Nothing $ vm -> runWasmFile vm "hello.wasm" "main" [] >>= print
For specialized features, import the dedicated modules:
- WasmEdge.Configure — WASM proposals, compiler settings, run mode
- WasmEdge.VM — High-level VM API
- WasmEdge.Module — Host module building
- WasmEdge.Function — Host function creation and
ToHostFunc - WasmEdge.Caller — Caller frame and memory access
- WasmEdge.Memory — Memory instance access
- WasmEdge.Plugin — Plugin loading
- WasmEdge.Async — Async execution
- WasmEdge.Log — Logging configuration
Synopsis
- data WasmVal
- data ValType
- data Mutability
- data Limit = Limit {}
- data Proposal
- data RunMode
- data WasmEdgeError = WasmEdgeError {
- weErrCategory :: !ErrCategory
- weErrCode :: !Word32
- weErrMessage :: !Text
- type WasmResult a = Either WasmEdgeError a
- data ErrCategory
- formatError :: WasmEdgeError -> String
- throwOnLeft :: WasmResult a -> IO a
- version :: IO Text
- versionMajor :: IO Word32
- versionMinor :: IO Word32
- versionPatch :: IO Word32
- data VM
- createVM :: Maybe Configure -> Maybe Store -> IO VM
- deleteVM :: VM -> IO ()
- withVM :: Maybe Configure -> Maybe Store -> (VM -> IO a) -> IO a
- data VMConfig = VMConfig {}
- defaultVMConfig :: VMConfig
- withVMConfig :: VMConfig -> (VM -> IO a) -> IO a
- runWasmFile :: VM -> FilePath -> Text -> [WasmVal] -> IO (WasmResult [WasmVal])
- runWasmBytes :: VM -> ByteString -> Text -> [WasmVal] -> IO (WasmResult [WasmVal])
- loadWasmFile :: VM -> FilePath -> IO (WasmResult ())
- loadWasmBytes :: VM -> ByteString -> IO (WasmResult ())
- validate :: VM -> IO (WasmResult ())
- instantiate :: VM -> IO (WasmResult ())
- execute :: VM -> Text -> [WasmVal] -> IO (WasmResult [WasmVal])
- registerModuleFromFile :: VM -> Text -> FilePath -> IO (WasmResult ())
- registerModuleFromBytes :: VM -> Text -> ByteString -> IO (WasmResult ())
- registerModuleFromImport :: VM -> ModuleInstance -> IO (WasmResult ())
- registerModuleFromImportWithAlias :: VM -> Text -> ModuleInstance -> IO (WasmResult ())
- executeRegistered :: VM -> Text -> Text -> [WasmVal] -> IO (WasmResult [WasmVal])
- 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
- class WasmMemory a where
- wasmPeek :: MemoryInstance -> Word32 -> Word32 -> IO (WasmResult a)
- wasmPoke :: MemoryInstance -> Word32 -> a -> IO (WasmResult Word32)
- class WasmMemory a => FixedWasmMemory a where
- wasmWidth :: Proxy a -> Word32
- serialise :: a -> ByteString
- deserialise :: ByteString -> Either String a
- newtype WasmJSON a = WasmJSON {
- unWasmJSON :: a
- jsonWasmPoke :: ToJSON a => MemoryInstance -> Word32 -> a -> IO (WasmResult Word32)
- jsonWasmPeek :: FromJSON a => MemoryInstance -> Word32 -> Word32 -> IO (WasmResult a)
- 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
- module WasmEdge.Bindgen
- liftMemFunc :: (WasmMemory a, WasmMemory b) => (a -> IO (Either WasmEdgeError b)) -> CallingFrame -> [WasmVal] -> IO (WasmResult [WasmVal])
- liftMemFunc_ :: WasmMemory a => (a -> IO (Either WasmEdgeError ())) -> CallingFrame -> [WasmVal] -> IO (WasmResult [WasmVal])
- liftMemFuncWith :: (WasmMemory a, WasmMemory b) => VM -> String -> AllocConvention -> (a -> IO (Either WasmEdgeError b)) -> CallingFrame -> [WasmVal] -> IO (WasmResult [WasmVal])
- callingFrameMemory :: CallingFrame -> IO (WasmResult MemoryInstance)
- 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)
Core types (re-exported from WasmEdge.Types)
A WebAssembly value, covering all WASM numeric and reference types.
Constructors
| WasmI32 !Int32 | 32-bit integer (signed in Haskell, interchangeable with unsigned in WASM) |
| WasmI64 !Int64 | 64-bit integer |
| WasmF32 !Float | 32-bit IEEE 754 float |
| WasmF64 !Double | 64-bit IEEE 754 double |
| WasmV128 !Int128 | 128-bit SIMD vector (from |
| WasmFuncRef !(Maybe FuncRef) | Function reference (nullable). |
| WasmExternRef !(Maybe ExternRef) | External reference (nullable). |
Instances
The type of a WebAssembly value. Used in function signatures and global/table type descriptors.
Constructors
| I32 | 32-bit integer |
| I64 | 64-bit integer |
| F32 | 32-bit float |
| F64 | 64-bit float |
| V128 | 128-bit SIMD vector |
| FuncRefT | Function reference ( |
| ExternRefT | External reference ( |
Instances
| NFData ValType # | |||||
Defined in WasmEdge.Types | |||||
| Bounded ValType # | |||||
| Enum ValType # | |||||
| Generic ValType # | |||||
Defined in WasmEdge.Types Associated Types
| |||||
| Show ValType # | |||||
| Eq ValType # | |||||
| Ord ValType # | |||||
| type Rep ValType # | |||||
Defined in WasmEdge.Types type Rep ValType = D1 ('MetaData "ValType" "WasmEdge.Types" "wasmedge-hs-0.17.1.0-inplace" 'False) ((C1 ('MetaCons "I32" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "I64" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "F32" 'PrefixI 'False) (U1 :: Type -> Type))) :+: ((C1 ('MetaCons "F64" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "V128" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "FuncRefT" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "ExternRefT" 'PrefixI 'False) (U1 :: Type -> Type)))) | |||||
data Mutability #
Global mutability.
Instances
| NFData Mutability # | |||||
Defined in WasmEdge.Types Methods rnf :: Mutability -> () # | |||||
| Bounded Mutability # | |||||
Defined in WasmEdge.Types | |||||
| Enum Mutability # | |||||
Defined in WasmEdge.Types Methods succ :: Mutability -> Mutability # pred :: Mutability -> Mutability # toEnum :: Int -> Mutability # fromEnum :: Mutability -> Int # enumFrom :: Mutability -> [Mutability] # enumFromThen :: Mutability -> Mutability -> [Mutability] # enumFromTo :: Mutability -> Mutability -> [Mutability] # enumFromThenTo :: Mutability -> Mutability -> Mutability -> [Mutability] # | |||||
| Generic Mutability # | |||||
Defined in WasmEdge.Types Associated Types
| |||||
| Show Mutability # | |||||
Defined in WasmEdge.Types Methods showsPrec :: Int -> Mutability -> ShowS # show :: Mutability -> String # showList :: [Mutability] -> ShowS # | |||||
| Eq Mutability # | |||||
Defined in WasmEdge.Types | |||||
| Ord Mutability # | |||||
Defined in WasmEdge.Types Methods compare :: Mutability -> Mutability -> Ordering # (<) :: Mutability -> Mutability -> Bool # (<=) :: Mutability -> Mutability -> Bool # (>) :: Mutability -> Mutability -> Bool # (>=) :: Mutability -> Mutability -> Bool # max :: Mutability -> Mutability -> Mutability # min :: Mutability -> Mutability -> Mutability # | |||||
| type Rep Mutability # | |||||
A WebAssembly limit (min, optional max) as used in table and memory types.
Constructors
| Limit | |
Instances
| NFData Limit # | |||||
Defined in WasmEdge.Types | |||||
| Generic Limit # | |||||
Defined in WasmEdge.Types Associated Types
| |||||
| Show Limit # | |||||
| Eq Limit # | |||||
| type Rep Limit # | |||||
Defined in WasmEdge.Types type Rep Limit = D1 ('MetaData "Limit" "WasmEdge.Types" "wasmedge-hs-0.17.1.0-inplace" 'False) (C1 ('MetaCons "Limit" 'PrefixI 'True) (S1 ('MetaSel ('Just "limitMin") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Word32) :*: (S1 ('MetaSel ('Just "limitMax") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Maybe Word32)) :*: S1 ('MetaSel ('Just "limitShared") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Bool)))) | |||||
WebAssembly proposals that can be enabled in Configure.
Constructors
| ImportExportMutGlobals | 0: Import/export mutable globals |
| NonTrapFloatToIntConversions | 1: Non-trap float-to-int conversions |
| SignExtensionOperators | 2: Sign-extension operators |
| MultiValue | 3: Multiple return values |
| BulkMemoryOperations | 4: Bulk memory operations |
| ReferenceTypes | 5: Reference types |
| SIMD | 6: SIMD (fixed-width) |
| TailCall | 7: Tail call optimization |
| ExtendedConst | 8: Extended constant expressions |
| FunctionReferences | 9: Typed function references |
| GC | 10: Garbage collection |
| MultiMemories | 11: Multiple memories |
| RelaxedSIMD | 12: Relaxed SIMD |
| Annotations | 13: Custom section annotations |
| ExceptionHandling | 14: Exception handling |
| Memory64 | 15: 64-bit memory indices |
| Threads | 16: Threads (shared memory + atomics) |
| ComponentModel | 17: Component model |
Instances
| NFData Proposal # | |||||
Defined in WasmEdge.Types | |||||
| Bounded Proposal # | |||||
| Enum Proposal # | |||||
| Generic Proposal # | |||||
Defined in WasmEdge.Types Associated Types
| |||||
| Show Proposal # | |||||
| Eq Proposal # | |||||
| Ord Proposal # | |||||
Defined in WasmEdge.Types | |||||
| type Rep Proposal # | |||||
Defined in WasmEdge.Types type Rep Proposal = D1 ('MetaData "Proposal" "WasmEdge.Types" "wasmedge-hs-0.17.1.0-inplace" 'False) ((((C1 ('MetaCons "ImportExportMutGlobals" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "NonTrapFloatToIntConversions" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "SignExtensionOperators" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "MultiValue" 'PrefixI 'False) (U1 :: Type -> Type))) :+: ((C1 ('MetaCons "BulkMemoryOperations" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "ReferenceTypes" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "SIMD" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "TailCall" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "ExtendedConst" 'PrefixI 'False) (U1 :: Type -> Type))))) :+: (((C1 ('MetaCons "FunctionReferences" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "GC" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "MultiMemories" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "RelaxedSIMD" 'PrefixI 'False) (U1 :: Type -> Type))) :+: ((C1 ('MetaCons "Annotations" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "ExceptionHandling" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "Memory64" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "Threads" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "ComponentModel" 'PrefixI 'False) (U1 :: Type -> Type)))))) | |||||
The execution mode for the WasmEdge runtime.
Constructors
| RunModeInterpreter | Pure interpreter (always available) |
| RunModeAOT | AOT compiled (requires compiled binary) |
| RunModeLazyJIT | Lazy JIT: compile on first function call (0.18+) |
Instances
| NFData RunMode # | |||||
Defined in WasmEdge.Types | |||||
| Bounded RunMode # | |||||
| Enum RunMode # | |||||
| Generic RunMode # | |||||
Defined in WasmEdge.Types Associated Types
| |||||
| Show RunMode # | |||||
| Eq RunMode # | |||||
| Ord RunMode # | |||||
| type Rep RunMode # | |||||
Defined in WasmEdge.Types type Rep RunMode = D1 ('MetaData "RunMode" "WasmEdge.Types" "wasmedge-hs-0.17.1.0-inplace" 'False) (C1 ('MetaCons "RunModeInterpreter" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "RunModeAOT" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "RunModeLazyJIT" 'PrefixI 'False) (U1 :: Type -> Type))) | |||||
Error types (re-exported from WasmEdge.Error)
data WasmEdgeError #
An error returned by the WasmEdge runtime.
Thrown as a Haskell exception by all public API functions that encounter
a failed WasmEdge_Result. Can also be caught as SomeException.
Constructors
| WasmEdgeError | |
Fields
| |
Instances
| NFData WasmEdgeError # | |||||
Defined in WasmEdge.Error Methods rnf :: WasmEdgeError -> () # | |||||
| Exception WasmEdgeError # | |||||
Defined in WasmEdge.Error Methods toException :: WasmEdgeError -> SomeException # fromException :: SomeException -> Maybe WasmEdgeError # displayException :: WasmEdgeError -> String # backtraceDesired :: WasmEdgeError -> Bool # | |||||
| Generic WasmEdgeError # | |||||
Defined in WasmEdge.Error Associated Types
| |||||
| Show WasmEdgeError # | |||||
Defined in WasmEdge.Error Methods showsPrec :: Int -> WasmEdgeError -> ShowS # show :: WasmEdgeError -> String # showList :: [WasmEdgeError] -> ShowS # | |||||
| Eq WasmEdgeError # | |||||
Defined in WasmEdge.Error Methods (==) :: WasmEdgeError -> WasmEdgeError -> Bool # (/=) :: WasmEdgeError -> WasmEdgeError -> Bool # | |||||
| type Rep WasmEdgeError # | |||||
Defined in WasmEdge.Error type Rep WasmEdgeError = D1 ('MetaData "WasmEdgeError" "WasmEdge.Error" "wasmedge-hs-0.17.1.0-inplace" 'False) (C1 ('MetaCons "WasmEdgeError" 'PrefixI 'True) (S1 ('MetaSel ('Just "weErrCategory") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ErrCategory) :*: (S1 ('MetaSel ('Just "weErrCode") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Word32) :*: S1 ('MetaSel ('Just "weErrMessage") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text)))) | |||||
type WasmResult a = Either WasmEdgeError a #
A computation that may return a WasmEdgeError.
Use throwOnLeft to re-raise as an exception, or pattern-match directly.
data ErrCategory #
Maps to the C WasmEdge_ErrCategory enum.
Constructors
| ErrCategory_WASM | Error from the WebAssembly spec (code is |
| ErrCategory_UserDefined | User-defined error code (e.g., from host functions) |
| ErrCategory_Unknown Word32 | An unrecognised category (future-proofing) |
Instances
| NFData ErrCategory # | |||||
Defined in WasmEdge.Error Methods rnf :: ErrCategory -> () # | |||||
| Generic ErrCategory # | |||||
Defined in WasmEdge.Error Associated Types
| |||||
| Show ErrCategory # | |||||
Defined in WasmEdge.Error Methods showsPrec :: Int -> ErrCategory -> ShowS # show :: ErrCategory -> String # showList :: [ErrCategory] -> ShowS # | |||||
| Eq ErrCategory # | |||||
Defined in WasmEdge.Error | |||||
| type Rep ErrCategory # | |||||
Defined in WasmEdge.Error type Rep ErrCategory = D1 ('MetaData "ErrCategory" "WasmEdge.Error" "wasmedge-hs-0.17.1.0-inplace" 'False) (C1 ('MetaCons "ErrCategory_WASM" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "ErrCategory_UserDefined" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "ErrCategory_Unknown" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Word32)))) | |||||
formatError :: WasmEdgeError -> String #
Format a WasmEdgeError into a human-readable string with a hex error code.
throwOnLeft :: WasmResult a -> IO a #
Throw a WasmEdgeError as an IO exception on Left, or unwrap Right.
Useful for step-by-step WASM pipelines where you want errors to propagate as exceptions rather than handling them inline:
withVM Nothing Nothing $ vm -> do loadWasmFile vm "module.wasm" >>= throwOnLeft validate vm >>= throwOnLeft instantiate vm >>= throwOnLeft execute vm "run" [] >>= throwOnLeft
Version
versionMajor :: IO Word32 #
Major version number.
versionMinor :: IO Word32 #
Minor version number.
versionPatch :: IO Word32 #
Patch version number.
VM (re-exported from WasmEdge.VM)
Opaque WasmEdge VM context.
Automatically retains registered host ModuleInstances to guarantee their
lifetime as long as the VM is alive, preventing use-after-free bugs.
createVM :: Maybe Configure -> Maybe Store -> IO VM #
Create a VM context.
createVM mCfg mStore uses the optional Configure context mCfg and
optional shared Store context mStore. Pass Nothing to use defaults.
Declarative configuration record for initializing a VM.
Inspired by wasmedge-sdk's VmBuilder in Rust.
Default VM configuration with default options and fresh store.
Arguments
| :: VM | |
| -> FilePath | Path to the .wasm or .so file |
| -> Text | Function name |
| -> [WasmVal] | Parameters |
| -> IO (WasmResult [WasmVal]) |
Load, instantiate, and run a WASM file in one call.
runWasmBytes :: VM -> ByteString -> Text -> [WasmVal] -> IO (WasmResult [WasmVal]) #
Load, instantiate, and run a WASM module from a ByteString.
loadWasmFile :: VM -> FilePath -> IO (WasmResult ()) #
Load a WASM file into the VM (does not validate or instantiate).
loadWasmBytes :: VM -> ByteString -> IO (WasmResult ()) #
Load a WASM module from bytes.
validate :: VM -> IO (WasmResult ()) #
Validate the loaded WASM module.
instantiate :: VM -> IO (WasmResult ()) #
Instantiate the loaded and validated WASM module.
execute :: VM -> Text -> [WasmVal] -> IO (WasmResult [WasmVal]) #
Execute a function in the instantiated module.
registerModuleFromFile :: VM -> Text -> FilePath -> IO (WasmResult ()) #
Register and instantiate a named module from a file.
registerModuleFromBytes :: VM -> Text -> ByteString -> IO (WasmResult ()) #
Register and instantiate a named module from bytes.
registerModuleFromImport :: VM -> ModuleInstance -> IO (WasmResult ()) #
Register an existing ModuleInstance into the VM.
The VM automatically retains the module to guarantee its lifetime.
registerModuleFromImportWithAlias :: VM -> Text -> ModuleInstance -> IO (WasmResult ()) #
Register an existing ModuleInstance into the VM under a custom alias name.
The VM automatically retains the module to guarantee its lifetime.
executeRegistered :: VM -> Text -> Text -> [WasmVal] -> IO (WasmResult [WasmVal]) #
Execute a function from a named registered module.
Host Functions (re-exported from WasmEdge.Function)
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
| WasmReturn r => ToHostFunc (IO r) # | |
Defined in WasmEdge.Function Methods toHostFunc :: IO r -> HostFunc # hostFuncSignature :: Proxy (IO r) -> ([ValType], [ValType]) # | |
| WasmReturn r => ToHostFunc (CallingFrame -> IO r) # | |
Defined in WasmEdge.Function Methods toHostFunc :: (CallingFrame -> IO r) -> HostFunc # hostFuncSignature :: Proxy (CallingFrame -> IO r) -> ([ValType], [ValType]) # | |
| (WasmValType a, WasmReturn r) => ToHostFunc (CallingFrame -> a -> IO r) # | |
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) # | |
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) # | |
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) # | |
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) # | |
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) # | |
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) # | |
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) # | |
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
| 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.
Complex Data & Linear Memory (re-exported from WasmEdge.Memory.Codec and WasmEdge.Memory.Alloc)
class WasmMemory a where #
A type that can be serialised to and deserialised from a contiguous slice of WebAssembly linear memory.
Minimal complete definition: wasmPeek + wasmPoke.
For fixed-size types (primitives, fixed-size tuples), implement
FixedWasmMemory and use fixedPeek / fixedPoke as the default methods.
For JSON-serialisable ADTs use jsonWasmPeek / jsonWasmPoke.
Methods
Arguments
| :: MemoryInstance | The linear memory instance |
| -> Word32 | Byte offset in linear memory |
| -> Word32 | Byte length of the slice |
| -> IO (WasmResult a) |
Read a value of type a from len bytes of linear memory at offset.
Returns Left on out-of-bounds access or deserialisation failure.
Arguments
| :: MemoryInstance | The linear memory instance |
| -> Word32 | Byte offset to write at |
| -> a | The value to serialise |
| -> IO (WasmResult Word32) |
|
Write a value of type a into linear memory at offset.
Returns Right n where n is the number of bytes written, or Left on
out-of-bounds access.
Instances
| WasmMemory ByteString # |
The entire slice |
Defined in WasmEdge.Memory.Codec Methods wasmPeek :: MemoryInstance -> Word32 -> Word32 -> IO (WasmResult ByteString) # wasmPoke :: MemoryInstance -> Word32 -> ByteString -> IO (WasmResult Word32) # | |
| WasmMemory Int16 # |
|
Defined in WasmEdge.Memory.Codec Methods wasmPeek :: MemoryInstance -> Word32 -> Word32 -> IO (WasmResult Int16) # wasmPoke :: MemoryInstance -> Word32 -> Int16 -> IO (WasmResult Word32) # | |
| WasmMemory Int32 # |
|
Defined in WasmEdge.Memory.Codec Methods wasmPeek :: MemoryInstance -> Word32 -> Word32 -> IO (WasmResult Int32) # wasmPoke :: MemoryInstance -> Word32 -> Int32 -> IO (WasmResult Word32) # | |
| WasmMemory Int64 # |
|
Defined in WasmEdge.Memory.Codec Methods wasmPeek :: MemoryInstance -> Word32 -> Word32 -> IO (WasmResult Int64) # wasmPoke :: MemoryInstance -> Word32 -> Int64 -> IO (WasmResult Word32) # | |
| WasmMemory Int8 # |
|
Defined in WasmEdge.Memory.Codec Methods wasmPeek :: MemoryInstance -> Word32 -> Word32 -> IO (WasmResult Int8) # wasmPoke :: MemoryInstance -> Word32 -> Int8 -> IO (WasmResult Word32) # | |
| WasmMemory Word16 # |
|
Defined in WasmEdge.Memory.Codec Methods wasmPeek :: MemoryInstance -> Word32 -> Word32 -> IO (WasmResult Word16) # wasmPoke :: MemoryInstance -> Word32 -> Word16 -> IO (WasmResult Word32) # | |
| WasmMemory Word32 # |
|
Defined in WasmEdge.Memory.Codec Methods wasmPeek :: MemoryInstance -> Word32 -> Word32 -> IO (WasmResult Word32) # wasmPoke :: MemoryInstance -> Word32 -> Word32 -> IO (WasmResult Word32) # | |
| WasmMemory Word64 # |
|
Defined in WasmEdge.Memory.Codec Methods wasmPeek :: MemoryInstance -> Word32 -> Word32 -> IO (WasmResult Word64) # wasmPoke :: MemoryInstance -> Word32 -> Word64 -> IO (WasmResult Word32) # | |
| WasmMemory Word8 # |
|
Defined in WasmEdge.Memory.Codec Methods wasmPeek :: MemoryInstance -> Word32 -> Word32 -> IO (WasmResult Word8) # wasmPoke :: MemoryInstance -> Word32 -> Word8 -> IO (WasmResult Word32) # | |
| WasmMemory Text # |
The codec is strict UTF-8: an invalid byte sequence returns |
Defined in WasmEdge.Memory.Codec Methods wasmPeek :: MemoryInstance -> Word32 -> Word32 -> IO (WasmResult Text) # wasmPoke :: MemoryInstance -> Word32 -> Text -> IO (WasmResult Word32) # | |
| WasmMemory Bool # | |
Defined in WasmEdge.Memory.Codec Methods wasmPeek :: MemoryInstance -> Word32 -> Word32 -> IO (WasmResult Bool) # wasmPoke :: MemoryInstance -> Word32 -> Bool -> IO (WasmResult Word32) # | |
| WasmMemory Double # |
|
Defined in WasmEdge.Memory.Codec Methods wasmPeek :: MemoryInstance -> Word32 -> Word32 -> IO (WasmResult Double) # wasmPoke :: MemoryInstance -> Word32 -> Double -> IO (WasmResult Word32) # | |
| WasmMemory Float # |
|
Defined in WasmEdge.Memory.Codec Methods wasmPeek :: MemoryInstance -> Word32 -> Word32 -> IO (WasmResult Float) # wasmPoke :: MemoryInstance -> Word32 -> Float -> IO (WasmResult Word32) # | |
| (ToJSON a, FromJSON a) => WasmMemory (WasmJSON a) # | |
Defined in WasmEdge.Memory.Codec Methods wasmPeek :: MemoryInstance -> Word32 -> Word32 -> IO (WasmResult (WasmJSON a)) # wasmPoke :: MemoryInstance -> Word32 -> WasmJSON a -> IO (WasmResult Word32) # | |
| WasmMemory [Word8] # |
|
Defined in WasmEdge.Memory.Codec Methods wasmPeek :: MemoryInstance -> Word32 -> Word32 -> IO (WasmResult [Word8]) # wasmPoke :: MemoryInstance -> Word32 -> [Word8] -> IO (WasmResult Word32) # | |
| (FixedWasmMemory a, FixedWasmMemory b) => WasmMemory (a, b) # | A pair of fixed-size values is stored sequentially: |
Defined in WasmEdge.Memory.Codec Methods wasmPeek :: MemoryInstance -> Word32 -> Word32 -> IO (WasmResult (a, b)) # wasmPoke :: MemoryInstance -> Word32 -> (a, b) -> IO (WasmResult Word32) # | |
class WasmMemory a => FixedWasmMemory a where #
A WasmMemory type with a statically-known byte width.
Implement this for all primitive types.
Methods
wasmWidth :: Proxy a -> Word32 #
Byte width of the serialised form.
serialise :: a -> ByteString #
Serialise to exactly wasmWidth bytes.
deserialise :: ByteString -> Either String a #
Deserialise from at least wasmWidth bytes.
Instances
| FixedWasmMemory Int16 # | |
Defined in WasmEdge.Memory.Codec Methods wasmWidth :: Proxy Int16 -> Word32 # serialise :: Int16 -> ByteString # deserialise :: ByteString -> Either String Int16 # | |
| FixedWasmMemory Int32 # | |
Defined in WasmEdge.Memory.Codec Methods wasmWidth :: Proxy Int32 -> Word32 # serialise :: Int32 -> ByteString # deserialise :: ByteString -> Either String Int32 # | |
| FixedWasmMemory Int64 # | |
Defined in WasmEdge.Memory.Codec Methods wasmWidth :: Proxy Int64 -> Word32 # serialise :: Int64 -> ByteString # deserialise :: ByteString -> Either String Int64 # | |
| FixedWasmMemory Int8 # | |
Defined in WasmEdge.Memory.Codec Methods wasmWidth :: Proxy Int8 -> Word32 # serialise :: Int8 -> ByteString # deserialise :: ByteString -> Either String Int8 # | |
| FixedWasmMemory Word16 # | |
Defined in WasmEdge.Memory.Codec Methods wasmWidth :: Proxy Word16 -> Word32 # serialise :: Word16 -> ByteString # deserialise :: ByteString -> Either String Word16 # | |
| FixedWasmMemory Word32 # | |
Defined in WasmEdge.Memory.Codec Methods wasmWidth :: Proxy Word32 -> Word32 # serialise :: Word32 -> ByteString # deserialise :: ByteString -> Either String Word32 # | |
| FixedWasmMemory Word64 # | |
Defined in WasmEdge.Memory.Codec Methods wasmWidth :: Proxy Word64 -> Word32 # serialise :: Word64 -> ByteString # deserialise :: ByteString -> Either String Word64 # | |
| FixedWasmMemory Word8 # | |
Defined in WasmEdge.Memory.Codec Methods wasmWidth :: Proxy Word8 -> Word32 # serialise :: Word8 -> ByteString # deserialise :: ByteString -> Either String Word8 # | |
| FixedWasmMemory Bool # | |
Defined in WasmEdge.Memory.Codec Methods wasmWidth :: Proxy Bool -> Word32 # serialise :: Bool -> ByteString # deserialise :: ByteString -> Either String Bool # | |
| FixedWasmMemory Double # | |
Defined in WasmEdge.Memory.Codec Methods wasmWidth :: Proxy Double -> Word32 # serialise :: Double -> ByteString # deserialise :: ByteString -> Either String Double # | |
| FixedWasmMemory Float # | |
Defined in WasmEdge.Memory.Codec Methods wasmWidth :: Proxy Float -> Word32 # serialise :: Float -> ByteString # deserialise :: ByteString -> Either String Float # | |
Newtype wrapper to derive WasmMemory for any type with ToJSON and FromJSON via DerivingVia.
data Person = Person { name :: Text, age :: Int }
deriving stock (Generic, Show, Eq)
deriving anyclass (ToJSON, FromJSON)
deriving (WasmMemory) via (WasmJSON Person)
Constructors
| WasmJSON | |
Fields
| |
Instances
| FromJSON a => FromJSON (WasmJSON a) # | |
Defined in WasmEdge.Memory.Codec | |
| ToJSON a => ToJSON (WasmJSON a) # | |
| Show a => Show (WasmJSON a) # | |
| Eq a => Eq (WasmJSON a) # | |
| (ToJSON a, FromJSON a) => WasmMemory (WasmJSON a) # | |
Defined in WasmEdge.Memory.Codec Methods wasmPeek :: MemoryInstance -> Word32 -> Word32 -> IO (WasmResult (WasmJSON a)) # wasmPoke :: MemoryInstance -> Word32 -> WasmJSON a -> IO (WasmResult Word32) # | |
jsonWasmPoke :: ToJSON a => MemoryInstance -> Word32 -> a -> IO (WasmResult Word32) #
Write any ToJSON value into linear memory as a UTF-8 JSON blob.
Returns the number of bytes written.
jsonWasmPeek :: FromJSON a => MemoryInstance -> Word32 -> Word32 -> IO (WasmResult a) #
Read a UTF-8 JSON blob from linear memory and decode it as a FromJSON
value.
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.
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
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.
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.
High-Level Guest Calling & Bindgen (re-exported from WasmEdge.Bindgen)
module WasmEdge.Bindgen
Lifted Host Functions (re-exported from WasmEdge.Function.Memory)
Arguments
| :: (WasmMemory a, WasmMemory b) | |
| => (a -> IO (Either WasmEdgeError b)) | The Haskell function to lift |
| -> CallingFrame | |
| -> [WasmVal] | |
| -> IO (WasmResult [WasmVal]) |
Lift a Haskell function a -> IO (Either WasmEdgeError b) into a raw
HostFunc that marshals arguments through Wasm linear memory.
The generated host function:
- Expects two
i32arguments:(ptr, len)— the pointer and length of the input buffer in Wasm linear memory. - Reads and deserialises
afrom that buffer usingwasmPeek. - Runs the user function.
- Serialises
binto a scratch region of Wasm linear memory at 1 MiB offset. - Returns a single
i64with(retPtr << 32 | retLen).
The function type to register with createFunction is [I32, I32] -> [I64].
Note: The scratch-region approach works for output up to ~63 MiB. For
larger outputs or when the Wasm module manages its own heap, use
liftMemFuncWith instead.
liftMemFunc_ :: WasmMemory a => (a -> IO (Either WasmEdgeError ())) -> CallingFrame -> [WasmVal] -> IO (WasmResult [WasmVal]) #
Like liftMemFunc but for functions with no return value (pure side-effects).
The generated host function returns no values.
The function type for createFunction is [I32, I32] -> [].
Arguments
| :: (WasmMemory a, WasmMemory b) | |
| => VM | The VM (used to call the guest's |
| -> String | Module name (identifies which module's |
| -> AllocConvention | Allocation convention (names the |
| -> (a -> IO (Either WasmEdgeError b)) | |
| -> CallingFrame | |
| -> [WasmVal] | |
| -> IO (WasmResult [WasmVal]) |
Advanced variant of liftMemFunc that uses the AllocConvention to
call the guest's alloc export for output buffer management.
This is the production-grade approach: the guest controls the heap, and the
host allocates an output buffer via alloc before writing the result. The
VM reference is needed to call back into the guest's alloc export.
The generated host function signature is [I32, I32] -> [I64] (same as
liftMemFunc).
callingFrameMemory :: CallingFrame -> IO (WasmResult MemoryInstance) #
Get the linear memory instance from a CallingFrame.
The calling frame always has memory index 0 available if the guest module declares linear memory, which all practical Wasm modules do.
Returns Left if no linear memory is available (null pointer from C API).
Caller memory helpers (re-exported from WasmEdge.Caller)
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.