Skip to main content

Module

Struct Module 

Source
pub struct Module { /* private fields */ }

Implementations§

Source§

impl Module

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

Source

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.

Source

pub fn export_count(&self) -> u32

Returns the number of exported functions in the module.

Source

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.

Source

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.

Source

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.

Source

pub fn cancel_handle(&self) -> CancelHandle

Returns a thread-safe cancellation handle for this module.

Source

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.

Source

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.

Source

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.

Source

pub fn memory_size(&self) -> usize

Returns the current linear memory size in bytes.

Source

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.

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.