1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use alloc::string::String;
use super::{Deserialize, Serialize, Error, VarUint7, VarUint32};
use crate::io;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Internal {
Function(u32),
Table(u32),
Memory(u32),
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())),
}
}
}
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(())
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ExportEntry {
field_str: String,
internal: Internal,
}
impl ExportEntry {
pub fn new(field: String, internal: Internal) -> Self {
ExportEntry {
field_str: field,
internal: internal
}
}
pub fn field(&self) -> &str { &self.field_str }
pub fn field_mut(&mut self) -> &mut String { &mut self.field_str }
pub fn internal(&self) -> &Internal { &self.internal }
pub fn internal_mut(&mut self) -> &mut Internal { &mut 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,
})
}
}
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(())
}
}