parse and store custom sections from wasm, guarded by vfs feature flag

This commit is contained in:
Mackenzie Clark
2019-03-12 10:45:44 -07:00
parent f014a05304
commit b9c3a49f3c
6 changed files with 53 additions and 5 deletions

View File

@ -56,6 +56,28 @@ pub struct ModuleInfo {
pub namespace_table: StringTable<NamespaceIndex>,
pub name_table: StringTable<NameIndex>,
#[cfg(feature = "vfs")]
pub custom_sections: HashMap<String, Vec<u8>>,
}
impl ModuleInfo {
#[cfg(feature = "vfs")]
pub fn import_custom_sections(&mut self, wasm: &[u8]) -> crate::error::ParseResult<()> {
let mut parser = wasmparser::ModuleReader::new(wasm)?;
while !parser.eof() {
let section = parser.read()?;
if let wasmparser::SectionCode::Custom { name, kind: _ } = section.code {
let mut reader = section.get_binary_reader();
let len = reader.bytes_remaining();
let bytes = reader.read_bytes(len)?;
let data = bytes.to_vec();
let name = String::from_utf8_lossy(name).to_string();
self.custom_sections.insert(name, data);
}
}
Ok(())
}
}
/// A compiled WebAssembly module.