Files
parity-wasm/src/elements/mod.rs

121 lines
3.3 KiB
Rust
Raw Normal View History

2017-04-04 03:03:57 +03:00
//! Elemets of the WebAssembly binary format.
2017-03-29 18:16:58 +03:00
use std::io;
mod module;
mod section;
2017-03-29 23:13:54 +03:00
mod primitives;
mod types;
2017-03-30 20:55:25 +03:00
mod import_entry;
2017-03-31 01:54:04 +03:00
mod export_entry;
2017-03-31 17:31:33 +03:00
mod global_entry;
2017-03-31 03:28:03 +03:00
mod ops;
2017-04-03 13:18:50 +03:00
mod func;
2017-04-03 13:42:04 +03:00
mod segment;
2017-03-29 18:16:58 +03:00
pub use self::module::Module;
pub use self::section::Section;
2017-03-31 17:31:33 +03:00
pub use self::import_entry::{ImportEntry, MemoryType, TableType, GlobalType, External};
2017-03-31 01:54:04 +03:00
pub use self::export_entry::{ExportEntry, Internal};
2017-03-31 17:31:33 +03:00
pub use self::global_entry::GlobalEntry;
2017-04-03 15:03:18 +03:00
pub use self::primitives::{
VarUint32, VarUint7, VarUint1, VarInt7, Uint32,
2017-04-03 15:56:37 +03:00
Uint64, VarUint64, CountedList, CountedWriter, CountedListWriter,
2017-04-03 15:03:18 +03:00
};
2017-03-31 03:28:03 +03:00
pub use self::types::{ValueType, BlockType};
2017-03-31 17:31:33 +03:00
pub use self::ops::{Opcode, Opcodes, InitExpr};
2017-04-03 13:58:49 +03:00
pub use self::func::{Func, FuncBody, Local};
2017-04-03 13:42:04 +03:00
pub use self::segment::{ElementSegment, DataSegment};
2017-03-29 18:16:58 +03:00
2017-04-04 03:03:57 +03:00
/// Deserialization from serial i/o
2017-03-29 18:16:58 +03:00
pub trait Deserialize : Sized {
2017-04-04 03:03:57 +03:00
/// Serialization error produced by deserialization routine.
2017-03-29 18:16:58 +03:00
type Error;
2017-04-04 03:03:57 +03:00
/// Deserialize type from serial i/o
2017-03-29 18:16:58 +03:00
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error>;
}
2017-04-04 03:03:57 +03:00
/// Serialization to serial i/o
2017-04-03 13:58:49 +03:00
pub trait Serialize {
2017-04-04 03:03:57 +03:00
/// Serialization error produced by serialization routine.
2017-04-03 13:58:49 +03:00
type Error;
2017-04-04 03:03:57 +03:00
/// Serialize type to serial i/o
2017-04-03 15:03:18 +03:00
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error>;
2017-04-03 13:58:49 +03:00
}
2017-04-04 03:03:57 +03:00
/// Deserialization/serialization error
2017-03-29 18:16:58 +03:00
#[derive(Debug)]
pub enum Error {
2017-04-04 03:03:57 +03:00
/// Unexpected end of input
2017-03-29 18:16:58 +03:00
UnexpectedEof,
2017-04-04 03:03:57 +03:00
/// Inconsistence between declared and actual length
InconsistentLength {
/// Expected length of the definition
expected: usize,
/// Actual length of the definition
actual: usize
},
/// Other static error
2017-03-29 18:16:58 +03:00
Other(&'static str),
2017-04-04 03:03:57 +03:00
/// Other allocated error
2017-03-29 18:16:58 +03:00
HeapOther(String),
2017-04-04 03:03:57 +03:00
/// Invalid/unknown value type declaration
2017-03-29 23:13:54 +03:00
UnknownValueType(i8),
2017-04-04 03:03:57 +03:00
/// Non-utf8 string
2017-03-30 20:55:25 +03:00
NonUtf8String,
2017-04-04 03:03:57 +03:00
/// Unknown external kind code
2017-03-30 20:55:25 +03:00
UnknownExternalKind(u8),
2017-04-04 03:03:57 +03:00
/// Unknown internal kind code
2017-03-31 01:54:04 +03:00
UnknownInternalKind(u8),
2017-04-04 03:03:57 +03:00
/// Unknown opcode encountered
2017-03-31 04:04:51 +03:00
UnknownOpcode(u8),
2017-03-29 18:16:58 +03:00
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::HeapOther(format!("I/O Error: {}", err))
}
}
2017-04-04 03:03:57 +03:00
/// Unparsed part of the module/section
pub struct Unparsed(pub Vec<u8>);
2017-03-29 18:16:58 +03:00
impl Deserialize for Unparsed {
type Error = Error;
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
let len = VarUint32::deserialize(reader)?.into();
2017-03-29 19:40:51 +03:00
let mut vec = vec![0u8; len];
reader.read_exact(&mut vec[..])?;
2017-03-29 18:16:58 +03:00
Ok(Unparsed(vec))
}
}
2017-03-29 23:13:54 +03:00
impl From<Unparsed> for Vec<u8> {
fn from(u: Unparsed) -> Vec<u8> {
u.0
2017-03-29 18:16:58 +03:00
}
}
2017-04-04 03:03:57 +03:00
/// Deserialize module from file.
2017-03-30 20:55:25 +03:00
pub fn deserialize_file<P: AsRef<::std::path::Path>>(p: P) -> Result<Module, Error> {
2017-03-29 23:13:54 +03:00
use std::io::Read;
2017-03-29 18:16:58 +03:00
2017-03-29 23:13:54 +03:00
let mut contents = Vec::new();
::std::fs::File::open(p)?.read_to_end(&mut contents)?;
2017-03-29 18:16:58 +03:00
2017-03-29 23:13:54 +03:00
deserialize_buffer(contents)
2017-03-29 18:16:58 +03:00
}
2017-04-04 03:03:57 +03:00
/// Deserialize deserializable type from buffer.
2017-03-30 20:55:25 +03:00
pub fn deserialize_buffer<T: Deserialize>(contents: Vec<u8>) -> Result<T, T::Error> {
2017-03-29 23:13:54 +03:00
let mut reader = io::Cursor::new(contents);
T::deserialize(&mut reader)
2017-04-03 18:15:13 +03:00
}
2017-04-04 03:03:57 +03:00
/// Create buffer with serialized value.
2017-04-03 18:15:13 +03:00
pub fn serialize<T: Serialize>(val: T) -> Result<Vec<u8>, T::Error> {
let mut buf = Vec::new();
val.serialize(&mut buf)?;
Ok(buf)
2017-03-29 23:13:54 +03:00
}