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

WasmEdge

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:

Synopsis

Core types (re-exported from WasmEdge.Types)

data WasmVal #

A WebAssembly value, covering all WASM numeric and reference types.

Use the constructor directly or via encode and decode.

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 wide-word)

WasmFuncRef !(Maybe FuncRef)

Function reference (nullable). Nothing is a null funcref.

WasmExternRef !(Maybe ExternRef)

External reference (nullable). Nothing is a null externref.

Instances

Instances details
NFData WasmVal # 
Instance details

Defined in WasmEdge.Types

Methods

rnf :: WasmVal -> () #

Generic WasmVal # 
Instance details

Defined in WasmEdge.Types

Methods

from :: WasmVal -> Rep WasmVal x #

to :: Rep WasmVal x -> WasmVal #

Show WasmVal # 
Instance details

Defined in WasmEdge.Types

Eq WasmVal # 
Instance details

Defined in WasmEdge.Types

Methods

(==) :: WasmVal -> WasmVal -> Bool #

(/=) :: WasmVal -> WasmVal -> Bool #

WasmValType WasmVal # 
Instance details

Defined in WasmEdge.Function

type Rep WasmVal # 
Instance details

Defined in WasmEdge.Types

data ValType #

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 (funcref)

ExternRefT

External reference (externref)

Instances

Instances details
NFData ValType # 
Instance details

Defined in WasmEdge.Types

Methods

rnf :: ValType -> () #

Bounded ValType # 
Instance details

Defined in WasmEdge.Types

Enum ValType # 
Instance details

Defined in WasmEdge.Types

Generic ValType # 
Instance details

Defined in WasmEdge.Types

Associated Types

type Rep ValType 
Instance details

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))))

Methods

from :: ValType -> Rep ValType x #

to :: Rep ValType x -> ValType #

Show ValType # 
Instance details

Defined in WasmEdge.Types

Eq ValType # 
Instance details

Defined in WasmEdge.Types

Methods

(==) :: ValType -> ValType -> Bool #

(/=) :: ValType -> ValType -> Bool #

Ord ValType # 
Instance details

Defined in WasmEdge.Types

type Rep ValType # 
Instance details

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.

Constructors

Const

Immutable global

Var

Mutable global

Instances

Instances details
NFData Mutability # 
Instance details

Defined in WasmEdge.Types

Methods

rnf :: Mutability -> () #

Bounded Mutability # 
Instance details

Defined in WasmEdge.Types

Enum Mutability # 
Instance details

Defined in WasmEdge.Types

Generic Mutability # 
Instance details

Defined in WasmEdge.Types

Associated Types

type Rep Mutability 
Instance details

Defined in WasmEdge.Types

type Rep Mutability = D1 ('MetaData "Mutability" "WasmEdge.Types" "wasmedge-hs-0.17.1.0-inplace" 'False) (C1 ('MetaCons "Const" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Var" 'PrefixI 'False) (U1 :: Type -> Type))
Show Mutability # 
Instance details

Defined in WasmEdge.Types

Eq Mutability # 
Instance details

Defined in WasmEdge.Types

Ord Mutability # 
Instance details

Defined in WasmEdge.Types

type Rep Mutability # 
Instance details

Defined in WasmEdge.Types

type Rep Mutability = D1 ('MetaData "Mutability" "WasmEdge.Types" "wasmedge-hs-0.17.1.0-inplace" 'False) (C1 ('MetaCons "Const" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Var" 'PrefixI 'False) (U1 :: Type -> Type))

data Limit #

A WebAssembly limit (min, optional max) as used in table and memory types.

Constructors

Limit 

Fields

Instances

Instances details
NFData Limit # 
Instance details

Defined in WasmEdge.Types

Methods

rnf :: Limit -> () #

Generic Limit # 
Instance details

Defined in WasmEdge.Types

Associated Types

type Rep Limit 
Instance details

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))))

Methods

from :: Limit -> Rep Limit x #

to :: Rep Limit x -> Limit #

Show Limit # 
Instance details

Defined in WasmEdge.Types

Methods

showsPrec :: Int -> Limit -> ShowS #

show :: Limit -> String #

showList :: [Limit] -> ShowS #

Eq Limit # 
Instance details

Defined in WasmEdge.Types

Methods

(==) :: Limit -> Limit -> Bool #

(/=) :: Limit -> Limit -> Bool #

type Rep Limit # 
Instance details

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))))

data Proposal #

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

Instances details
NFData Proposal # 
Instance details

Defined in WasmEdge.Types

Methods

rnf :: Proposal -> () #

Bounded Proposal # 
Instance details

Defined in WasmEdge.Types

Enum Proposal # 
Instance details

Defined in WasmEdge.Types

Generic Proposal # 
Instance details

Defined in WasmEdge.Types

Associated Types

type Rep Proposal 
Instance details

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))))))

Methods

from :: Proposal -> Rep Proposal x #

to :: Rep Proposal x -> Proposal #

Show Proposal # 
Instance details

Defined in WasmEdge.Types

Eq Proposal # 
Instance details

Defined in WasmEdge.Types

Ord Proposal # 
Instance details

Defined in WasmEdge.Types

type Rep Proposal # 
Instance details

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))))))

data RunMode #

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

Instances details
NFData RunMode # 
Instance details

Defined in WasmEdge.Types

Methods

rnf :: RunMode -> () #

Bounded RunMode # 
Instance details

Defined in WasmEdge.Types

Enum RunMode # 
Instance details

Defined in WasmEdge.Types

Generic RunMode # 
Instance details

Defined in WasmEdge.Types

Associated Types

type Rep RunMode 
Instance details

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)))

Methods

from :: RunMode -> Rep RunMode x #

to :: Rep RunMode x -> RunMode #

Show RunMode # 
Instance details

Defined in WasmEdge.Types

Eq RunMode # 
Instance details

Defined in WasmEdge.Types

Methods

(==) :: RunMode -> RunMode -> Bool #

(/=) :: RunMode -> RunMode -> Bool #

Ord RunMode # 
Instance details

Defined in WasmEdge.Types

type Rep RunMode # 
Instance details

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

Instances details
NFData WasmEdgeError # 
Instance details

Defined in WasmEdge.Error

Methods

rnf :: WasmEdgeError -> () #

Exception WasmEdgeError # 
Instance details

Defined in WasmEdge.Error

Generic WasmEdgeError # 
Instance details

Defined in WasmEdge.Error

Associated Types

type Rep WasmEdgeError 
Instance details

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))))
Show WasmEdgeError # 
Instance details

Defined in WasmEdge.Error

Eq WasmEdgeError # 
Instance details

Defined in WasmEdge.Error

type Rep WasmEdgeError # 
Instance details

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 ErrCode)

ErrCategory_UserDefined

User-defined error code (e.g., from host functions)

ErrCategory_Unknown Word32

An unrecognised category (future-proofing)

Instances

Instances details
NFData ErrCategory # 
Instance details

Defined in WasmEdge.Error

Methods

rnf :: ErrCategory -> () #

Generic ErrCategory # 
Instance details

Defined in WasmEdge.Error

Associated Types

type Rep ErrCategory 
Instance details

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))))
Show ErrCategory # 
Instance details

Defined in WasmEdge.Error

Eq ErrCategory # 
Instance details

Defined in WasmEdge.Error

type Rep ErrCategory # 
Instance details

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

version :: IO Text #

The version string of the linked WasmEdge library (e.g., 0.18.0).

versionMajor :: IO Word32 #

Major version number.

versionMinor :: IO Word32 #

Minor version number.

versionPatch :: IO Word32 #

Patch version number.

VM (re-exported from WasmEdge.VM)

data 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.

Instances

Instances details
NFData VM # 
Instance details

Defined in WasmEdge.VM

Methods

rnf :: VM -> () #

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.

deleteVM :: VM -> IO () #

Delete a VM context explicitly.

withVM :: Maybe Configure -> Maybe Store -> (VM -> IO a) -> IO a #

Scoped VM context.

data VMConfig #

Declarative configuration record for initializing a VM. Inspired by wasmedge-sdk's VmBuilder in Rust.

Constructors

VMConfig 

defaultVMConfig :: VMConfig #

Default VM configuration with default options and fresh store.

withVMConfig :: VMConfig -> (VM -> IO a) -> IO a #

Scoped VM context with declarative VMConfig.

runWasmFile #

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

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.

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

wasmPeek #

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.

wasmPoke #

Arguments

:: MemoryInstance

The linear memory instance

-> Word32

Byte offset to write at

-> a

The value to serialise

-> IO (WasmResult Word32)

Right bytes written, or Left error

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

Instances details
WasmMemory ByteString #

ByteString — raw bytes, zero-copy.

The entire slice [offset .. offset+len) is returned/written as-is. Writing an empty ByteString is a no-op (returns Right 0).

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Int16 #

Int16 — 2 bytes, little-endian two's complement.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Int32 #

Int32 — 4 bytes, little-endian two's complement.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Int64 #

Int64 — 8 bytes, little-endian two's complement.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Int8 #

Int8 — 1 byte, two's complement.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Word16 #

Word16 — 2 bytes, little-endian.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Word32 #

