From d1dc069c842e8ec79c8ed8c269bdc8e26e095ca0 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet Date: Wed, 28 Nov 2018 17:43:48 +0100 Subject: [PATCH] Helpers to manage start section --- res/cases/v1/start_mut.wasm | Bin 0 -> 178 bytes res/cases/v1/start_mut.wast | 24 ++++++++++++++++++++++++ src/elements/module.rs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 res/cases/v1/start_mut.wasm create mode 100644 res/cases/v1/start_mut.wast diff --git a/res/cases/v1/start_mut.wasm b/res/cases/v1/start_mut.wasm new file mode 100644 index 0000000000000000000000000000000000000000..3f9af663e2e7f2009053b089404dc5759b7dfa62 GIT binary patch literal 178 zcmYj{!485j07Ki&iDMxldh?|5>QCU^Z-5z>XtDtT)OebouE51Zo4&TNeINj?7ZoF8 zg;AKYI4kMuXBu2+y7$TUPDYH)a&81ZVnj9Ai+5}>je4TLw<_Dm&_hZ=vCL&;0W%wZ lDbZAAu=g5Vk(x*~6C99ZYs`iyGLzk$_swzlYC3=2e*vzBCjbBd literal 0 HcmV?d00001 diff --git a/res/cases/v1/start_mut.wast b/res/cases/v1/start_mut.wast new file mode 100644 index 0000000..8bff8f5 --- /dev/null +++ b/res/cases/v1/start_mut.wast @@ -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) + ) + ) + ) +) + diff --git a/src/elements/module.rs b/src/elements/module.rs index 31f1192..80830a5 100644 --- a/src/elements/module.rs +++ b/src/elements/module.rs @@ -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()); + } }