diff --git a/src/elements/module.rs b/src/elements/module.rs index 3e7af83..3a4f91a 100644 --- a/src/elements/module.rs +++ b/src/elements/module.rs @@ -14,6 +14,10 @@ impl Module { pub fn sections(&self) -> &[Section] { &self.sections } + + pub fn sections_mut(&mut self) -> &mut Vec
{ + &mut self.sections + } } impl Deserialize for Module { @@ -21,9 +25,7 @@ impl Deserialize for Module { fn deserialize(reader: &mut R) -> Result { let mut sections = Vec::new(); - let magic = Uint32::deserialize(reader)?; - let version = Uint32::deserialize(reader)?; loop { diff --git a/src/elements/section.rs b/src/elements/section.rs index b6ee302..1c9895b 100644 --- a/src/elements/section.rs +++ b/src/elements/section.rs @@ -98,6 +98,29 @@ impl Deserialize for Section { } } +impl Serialize for Section { + type Error = Error; + + fn serialize(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); impl TypeSection { @@ -117,6 +140,22 @@ impl Deserialize for TypeSection { } } +impl Serialize for TypeSection { + type Error = Error; + + fn serialize(self, writer: &mut W) -> Result<(), Self::Error> { + let mut counted_writer = CountedWriter::new(writer); + let data = self.0; + let counted_list = CountedListWriter::( + data.len(), + data.into_iter().map(Into::into), + ); + counted_list.serialize(&mut counted_writer)?; + counted_writer.done()?; + Ok(()) + } +} + pub struct ImportSection(Vec); impl ImportSection { @@ -136,6 +175,22 @@ impl Deserialize for ImportSection { } } +impl Serialize for ImportSection { + type Error = Error; + + fn serialize(self, writer: &mut W) -> Result<(), Self::Error> { + let mut counted_writer = CountedWriter::new(writer); + let data = self.0; + let counted_list = CountedListWriter::( + data.len(), + data.into_iter().map(Into::into), + ); + counted_list.serialize(&mut counted_writer)?; + counted_writer.done()?; + Ok(()) + } +} + pub struct FunctionsSection(Vec); impl FunctionsSection { @@ -159,6 +214,22 @@ impl Deserialize for FunctionsSection { } } +impl Serialize for FunctionsSection { + type Error = Error; + + fn serialize(self, writer: &mut W) -> Result<(), Self::Error> { + let mut counted_writer = CountedWriter::new(writer); + let data = self.0; + let counted_list = CountedListWriter::( + 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); impl TableSection { diff --git a/src/elements/types.rs b/src/elements/types.rs index d3db2d5..3bae55d 100644 --- a/src/elements/types.rs +++ b/src/elements/types.rs @@ -1,5 +1,8 @@ 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 { Function(FunctionType), @@ -13,6 +16,16 @@ impl Deserialize for Type { } } +impl Serialize for Type { + type Error = Error; + + fn serialize(self, writer: &mut W) -> Result<(), Self::Error> { + match self { + Type::Function(fn_type) => fn_type.serialize(writer) + } + } +} + #[derive(Clone, Copy, PartialEq, Debug)] pub enum ValueType { I32, @@ -91,7 +104,6 @@ impl Serialize for BlockType { } } - pub struct FunctionType { form: u8, params: Vec, @@ -126,3 +138,27 @@ impl Deserialize for FunctionType { }) } } + +impl Serialize for FunctionType { + type Error = Error; + + fn serialize(self, writer: &mut W) -> Result<(), Self::Error> { + VarUint7::from(self.form).serialize(writer)?; + + let data = self.params; + let counted_list = CountedListWriter::( + 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(()) + } +} \ No newline at end of file