soroban_wasmi/instance/
exports.rs1use crate::{
2 collections::map::Iter as MapIter,
3 AsContext,
4 Func,
5 FuncType,
6 Global,
7 GlobalType,
8 Memory,
9 MemoryType,
10 Table,
11 TableType,
12};
13use core::iter::FusedIterator;
14use std::boxed::Box;
15
16#[derive(Debug, Copy, Clone)]
21pub enum Extern {
22 Global(Global),
26 Table(Table),
28 Memory(Memory),
30 Func(Func),
32}
33
34impl From<Global> for Extern {
35 fn from(global: Global) -> Self {
36 Self::Global(global)
37 }
38}
39
40impl From<Table> for Extern {
41 fn from(table: Table) -> Self {
42 Self::Table(table)
43 }
44}
45
46impl From<Memory> for Extern {
47 fn from(memory: Memory) -> Self {
48 Self::Memory(memory)
49 }
50}
51
52impl From<Func> for Extern {
53 fn from(func: Func) -> Self {
54 Self::Func(func)
55 }
56}
57
58impl Extern {
59 pub fn into_global(self) -> Option<Global> {
63 if let Self::Global(global) = self {
64 return Some(global);
65 }
66 None
67 }
68
69 pub fn into_table(self) -> Option<Table> {
73 if let Self::Table(table) = self {
74 return Some(table);
75 }
76 None
77 }
78
79 pub fn into_memory(self) -> Option<Memory> {
83 if let Self::Memory(memory) = self {
84 return Some(memory);
85 }
86 None
87 }
88
89 pub fn into_func(self) -> Option<Func> {
93 if let Self::Func(func) = self {
94 return Some(func);
95 }
96 None
97 }
98
99 pub fn ty(&self, ctx: impl AsContext) -> ExternType {
105 match self {
106 Extern::Global(global) => global.ty(ctx).into(),
107 Extern::Table(table) => table.ty(ctx).into(),
108 Extern::Memory(memory) => memory.ty(ctx).into(),
109 Extern::Func(func) => func.ty(ctx).into(),
110 }
111 }
112}
113
114#[derive(Debug, Clone)]
118pub enum ExternType {
119 Global(GlobalType),
121 Table(TableType),
123 Memory(MemoryType),
125 Func(FuncType),
127}
128
129impl From<GlobalType> for ExternType {
130 fn from(global: GlobalType) -> Self {
131 Self::Global(global)
132 }
133}
134
135impl From<TableType> for ExternType {
136 fn from(table: TableType) -> Self {
137 Self::Table(table)
138 }
139}
140
141impl From<MemoryType> for ExternType {
142 fn from(memory: MemoryType) -> Self {
143 Self::Memory(memory)
144 }
145}
146
147impl From<FuncType> for ExternType {
148 fn from(func: FuncType) -> Self {
149 Self::Func(func)
150 }
151}
152
153impl ExternType {
154 pub fn global(&self) -> Option<&GlobalType> {
156 match self {
157 Self::Global(ty) => Some(ty),
158 _ => None,
159 }
160 }
161
162 pub fn table(&self) -> Option<&TableType> {
164 match self {
165 Self::Table(ty) => Some(ty),
166 _ => None,
167 }
168 }
169
170 pub fn memory(&self) -> Option<&MemoryType> {
172 match self {
173 Self::Memory(ty) => Some(ty),
174 _ => None,
175 }
176 }
177
178 pub fn func(&self) -> Option<&FuncType> {
180 match self {
181 Self::Func(ty) => Some(ty),
182 _ => None,
183 }
184 }
185}
186
187#[derive(Debug, Clone)]
192pub struct Export<'instance> {
193 name: &'instance str,
195 definition: Extern,
197}
198
199impl<'instance> Export<'instance> {
200 pub(crate) fn new(name: &'instance str, definition: Extern) -> Export<'instance> {
202 Self { name, definition }
203 }
204
205 pub fn name(&self) -> &'instance str {
207 self.name
208 }
209
210 pub fn ty(&self, ctx: impl AsContext) -> ExternType {
216 self.definition.ty(ctx)
217 }
218
219 pub fn into_extern(self) -> Extern {
221 self.definition
222 }
223
224 pub fn into_func(self) -> Option<Func> {
226 self.definition.into_func()
227 }
228
229 pub fn into_table(self) -> Option<Table> {
231 self.definition.into_table()
232 }
233
234 pub fn into_memory(self) -> Option<Memory> {
236 self.definition.into_memory()
237 }
238
239 pub fn into_global(self) -> Option<Global> {
241 self.definition.into_global()
242 }
243}
244
245#[derive(Debug)]
247pub struct ExportsIter<'instance> {
248 iter: MapIter<'instance, Box<str>, Extern>,
249}
250
251impl<'instance> ExportsIter<'instance> {
252 pub(super) fn new(iter: MapIter<'instance, Box<str>, Extern>) -> Self {
254 Self { iter }
255 }
256
257 #[allow(clippy::borrowed_box)]
259 fn convert_item((name, export): (&'instance Box<str>, &'instance Extern)) -> Export<'instance> {
260 Export::new(name, *export)
261 }
262}
263
264impl<'instance> Iterator for ExportsIter<'instance> {
265 type Item = Export<'instance>;
266
267 fn next(&mut self) -> Option<Self::Item> {
268 self.iter.next().map(Self::convert_item)
269 }
270
271 fn size_hint(&self) -> (usize, Option<usize>) {
272 self.iter.size_hint()
273 }
274}
275
276impl ExactSizeIterator for ExportsIter<'_> {}
277impl FusedIterator for ExportsIter<'_> {}