Add test involving custom section, fix the ensuing bug

This commit is contained in:
Joshua Warner 2018-12-28 18:17:28 -08:00
parent b91a0aa140
commit a39ab02b5c

View File

@ -245,17 +245,17 @@ impl Module {
/// Changes the module's start section. /// Changes the module's start section.
pub fn set_start_section(&mut self, new_start: u32) { pub fn set_start_section(&mut self, new_start: u32) {
let mut insert_after = 0; for section in self.sections_mut().iter_mut() {
for (i, section) in self.sections_mut().iter_mut().enumerate() {
if let &mut Section::Start(_sect) = section { if let &mut Section::Start(_sect) = section {
*section = Section::Start(new_start); *section = Section::Start(new_start);
return return
} }
if section.id() < 0x8 {
insert_after = i;
} }
} let insert_before = self.sections().iter().enumerate()
self.sections_mut().insert(insert_after + 1, Section::Start(new_start)); .filter_map(|(i, s)| if s.id() > 0x8 { Some(i) } else { None })
.next()
.unwrap_or(0);
self.sections_mut().insert(insert_before, Section::Start(new_start));
} }
/// Removes the module's start section. /// Removes the module's start section.
@ -770,4 +770,19 @@ mod integration_tests {
let sections = module.sections().iter().map(|s| s.id()).collect::<Vec<_>>(); let sections = module.sections().iter().map(|s| s.id()).collect::<Vec<_>>();
assert_eq!(sections, vec![1, 2, 3, 6, 7, 8, 9, 10, 11]); assert_eq!(sections, vec![1, 2, 3, 6, 7, 8, 9, 10, 11]);
} }
#[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]);
assert!(module.start_section().is_none());
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, 0]);
}
} }