From 54f59728c6303c35eeae52ed16593c2021db0c20 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 3 Apr 2017 13:18:50 +0300 Subject: [PATCH] move func to mod --- src/elements/func.rs | 45 ++++++++++++++++++++++++++++++++++++ src/elements/mod.rs | 2 ++ src/elements/section.rs | 51 ++++------------------------------------- 3 files changed, 51 insertions(+), 47 deletions(-) create mode 100644 src/elements/func.rs diff --git a/src/elements/func.rs b/src/elements/func.rs new file mode 100644 index 0000000..ec02dfd --- /dev/null +++ b/src/elements/func.rs @@ -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(reader: &mut R) -> Result { + 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, + 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(reader: &mut R) -> Result { + // todo: maybe use reader.take(section_length) + let _body_size = VarUint32::deserialize(reader)?; + let locals: Vec = CountedList::deserialize(reader)?.into_inner(); + let opcodes = Opcodes::deserialize(reader)?; + Ok(FuncBody { locals: locals, opcodes: opcodes }) + } +} \ No newline at end of file diff --git a/src/elements/mod.rs b/src/elements/mod.rs index 2b2eba1..6a694f2 100644 --- a/src/elements/mod.rs +++ b/src/elements/mod.rs @@ -8,6 +8,7 @@ mod import_entry; mod export_entry; mod global_entry; mod ops; +mod func; pub use self::module::Module; 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::types::{ValueType, BlockType}; pub use self::ops::{Opcode, Opcodes, InitExpr}; +pub use self::func::{FuncBody, Local}; pub trait Deserialize : Sized { type Error; diff --git a/src/elements/section.rs b/src/elements/section.rs index 6c75cee..b7e6417 100644 --- a/src/elements/section.rs +++ b/src/elements/section.rs @@ -10,9 +10,8 @@ use super::{ MemoryType, TableType, ExportEntry, - Opcodes, - ValueType, GlobalEntry, + FuncBody, }; use super::types::Type; @@ -231,10 +230,10 @@ impl Deserialize for ExportSection { } } -pub struct CodeSection(Vec); +pub struct CodeSection(Vec); impl CodeSection { - pub fn bodies(&self) -> &[FunctionBody] { + pub fn bodies(&self) -> &[FuncBody] { &self.0 } } @@ -245,53 +244,11 @@ impl Deserialize for CodeSection { fn deserialize(reader: &mut R) -> Result { // todo: maybe use reader.take(section_length) let _section_length = VarUint32::deserialize(reader)?; - let entries: Vec = CountedList::deserialize(reader)?.into_inner(); + let entries: Vec = CountedList::deserialize(reader)?.into_inner(); 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(reader: &mut R) -> Result { - 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, - 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(reader: &mut R) -> Result { - // todo: maybe use reader.take(section_length) - let _body_size = VarUint32::deserialize(reader)?; - let locals: Vec = CountedList::deserialize(reader)?.into_inner(); - let opcodes = Opcodes::deserialize(reader)?; - Ok(FunctionBody { locals: locals, opcodes: opcodes }) - } -} - #[cfg(test)] mod tests {