Word32 — 4 bytes, little-endian.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Word64 #

Word64 — 8 bytes, little-endian.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Word8 #

Word8 — 1 byte, identity encoding.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Text #

Text — UTF-8 encoded bytes.

The codec is strict UTF-8: an invalid byte sequence returns Left with a WasmEdgeError rather than silently replacing bytes with the replacement character. Use 'WasmMemory ByteString' if you need raw bytes. Writing empty Text is a no-op (returns Right 0).

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Bool #

Bool — 1 byte: 0 = False, 1 = True.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Double #

Double — 8 bytes, IEEE 754 double-precision little-endian.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Float #

Float — 4 bytes, IEEE 754 single-precision little-endian.

Instance details

Defined in WasmEdge.Memory.Codec

(ToJSON a, FromJSON a) => WasmMemory (WasmJSON a) # 
Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory [Word8] #

[Word8] — raw bytes (wraps via ByteString).

Instance details

Defined in WasmEdge.Memory.Codec

(FixedWasmMemory a, FixedWasmMemory b) => WasmMemory (a, b) #

A pair of fixed-size values is stored sequentially: a at offset, b at offset + wasmWidth @a.

Instance details

Defined in WasmEdge.Memory.Codec

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

Instances details
FixedWasmMemory Int16 # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Int32 # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Int64 # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Int8 # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Word16 # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Word32 # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Word64 # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Word8 # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Bool # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Double # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Float # 
Instance details

Defined in WasmEdge.Memory.Codec

newtype WasmJSON a #

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

Instances details
FromJSON a => FromJSON (WasmJSON a) # 
Instance details

Defined in WasmEdge.Memory.Codec

ToJSON a => ToJSON (WasmJSON a) # 
Instance details

Defined in WasmEdge.Memory.Codec

Show a => Show (WasmJSON a) # 
Instance details

Defined in WasmEdge.Memory.Codec

Methods

showsPrec :: Int -> WasmJSON a -> ShowS #

show :: WasmJSON a -> String #

showList :: [WasmJSON a] -> ShowS #

Eq a => Eq (WasmJSON a) # 
Instance details

Defined in WasmEdge.Memory.Codec

Methods

(==) :: WasmJSON a -> WasmJSON a -> Bool #

(/=) :: WasmJSON a -> WasmJSON a -> Bool #

(ToJSON a, FromJSON a) => WasmMemory (WasmJSON a) # 
Instance details

Defined in WasmEdge.Memory.Codec

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.

withWasmBuffer #

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):

Internally, this bracket:

  1. Allocates a scratch buffer in Wasm linear memory via the alloc export.
  2. Serialises the input a into that buffer using wasmPoke.
  3. Calls the Wasm function with (inputPtr, inputLen).
  4. Decodes the return value as (retPtr, retLen).
  5. Reads and deserialises b from Wasm memory at (retPtr, retLen).
  6. 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

wasmAlloc #

Arguments

:: VM 
-> ModuleName 
-> AllocConvention 
-> Word32

Number of bytes to allocate

-> IO (WasmResult Word32) 

Allocate n bytes inside a Wasm module's linear memory by calling the module-exported alloc function.

Returns the pointer (as Word32) on success, or Left on failure.

wasmDealloc #

Arguments

:: VM 
-> ModuleName 
-> AllocConvention 
-> Word32

Pointer returned by wasmAlloc

-> 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

ptr packed into upper 32 bits, len in lower 32 bits of a single i64. Guest: return ((ptr as i64) << 32) | (len as i64)

SeparateI32s

Two separate i32 return values: [WasmI32 ptr, WasmI32 len].

defaultConvention :: AllocConvention #

The default convention: alloc, dealloc, packed i64 result, 64 KiB scratch.

High-Level Guest Calling & Bindgen (re-exported from WasmEdge.Bindgen)

Lifted Host Functions (re-exported from WasmEdge.Function.Memory)

liftMemFunc #

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 i32 arguments: (ptr, len) — the pointer and length of the input buffer in Wasm linear memory.
  • Reads and deserialises a from that buffer using wasmPeek.
  • Runs the user function.
  • Serialises b into a scratch region of Wasm linear memory at 1 MiB offset.
  • Returns a single i64 with (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] -> [].

liftMemFuncWith #

Arguments

:: (WasmMemory a, WasmMemory b) 
=> VM

The VM (used to call the guest's alloc export)

-> String

Module name (identifies which module's alloc to call)

-> AllocConvention

Allocation convention (names the alloc / dealloc exports)

-> (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.

readCallerMemory #

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.

writeCallerMemory #

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.

readCallerCString #

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.