proper parsing

This commit is contained in:
NikVolf
2017-06-19 21:14:41 +03:00
parent ca6e41b66b
commit a200bd0862
2 changed files with 39 additions and 4 deletions

View File

@ -29,7 +29,7 @@ run_test!("comments", wasm_comments);
// TODO: commented out until sNaN issue is resolved: // TODO: commented out until sNaN issue is resolved:
// https://github.com/NikVolf/parity-wasm/blob/b5aaf103cf28f1e36df832f4883f55043e67894b/src/interpreter/value.rs#L510 // https://github.com/NikVolf/parity-wasm/blob/b5aaf103cf28f1e36df832f4883f55043e67894b/src/interpreter/value.rs#L510
// run_test!("conversions", wasm_conversions); // run_test!("conversions", wasm_conversions);
// TODO: run_test!("custom_section", wasm_custom_section); run_test!("custom_section", wasm_custom_section);
run_test!("endianness", wasm_endianness); run_test!("endianness", wasm_endianness);
run_test!("f32_exports", wasm_exports); run_test!("f32_exports", wasm_exports);
run_test!("f32_bitwise", wasm_f32_bitwise); run_test!("f32_bitwise", wasm_f32_bitwise);

View File

@ -32,7 +32,7 @@ pub enum Section {
payload: Vec<u8>, payload: Vec<u8>,
}, },
/// Custom section (`id=0`) /// Custom section (`id=0`)
Custom(Vec<u8>), Custom(CustomSection),
/// Types section /// Types section
Type(TypeSection), Type(TypeSection),
/// Import section /// Import section
@ -70,7 +70,7 @@ impl Deserialize for Section {
Ok( Ok(
match id.into() { match id.into() {
0 => { 0 => {
Section::Custom(Unparsed::deserialize(reader)?.into()) Section::Custom(CustomSection::deserialize(reader)?.into())
}, },
1 => { 1 => {
Section::Type(TypeSection::deserialize(reader)?) Section::Type(TypeSection::deserialize(reader)?)
@ -121,7 +121,7 @@ impl Serialize for Section {
match self { match self {
Section::Custom(custom_section) => { Section::Custom(custom_section) => {
VarUint7::from(0x00).serialize(writer)?; VarUint7::from(0x00).serialize(writer)?;
writer.write_all(&custom_section[..])?; custom_section.serialize(writer)?;
}, },
Section::Unparsed { id, payload } => { Section::Unparsed { id, payload } => {
VarUint7::from(id).serialize(writer)?; VarUint7::from(id).serialize(writer)?;
@ -178,6 +178,41 @@ impl Serialize for Section {
} }
} }
pub struct CustomSection {
name: String,
payload: Vec<u8>,
}
impl Deserialize for CustomSection {
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: u32 = VarUint32::deserialize(reader)?.into();
let name = String::deserialize(reader)?;
let payload_left = section_length - (name.len() as u32 + name.len() as u32 / 128 + 1);
let mut payload = vec![0u8; payload_left as usize];
reader.read_exact(&mut payload[..])?;
Ok(CustomSection { name: name, payload: payload })
}
}
impl Serialize for CustomSection {
type Error = Error;
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
use std::io::Write;
let mut counted_writer = CountedWriter::new(writer);
self.name.serialize(&mut counted_writer)?;
counted_writer.write_all(&self.payload[..])?;
counted_writer.done()?;
Ok(())
}
}
/// Section with type declarations /// Section with type declarations
#[derive(Default)] #[derive(Default)]
pub struct TypeSection(Vec<Type>); pub struct TypeSection(Vec<Type>);