move func to mod

This commit is contained in:
NikVolf 2017-04-03 13:18:50 +03:00
parent f60b941cd4
commit 54f59728c6
3 changed files with 51 additions and 47 deletions

45
src/elements/func.rs Normal file
View File

@ -0,0 +1,45 @@
use std::io;
use super::{Deserialize, Error, ValueType, VarUint32, CountedList, Opcodes};
pub struct Local {
count: u32,
value_type: ValueType,
}
impl Local {
pub fn count(&self) -> u32 { self.count }
pub fn value_type(&self) -> ValueType { self.value_type }
}
impl Deserialize for Local {
type Error = Error;
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
let count = VarUint32::deserialize(reader)?;
let value_type = ValueType::deserialize(reader)?;
Ok(Local { count: count.into(), value_type: value_type })
}
}
pub struct FuncBody {
locals: Vec<Local>,
opcodes: Opcodes,
}
impl FuncBody {
pub fn locals(&self) -> &[Local] { &self.locals }
pub fn code(&self) -> &Opcodes { &self.opcodes }
}
impl Deserialize for FuncBody {
type Error = Error;
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
// todo: maybe use reader.take(section_length)
let _body_size = VarUint32::deserialize(reader)?;
let locals: Vec<Local> = CountedList::deserialize(reader)?.into_inner();
let opcodes = Opcodes::deserialize(reader)?;
Ok(FuncBody { locals: locals, opcodes: opcodes })
}
}

View File

@ -8,6 +8,7 @@ mod import_entry;
mod export_entry; mod export_entry;
mod global_entry; mod global_entry;
mod ops; mod ops;
mod func;
pub use self::module::Module; pub use self::module::Module;
pub use self::section::Section; pub use self::section::Section;
@ -17,6 +18,7 @@ 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, InitExpr}; pub use self::ops::{Opcode, Opcodes, InitExpr};
pub use self::func::{FuncBody, Local};
pub trait Deserialize : Sized { pub trait Deserialize : Sized {
type Error; type Error;

View File

@ -10,9 +10,8 @@ use super::{
MemoryType, MemoryType,
TableType, TableType,
ExportEntry, ExportEntry,
Opcodes,
ValueType,
GlobalEntry, GlobalEntry,
FuncBody,
}; };
use super::types::Type; use super::types::Type;
@ -231,10 +230,10 @@ impl Deserialize for ExportSection {
} }
} }
pub struct CodeSection(Vec<FunctionBody>); pub struct CodeSection(Vec<FuncBody>);
impl CodeSection { impl CodeSection {
pub fn bodies(&self) -> &[FunctionBody] { pub fn bodies(&self) -> &[FuncBody] {
&self.0 &self.0
} }
} }
@ -245,53 +244,11 @@ impl Deserialize for CodeSection {
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> { fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
// todo: maybe use reader.take(section_length) // todo: maybe use reader.take(section_length)
let _section_length = VarUint32::deserialize(reader)?; let _section_length = VarUint32::deserialize(reader)?;
let entries: Vec<FunctionBody> = CountedList::deserialize(reader)?.into_inner(); let entries: Vec<FuncBody> = CountedList::deserialize(reader)?.into_inner();
Ok(CodeSection(entries)) Ok(CodeSection(entries))
} }
} }
pub struct Local {
count: u32,
value_type: ValueType,
}
impl Local {
pub fn count(&self) -> u32 { self.count }
pub fn value_type(&self) -> ValueType { self.value_type }
}
impl Deserialize for Local {
type Error = Error;
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
let count = VarUint32::deserialize(reader)?;
let value_type = ValueType::deserialize(reader)?;
Ok(Local { count: count.into(), value_type: value_type })
}
}
pub struct FunctionBody {
locals: Vec<Local>,
opcodes: Opcodes,
}
impl FunctionBody {
pub fn locals(&self) -> &[Local] { &self.locals }
pub fn code(&self) -> &Opcodes { &self.opcodes }
}
impl Deserialize for FunctionBody {
type Error = Error;
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
// todo: maybe use reader.take(section_length)
let _body_size = VarUint32::deserialize(reader)?;
let locals: Vec<Local> = CountedList::deserialize(reader)?.into_inner();
let opcodes = Opcodes::deserialize(reader)?;
Ok(FunctionBody { locals: locals, opcodes: opcodes })
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {