Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
Handle the DataCount section (#264)
Browse files Browse the repository at this point in the history
This is a relatively recent addition to the bulk-memory-ops proposal
  • Loading branch information
alexcrichton authored and NikVolf committed Feb 1, 2019
1 parent e31060c commit 87df081
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
28 changes: 14 additions & 14 deletions src/elements/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl Module {
}
}
let insert_before = self.sections().iter().enumerate()
.filter_map(|(i, s)| if s.id() > 0x8 { Some(i) } else { None })
.filter_map(|(i, s)| if s.order() > 0x8 { Some(i) } else { None })
.next()
.unwrap_or(0);
self.sections_mut().insert(insert_before, Section::Start(new_start));
Expand Down Expand Up @@ -482,20 +482,20 @@ impl Deserialize for Module {
return Err(Error::UnsupportedVersion(version));
}

let mut last_section_id = 0;
let mut last_section_order = 0;

loop {
match Section::deserialize(reader) {
Err(Error::UnexpectedEof) => { break; },
Err(e) => { return Err(e) },
Ok(section) => {
if section.id() != 0 {
if last_section_id > section.id() {
if section.order() != 0 {
if last_section_order > section.order() {
return Err(Error::SectionsOutOfOrder);
} else if last_section_id == section.id() {
return Err(Error::DuplicatedSections(last_section_id));
} else if last_section_order == section.order() {
return Err(Error::DuplicatedSections(last_section_order));
}
last_section_id = section.id();
last_section_order = section.order();
}
sections.push(section);
}
Expand Down Expand Up @@ -837,22 +837,22 @@ mod integration_tests {
module.set_start_section(0);
assert_eq!(module.start_section().expect("Did not find any start section"), 0);

let sections = module.sections().iter().map(|s| s.id()).collect::<Vec<_>>();
assert_eq!(sections, vec![1, 2, 3, 6, 7, 8, 9, 10, 11]);
let sections = module.sections().iter().map(|s| s.order()).collect::<Vec<_>>();
assert_eq!(sections, vec![1, 2, 3, 6, 7, 8, 9, 11, 12]);
}

#[test]
fn add_start_custom() {
let mut module = deserialize_file("./res/cases/v1/start_add_custom.wasm").expect("failed to deserialize");

let sections = module.sections().iter().map(|s| s.id()).collect::<Vec<_>>();
assert_eq!(sections, vec![1, 2, 3, 6, 7, 9, 10, 11, 0]);
let sections = module.sections().iter().map(|s| s.order()).collect::<Vec<_>>();
assert_eq!(sections, vec![1, 2, 3, 6, 7, 9, 11, 12, 0]);

assert!(module.start_section().is_none());
module.set_start_section(0);
assert_eq!(module.start_section().expect("Did not find any start section"), 0);
assert_eq!(module.start_section().expect("Dorder not find any start section"), 0);

let sections = module.sections().iter().map(|s| s.id()).collect::<Vec<_>>();
assert_eq!(sections, vec![1, 2, 3, 6, 7, 8, 9, 10, 11, 0]);
let sections = module.sections().iter().map(|s| s.order()).collect::<Vec<_>>();
assert_eq!(sections, vec![1, 2, 3, 6, 7, 8, 9, 11, 12, 0]);
}
}
21 changes: 18 additions & 3 deletions src/elements/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pub enum Section {
Start(u32),
/// Elements section.
Element(ElementSection),
/// Number of passive data entries in the data section
DataCount(u32),
/// Function bodies section.
Code(CodeSection),
/// Data definition section.
Expand Down Expand Up @@ -127,6 +129,12 @@ impl Deserialize for Section {
11 => {
Section::Data(DataSection::deserialize(reader)?)
},
12 => {
let mut section_reader = SectionReader::new(reader)?;
let count = VarUint32::deserialize(&mut section_reader)?;
section_reader.close()?;
Section::DataCount(count.into())
},
invalid_id => {
return Err(Error::InvalidSectionId(invalid_id))
},
Expand Down Expand Up @@ -182,6 +190,12 @@ impl Serialize for Section {
VarUint32::from(index).serialize(&mut counted_writer)?;
counted_writer.done()?;
},
Section::DataCount(count) => {
VarUint7::from(0x0c).serialize(writer)?;
let mut counted_writer = CountedWriter::new(writer);
VarUint32::from(count).serialize(&mut counted_writer)?;
counted_writer.done()?;
},
Section::Element(element_section) => {
VarUint7::from(0x09).serialize(writer)?;
element_section.serialize(writer)?;
Expand Down Expand Up @@ -212,7 +226,7 @@ impl Serialize for Section {
}

impl Section {
pub(crate) fn id(&self) -> u8 {
pub(crate) fn order(&self) -> u8 {
match *self {
Section::Custom(_) => 0x00,
Section::Unparsed { .. } => 0x00,
Expand All @@ -225,8 +239,9 @@ impl Section {
Section::Export(_) => 0x7,
Section::Start(_) => 0x8,
Section::Element(_) => 0x9,
Section::Code(_) => 0x0a,
Section::Data(_) => 0x0b,
Section::DataCount(_) => 0x0a,
Section::Code(_) => 0x0b,
Section::Data(_) => 0x0c,
Section::Name(_) => 0x00,
Section::Reloc(_) => 0x00,
}
Expand Down

0 comments on commit 87df081

Please sign in to comment.