tetsy_wasm/elements/
export_entry.rs

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