mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-01 17:11:53 +00:00
Merge pull request #260 from joshuawarner32/master
Fix section ordering when adding a Start section
This commit is contained in:
commit
122c8e2450
BIN
res/cases/v1/start_add.wasm
Normal file
BIN
res/cases/v1/start_add.wasm
Normal file
Binary file not shown.
BIN
res/cases/v1/start_add_custom.wasm
Normal file
BIN
res/cases/v1/start_add_custom.wasm
Normal file
Binary file not shown.
@ -244,14 +244,18 @@ impl Module {
|
||||
}
|
||||
|
||||
/// Changes the module's start section.
|
||||
pub fn set_start_section(&mut self, new_start : u32) {
|
||||
for section in self.sections_mut() {
|
||||
pub fn set_start_section(&mut self, new_start: u32) {
|
||||
for section in self.sections_mut().iter_mut() {
|
||||
if let &mut Section::Start(_sect) = section {
|
||||
*section = Section::Start(new_start);
|
||||
return
|
||||
}
|
||||
}
|
||||
self.sections_mut().push(Section::Start(new_start));
|
||||
let insert_before = self.sections().iter().enumerate()
|
||||
.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.
|
||||
@ -755,4 +759,30 @@ mod integration_tests {
|
||||
module.clear_start_section();
|
||||
assert_eq!(None, module.start_section());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_start() {
|
||||
let mut module = deserialize_file("./res/cases/v1/start_add.wasm").expect("failed to deserialize");
|
||||
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]);
|
||||
}
|
||||
|
||||
#[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]);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user