Helpers to manage start section

This commit is contained in:
Guillaume Ballet 2018-11-28 17:43:48 +01:00
parent 8c774ba71d
commit d1dc069c84
3 changed files with 60 additions and 0 deletions

BIN
res/cases/v1/start_mut.wasm Normal file

Binary file not shown.

View File

@ -0,0 +1,24 @@
(module
(type $0 (func (param i32) (result i32)))
(type $1 (func (result i32)))
(type $2 (func))
(import "env" "memoryBase" (global $gimport$0 i32))
(import "env" "memory" (memory $0 256))
(import "env" "table" (table 0 anyfunc))
(import "env" "tableBase" (global $gimport$4 i32))
(import "env" "_puts" (func $fimport$1 (param i32) (result i32)))
(global $global$0 (mut i32) (i32.const 0))
(global $global$1 (mut i32) (i32.const 0))
(global $global$2 i32 (i32.const 0))
(data (i32.const 13) "hello, world!")
(export "_main" (func $0))
(start $0)
(func $0 (type $2)
(drop
(call $fimport$1
(get_global $gimport$0)
)
)
)
)

View File

@ -241,6 +241,32 @@ impl Module {
None
}
/// Changes the module's start section.
pub fn set_start_section(&mut self, new_start : u32) {
for section in self.sections_mut() {
if let &mut Section::Start(_sect) = section {
*section = Section::Start(new_start);
return
}
}
self.sections_mut().push(Section::Start(new_start));
}
/// Removes the module's start section.
pub fn clear_start_section(&mut self) {
let sections = self.sections_mut();
let mut rmidx = sections.len();
for (index, section) in sections.iter_mut().enumerate() {
if let Section::Start(_sect) = section {
rmidx = index;
break;
}
}
if rmidx < sections.len() {
sections.remove(rmidx);
}
}
/// Functions signatures section reference, if any.
/// NOTE: name section is not parsed by default so `names_section` could return None even if name section exists.
/// Call `parse_names` to parse name section
@ -713,4 +739,14 @@ mod integration_tests {
let module = deserialize_file("./res/cases/v1/two-mems.wasm").expect("failed to deserialize");
assert_eq!(module.memory_space(), 2);
}
#[test]
fn mut_start() {
let mut module = deserialize_file("./res/cases/v1/start_mut.wasm").expect("failed to deserialize");
assert_eq!(module.start_section().expect("Did not find any start section"), 1);
module.set_start_section(0);
assert_eq!(module.start_section().expect("Did not find any start section"), 0);
module.clear_start_section();
assert_eq!(None, module.start_section());
}
}