2017-03-31 01:54:04 +03:00
|
|
|
use std::io;
|
2017-04-03 22:29:44 +03:00
|
|
|
use super::{Deserialize, Serialize, Error, VarUint7, VarUint32};
|
2017-03-31 01:54:04 +03:00
|
|
|
|
2017-04-04 03:03:57 +03:00
|
|
|
/// Internal reference of the exported entry.
|
2017-03-31 01:54:04 +03:00
|
|
|
pub enum Internal {
|
2017-04-04 03:03:57 +03:00
|
|
|
/// Function reference.
|
2017-03-31 01:54:04 +03:00
|
|
|
Function(u32),
|
2017-04-04 03:03:57 +03:00
|
|
|
/// Table reference.
|
2017-03-31 01:54:04 +03:00
|
|
|
Table(u32),
|
2017-04-04 03:03:57 +03:00
|
|
|
/// Memory reference.
|
2017-03-31 01:54:04 +03:00
|
|
|
Memory(u32),
|
2017-04-04 03:03:57 +03:00
|
|
|
/// Global reference.
|
2017-03-31 01:54:04 +03:00
|
|
|
Global(u32),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for Internal {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
|
|
|
|
let kind = VarUint7::deserialize(reader)?;
|
|
|
|
match kind.into() {
|
|
|
|
0x00 => Ok(Internal::Function(VarUint32::deserialize(reader)?.into())),
|
|
|
|
0x01 => Ok(Internal::Table(VarUint32::deserialize(reader)?.into())),
|
|
|
|
0x02 => Ok(Internal::Memory(VarUint32::deserialize(reader)?.into())),
|
|
|
|
0x03 => Ok(Internal::Global(VarUint32::deserialize(reader)?.into())),
|
|
|
|
_ => Err(Error::UnknownInternalKind(kind.into())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-03 22:29:44 +03:00
|
|
|
impl Serialize for Internal {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
|
|
|
|
let (bt, arg) = match self {
|
|
|
|
Internal::Function(arg) => (0x00, arg),
|
|
|
|
Internal::Table(arg) => (0x01, arg),
|
|
|
|
Internal::Memory(arg) => (0x02, arg),
|
|
|
|
Internal::Global(arg) => (0x03, arg),
|
|
|
|
};
|
|
|
|
|
|
|
|
VarUint7::from(bt).serialize(writer)?;
|
|
|
|
VarUint32::from(arg).serialize(writer)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-04 03:03:57 +03:00
|
|
|
/// Export entry.
|
2017-03-31 01:54:04 +03:00
|
|
|
pub struct ExportEntry {
|
|
|
|
field_str: String,
|
|
|
|
internal: Internal,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ExportEntry {
|
2017-04-04 03:03:57 +03:00
|
|
|
/// Public name
|
2017-03-31 01:54:04 +03:00
|
|
|
pub fn field(&self) -> &str { &self.field_str }
|
2017-04-04 03:03:57 +03:00
|
|
|
/// Internal reference of the export entry.
|
2017-03-31 01:54:04 +03:00
|
|
|
pub fn internal(&self) -> &Internal { &self.internal }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for ExportEntry {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
|
|
|
|
let field_str = String::deserialize(reader)?;
|
|
|
|
let internal = Internal::deserialize(reader)?;
|
|
|
|
|
|
|
|
Ok(ExportEntry {
|
|
|
|
field_str: field_str,
|
|
|
|
internal: internal,
|
|
|
|
})
|
|
|
|
}
|
2017-04-03 22:29:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for ExportEntry {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
|
|
|
|
self.field_str.serialize(writer)?;
|
|
|
|
self.internal.serialize(writer)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-03-31 01:54:04 +03:00
|
|
|
}
|