wamex-cli 0.1.0

Command line interface for wamex splitter
Documentation
use anyhow::Result;
use vec_map::VecMap;
pub use wasmparser::RelocationEntry;
use wasmparser::SectionLimited;

use super::CustomSectionReader;
use crate::index::SectionId;

#[derive(Default, Debug)]
/// Information stored in "reloc.*" sections
pub struct Relocation {
    ///
    /// "reloc.*" sections.
    /// The key is an index to which relocation entry corresponds
    pub relocs: VecMap<RelocationSection>,
}
impl Relocation {
    pub(super) fn push_section(&mut self, reader: wasmparser::RelocSectionReader) -> Result<()> {
        let exist = self.relocs.insert(
            reader.section_index() as SectionId,
            RelocationSection::read(reader.entries())?,
        );
        if exist.is_some() {
            log::error!(
                "duplicate reloc section for index {}",
                reader.section_index()
            );
        }
        Ok(())
    }
    pub fn get_section(&self, index: SectionId) -> Option<&RelocationSection> {
        self.relocs.get(index)
    }
}

#[derive(Default, Debug)]
pub struct RelocationSection {
    pub entries: Vec<RelocationEntry>,
}

impl<'a> CustomSectionReader<'a> for RelocationSection {
    type Reader = SectionLimited<'a, RelocationEntry>;

    fn read(reader: Self::Reader) -> Result<Self> {
        let entries = reader.into_iter().collect::<Result<Vec<_>, _>>()?;
        Ok(RelocationSection { entries })
    }
}