also module extensions

This commit is contained in:
NikVolf 2017-04-07 20:00:30 +03:00
parent ceca58431b
commit f85ef8f5f8
2 changed files with 48 additions and 1 deletions

View File

@ -8,11 +8,20 @@ pub struct ModuleBuilder<F=Identity> {
module: ModuleScaffold, module: ModuleScaffold,
} }
/// Location of the internal module function
pub struct CodeLocation {
/// Location (index in 'functions' section) of the signature
pub signature: u32,
/// Location (index in the 'code' section) of the body
pub body: u32,
}
#[derive(Default)] #[derive(Default)]
struct ModuleScaffold { struct ModuleScaffold {
pub functions: elements::FunctionsSection, pub functions: elements::FunctionsSection,
pub types: elements::TypeSection, pub types: elements::TypeSection,
pub import: elements::ImportSection, pub import: elements::ImportSection,
pub code: elements::CodeSection,
pub other: Vec<elements::Section>, pub other: Vec<elements::Section>,
} }
@ -21,6 +30,7 @@ impl From<elements::Module> for ModuleScaffold {
let mut funcs: Option<elements::FunctionsSection> = None; 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 code: Option<elements::CodeSection> = 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() {
@ -28,6 +38,7 @@ impl From<elements::Module> for ModuleScaffold {
elements::Section::Type(sect) => { types = Some(sect); } elements::Section::Type(sect) => { types = Some(sect); }
elements::Section::Function(sect) => { funcs = 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::Code(sect) => { code = Some(sect); }
_ => {} _ => {}
} }
} }
@ -36,6 +47,7 @@ impl From<elements::Module> for ModuleScaffold {
functions: funcs.unwrap_or_default(), 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(),
code: code.unwrap_or_default(),
other: sections, other: sections,
} }
} }
@ -98,6 +110,35 @@ impl<F> ModuleBuilder<F> where F: Invoke<elements::Module> {
self self
} }
/// Push stand-alone function definition, creating sections, signature and code blocks
/// in corresponding sections.
/// `FunctionDefinition` can be build using `builder::function` builder
pub fn push_function(&mut self, func: code::FunctionDefinition) -> CodeLocation {
let signature = func.signature;
let body = func.code;
let module = &mut self.module;
let type_ref = match signature {
code::Signature::Inline(func_type) => {
module.types.types_mut().push(elements::Type::Function(func_type));
module.types.types().len() as u32 - 1
}
code::Signature::TypeReference(type_ref) => {
type_ref
}
};
module.functions.entries_mut().push(elements::Func::new(type_ref));
let signature_index = module.functions.entries_mut().len() as u32 - 1;
module.code.bodies_mut().push(body);
let body_index = module.code.bodies_mut().len() as u32 - 1;
CodeLocation {
signature: signature_index,
body: body_index,
}
}
/// Push signatures in the module, returning corresponding indices of pushed signatures /// Push signatures in the module, returning corresponding indices of pushed signatures
pub fn push_signatures(&mut self, signatures: code::SignatureBindings) -> Vec<u32> { pub fn push_signatures(&mut self, signatures: code::SignatureBindings) -> Vec<u32> {
let module = &mut self.module; let module = &mut self.module;

View File

@ -457,6 +457,7 @@ impl Serialize for ExportSection {
} }
/// Section with function bodies of the module. /// Section with function bodies of the module.
#[derive(Default)]
pub struct CodeSection(Vec<FuncBody>); pub struct CodeSection(Vec<FuncBody>);
impl CodeSection { impl CodeSection {
@ -465,10 +466,15 @@ impl CodeSection {
CodeSection(bodies) CodeSection(bodies)
} }
/// All function bodies in the section /// All function bodies in the section.
pub fn bodies(&self) -> &[FuncBody] { pub fn bodies(&self) -> &[FuncBody] {
&self.0 &self.0
} }
/// All function bodies in the section, mutable.
pub fn bodies_mut(&mut self) -> &mut Vec<FuncBody> {
&mut self.0
}
} }
impl Deserialize for CodeSection { impl Deserialize for CodeSection {