wasmer_types/
indexes.rs

1//! Helper functions and structures for the translation.
2use crate::entity::entity_impl;
3use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
4#[cfg(feature = "enable-serde")]
5use serde::{Deserialize, Serialize};
6
7/*
8 * [todo] xdoardo (on 2024/10/09)
9 *
10 * While updating rkyv from 0.7.x to 0.8.x it became unfeasible to represent archived
11 * version of some types as themselves (i.e. `rkyv(as = Self)`). This means that in cases
12 * like this one we can't simply return a reference to the underlying value.
13 *
14 * Hopefully as 0.8.x matures a bit more we can return to `rkyv(as = Self)`, making all
15 * of this faster and leaner. The missing bits for it are:
16 *
17 * 1. rkyv::rend::{u32_le, i32_le, u64_le, ..} should implement `serde::Serialize` and
18 *    `serde::Deserialize`.
19 *
20 */
21
22/// Index type of a function defined locally inside the WebAssembly module.
23#[derive(
24    Copy,
25    Clone,
26    PartialEq,
27    Eq,
28    Hash,
29    PartialOrd,
30    Ord,
31    Debug,
32    RkyvSerialize,
33    RkyvDeserialize,
34    Archive,
35)]
36#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
37#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
38#[rkyv(derive(Debug), compare(PartialOrd, PartialEq))]
39pub struct LocalFunctionIndex(u32);
40entity_impl!(LocalFunctionIndex);
41
42/// Index type of a table defined locally inside the WebAssembly module.
43#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
44#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
45#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
46pub struct LocalTableIndex(u32);
47entity_impl!(LocalTableIndex);
48
49/// Index type of a memory defined locally inside the WebAssembly module.
50#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
51#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
52#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
53pub struct LocalMemoryIndex(u32);
54entity_impl!(LocalMemoryIndex);
55
56/// Index type of a global defined locally inside the WebAssembly module.
57#[derive(
58    Copy,
59    Clone,
60    PartialEq,
61    Eq,
62    Hash,
63    PartialOrd,
64    Ord,
65    Debug,
66    RkyvSerialize,
67    RkyvDeserialize,
68    Archive,
69)]
70#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
71#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
72#[rkyv(derive(Debug), compare(PartialOrd, PartialEq))]
73pub struct LocalGlobalIndex(u32);
74entity_impl!(LocalGlobalIndex);
75
76/// Index type of a function (imported or local) inside the WebAssembly module.
77#[derive(
78    Copy,
79    Clone,
80    PartialEq,
81    Eq,
82    Hash,
83    PartialOrd,
84    Ord,
85    Debug,
86    RkyvSerialize,
87    RkyvDeserialize,
88    Archive,
89)]
90#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
91#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
92#[rkyv(
93    derive(Debug, PartialOrd, Ord, PartialEq, Eq),
94    compare(PartialOrd, PartialEq)
95)]
96pub struct FunctionIndex(u32);
97entity_impl!(FunctionIndex);
98
99/// Index type of a table (imported or local) inside the WebAssembly module.
100#[derive(
101    Copy,
102    Clone,
103    PartialEq,
104    Eq,
105    Hash,
106    PartialOrd,
107    Ord,
108    Debug,
109    RkyvSerialize,
110    RkyvDeserialize,
111    Archive,
112)]
113#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
114#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
115#[rkyv(
116    derive(Debug, PartialOrd, Ord, PartialEq, Eq),
117    compare(PartialOrd, PartialEq)
118)]
119pub struct TableIndex(u32);
120entity_impl!(TableIndex);
121
122/// Index type of a global variable (imported or local) inside the WebAssembly module.
123#[derive(
124    Copy,
125    Clone,
126    PartialEq,
127    Eq,
128    Hash,
129    PartialOrd,
130    Ord,
131    Debug,
132    RkyvSerialize,
133    RkyvDeserialize,
134    Archive,
135)]
136#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
137#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
138#[rkyv(derive(Debug), compare(PartialOrd, PartialEq))]
139pub struct GlobalIndex(u32);
140entity_impl!(GlobalIndex);
141
142/// Index type of a linear memory (imported or local) inside the WebAssembly module.
143#[derive(
144    Copy,
145    Clone,
146    PartialEq,
147    Eq,
148    Hash,
149    PartialOrd,
150    Ord,
151    Debug,
152    RkyvSerialize,
153    RkyvDeserialize,
154    Archive,
155)]
156#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
157#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
158#[rkyv(derive(Debug), compare(PartialOrd, PartialEq))]
159pub struct MemoryIndex(pub(crate) u32);
160entity_impl!(MemoryIndex);
161
162/// Index type of a signature (imported or local) inside the WebAssembly module.
163#[derive(
164    Copy,
165    Clone,
166    PartialEq,
167    Eq,
168    Hash,
169    PartialOrd,
170    Ord,
171    Debug,
172    RkyvSerialize,
173    RkyvDeserialize,
174    Archive,
175)]
176#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
177#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
178#[rkyv(derive(Debug), compare(PartialOrd, PartialEq))]
179pub struct SignatureIndex(u32);
180entity_impl!(SignatureIndex);
181
182/// Index type of a passive data segment inside the WebAssembly module.
183#[derive(
184    Copy,
185    Clone,
186    PartialEq,
187    Eq,
188    Hash,
189    PartialOrd,
190    Ord,
191    Debug,
192    RkyvSerialize,
193    RkyvDeserialize,
194    Archive,
195)]
196#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
197#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
198#[rkyv(
199    derive(Debug, PartialOrd, Ord, PartialEq, Eq),
200    compare(PartialOrd, PartialEq)
201)]
202pub struct DataIndex(u32);
203entity_impl!(DataIndex);
204
205/// Index type of a passive element segment inside the WebAssembly module.
206#[derive(
207    Copy,
208    Clone,
209    PartialEq,
210    Eq,
211    Hash,
212    PartialOrd,
213    Ord,
214    Debug,
215    RkyvSerialize,
216    RkyvDeserialize,
217    Archive,
218)]
219#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
220#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
221#[rkyv(
222    derive(Debug, PartialOrd, Ord, PartialEq, Eq),
223    compare(PartialOrd, PartialEq)
224)]
225pub struct ElemIndex(u32);
226entity_impl!(ElemIndex);
227
228/// Index type of a custom section inside a WebAssembly module.
229#[derive(
230    Copy,
231    Clone,
232    PartialEq,
233    Eq,
234    Hash,
235    PartialOrd,
236    Ord,
237    Debug,
238    RkyvSerialize,
239    RkyvDeserialize,
240    Archive,
241)]
242#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
243#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
244#[rkyv(derive(Debug), compare(PartialOrd, PartialEq))]
245pub struct CustomSectionIndex(u32);
246entity_impl!(CustomSectionIndex);
247
248/// An entity to export.
249#[derive(
250    Copy,
251    Clone,
252    Debug,
253    Hash,
254    PartialEq,
255    Eq,
256    PartialOrd,
257    Ord,
258    RkyvSerialize,
259    RkyvDeserialize,
260    Archive,
261)]
262#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
263#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
264#[rkyv(derive(Debug), compare(PartialOrd, PartialEq))]
265#[repr(u8)]
266pub enum ExportIndex {
267    /// Function export.
268    Function(FunctionIndex),
269    /// Table export.
270    Table(TableIndex),
271    /// Memory export.
272    Memory(MemoryIndex),
273    /// Global export.
274    Global(GlobalIndex),
275}
276
277/// An entity to import.
278#[derive(
279    Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, RkyvSerialize, RkyvDeserialize, Archive,
280)]
281#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
282#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
283#[rkyv(derive(Debug), compare(PartialOrd, PartialEq))]
284#[repr(u8)]
285pub enum ImportIndex {
286    /// Function import.
287    Function(FunctionIndex),
288    /// Table import.
289    Table(TableIndex),
290    /// Memory import.
291    Memory(MemoryIndex),
292    /// Global import.
293    Global(GlobalIndex),
294}