This commit is contained in:
NikVolf
2017-03-31 17:31:33 +03:00
parent 5b93ee2236
commit f60b941cd4
6 changed files with 101 additions and 25 deletions

View File

@ -17,13 +17,16 @@ fn main() {
for section in module.sections() { for section in module.sections() {
match section { match section {
&Section::Import(ref import_section) => { &Section::Import(ref import_section) => {
println!("Imports {}", import_section.entries().len()); println!(" Imports: {}", import_section.entries().len());
}, },
&Section::Export(ref exports_section) => { &Section::Export(ref exports_section) => {
println!("Exports {}", exports_section.entries().len()); println!(" Exports: {}", exports_section.entries().len());
}, },
&Section::Function(ref functions_section) => { &Section::Function(ref functions_section) => {
println!("Functions {}", functions_section.entries().len()); println!(" Functions: {}", functions_section.entries().len());
},
&Section::Global(ref globals_section) => {
println!(" Globals: {}", globals_section.entries().len());
}, },
_ => {}, _ => {},
} }

View File

@ -0,0 +1,26 @@
use std::io;
use super::{Deserialize, Error, GlobalType, InitExpr};
pub struct GlobalEntry {
global_type: GlobalType,
init_expr: InitExpr,
}
impl GlobalEntry {
pub fn global_type(&self) -> &GlobalType { &self.global_type }
pub fn init_expr(&self) -> &InitExpr { &self.init_expr }
}
impl Deserialize for GlobalEntry {
type Error = Error;
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
let global_type = GlobalType::deserialize(reader)?;
let init_expr = InitExpr::deserialize(reader)?;
Ok(GlobalEntry {
global_type: global_type,
init_expr: init_expr,
})
}
}

View File

@ -6,15 +6,17 @@ mod primitives;
mod types; mod types;
mod import_entry; mod import_entry;
mod export_entry; mod export_entry;
mod global_entry;
mod ops; mod ops;
pub use self::module::Module; pub use self::module::Module;
pub use self::section::Section; pub use self::section::Section;
pub use self::import_entry::{ImportEntry, MemoryType, TableType, 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};
pub use self::global_entry::GlobalEntry;
pub use self::primitives::{VarUint32, VarUint7, VarUint1, VarInt7, Uint32, Uint64, VarUint64, CountedList}; pub use self::primitives::{VarUint32, VarUint7, VarUint1, VarInt7, Uint32, Uint64, VarUint64, CountedList};
pub use self::types::{ValueType, BlockType}; pub use self::types::{ValueType, BlockType};
pub use self::ops::{Opcode, Opcodes}; pub use self::ops::{Opcode, Opcodes, InitExpr};
pub trait Deserialize : Sized { pub trait Deserialize : Sized {
type Error; type Error;

View File

@ -10,6 +10,47 @@ impl Opcodes {
pub fn elements(&self) -> &[Opcode] { &self.0 } pub fn elements(&self) -> &[Opcode] { &self.0 }
} }
impl Deserialize for Opcodes {
type Error = Error;
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
let mut opcodes = Vec::new();
loop {
let opcode = Opcode::deserialize(reader)?;
let is_terminal = opcode.is_terminal();
opcodes.push(opcode);
if is_terminal {
break;
}
}
Ok(Opcodes(opcodes))
}
}
pub struct InitExpr(Vec<Opcode>);
// todo: check if kind of opcode sequence is valid as an expression
impl Deserialize for InitExpr {
type Error = Error;
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
let mut opcodes = Vec::new();
loop {
let opcode = Opcode::deserialize(reader)?;
let is_terminal = opcode.is_terminal();
opcodes.push(opcode);
if is_terminal {
break;
}
}
Ok(InitExpr(opcodes))
}
}
pub enum Opcode { pub enum Opcode {
Unreachable, Unreachable,
Nop, Nop,
@ -486,22 +527,3 @@ impl Deserialize for Opcode {
) )
} }
} }
impl Deserialize for Opcodes {
type Error = Error;
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
let mut opcodes = Vec::new();
loop {
let opcode = Opcode::deserialize(reader)?;
let is_terminal = opcode.is_terminal();
opcodes.push(opcode);
if is_terminal {
break;
}
}
Ok(Opcodes(opcodes))
}
}

View File

@ -37,7 +37,6 @@ impl Deserialize for VarUint32 {
} }
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct VarUint64(u64); pub struct VarUint64(u64);

View File

@ -12,6 +12,7 @@ use super::{
ExportEntry, ExportEntry,
Opcodes, Opcodes,
ValueType, ValueType,
GlobalEntry,
}; };
use super::types::Type; use super::types::Type;
@ -27,6 +28,7 @@ pub enum Section {
Function(FunctionsSection), Function(FunctionsSection),
Table(TableSection), Table(TableSection),
Memory(MemorySection), Memory(MemorySection),
Global(GlobalSection),
Export(ExportSection), Export(ExportSection),
Start(u32), Start(u32),
Code(CodeSection), Code(CodeSection),
@ -62,6 +64,9 @@ impl Deserialize for Section {
5 => { 5 => {
Section::Memory(MemorySection::deserialize(reader)?) Section::Memory(MemorySection::deserialize(reader)?)
}, },
6 => {
Section::Global(GlobalSection::deserialize(reader)?)
},
7 => { 7 => {
Section::Export(ExportSection::deserialize(reader)?) Section::Export(ExportSection::deserialize(reader)?)
}, },
@ -188,6 +193,25 @@ impl Deserialize for MemorySection {
} }
} }
pub struct GlobalSection(Vec<GlobalEntry>);
impl GlobalSection {
pub fn entries(&self) -> &[GlobalEntry] {
&self.0
}
}
impl Deserialize for GlobalSection {
type Error = Error;
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
// todo: maybe use reader.take(section_length)
let _section_length = VarUint32::deserialize(reader)?;
let entries: Vec<GlobalEntry> = CountedList::deserialize(reader)?.into_inner();
Ok(GlobalSection(entries))
}
}
pub struct ExportSection(Vec<ExportEntry>); pub struct ExportSection(Vec<ExportEntry>);
impl ExportSection { impl ExportSection {