2017-03-29 18:16:58 +03:00
|
|
|
use std::io;
|
2017-03-30 20:55:25 +03:00
|
|
|
use super::{Deserialize, Error, Uint32};
|
2017-03-29 18:16:58 +03:00
|
|
|
use super::section::Section;
|
|
|
|
|
|
|
|
pub struct Module {
|
2017-03-30 20:55:25 +03:00
|
|
|
_magic: u32,
|
2017-03-29 18:16:58 +03:00
|
|
|
version: u32,
|
|
|
|
sections: Vec<Section>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Module {
|
|
|
|
pub fn version(&self) -> u32 { self.version }
|
2017-03-29 19:40:51 +03:00
|
|
|
|
|
|
|
pub fn sections(&self) -> &[Section] {
|
|
|
|
&self.sections
|
|
|
|
}
|
2017-04-03 22:58:00 +03:00
|
|
|
|
|
|
|
pub fn sections_mut(&mut self) -> &mut Vec<Section> {
|
|
|
|
&mut self.sections
|
|
|
|
}
|
2017-03-29 18:16:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for Module {
|
|
|
|
type Error = super::Error;
|
|
|
|
|
|
|
|
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
|
|
|
|
let mut sections = Vec::new();
|
|
|
|
let magic = Uint32::deserialize(reader)?;
|
|
|
|
let version = Uint32::deserialize(reader)?;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match Section::deserialize(reader) {
|
|
|
|
Err(Error::UnexpectedEof) => { break; },
|
|
|
|
Err(e) => { return Err(e) },
|
|
|
|
Ok(section) => { sections.push(section); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Module {
|
2017-03-30 20:55:25 +03:00
|
|
|
_magic: magic.into(),
|
2017-03-29 23:13:54 +03:00
|
|
|
version: version.into(),
|
2017-03-29 18:16:58 +03:00
|
|
|
sections: sections,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod integration_tests {
|
|
|
|
|
2017-03-30 23:23:54 +03:00
|
|
|
use super::super::deserialize_file;
|
2017-03-29 18:16:58 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hello() {
|
2017-03-30 20:55:25 +03:00
|
|
|
let module = deserialize_file("./res/cases/v1/hello.wasm").expect("Should be deserialized");
|
2017-03-29 18:16:58 +03:00
|
|
|
|
|
|
|
assert_eq!(module.version(), 1);
|
2017-03-29 19:40:51 +03:00
|
|
|
assert_eq!(module.sections().len(), 8);
|
2017-03-29 18:16:58 +03:00
|
|
|
}
|
|
|
|
}
|