parity_wasm/elements/
export_entry.rs

1use super::{Deserialize, Error, Serialize, VarUint32, VarUint7};
2use crate::io;
3use alloc::string::String;
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_str: String, internal: Internal) -> Self {
61		ExportEntry { field_str, internal }
62	}
63
64	/// Public name.
65	pub fn field(&self) -> &str {
66		&self.field_str
67	}
68
69	/// Public name (mutable).
70	pub fn field_mut(&mut self) -> &mut String {
71		&mut self.field_str
72	}
73
74	/// Internal reference of the export entry.
75	pub fn internal(&self) -> &Internal {
76		&self.internal
77	}
78
79	/// Internal reference of the export entry (mutable).
80	pub fn internal_mut(&mut self) -> &mut Internal {
81		&mut self.internal
82	}
83}
84
85impl Deserialize for ExportEntry {
86	type Error = Error;
87
88	fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
89		let field_str = String::deserialize(reader)?;
90		let internal = Internal::deserialize(reader)?;
91
92		Ok(ExportEntry { field_str, internal })
93	}
94}
95
96impl Serialize for ExportEntry {
97	type Error = Error;
98
99	fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
100		self.field_str.serialize(writer)?;
101		self.internal.serialize(writer)?;
102		Ok(())
103	}
104}