pub struct Module { /* private fields */ }Implementations§
Source§impl Module
impl Module
Sourcepub fn new(wasm_bytes: &[u8]) -> Result<Self, ZwasmError>
pub fn new(wasm_bytes: &[u8]) -> Result<Self, ZwasmError>
Creates a new WebAssembly module instance from raw Wasm bytes using the zwasm runtime.
This is the most basic way to instantiate a Wasm module. For WASI or custom configuration, see Self::new_wasi and Self::new_configured.
Sourcepub fn new_wasi(wasm_bytes: &[u8]) -> Result<Self, ZwasmError>
pub fn new_wasi(wasm_bytes: &[u8]) -> Result<Self, ZwasmError>
Creates a new WASI-enabled module instance from raw Wasm bytes using the zwasm runtime.
WASI syscalls are enabled for this module. For custom WASI configuration, use Self::new_wasi_configured.
Sourcepub fn new_configured(
wasm_bytes: &[u8],
config: &Config,
) -> Result<Self, ZwasmError>
pub fn new_configured( wasm_bytes: &[u8], config: &Config, ) -> Result<Self, ZwasmError>
Creates a module with an explicit runtime configuration (memory, fuel, limits, etc).
Allows fine-grained control over the execution environment. See Config for options.
Sourcepub fn new_wasi_configured(
wasm_bytes: &[u8],
wasi_config: &WasiConfig,
) -> Result<Self, ZwasmError>
pub fn new_wasi_configured( wasm_bytes: &[u8], wasi_config: &WasiConfig, ) -> Result<Self, ZwasmError>
Creates a WASI-enabled module with an explicit WASI configuration.
Use this to provide argv, env, preopens, and other WASI settings. See WasiConfig.
Sourcepub fn new_wasi_configured2(
wasm_bytes: &[u8],
wasi_config: &WasiConfig,
config: &Config,
) -> Result<Self, ZwasmError>
pub fn new_wasi_configured2( wasm_bytes: &[u8], wasi_config: &WasiConfig, config: &Config, ) -> Result<Self, ZwasmError>
Creates a WASI-enabled module with both WASI and runtime configuration.
Combines all options from Config and WasiConfig.
Sourcepub fn new_with_imports(
wasm_bytes: &[u8],
imports: &Imports,
) -> Result<Self, ZwasmError>
pub fn new_with_imports( wasm_bytes: &[u8], imports: &Imports, ) -> Result<Self, ZwasmError>
Creates a module with host functions registered via Imports.
Use this to expose custom native functions to Wasm code.
Sourcepub fn validate(wasm_bytes: &[u8]) -> Result<(), ZwasmError>
pub fn validate(wasm_bytes: &[u8]) -> Result<(), ZwasmError>
Validates raw Wasm bytes for correctness according to the Wasm spec, without instantiating a module.
Returns Ok(()) if the bytes are valid Wasm, or an error otherwise.
Sourcepub fn invoke(&self, name: &str, args: &[u64]) -> Result<Vec<u64>, ZwasmError>
pub fn invoke(&self, name: &str, args: &[u64]) -> Result<Vec<u64>, ZwasmError>
Invokes an exported function by name in the instantiated Wasm module.
Arguments and return values are always 64-bit (Wasm i32/i64/f32/f64 are bitcast as needed). This is the main entrypoint for calling Wasm code from Rust.
§Safety
This method is safe to call from Rust, but it ultimately delegates to the native runtime. Callers must ensure no concurrent native access is performed on the same module instance through other aliases (for example via raw pointers in foreign code).
Sourcepub fn invoke_start(&self) -> Result<(), ZwasmError>
pub fn invoke_start(&self) -> Result<(), ZwasmError>
Invokes the module start function, if present.
This is typically used for modules with a _start or similar entrypoint.
§Safety
This method is safe to call from Rust, but it executes in the native runtime.
The same aliasing/concurrency requirements as Self::invoke apply.
Sourcepub fn export_count(&self) -> u32
pub fn export_count(&self) -> u32
Returns the number of exported functions in the module.
Sourcepub fn export_name(&self, index: u32) -> Option<String>
pub fn export_name(&self, index: u32) -> Option<String>
Returns the export name at index, if present.
Returns None if the index is out of bounds.
Sourcepub fn export_param_count(&self, index: u32) -> u32
pub fn export_param_count(&self, index: u32) -> u32
Returns the number of parameters for the export at index.
Parameters are always 64-bit values.
Sourcepub fn export_result_count(&self, index: u32) -> u32
pub fn export_result_count(&self, index: u32) -> u32
Returns the number of results for the export at index.
Results are always 64-bit values.
Sourcepub fn cancel_handle(&self) -> CancelHandle
pub fn cancel_handle(&self) -> CancelHandle
Returns a thread-safe cancellation handle for this module.
Sourcepub fn cancel(&self)
pub fn cancel(&self)
Requests cancellation of currently running Wasm execution in this module.
§Safety
Cancellation behavior is implemented by the native runtime. Callers should only use this as an asynchronous signal and must not assume immediate termination semantics.
Sourcepub unsafe fn memory_data(&self) -> Option<&[u8]>
pub unsafe fn memory_data(&self) -> Option<&[u8]>
Returns a direct view of the module’s linear memory (if present).
This is a zero-copy view into the Wasm memory. Use with care.
§Safety
The returned slice points to memory owned by the runtime. The caller must ensure the memory is not mutated or invalidated (for example by invocation or memory growth) while this slice is alive.
Sourcepub fn memory_data_copy(&self) -> Option<Vec<u8>>
pub fn memory_data_copy(&self) -> Option<Vec<u8>>
Returns a copy of the current linear memory snapshot.
This is the safest way to access Wasm memory from Rust.
§Safety
This method avoids exposing a borrowed native memory view and is therefore preferred
over Self::memory_data when possible.
Sourcepub fn memory_size(&self) -> usize
pub fn memory_size(&self) -> usize
Returns the current linear memory size in bytes.
Sourcepub fn memory_read(&self, offset: u32, buf: &mut [u8]) -> Result<(), ZwasmError>
pub fn memory_read(&self, offset: u32, buf: &mut [u8]) -> Result<(), ZwasmError>
Reads bytes from the module’s linear memory into buf.
§Safety
This method is safe to call from Rust, but reads from native-managed memory. The
same aliasing/concurrency requirements as Self::invoke apply.
Sourcepub fn memory_write(&self, offset: u32, data: &[u8]) -> Result<(), ZwasmError>
pub fn memory_write(&self, offset: u32, data: &[u8]) -> Result<(), ZwasmError>
Writes bytes from data into the module’s linear memory.
§Safety
This method is safe to call from Rust, but writes to native-managed memory. The
same aliasing/concurrency requirements as Self::invoke apply.