Struct orca_wasm::ir::module::Module

source ·
pub struct Module<'a> {
    pub module_name: Option<String>,
    pub types: ModuleTypes,
    pub imports: ModuleImports<'a>,
    pub functions: Functions<'a>,
    pub tables: ModuleTables<'a>,
    pub memories: Vec<MemoryType>,
    pub globals: ModuleGlobals,
    pub data: Vec<DataSegment>,
    pub exports: ModuleExports,
    pub start: Option<FunctionID>,
    pub elements: Vec<(ElementKind<'a>, ElementItems<'a>)>,
    pub custom_sections: CustomSections<'a>,
    /* private fields */
}
Expand description

Intermediate Representation of a wasm module. See the WASM Spec for different sections.

Fields§

§module_name: Option<String>

name of module

§types: ModuleTypes

Types

§imports: ModuleImports<'a>

Imports

§functions: Functions<'a>

Mapping from function index to type index. Note that |functions| == num_functions + num_imported_functions

§tables: ModuleTables<'a>

Each table has a type and optional initialization expression.

§memories: Vec<MemoryType>

Memories

§globals: ModuleGlobals

Globals

§data: Vec<DataSegment>

Data Sections

§exports: ModuleExports

Exports

§start: Option<FunctionID>

Index of the start function.

§elements: Vec<(ElementKind<'a>, ElementItems<'a>)>

Elements

§custom_sections: CustomSections<'a>

Custom Sections

Implementations§

source§

impl<'a> Module<'a>

source

pub fn parse(wasm: &'a [u8], enable_multi_memory: bool) -> Result<Self, Error>

Parses a Module from a wasm binary.

§Example
use orca_wasm::Module;

let file = "path_to_file";
let buff = wat::parse_file(file).expect("couldn't convert the input wat to Wasm");
let module = Module::parse(&buff, false).unwrap();
source

pub fn emit_wasm(&mut self, file_name: &str) -> Result<(), Error>

Emit the module into a wasm binary file.

source

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

Encode the module into a wasm binary.

§Example
use orca_wasm::Module;

let file = "path_to_file";
let buff = wat::parse_file(file).expect("couldn't convert the input wat to Wasm");
let mut module = Module::parse(&buff, false).unwrap();
let result = module.encode();
source

pub fn add_data(&mut self, data: DataSegment) -> DataSegmentID

Add a new Data Segment to the module. Returns the index of the new Data Segment in the Data Section.

source

pub fn get_memory_id(&self) -> Option<MemoryID>

Get the memory ID of a module. Does not support multiple memories

source

pub fn add_import_func( &mut self, module: String, name: String, ty_id: TypeID, ) -> (FunctionID, ImportsID)

Add a new function to the module, returns:

  • FunctionID: The ID that indexes into the function ID space. To be used when referring to the function, like in call.
  • ImportsID: The ID that indexes into the import section.
source

pub fn num_import_func(&self) -> u32

Get the number of imported functions in the module (including any added ones).

source

pub fn delete_func(&mut self, function_id: FunctionID)

Delete a function from the module.

source

pub fn convert_local_fn_to_import( &mut self, function_id: FunctionID, module: String, name: String, ty_id: TypeID, ) -> bool

Convert a local function to an imported function. Continue using the FunctionID as normal (like in call instructions), this library will take care of ID changes for you during encoding. Returns false if it is an imported function.

source

pub fn set_fn_name(&mut self, id: FunctionID, name: String)

Set the name of a function using its ID.

source

pub fn add_global( &mut self, init_expr: InitExpr, content_ty: DataType, mutable: bool, shared: bool, ) -> GlobalID

Create a new locally-defined global and add it to the module.

source

pub fn add_imported_global( &mut self, module: String, name: String, content_ty: DataType, mutable: bool, shared: bool, ) -> (GlobalID, ImportsID)

Add a new imported global to the module, returns:

  • GlobalID: The ID that indexes into the global ID space. To be used when referring to the global, like in global.get.
  • ImportsID: The ID that indexes into the import section.
source

pub fn delete_global(&mut self, global_id: GlobalID)

Delete a global from the module (can either be an imported or locally-defined global). Use the global ID for this operation, not the import ID!

Trait Implementations§

source§

impl<'a> Debug for Module<'a>

source§

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

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

impl<'a> Default for Module<'a>

source§

fn default() -> Module<'a>

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

Auto Trait Implementations§

§

impl<'a> Freeze for Module<'a>

§

impl<'a> RefUnwindSafe for Module<'a>

§

impl<'a> Send for Module<'a>

§

impl<'a> Sync for Module<'a>

§

impl<'a> Unpin for Module<'a>

§

impl<'a> UnwindSafe for Module<'a>

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.