sophon_wasm/elements/
import_entry.rs

1use std::io;
2use super::{
3    Deserialize, Serialize, Error, VarUint7, VarInt7, VarUint32, VarUint1,
4    ValueType, TableElementType
5};
6
7/// Global definition struct
8#[derive(Debug, Clone)]
9pub struct GlobalType {
10    content_type: ValueType,
11    is_mutable: bool,
12}
13
14impl GlobalType {
15    /// New global type
16    pub fn new(content_type: ValueType, is_mutable: bool) -> Self {
17        GlobalType {
18            content_type: content_type,
19            is_mutable: is_mutable,
20        }
21    }
22
23    /// Type of the global entry
24    pub fn content_type(&self) -> ValueType { self.content_type }
25
26    /// Is global entry is declared as mutable
27    pub fn is_mutable(&self) -> bool { self.is_mutable }
28}
29
30impl Deserialize for GlobalType {
31    type Error = Error;
32
33    fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
34        let content_type = ValueType::deserialize(reader)?;
35        let is_mutable = VarUint1::deserialize(reader)?;
36        Ok(GlobalType {
37            content_type: content_type,
38            is_mutable: is_mutable.into(),
39        })
40    }
41}
42
43impl Serialize for GlobalType {
44    type Error = Error;
45
46    fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
47        self.content_type.serialize(writer)?;
48        VarUint1::from(self.is_mutable).serialize(writer)?;
49        Ok(())
50    }
51}
52
53/// Table entry
54#[derive(Debug, Clone)]
55pub struct TableType {
56    elem_type: TableElementType,
57    limits: ResizableLimits,
58}
59
60impl TableType {
61    /// New table definition
62    pub fn new(min: u32, max: Option<u32>) -> Self {
63        TableType {
64            elem_type: TableElementType::AnyFunc,
65            limits: ResizableLimits::new(min, max),
66        }
67    }
68
69    /// Table memory specification
70    pub fn limits(&self) -> &ResizableLimits { &self.limits }
71
72    /// Table element type
73    pub fn elem_type(&self) -> TableElementType { self.elem_type }
74}
75
76impl Deserialize for TableType {
77    type Error = Error;
78
79    fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
80        let elem_type = TableElementType::deserialize(reader)?;
81        let limits = ResizableLimits::deserialize(reader)?;
82        Ok(TableType {
83            elem_type: elem_type,
84            limits: limits,
85        })
86    }
87}
88
89impl Serialize for TableType {
90    type Error = Error;
91
92    fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
93        self.elem_type.serialize(writer)?;
94        self.limits.serialize(writer)
95    }
96}
97
98/// Memory limits
99#[derive(Debug, Clone)]
100pub struct ResizableLimits {
101    initial: u32,
102    maximum: Option<u32>,
103}
104
105impl ResizableLimits {
106    /// New memory limits definition
107    pub fn new(min: u32, max: Option<u32>) -> Self {
108        ResizableLimits {
109            initial: min,
110            maximum: max,
111        }
112    }
113    /// Initial size
114    pub fn initial(&self) -> u32 { self.initial }
115    /// Maximum size
116    pub fn maximum(&self) -> Option<u32> { self.maximum }
117}
118
119impl Deserialize for ResizableLimits {
120    type Error = Error;
121
122    fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
123        let has_max = VarUint1::deserialize(reader)?;
124        let initial = VarUint32::deserialize(reader)?;
125        let maximum = if has_max.into() {
126            Some(VarUint32::deserialize(reader)?.into())
127        } else {
128            None
129        };
130
131        Ok(ResizableLimits {
132            initial: initial.into(),
133            maximum: maximum,
134        })
135    }
136}
137
138impl Serialize for ResizableLimits {
139    type Error = Error;
140
141    fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
142        let max = self.maximum;
143        VarUint1::from(max.is_some()).serialize(writer)?;
144        VarUint32::from(self.initial).serialize(writer)?;
145        if let Some(val) = max {
146            VarUint32::from(val).serialize(writer)?;
147        }
148        Ok(())
149    }
150}
151
152/// Memory entry.
153#[derive(Debug, Clone)]
154pub struct MemoryType(ResizableLimits);
155
156impl MemoryType {
157    /// New memory definition
158    pub fn new(min: u32, max: Option<u32>) -> Self {
159        MemoryType(ResizableLimits::new(min, max))
160    }
161    /// Limits of the memory entry.
162    pub fn limits(&self) -> &ResizableLimits {
163        &self.0
164    }
165}
166
167impl Deserialize for MemoryType {
168    type Error = Error;
169
170    fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
171        Ok(MemoryType(ResizableLimits::deserialize(reader)?))
172    }
173}
174
175impl Serialize for MemoryType {
176    type Error = Error;
177
178    fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
179        self.0.serialize(writer)
180    }
181}
182
183/// External to local binding.
184#[derive(Debug, Clone)]
185pub enum External {
186    /// Binds to function with index.
187    Function(u32),
188    /// Describes local table definition to be imported as.
189    Table(TableType),
190    /// Describes local memory definition to be imported as.
191    Memory(MemoryType),
192    /// Describes local global entry to be imported as.
193    Global(GlobalType),
194}
195
196impl Deserialize for External {
197    type Error = Error;
198
199    fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
200        let kind = VarUint7::deserialize(reader)?;
201        match kind.into() {
202            0x00 => Ok(External::Function(VarUint32::deserialize(reader)?.into())),
203            0x01 => Ok(External::Table(TableType::deserialize(reader)?)),
204            0x02 => Ok(External::Memory(MemoryType::deserialize(reader)?)),
205            0x03 => Ok(External::Global(GlobalType::deserialize(reader)?)),
206            _ => Err(Error::UnknownExternalKind(kind.into())),
207        }
208    }
209}
210
211impl Serialize for External {
212    type Error = Error;
213
214    fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
215        use self::External::*;
216
217        match self {
218            Function(index) => {
219                VarUint7::from(0x00).serialize(writer)?;
220                VarUint32::from(index).serialize(writer)?;
221            },
222            Table(tt) => {
223                VarInt7::from(0x01).serialize(writer)?;
224                tt.serialize(writer)?;
225            },
226            Memory(mt) => {
227                VarInt7::from(0x02).serialize(writer)?;
228                mt.serialize(writer)?;
229            },
230            Global(gt) => {
231                VarInt7::from(0x03).serialize(writer)?;
232                gt.serialize(writer)?;
233            },
234        }
235
236        Ok(())
237    }
238}
239
240/// Import entry.
241#[derive(Debug, Clone)]
242pub struct ImportEntry {
243    module_str: String,
244    field_str: String,
245    external: External,
246}
247
248impl ImportEntry {
249    /// New import entry.
250    pub fn new(module_str: String, field_str: String, external: External) -> Self {
251        ImportEntry {
252            module_str: module_str,
253            field_str: field_str,
254            external: external,
255        }
256    }
257
258    /// Module reference of the import entry.
259    pub fn module(&self) -> &str { &self.module_str }
260
261    /// Module reference of the import entry (mutable).
262    pub fn module_mut(&mut self) -> &mut String {
263        &mut self.module_str
264    }
265
266    /// Field reference of the import entry.
267    pub fn field(&self) -> &str { &self.field_str }
268
269    /// Field reference of the import entry (mutable)
270    pub fn field_mut(&mut self) -> &mut String {
271        &mut self.field_str
272    }
273
274    /// Local binidng of the import entry.
275    pub fn external(&self) -> &External { &self.external }
276
277    /// Local binidng of the import entry (mutable)
278    pub fn external_mut(&mut self) -> &mut External { &mut self.external }
279}
280
281impl Deserialize for ImportEntry {
282    type Error = Error;
283
284    fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
285        let module_str = String::deserialize(reader)?;
286        let field_str = String::deserialize(reader)?;
287        let external = External::deserialize(reader)?;
288
289        Ok(ImportEntry {
290            module_str: module_str,
291            field_str: field_str,
292            external: external,
293        })
294    }
295}
296
297impl Serialize for ImportEntry {
298    type Error = Error;
299
300    fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
301        self.module_str.serialize(writer)?;
302        self.field_str.serialize(writer)?;
303        self.external.serialize(writer)
304    }
305}