pub struct Module {
pub sections: Vec<Section>,
}
Expand description
Unless you’re doing something very specific, this will be your entry point to the library as it represents the module as a whole. Check out its fields for nested structures.
Fields§
§sections: Vec<Section>
Module sections.
Note that the spec mandates a specific order in which sections must appear, but the representation here is currently a flat Vec<{enum}> for efficiency.
Use Module::find_std_section
and Module::find_std_section_mut
to find sections
of the specific type and Module::find_or_insert_std_section
to insert one in the correct
position.
The section order will be checked both during decoding and encoding.
Implementations§
source§impl Module
impl Module
sourcepub fn decode_from(r: impl Read) -> Result<Module, DecodeError>
pub fn decode_from(r: impl Read) -> Result<Module, DecodeError>
Decode a module from an arbitrary input.
Example
use std::fs::File;
use std::io::BufReader;
use wasmbin::Module;
let file = File::open("module.wasm")?;
let mut reader = BufReader::new(file);
let module = Module::decode_from(reader)?;
println!("{module:#?}");
sourcepub fn encode_into<W: Write>(&self, w: W) -> Result<W>
pub fn encode_into<W: Write>(&self, w: W) -> Result<W>
Encode the module into an arbitrary output.
Example
use std::fs::File;
use std::io::BufWriter;
use wasmbin::Module;
let file = File::create("module.wasm")?;
let mut writer = BufWriter::new(file);
module.encode_into(writer)?;
sourcepub fn find_std_section<T: StdPayload>(&self) -> Option<&Blob<T>>
pub fn find_std_section<T: StdPayload>(&self) -> Option<&Blob<T>>
Find a standard section by its payload type.
Example
use wasmbin::{Module, sections::payload};
if let Some(imports) = module.find_std_section::<payload::Import>() {
for import in imports.try_contents()? {
println!("Module imports a {:?} from {}.{}", import.desc, import.path.module, import.path.name);
}
}
sourcepub fn find_std_section_mut<T: StdPayload>(&mut self) -> Option<&mut Blob<T>>
pub fn find_std_section_mut<T: StdPayload>(&mut self) -> Option<&mut Blob<T>>
Find a standard section by its payload type and return a mutable reference.
Example
use wasmbin::Module;
use wasmbin::sections::{payload, Import, ImportPath, ImportDesc};
if let Some(imports) = module.find_std_section_mut::<payload::Import>() {
for import in imports.try_contents_mut()? {
// Compress references to the "env" module.
if import.path.module == "env" {
import.path.module = "a".to_owned();
}
}
}
sourcepub fn find_or_insert_std_section<T: StdPayload>(
&mut self,
insert_callback: impl FnOnce() -> T
) -> &mut Blob<T>
pub fn find_or_insert_std_section<T: StdPayload>( &mut self, insert_callback: impl FnOnce() -> T ) -> &mut Blob<T>
Find a standard section by its payload type or insert it if it’s not present.
The section will be inserted in the correct position according to the spec and a mutable reference will be returned for further modification.
Example
use wasmbin::Module;
use wasmbin::sections::{payload, Import, ImportPath, ImportDesc};
use wasmbin::indices::TypeId;
module
.find_or_insert_std_section(|| payload::Import::default())
.try_contents_mut()?
.push(Import {
path: ImportPath {
module: "env".to_owned(),
name: "my_func".to_owned(),
},
desc: ImportDesc::Func(TypeId::from(42)),
});