| Copyright | (c) 2026 Tushar Adhatrao |
|---|---|
| License | MIT |
| Stability | stable |
| Safe Haskell | None |
| Language | GHC2021 |
WasmEdge.Module
Description
Provides types and functions for managing WebAssembly module instances:
ModuleInstance— an instantiated WASM module or a custom host module- Host module construction via
buildHostModuleoraddFunctionaddMemoryetc. - Full WASI initialization and exit code querying via
createWASIModule
Synopsis
- newtype ModuleInstance = ModuleInstance (OwnedCtx ModuleInstanceTag)
- createHostModule :: Text -> IO ModuleInstance
- createWASIModule :: [Text] -> [Text] -> [Text] -> IO ModuleInstance
- deleteModuleInstance :: ModuleInstance -> IO ()
- withHostModule :: Text -> (ModuleInstance -> IO a) -> IO a
- withWASIModule :: [Text] -> [Text] -> [Text] -> (ModuleInstance -> IO a) -> IO a
- initWASI :: ModuleInstance -> [Text] -> [Text] -> [Text] -> IO ()
- getWASIExitCode :: ModuleInstance -> IO Word32
- getModuleName :: ModuleInstance -> IO Text
- findFunction :: ModuleInstance -> Text -> IO (Maybe FunctionInstance)
- findTable :: ModuleInstance -> Text -> IO (Maybe TableInstance)
- findMemory :: ModuleInstance -> Text -> IO (Maybe MemoryInstance)
- findGlobal :: ModuleInstance -> Text -> IO (Maybe GlobalInstance)
- findTag :: ModuleInstance -> Text -> IO (Maybe TagInstance)
- listFunctions :: ModuleInstance -> IO [Text]
- listTables :: ModuleInstance -> IO [Text]
- listMemories :: ModuleInstance -> IO [Text]
- listGlobals :: ModuleInstance -> IO [Text]
- listTags :: ModuleInstance -> IO [Text]
- addFunction :: ModuleInstance -> Text -> FunctionInstance -> IO ()
- addTable :: ModuleInstance -> Text -> TableInstance -> IO ()
- addMemory :: ModuleInstance -> Text -> MemoryInstance -> IO ()
- addGlobal :: ModuleInstance -> Text -> GlobalInstance -> IO ()
- data ModuleBuilder
- buildHostModule :: Text -> (ModuleBuilder -> IO ()) -> IO ModuleInstance
- builderAddFunction :: ModuleBuilder -> Text -> FunctionInstance -> IO ()
- builderAddHostFunc :: ToHostFunc f => ModuleBuilder -> Text -> f -> IO ()
- builderAddTable :: ModuleBuilder -> Text -> TableInstance -> IO ()
- builderAddMemory :: ModuleBuilder -> Text -> MemoryInstance -> IO ()
- builderAddGlobal :: ModuleBuilder -> Text -> GlobalInstance -> IO ()
- unsafeModulePtr :: ModuleInstance -> IO (Ptr ModuleInstanceTag)
- borrowModule :: Ptr ModuleInstanceTag -> IO ModuleInstance
- ownModule :: Ptr ModuleInstanceTag -> IO ModuleInstance
- disarmModule :: ModuleInstance -> IO ()
Module instance
newtype ModuleInstance #
A WebAssembly module instance (guest module or host module).
Constructors
| ModuleInstance (OwnedCtx ModuleInstanceTag) |
createHostModule :: Text -> IO ModuleInstance #
Create an empty host module instance with the given module name.
createWASIModule :: [Text] -> [Text] -> [Text] -> IO ModuleInstance #
Create a WASI module instance initialized with arguments, environment variables, and preopened directory mappings.
args: command-line arguments (e.g.,["prog", "arg1"])envs: environment variables in"KEY=VALUE"formatpreopens: directory mappings in"guest_dir:host_dir"format
deleteModuleInstance :: ModuleInstance -> IO () #
Delete a ModuleInstance.
withHostModule :: Text -> (ModuleInstance -> IO a) -> IO a #
Scoped host module instance.
withWASIModule :: [Text] -> [Text] -> [Text] -> (ModuleInstance -> IO a) -> IO a #
Scoped WASI module instance.
WASI functions
initWASI :: ModuleInstance -> [Text] -> [Text] -> [Text] -> IO () #
Re-initialize WASI parameters on an existing WASI module instance.
getWASIExitCode :: ModuleInstance -> IO Word32 #
Get the exit code from WASI execution.
Queries
getModuleName :: ModuleInstance -> IO Text #
Get the exported module name.
findFunction :: ModuleInstance -> Text -> IO (Maybe FunctionInstance) #
Find an exported function by name.
findTable :: ModuleInstance -> Text -> IO (Maybe TableInstance) #
Find an exported table by name.
findMemory :: ModuleInstance -> Text -> IO (Maybe MemoryInstance) #
Find an exported memory by name.
findGlobal :: ModuleInstance -> Text -> IO (Maybe GlobalInstance) #
Find an exported global by name.
findTag :: ModuleInstance -> Text -> IO (Maybe TagInstance) #
Find an exported exception tag by name (0.18+).
listFunctions :: ModuleInstance -> IO [Text] #
List all exported function names.
listTables :: ModuleInstance -> IO [Text] #
List all exported table names.
listMemories :: ModuleInstance -> IO [Text] #
List all exported memory names.
listGlobals :: ModuleInstance -> IO [Text] #
List all exported global names.
listTags :: ModuleInstance -> IO [Text] #
List all exported tag names (0.18+).
Population (building host modules)
addFunction :: ModuleInstance -> Text -> FunctionInstance -> IO () #
Add a FunctionInstance to a host module under the given export name.
Moves ownership of the function instance into the module.
addTable :: ModuleInstance -> Text -> TableInstance -> IO () #
Add a TableInstance to a host module under the given export name.
Moves ownership of the table instance into the module.
addMemory :: ModuleInstance -> Text -> MemoryInstance -> IO () #
Add a MemoryInstance to a host module under the given export name.
Moves ownership of the memory instance into the module.
addGlobal :: ModuleInstance -> Text -> GlobalInstance -> IO () #
Add a GlobalInstance to a host module under the given export name.
Moves ownership of the global instance into the module.
Builder pattern
data ModuleBuilder #
A builder for incrementally assembling a host module.
buildHostModule :: Text -> (ModuleBuilder -> IO ()) -> IO ModuleInstance #
Build a host module using the builder pattern.
builderAddFunction :: ModuleBuilder -> Text -> FunctionInstance -> IO () #
Add a function in a ModuleBuilder.
builderAddHostFunc :: ToHostFunc f => ModuleBuilder -> Text -> f -> IO () #
Add a typed Haskell host function to a host module builder, automatically inferring its WebAssembly signature and handling argument/result conversions.
builderAddTable :: ModuleBuilder -> Text -> TableInstance -> IO () #
Add a table in a ModuleBuilder.
builderAddMemory :: ModuleBuilder -> Text -> MemoryInstance -> IO () #
Add a memory in a ModuleBuilder.
builderAddGlobal :: ModuleBuilder -> Text -> GlobalInstance -> IO () #
Add a global in a ModuleBuilder.
Internal / borrowed pointers
unsafeModulePtr :: ModuleInstance -> IO (Ptr ModuleInstanceTag) #
Extract the raw pointer of a ModuleInstance.
borrowModule :: Ptr ModuleInstanceTag -> IO ModuleInstance #
Create a borrowed ModuleInstance from a raw pointer (does not finalize).
ownModule :: Ptr ModuleInstanceTag -> IO ModuleInstance #
Create an owned ModuleInstance from a raw pointer (finalizes with c_ModuleInstanceDelete).
disarmModule :: ModuleInstance -> IO () #
Disarm the GC finalizer of an owned ModuleInstance.
Used when ownership is transferred to the C runtime or force-deleted by the VM.