elements & data section

This commit is contained in:
NikVolf
2017-04-03 13:42:04 +03:00
parent 54f59728c6
commit d375cec134
3 changed files with 115 additions and 0 deletions

View File

@ -12,6 +12,8 @@ use super::{
ExportEntry,
GlobalEntry,
FuncBody,
ElementSegment,
DataSegment,
};
use super::types::Type;
@ -30,7 +32,9 @@ pub enum Section {
Global(GlobalSection),
Export(ExportSection),
Start(u32),
Element(ElementSection),
Code(CodeSection),
Data(DataSection),
}
impl Deserialize for Section {
@ -73,9 +77,15 @@ impl Deserialize for Section {
let _section_length = VarUint32::deserialize(reader)?;
Section::Start(VarUint32::deserialize(reader)?.into())
},
9 => {
Section::Element(ElementSection::deserialize(reader)?)
},
10 => {
Section::Code(CodeSection::deserialize(reader)?)
},
11 => {
Section::Data(DataSection::deserialize(reader)?)
},
_ => {
Section::Unparsed { id: id.into(), payload: Unparsed::deserialize(reader)?.into() }
}
@ -249,6 +259,44 @@ impl Deserialize for CodeSection {
}
}
pub struct ElementSection(Vec<ElementSegment>);
impl ElementSection {
pub fn entries(&self) -> &[ElementSegment] {
&self.0
}
}
impl Deserialize for ElementSection {
type Error = Error;
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
// todo: maybe use reader.take(section_length)
let _section_length = VarUint32::deserialize(reader)?;
let entries: Vec<ElementSegment> = CountedList::deserialize(reader)?.into_inner();
Ok(ElementSection(entries))
}
}
pub struct DataSection(Vec<DataSegment>);
impl DataSection {
pub fn entries(&self) -> &[DataSegment] {
&self.0
}
}
impl Deserialize for DataSection {
type Error = Error;
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
// todo: maybe use reader.take(section_length)
let _section_length = VarUint32::deserialize(reader)?;
let entries: Vec<DataSegment> = CountedList::deserialize(reader)?.into_inner();
Ok(DataSection(entries))
}
}
#[cfg(test)]
mod tests {