function signatures

This commit is contained in:
NikVolf 2017-03-30 23:23:54 +03:00
parent 779f3257cd
commit 054e9d71e2
3 changed files with 36 additions and 7 deletions

View File

@ -19,6 +19,9 @@ fn main() {
&Section::Import(ref import_section) => {
println!("Imports {}", import_section.entries().len());
},
&Section::Function(ref functions_section) => {
println!("Functions {}", functions_section.entries().len());
},
_ => {},
}
}

View File

@ -45,12 +45,8 @@ impl Deserialize for Module {
#[cfg(test)]
mod integration_tests {
use std::io::{self, Read};
use std::fs::File;
use super::super::deserialize_file;
use super::super::{Deserialize, deserialize_file};
use super::Module;
#[test]
fn hello() {
let module = deserialize_file("./res/cases/v1/hello.wasm").expect("Should be deserialized");

View File

@ -10,6 +10,7 @@ pub enum Section {
Custom(Vec<u8>),
Type(TypeSection),
Import(ImportSection),
Function(FunctionsSection),
}
impl Deserialize for Section {
@ -33,6 +34,9 @@ impl Deserialize for Section {
2 => {
Section::Import(ImportSection::deserialize(reader)?)
},
3 => {
Section::Function(FunctionsSection::deserialize(reader)?)
},
_ => {
Section::Unparsed { id: id.into(), payload: Unparsed::deserialize(reader)?.into() }
}
@ -79,6 +83,33 @@ impl Deserialize for ImportSection {
}
}
/// Function signature (type reference)
pub struct Function(pub u32);
pub struct FunctionsSection(Vec<Function>);
impl FunctionsSection {
pub fn entries(&self) -> &[Function] {
&self.0
}
}
impl Deserialize for FunctionsSection {
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 = VarUint32::deserialize(reader)?;
let funcs: Vec<Function> = CountedList::<VarUint32>::deserialize(reader)?
.into_inner()
.into_iter()
.map(|f| Function(f.into()))
.collect();
Ok(FunctionsSection(funcs))
}
}
#[cfg(test)]
mod tests {
@ -121,8 +152,7 @@ mod tests {
assert_eq!(type_section.types().len(), 1);
match type_section.types()[0] {
Type::Function(_) => {},
_ => panic!("Type should be a function")
Type::Function(_) => {}
}
}