wasm: Custom Debug impl for NameSection

This commit is contained in:
Brian Carroll 2022-02-13 12:42:50 +00:00
parent a9df6f4ff9
commit b46690ecf2
2 changed files with 28 additions and 2 deletions

View file

@ -119,6 +119,7 @@ impl<'a> WasmModule<'a> {
+ self.element.size() + self.element.size()
+ self.code.size() + self.code.size()
+ self.data.size() + self.data.size()
+ self.names.size()
} }
pub fn preload(arena: &'a Bump, bytes: &[u8]) -> Self { pub fn preload(arena: &'a Bump, bytes: &[u8]) -> Self {
@ -146,7 +147,6 @@ impl<'a> WasmModule<'a> {
let global = GlobalSection::preload(arena, bytes, &mut cursor); let global = GlobalSection::preload(arena, bytes, &mut cursor);
ExportSection::skip_bytes(bytes, &mut cursor); ExportSection::skip_bytes(bytes, &mut cursor);
let export = ExportSection::empty(arena); let export = ExportSection::empty(arena);
let start = OpaqueSection::preload(SectionId::Start, arena, bytes, &mut cursor); let start = OpaqueSection::preload(SectionId::Start, arena, bytes, &mut cursor);

View file

@ -1,3 +1,5 @@
use std::fmt::Debug;
use bumpalo::collections::vec::Vec; use bumpalo::collections::vec::Vec;
use bumpalo::Bump; use bumpalo::Bump;
use roc_collections::all::MutMap; use roc_collections::all::MutMap;
@ -1153,7 +1155,6 @@ enum NameSubSections {
LocalNames = 2, LocalNames = 2,
} }
#[derive(Debug)]
pub struct NameSection<'a> { pub struct NameSection<'a> {
pub bytes: Vec<'a, u8>, pub bytes: Vec<'a, u8>,
pub functions: MutMap<&'a [u8], u32>, pub functions: MutMap<&'a [u8], u32>,
@ -1189,6 +1190,10 @@ impl<'a> NameSection<'a> {
section section
} }
pub fn size(&self) -> usize {
self.bytes.len()
}
fn parse_body( fn parse_body(
&mut self, &mut self,
arena: &'a Bump, arena: &'a Bump,
@ -1253,6 +1258,27 @@ impl<'a> Serialize for NameSection<'a> {
} }
} }
impl<'a> Debug for NameSection<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "NameSection\n")?;
// We want to display index->name because it matches the binary format and looks nicer.
// But our hashmap is name->index because that's what code gen wants to look up.
let mut by_index = std::vec::Vec::with_capacity(self.functions.len());
for (name, index) in self.functions.iter() {
by_index.push((*index, name));
}
by_index.sort_unstable();
for (index, name) in by_index.iter() {
let name_str = unsafe { std::str::from_utf8_unchecked(name) };
write!(f, " {:4}: {}\n", index, name_str)?;
}
Ok(())
}
}
/******************************************************************* /*******************************************************************
* *
* Unit tests * Unit tests