diff --git a/src/builder/module.rs b/src/builder/module.rs index dd272f5..7e81319 100644 --- a/src/builder/module.rs +++ b/src/builder/module.rs @@ -12,6 +12,7 @@ pub struct ModuleBuilder { struct ModuleScaffold { pub functions: elements::FunctionsSection, pub types: elements::TypeSection, + pub import: elements::ImportSection, pub other: Vec, } @@ -19,12 +20,14 @@ 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 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); } _ => {} } } @@ -32,6 +35,7 @@ impl From for ModuleScaffold { ModuleScaffold { functions: funcs.unwrap_or_default(), types: types.unwrap_or_default(), + import: import.unwrap_or_default(), other: sections, } } @@ -49,6 +53,10 @@ impl From for elements::Module { if functions.entries().len() > 0 { sections.push(elements::Section::Function(functions)); } + let import = module.import; + if import.entries().len() > 0 { + sections.push(elements::Section::Import(import)); + } sections.extend(module.other); elements::Module::new(sections) } diff --git a/src/elements/section.rs b/src/elements/section.rs index 8fb6438..c2220b1 100644 --- a/src/elements/section.rs +++ b/src/elements/section.rs @@ -220,6 +220,7 @@ impl Serialize for TypeSection { } /// Section of the imports definition. +#[derive(Default)] pub struct ImportSection(Vec); impl ImportSection {