missing sections in builder and order

This commit is contained in:
NikVolf
2017-04-12 19:07:10 +03:00
parent c188647003
commit 92c01ce53e
3 changed files with 69 additions and 6 deletions

View File

@ -19,36 +19,64 @@ pub struct CodeLocation {
#[derive(Default)] #[derive(Default)]
struct ModuleScaffold { struct ModuleScaffold {
pub functions: elements::FunctionsSection,
pub types: elements::TypeSection, pub types: elements::TypeSection,
pub import: elements::ImportSection, 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<u32>,
pub element: elements::ElementSection,
pub code: elements::CodeSection, pub code: elements::CodeSection,
pub data: elements::DataSection,
pub other: Vec<elements::Section>, pub other: Vec<elements::Section>,
} }
impl From<elements::Module> for ModuleScaffold { impl From<elements::Module> for ModuleScaffold {
fn from(module: elements::Module) -> Self { fn from(module: elements::Module) -> Self {
let mut funcs: Option<elements::FunctionsSection> = None;
let mut types: Option<elements::TypeSection> = None; let mut types: Option<elements::TypeSection> = None;
let mut import: Option<elements::ImportSection> = None; let mut import: Option<elements::ImportSection> = None;
let mut funcs: Option<elements::FunctionsSection> = None;
let mut table: Option<elements::TableSection> = None;
let mut memory: Option<elements::MemorySection> = None;
let mut global: Option<elements::GlobalSection> = None;
let mut export: Option<elements::ExportSection> = None;
let mut start: Option<u32> = None;
let mut element: Option<elements::ElementSection> = None;
let mut code: Option<elements::CodeSection> = None; let mut code: Option<elements::CodeSection> = None;
let mut data: Option<elements::DataSection> = None;
let mut sections = module.into_sections(); let mut sections = module.into_sections();
while let Some(section) = sections.pop() { while let Some(section) = sections.pop() {
match section { match section {
elements::Section::Type(sect) => { types = Some(sect); } elements::Section::Type(sect) => { types = Some(sect); }
elements::Section::Function(sect) => { funcs = Some(sect); }
elements::Section::Import(sect) => { import = 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::Code(sect) => { code = Some(sect); }
elements::Section::Data(sect) => { data = Some(sect); }
_ => {} _ => {}
} }
} }
ModuleScaffold { ModuleScaffold {
functions: funcs.unwrap_or_default(),
types: types.unwrap_or_default(), types: types.unwrap_or_default(),
import: import.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(), code: code.unwrap_or_default(),
data: data.unwrap_or_default(),
other: sections, other: sections,
} }
} }
@ -69,11 +97,38 @@ impl From<ModuleScaffold> for elements::Module {
let functions = module.functions; let functions = module.functions;
if functions.entries().len() > 0 { if functions.entries().len() > 0 {
sections.push(elements::Section::Function(functions)); 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; let code = module.code;
if code.bodies().len() > 0 { if code.bodies().len() > 0 {
sections.push(elements::Section::Code(code)); 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); sections.extend(module.other);
elements::Module::new(sections) elements::Module::new(sections)
} }

View File

@ -16,7 +16,8 @@ mod segment;
pub use self::module::Module; pub use self::module::Module;
pub use self::section::{ pub use self::section::{
Section, FunctionsSection, CodeSection, MemorySection, DataSection, 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::import_entry::{ImportEntry, MemoryType, TableType, GlobalType, External};
pub use self::export_entry::{ExportEntry, Internal}; pub use self::export_entry::{ExportEntry, Internal};

View File

@ -315,9 +315,11 @@ impl Serialize for FunctionsSection {
} }
/// Section with table definition (currently only one is allowed). /// Section with table definition (currently only one is allowed).
#[derive(Default)]
pub struct TableSection(Vec<TableType>); pub struct TableSection(Vec<TableType>);
impl TableSection { impl TableSection {
/// Table entries.
pub fn entries(&self) -> &[TableType] { pub fn entries(&self) -> &[TableType] {
&self.0 &self.0
} }
@ -351,6 +353,7 @@ impl Serialize for TableSection {
} }
/// Section with table definition (currently only one entry is allowed). /// Section with table definition (currently only one entry is allowed).
#[derive(Default)]
pub struct MemorySection(Vec<MemoryType>); pub struct MemorySection(Vec<MemoryType>);
impl MemorySection { impl MemorySection {
@ -388,6 +391,7 @@ impl Serialize for MemorySection {
} }
/// Globals definition section. /// Globals definition section.
#[derive(Default)]
pub struct GlobalSection(Vec<GlobalEntry>); pub struct GlobalSection(Vec<GlobalEntry>);
impl GlobalSection { impl GlobalSection {
@ -425,6 +429,7 @@ impl Serialize for GlobalSection {
} }
/// List of exports definition. /// List of exports definition.
#[derive(Default)]
pub struct ExportSection(Vec<ExportEntry>); pub struct ExportSection(Vec<ExportEntry>);
impl ExportSection { impl ExportSection {
@ -510,6 +515,7 @@ impl Serialize for CodeSection {
} }
/// Element entries section. /// Element entries section.
#[derive(Default)]
pub struct ElementSection(Vec<ElementSegment>); pub struct ElementSection(Vec<ElementSegment>);
impl ElementSection { impl ElementSection {
@ -552,6 +558,7 @@ impl Serialize for ElementSection {
} }
/// Data entries definitions. /// Data entries definitions.
#[derive(Default)]
pub struct DataSection(Vec<DataSegment>); pub struct DataSection(Vec<DataSegment>);
impl DataSection { impl DataSection {