wasmer_types/
indexes.rs

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