pub struct Module<'a> {Show 13 fields
pub module_name: Option<String>,
pub types: ModuleTypes,
pub imports: ModuleImports<'a>,
pub functions: Functions<'a>,
pub tables: ModuleTables<'a>,
pub memories: Memories,
pub globals: ModuleGlobals,
pub data: Vec<DataSegment>,
pub exports: ModuleExports,
pub start: Option<FunctionID>,
pub elements: Vec<(ElementKind<'a>, ElementItems<'a>)>,
pub tags: Vec<TagType>,
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: Memories
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
Tags
custom_sections: CustomSections<'a>
Custom Sections
Implementations§
Source§impl<'a> Module<'a>
impl<'a> Module<'a>
Sourcepub fn parse(wasm: &'a [u8], enable_multi_memory: bool) -> Result<Self, Error>
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();
Sourcepub fn get_func_metadata(&self) -> Vec<(FunctionID, usize)>
pub fn get_func_metadata(&self) -> Vec<(FunctionID, usize)>
Creates Vec of (Function, Number of Instructions)
Sourcepub fn emit_wasm(&mut self, file_name: &str) -> Result<(), Error>
pub fn emit_wasm(&mut self, file_name: &str) -> Result<(), Error>
Emit the module into a wasm binary file.
Sourcepub fn encode(&mut self) -> Vec<u8> ⓘ
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();
Sourcepub fn add_data(&mut self, data: DataSegment) -> DataSegmentID
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.
Sourcepub fn get_memory_id(&self) -> Option<MemoryID>
pub fn get_memory_id(&self) -> Option<MemoryID>
Get the memory ID of a module. Does not support multiple memories
pub fn add_local_memory(&mut self, ty: MemoryType) -> MemoryID
pub fn add_import_memory( &mut self, module: String, name: String, ty: MemoryType, ) -> (MemoryID, ImportsID)
Sourcepub fn delete_memory(&mut self, mem_id: MemoryID)
pub fn delete_memory(&mut self, mem_id: MemoryID)
Delete a memory from the module.
Sourcepub fn add_import_func(
&mut self,
module: String,
name: String,
ty_id: TypeID,
) -> (FunctionID, ImportsID)
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.
Sourcepub fn num_import_func(&self) -> u32
pub fn num_import_func(&self) -> u32
Get the number of imported functions in the module (including any added ones).
Sourcepub fn delete_func(&mut self, function_id: FunctionID)
pub fn delete_func(&mut self, function_id: FunctionID)
Delete a function from the module.
Sourcepub fn convert_local_fn_to_import(
&mut self,
function_id: FunctionID,
module: String,
name: String,
ty_id: TypeID,
) -> bool
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.
Sourcepub fn set_fn_name(&mut self, id: FunctionID, name: String)
pub fn set_fn_name(&mut self, id: FunctionID, name: String)
Set the name of a function using its ID.
Sourcepub fn add_global(
&mut self,
init_expr: InitExpr,
content_ty: DataType,
mutable: bool,
shared: bool,
) -> GlobalID
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.
Sourcepub fn add_imported_global(
&mut self,
module: String,
name: String,
content_ty: DataType,
mutable: bool,
shared: bool,
) -> (GlobalID, ImportsID)
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.
Sourcepub fn delete_global(&mut self, global_id: GlobalID)
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!
Sourcepub fn mod_global_init_expr(&mut self, global_id: GlobalID, new_expr: InitExpr)
pub fn mod_global_init_expr(&mut self, global_id: GlobalID, new_expr: InitExpr)
Change a locally-defined global’s init expression.