Struct walrus::Module

source ·
pub struct Module {
Show 15 fields pub imports: ModuleImports, pub tables: ModuleTables, pub types: ModuleTypes, pub funcs: ModuleFunctions, pub globals: ModuleGlobals, pub locals: ModuleLocals, pub exports: ModuleExports, pub memories: ModuleMemories, pub data: ModuleData, pub elements: ModuleElements, pub start: Option<FunctionId>, pub producers: ModuleProducers, pub customs: ModuleCustomSections, pub debug: ModuleDebugData, pub name: Option<String>, /* private fields */
}
Expand description

A wasm module.

Fields§

§imports: ModuleImports§tables: ModuleTables§types: ModuleTypes§funcs: ModuleFunctions§globals: ModuleGlobals§locals: ModuleLocals§exports: ModuleExports§memories: ModuleMemories§data: ModuleData

Registration of passive data segments, if any

§elements: ModuleElements

Registration of passive element segments, if any

§start: Option<FunctionId>

The start function, if any

§producers: ModuleProducers

Representation of the eventual custom section, producers

§customs: ModuleCustomSections

Custom sections found in this module.

§debug: ModuleDebugData

Dwarf debug data.

§name: Option<String>

The name of this module, used for debugging purposes in the name custom section.

Implementations§

source§

impl Module

source

pub fn write_graphviz_dot(&self, path: impl AsRef<Path>) -> Result<()>

Generate a GraphViz Dot file for this module, showing the relationship between various structures in the module and its instructions.

Example

First, generate a .dot file with this method:

let my_module: walrus::Module = get_module_from_somewhere();
my_module.write_graphviz_dot("my_module.dot")?;

Second, use the dot command-line tool to render an SVG (or PNG, etc…):

dot my_module.dot \       # Provide our generated `.dot`.
    -T svg \              # Generate an SVG image.
    -o my_module.svg      # Write to this output file.
source§

impl Module

source

pub fn get_memory_id(&self) -> Result<MemoryId>

Retrieve the ID for the first exported memory.

This method does not work in contexts with multi-memory enabled, and will error if more than one memory is present.

source

pub fn replace_exported_func( &mut self, fid: FunctionId, builder_fn: impl FnOnce((&mut InstrSeqBuilder<'_>, &Vec<LocalId>)) ) -> Result<FunctionId>

Replace a single exported function with the result of the provided builder function.

The builder function is provided a mutable reference to an InstrSeqBuilder which can be used to build the function as necessary.

For example, if you wanted to replace an exported function with a no-op,

module.replace_exported_func(fid, |(body, arg_locals)| {
    builder.func_body().unreachable();
});

The arguments passed to the original function will be passed to the new exported function that was built in your closure.

This function returns the function ID of the new function, after it has been inserted into the module as an export.

source

pub fn replace_imported_func( &mut self, fid: FunctionId, builder_fn: impl FnOnce((&mut InstrSeqBuilder<'_>, &Vec<LocalId>)) ) -> Result<FunctionId>

Replace a single imported function with the result of the provided builder function.

The builder function is provided a mutable reference to an InstrSeqBuilder which can be used to build the function as necessary.

For example, if you wanted to replace an imported function with a no-op,

module.replace_imported_func(fid, |(body, arg_locals)| {
    builder.func_body().unreachable();
});

The arguments passed to the original function will be passed to the new exported function that was built in your closure.

This function returns the function ID of the new function, and removes the existing import that has been replaced (the function will become local).

source§

impl Module

source

pub fn add_import_func( &mut self, module: &str, name: &str, ty: TypeId ) -> (FunctionId, ImportId)

Add an imported function to this module

source

pub fn add_import_memory( &mut self, module: &str, name: &str, shared: bool, initial: u32, maximum: Option<u32> ) -> (MemoryId, ImportId)

Add an imported memory to this module

source

pub fn add_import_table( &mut self, module: &str, name: &str, initial: u32, max: Option<u32>, ty: ValType ) -> (TableId, ImportId)

Add an imported table to this module

source

pub fn add_import_global( &mut self, module: &str, name: &str, ty: ValType, mutable: bool ) -> (GlobalId, ImportId)

Add an imported global to this module

source§

impl Module

source

pub fn with_config(config: ModuleConfig) -> Self

Create a default, empty module that uses the given configuration.

source

pub fn from_file<P>(path: P) -> Result<Module>where P: AsRef<Path>,

Construct a new module from the given path with the default configuration.

source

pub fn from_file_with_config<P>( path: P, config: &ModuleConfig ) -> Result<Module>where P: AsRef<Path>,

Construct a new module from the given path and configuration.

source

pub fn from_buffer(wasm: &[u8]) -> Result<Module>

Construct a new module from the in-memory wasm buffer with the default configuration.

source

pub fn emit_wasm_file<P>(&mut self, path: P) -> Result<()>where P: AsRef<Path>,

Emit this module into a .wasm file at the given path.

source

pub fn emit_wasm(&mut self) -> Vec<u8>

Emit this module into an in-memory wasm buffer.

source

pub fn functions(&self) -> impl Iterator<Item = &Function>

Returns an iterator over all functions in this module

Trait Implementations§

source§

impl Debug for Module

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Module

source§

fn default() -> Module

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Module

§

impl Send for Module

§

impl Sync for Module

§

impl Unpin for Module

§

impl !UnwindSafe for Module

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.