remaining sections

This commit is contained in:
NikVolf
2017-04-03 22:58:00 +03:00
parent 4dd2229243
commit 4e1f1f2d86
3 changed files with 113 additions and 4 deletions

View File

@ -14,6 +14,10 @@ impl Module {
pub fn sections(&self) -> &[Section] { pub fn sections(&self) -> &[Section] {
&self.sections &self.sections
} }
pub fn sections_mut(&mut self) -> &mut Vec<Section> {
&mut self.sections
}
} }
impl Deserialize for Module { impl Deserialize for Module {
@ -21,9 +25,7 @@ impl Deserialize for Module {
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> { fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
let mut sections = Vec::new(); let mut sections = Vec::new();
let magic = Uint32::deserialize(reader)?; let magic = Uint32::deserialize(reader)?;
let version = Uint32::deserialize(reader)?; let version = Uint32::deserialize(reader)?;
loop { loop {

View File

@ -98,6 +98,29 @@ impl Deserialize for Section {
} }
} }
impl Serialize for Section {
type Error = Error;
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
match self {
Section::Custom(custom_section) => {
VarUint7::from(0x00).serialize(writer)?;
writer.write_all(&custom_section[..])?;
},
Section::Unparsed { id, payload } => {
VarUint7::from(id).serialize(writer)?;
writer.write_all(&payload[..])?;
},
Section::Type(type_section) => {
VarUint7::from(0x01).serialize(writer)?;
type_section.serialize(writer)?;
},
_ => unreachable!()
}
Ok(())
}
}
pub struct TypeSection(Vec<Type>); pub struct TypeSection(Vec<Type>);
impl TypeSection { impl TypeSection {
@ -117,6 +140,22 @@ impl Deserialize for TypeSection {
} }
} }
impl Serialize for TypeSection {
type Error = Error;
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
let mut counted_writer = CountedWriter::new(writer);
let data = self.0;
let counted_list = CountedListWriter::<Type, _>(
data.len(),
data.into_iter().map(Into::into),
);
counted_list.serialize(&mut counted_writer)?;
counted_writer.done()?;
Ok(())
}
}
pub struct ImportSection(Vec<ImportEntry>); pub struct ImportSection(Vec<ImportEntry>);
impl ImportSection { impl ImportSection {
@ -136,6 +175,22 @@ impl Deserialize for ImportSection {
} }
} }
impl Serialize for ImportSection {
type Error = Error;
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
let mut counted_writer = CountedWriter::new(writer);
let data = self.0;
let counted_list = CountedListWriter::<ImportEntry, _>(
data.len(),
data.into_iter().map(Into::into),
);
counted_list.serialize(&mut counted_writer)?;
counted_writer.done()?;
Ok(())
}
}
pub struct FunctionsSection(Vec<Func>); pub struct FunctionsSection(Vec<Func>);
impl FunctionsSection { impl FunctionsSection {
@ -159,6 +214,22 @@ impl Deserialize for FunctionsSection {
} }
} }
impl Serialize for FunctionsSection {
type Error = Error;
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
let mut counted_writer = CountedWriter::new(writer);
let data = self.0;
let counted_list = CountedListWriter::<VarUint32, _>(
data.len(),
data.into_iter().map(|func| func.type_ref().into())
);
counted_list.serialize(&mut counted_writer)?;
counted_writer.done()?;
Ok(())
}
}
pub struct TableSection(Vec<TableType>); pub struct TableSection(Vec<TableType>);
impl TableSection { impl TableSection {

View File

@ -1,5 +1,8 @@
use std::io; use std::io;
use super::{Deserialize, Serialize, Error, VarUint7, VarInt7, VarUint1, CountedList}; use super::{
Deserialize, Serialize, Error, VarUint7, VarInt7, VarUint1, CountedList,
CountedListWriter
};
pub enum Type { pub enum Type {
Function(FunctionType), Function(FunctionType),
@ -13,6 +16,16 @@ impl Deserialize for Type {
} }
} }
impl Serialize for Type {
type Error = Error;
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
match self {
Type::Function(fn_type) => fn_type.serialize(writer)
}
}
}
#[derive(Clone, Copy, PartialEq, Debug)] #[derive(Clone, Copy, PartialEq, Debug)]
pub enum ValueType { pub enum ValueType {
I32, I32,
@ -91,7 +104,6 @@ impl Serialize for BlockType {
} }
} }
pub struct FunctionType { pub struct FunctionType {
form: u8, form: u8,
params: Vec<ValueType>, params: Vec<ValueType>,
@ -126,3 +138,27 @@ impl Deserialize for FunctionType {
}) })
} }
} }
impl Serialize for FunctionType {
type Error = Error;
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
VarUint7::from(self.form).serialize(writer)?;
let data = self.params;
let counted_list = CountedListWriter::<ValueType, _>(
data.len(),
data.into_iter().map(Into::into),
);
counted_list.serialize(writer)?;
if let Some(return_type) = self.return_type {
VarUint1::from(true).serialize(writer)?;
return_type.serialize(writer)?;
} else {
VarUint1::from(false).serialize(writer)?;
}
Ok(())
}
}