| Copyright | (c) 2026 Tushar Adhatrao |
|---|---|
| License | MIT |
| Stability | stable |
| Safe Haskell | None |
| Language | GHC2021 |
WasmEdge.VM
Description
The VM context provides the highest-level interface for running WASM
modules. It bundles a loader, validator, executor, and store internally.
Quick start
import WasmEdge.VM import WasmEdge.Types main :: IO () main = do result withVM Nothing Nothing $ vm - do runWasmFile vm "hello.wasm" "main" [] case result of Left err -> putStrLn $ "Error: " ++ show err Right vals -> print vals
Step-by-step pipeline
For more control, use the load → validate → instantiate → execute pipeline:
withVM (Just cfg) Nothing $ vm -> do loadWasmFile vm "module.wasm" >>= throwOnFail validate vm >>= throwOnFail instantiate vm >>= throwOnFail execute vm "run" [WasmI32 42] >>= either throwIO pure
Synopsis
- 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
- createVMWithConfig :: VMConfig -> IO VM
- withVMConfig :: VMConfig -> (VM -> IO a) -> IO a
- runWasmFile :: VM -> FilePath -> Text -> [WasmVal] -> IO (WasmResult [WasmVal])
- runWasmBytes :: VM -> ByteString -> Text -> [WasmVal] -> IO (WasmResult [WasmVal])
- asyncRunWasmFile :: VM -> FilePath -> Text -> [WasmVal] -> IO Async
- asyncRunWasmBytes :: VM -> ByteString -> Text -> [WasmVal] -> IO Async
- registerModuleFromFile :: VM -> Text -> FilePath -> IO (WasmResult ())
- registerModuleFromBytes :: VM -> Text -> ByteString -> IO (WasmResult ())
- registerModuleFromImport :: VM -> ModuleInstance -> IO (WasmResult ())
- registerModuleFromImportWithAlias :: VM -> Text -> ModuleInstance -> IO (WasmResult ())
- 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])
- executeRegistered :: VM -> Text -> Text -> [WasmVal] -> IO (WasmResult [WasmVal])
- asyncExecute :: VM -> Text -> [WasmVal] -> IO Async
- asyncExecuteRegistered :: VM -> Text -> Text -> [WasmVal] -> IO Async
- getFunctionType :: VM -> Text -> IO (Maybe FunctionType)
- getFunctionTypeRegistered :: VM -> Text -> Text -> IO (Maybe FunctionType)
- cleanup :: VM -> IO ()
- getStore :: VM -> IO Store
- getStatistics :: VM -> IO Statistics
- listRegisteredModules :: VM -> IO [Text]
- deleteRegisteredModule :: VM -> Text -> IO ()
VM context
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 VM configuration
Declarative configuration record for initializing a VM.
Inspired by wasmedge-sdk's VmBuilder in Rust.
Default VM configuration with default options and fresh store.
One-shot execution
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.
Async one-shot execution
asyncRunWasmFile :: VM -> FilePath -> Text -> [WasmVal] -> IO Async #
Async variant of runWasmFile.
asyncRunWasmBytes :: VM -> ByteString -> Text -> [WasmVal] -> IO Async #
Async variant of runWasmBytes.
Module registration
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.
Step-by-step pipeline
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.
executeRegistered :: VM -> Text -> Text -> [WasmVal] -> IO (WasmResult [WasmVal]) #
Execute a function from a named registered module.
Async step-by-step
asyncExecuteRegistered :: VM -> Text -> Text -> [WasmVal] -> IO Async #
Async variant of executeRegistered.
Query
getFunctionType :: VM -> Text -> IO (Maybe FunctionType) #
Get the function type of an exported function.
getFunctionTypeRegistered :: VM -> Text -> Text -> IO (Maybe FunctionType) #
Get the function type of a function in a registered module.
getStatistics :: VM -> IO Statistics #
Get the statistics context from the VM (borrowed).
listRegisteredModules :: VM -> IO [Text] #
List all registered module names.
deleteRegisteredModule :: VM -> Text -> IO () #
Delete a registered module from the VM by name (0.18+).