-
Notifications
You must be signed in to change notification settings - Fork 11
Introduce dropsection #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| use super::{ModuleError, ModuleTranslator}; | ||
|
|
||
| use parity_wasm::builder::*; | ||
| use parity_wasm::elements::*; | ||
|
|
||
| /// Enum on which ModuleTranslator is implemented. | ||
| pub enum DropSection<'a> { | ||
| NamesSection, | ||
| /// Name of the custom section. | ||
| CustomSectionByName(&'a String), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps another mode for DropSection would be identifying data sections that are used for rust debugging and panic messages. |
||
| /// Index of the custom section. | ||
| CustomSectionByIndex(usize), | ||
| /// Index of the unknown section. | ||
| UnknownSectionByIndex(usize), | ||
| } | ||
|
|
||
| fn names_section_index_for(module: &Module) -> Option<usize> { | ||
| module.sections().iter().position(|e| match e { | ||
| Section::Name(_) => true, | ||
| _ => false, | ||
| }) | ||
| } | ||
|
|
||
| fn custom_section_index_for(module: &Module, name: &String) -> Option<usize> { | ||
| module.sections().iter().position(|e| match e { | ||
| Section::Custom(_section) => _section.name() == name, | ||
| _ => false, | ||
| }) | ||
| } | ||
|
|
||
| impl<'a> DropSection<'a> { | ||
| fn find_index(&self, module: &Module) -> Option<usize> { | ||
| match &self { | ||
| DropSection::NamesSection => names_section_index_for(module), | ||
| DropSection::CustomSectionByName(name) => custom_section_index_for(module, &name), | ||
| DropSection::CustomSectionByIndex(index) => Some(*index), | ||
| DropSection::UnknownSectionByIndex(index) => Some(*index), | ||
| } | ||
| } | ||
|
|
||
| fn drop_section(&self, module: &mut Module) -> Result<bool, ModuleError> { | ||
| let index = self.find_index(&module); | ||
| if index.is_none() { | ||
| return Ok(false); | ||
| } | ||
| let index = index.unwrap(); | ||
|
|
||
| let sections = module.sections_mut(); | ||
| if index >= sections.len() { | ||
| return Ok(false); | ||
| } | ||
| sections.remove(index); | ||
|
|
||
| Ok(true) | ||
| } | ||
| } | ||
|
|
||
| impl<'a> ModuleTranslator for DropSection<'a> { | ||
| fn translate_inplace(&self, module: &mut Module) -> Result<bool, ModuleError> { | ||
| Ok(self.drop_section(module)?) | ||
| } | ||
|
|
||
| fn translate(&self, module: &Module) -> Result<Option<Module>, ModuleError> { | ||
| let mut ret = module.clone(); | ||
| if self.drop_section(&mut ret)? { | ||
| Ok(Some(ret)) | ||
| } else { | ||
| Ok(None) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use parity_wasm::builder; | ||
|
|
||
| #[test] | ||
| fn keep_intact() { | ||
| let mut module = builder::module().build(); | ||
| let name = "empty".to_string(); | ||
| let dropper = DropSection::CustomSectionByName(&name); | ||
| let did_change = dropper.translate_inplace(&mut module).unwrap(); | ||
| assert_eq!(did_change, false); | ||
| } | ||
|
|
||
| #[test] | ||
| fn keep_intact_custom_section() { | ||
| let mut module = builder::module() | ||
| .with_section(Section::Custom(CustomSection::new( | ||
| "test".to_string(), | ||
| vec![], | ||
| ))) | ||
| .build(); | ||
| let name = "empty".to_string(); | ||
| let dropper = DropSection::CustomSectionByName(&name); | ||
| let did_change = dropper.translate_inplace(&mut module).unwrap(); | ||
| assert_eq!(did_change, false); | ||
| } | ||
|
|
||
| #[test] | ||
| fn remove_custom_section() { | ||
| let mut module = builder::module() | ||
| .with_section(Section::Custom(CustomSection::new( | ||
| "test".to_string(), | ||
| vec![], | ||
| ))) | ||
| .build(); | ||
| let name = "test".to_string(); | ||
| let dropper = DropSection::CustomSectionByName(&name); | ||
| let did_change = dropper.translate_inplace(&mut module).unwrap(); | ||
| assert_eq!(did_change, true); | ||
| } | ||
|
|
||
| #[test] | ||
| fn remove_custom_section_by_index() { | ||
| let mut module = builder::module() | ||
| .with_section(Section::Custom(CustomSection::new( | ||
| "test".to_string(), | ||
| vec![], | ||
| ))) | ||
| .build(); | ||
| let dropper = DropSection::CustomSectionByIndex(0); | ||
| let did_change = dropper.translate_inplace(&mut module).unwrap(); | ||
| assert_eq!(did_change, true); | ||
| } | ||
|
|
||
| #[test] | ||
| fn remove_oob_custom_section_by_index() { | ||
| let mut module = builder::module() | ||
| .with_section(Section::Custom(CustomSection::new( | ||
| "test".to_string(), | ||
| vec![], | ||
| ))) | ||
| .build(); | ||
| let dropper = DropSection::CustomSectionByIndex(1); | ||
| let did_change = dropper.translate_inplace(&mut module).unwrap(); | ||
| assert_eq!(did_change, false); | ||
| } | ||
|
|
||
| #[test] | ||
| fn remove_custom_unknown_by_index() { | ||
| let mut module = builder::module() | ||
| .with_section(Section::Custom(CustomSection::new( | ||
| "test".to_string(), | ||
| vec![], | ||
| ))) | ||
| .build(); | ||
| let dropper = DropSection::UnknownSectionByIndex(0); | ||
| let did_change = dropper.translate_inplace(&mut module).unwrap(); | ||
| assert_eq!(did_change, true); | ||
| } | ||
|
|
||
| #[test] | ||
| fn remove_oob_unknown_section_by_index() { | ||
| let mut module = builder::module() | ||
| .with_section(Section::Custom(CustomSection::new( | ||
| "test".to_string(), | ||
| vec![], | ||
| ))) | ||
| .build(); | ||
| let dropper = DropSection::UnknownSectionByIndex(1); | ||
jakelang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let did_change = dropper.translate_inplace(&mut module).unwrap(); | ||
| assert_eq!(did_change, false); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably will rewrite to have this as
DropSectionKindandDropSectionas a vector ofDropSectionKind. In that case the entire code can be rewritten somewhat, potentially with a single loop, though one must be careful when removing sections with a lower index.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After this rewrite I could also introduce
with_presetwithewasmwhich removes the namessection and all custom sections with namesdebug.