From 054e9d71e2a3b61fcabfa08bf73282c4f444e41c Mon Sep 17 00:00:00 2001 From: NikVolf Date: Thu, 30 Mar 2017 23:23:54 +0300 Subject: [PATCH] function signatures --- examples/info.rs | 3 +++ src/elements/module.rs | 6 +----- src/elements/section.rs | 34 ++++++++++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/examples/info.rs b/examples/info.rs index ce17e83..fc530c0 100644 --- a/examples/info.rs +++ b/examples/info.rs @@ -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()); + }, _ => {}, } } diff --git a/src/elements/module.rs b/src/elements/module.rs index 0ddbab9..3e7af83 100644 --- a/src/elements/module.rs +++ b/src/elements/module.rs @@ -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"); diff --git a/src/elements/section.rs b/src/elements/section.rs index a5f67fe..88f5a21 100644 --- a/src/elements/section.rs +++ b/src/elements/section.rs @@ -10,6 +10,7 @@ pub enum Section { Custom(Vec), 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); + +impl FunctionsSection { + pub fn entries(&self) -> &[Function] { + &self.0 + } +} + +impl Deserialize for FunctionsSection { + type Error = Error; + + fn deserialize(reader: &mut R) -> Result { + // todo: maybe use reader.take(section_length) + let _section_length = VarUint32::deserialize(reader)?; + let funcs: Vec = CountedList::::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(_) => {} } }