diff --git a/src/builder/module.rs b/src/builder/module.rs index 5b80559..096fb94 100644 --- a/src/builder/module.rs +++ b/src/builder/module.rs @@ -19,36 +19,64 @@ pub struct CodeLocation { #[derive(Default)] struct ModuleScaffold { - pub functions: elements::FunctionsSection, pub types: elements::TypeSection, pub import: elements::ImportSection, + pub functions: elements::FunctionsSection, + pub table: elements::TableSection, + pub memory: elements::MemorySection, + pub global: elements::GlobalSection, + pub export: elements::ExportSection, + pub start: Option, + pub element: elements::ElementSection, pub code: elements::CodeSection, + pub data: elements::DataSection, pub other: Vec, } impl From for ModuleScaffold { fn from(module: elements::Module) -> Self { - let mut funcs: Option = None; let mut types: Option = None; let mut import: Option = None; + let mut funcs: Option = None; + let mut table: Option = None; + let mut memory: Option = None; + let mut global: Option = None; + let mut export: Option = None; + let mut start: Option = None; + let mut element: Option = None; let mut code: Option = None; + let mut data: Option = None; let mut sections = module.into_sections(); while let Some(section) = sections.pop() { match section { elements::Section::Type(sect) => { types = Some(sect); } - elements::Section::Function(sect) => { funcs = Some(sect); } elements::Section::Import(sect) => { import = Some(sect); } + elements::Section::Function(sect) => { funcs = Some(sect); } + elements::Section::Table(sect) => { table = Some(sect); } + elements::Section::Memory(sect) => { memory = Some(sect); } + elements::Section::Global(sect) => { global = Some(sect); } + elements::Section::Export(sect) => { export = Some(sect); } + elements::Section::Start(index) => { start = Some(index); } + elements::Section::Element(sect) => { element = Some(sect); } elements::Section::Code(sect) => { code = Some(sect); } + elements::Section::Data(sect) => { data = Some(sect); } _ => {} } } ModuleScaffold { - functions: funcs.unwrap_or_default(), types: types.unwrap_or_default(), import: import.unwrap_or_default(), + functions: funcs.unwrap_or_default(), + table: table.unwrap_or_default(), + memory: memory.unwrap_or_default(), + global: global.unwrap_or_default(), + export: export.unwrap_or_default(), + start: start, + element: element.unwrap_or_default(), code: code.unwrap_or_default(), + data: data.unwrap_or_default(), other: sections, } } @@ -69,11 +97,38 @@ impl From for elements::Module { let functions = module.functions; if functions.entries().len() > 0 { sections.push(elements::Section::Function(functions)); - } + } + let table = module.table; + if table.entries().len() > 0 { + sections.push(elements::Section::Table(table)); + } + let memory = module.memory; + if memory.entries().len() > 0 { + sections.push(elements::Section::Memory(memory)); + } + let global = module.global; + if global.entries().len() > 0 { + sections.push(elements::Section::Global(global)); + } + let export = module.export; + if export.entries().len() > 0 { + sections.push(elements::Section::Export(export)); + } + if let Some(start) = module.start { + sections.push(elements::Section::Start(start)); + } + let element = module.element; + if element.entries().len() > 0 { + sections.push(elements::Section::Element(element)); + } let code = module.code; if code.bodies().len() > 0 { sections.push(elements::Section::Code(code)); } + let data = module.data; + if data.entries().len() > 0 { + sections.push(elements::Section::Data(data)); + } sections.extend(module.other); elements::Module::new(sections) } diff --git a/src/elements/mod.rs b/src/elements/mod.rs index 6fa0d00..e9f8dfb 100644 --- a/src/elements/mod.rs +++ b/src/elements/mod.rs @@ -16,7 +16,8 @@ mod segment; pub use self::module::Module; pub use self::section::{ Section, FunctionsSection, CodeSection, MemorySection, DataSection, - ImportSection, ExportSection, GlobalSection, TypeSection, + ImportSection, ExportSection, GlobalSection, TypeSection, ElementSection, + TableSection, }; pub use self::import_entry::{ImportEntry, MemoryType, TableType, GlobalType, External}; pub use self::export_entry::{ExportEntry, Internal}; diff --git a/src/elements/section.rs b/src/elements/section.rs index e9e4614..63b335d 100644 --- a/src/elements/section.rs +++ b/src/elements/section.rs @@ -315,9 +315,11 @@ impl Serialize for FunctionsSection { } /// Section with table definition (currently only one is allowed). +#[derive(Default)] pub struct TableSection(Vec); impl TableSection { + /// Table entries. pub fn entries(&self) -> &[TableType] { &self.0 } @@ -351,6 +353,7 @@ impl Serialize for TableSection { } /// Section with table definition (currently only one entry is allowed). +#[derive(Default)] pub struct MemorySection(Vec); impl MemorySection { @@ -388,6 +391,7 @@ impl Serialize for MemorySection { } /// Globals definition section. +#[derive(Default)] pub struct GlobalSection(Vec); impl GlobalSection { @@ -425,6 +429,7 @@ impl Serialize for GlobalSection { } /// List of exports definition. +#[derive(Default)] pub struct ExportSection(Vec); impl ExportSection { @@ -510,6 +515,7 @@ impl Serialize for CodeSection { } /// Element entries section. +#[derive(Default)] pub struct ElementSection(Vec); impl ElementSection { @@ -552,6 +558,7 @@ impl Serialize for ElementSection { } /// Data entries definitions. +#[derive(Default)] pub struct DataSection(Vec); impl DataSection {