sophon_wasm/elements/
export_entry.rs1use std::io;
2use super::{Deserialize, Serialize, Error, VarUint7, VarUint32};
3
4#[derive(Debug, Clone, Copy)]
6pub enum Internal {
7 Function(u32),
9 Table(u32),
11 Memory(u32),
13 Global(u32),
15}
16
17impl Deserialize for Internal {
18 type Error = Error;
19
20 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
21 let kind = VarUint7::deserialize(reader)?;
22 match kind.into() {
23 0x00 => Ok(Internal::Function(VarUint32::deserialize(reader)?.into())),
24 0x01 => Ok(Internal::Table(VarUint32::deserialize(reader)?.into())),
25 0x02 => Ok(Internal::Memory(VarUint32::deserialize(reader)?.into())),
26 0x03 => Ok(Internal::Global(VarUint32::deserialize(reader)?.into())),
27 _ => Err(Error::UnknownInternalKind(kind.into())),
28 }
29 }
30}
31
32impl Serialize for Internal {
33 type Error = Error;
34
35 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
36 let (bt, arg) = match self {
37 Internal::Function(arg) => (0x00, arg),
38 Internal::Table(arg) => (0x01, arg),
39 Internal::Memory(arg) => (0x02, arg),
40 Internal::Global(arg) => (0x03, arg),
41 };
42
43 VarUint7::from(bt).serialize(writer)?;
44 VarUint32::from(arg).serialize(writer)?;
45
46 Ok(())
47 }
48}
49
50#[derive(Debug, Clone)]
52pub struct ExportEntry {
53 field_str: String,
54 internal: Internal,
55}
56
57impl ExportEntry {
58 pub fn new(field: String, internal: Internal) -> Self {
60 ExportEntry {
61 field_str: field,
62 internal: internal
63 }
64 }
65
66 pub fn field(&self) -> &str { &self.field_str }
68
69 pub fn field_mut(&mut self) -> &mut String { &mut self.field_str }
71
72 pub fn internal(&self) -> &Internal { &self.internal }
74
75 pub fn internal_mut(&mut self) -> &mut Internal { &mut self.internal }
77}
78
79impl Deserialize for ExportEntry {
80 type Error = Error;
81
82 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
83 let field_str = String::deserialize(reader)?;
84 let internal = Internal::deserialize(reader)?;
85
86 Ok(ExportEntry {
87 field_str: field_str,
88 internal: internal,
89 })
90 }
91}
92
93impl Serialize for ExportEntry {
94 type Error = Error;
95
96 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
97 self.field_str.serialize(writer)?;
98 self.internal.serialize(writer)?;
99 Ok(())
100 }
101}