wasmtime/runtime/
externals.rs1use crate::store::StoreOpaque;
2use crate::{AsContext, Engine, ExternType, Func, Memory, SharedMemory};
3
4mod global;
5mod table;
6mod tag;
7
8pub use global::Global;
9pub use table::Table;
10pub use tag::Tag;
11
12#[derive(Clone, Debug)]
22pub enum Extern {
23 Func(Func),
25 Global(Global),
28 Table(Table),
30 Memory(Memory),
32 SharedMemory(SharedMemory),
35 Tag(Tag),
38}
39
40impl Extern {
41 #[inline]
45 pub fn into_func(self) -> Option<Func> {
46 match self {
47 Extern::Func(func) => Some(func),
48 _ => None,
49 }
50 }
51
52 #[inline]
56 pub fn into_global(self) -> Option<Global> {
57 match self {
58 Extern::Global(global) => Some(global),
59 _ => None,
60 }
61 }
62
63 #[inline]
67 pub fn into_table(self) -> Option<Table> {
68 match self {
69 Extern::Table(table) => Some(table),
70 _ => None,
71 }
72 }
73
74 #[inline]
78 pub fn into_memory(self) -> Option<Memory> {
79 match self {
80 Extern::Memory(memory) => Some(memory),
81 _ => None,
82 }
83 }
84
85 #[inline]
90 pub fn into_shared_memory(self) -> Option<SharedMemory> {
91 match self {
92 Extern::SharedMemory(memory) => Some(memory),
93 _ => None,
94 }
95 }
96
97 #[inline]
101 pub fn into_tag(self) -> Option<Tag> {
102 match self {
103 Extern::Tag(tag) => Some(tag),
104 _ => None,
105 }
106 }
107
108 pub fn ty(&self, store: impl AsContext) -> ExternType {
117 let store = store.as_context();
118 match self {
119 Extern::Func(ft) => ExternType::Func(ft.ty(store)),
120 Extern::Memory(ft) => ExternType::Memory(ft.ty(store)),
121 Extern::SharedMemory(ft) => ExternType::Memory(ft.ty()),
122 Extern::Table(tt) => ExternType::Table(tt.ty(store)),
123 Extern::Global(gt) => ExternType::Global(gt.ty(store)),
124 Extern::Tag(tt) => ExternType::Tag(tt.ty(store)),
125 }
126 }
127
128 pub(crate) unsafe fn from_wasmtime_export(
129 wasmtime_export: crate::runtime::vm::Export,
130 store: &StoreOpaque,
131 ) -> Extern {
132 match wasmtime_export {
133 crate::runtime::vm::Export::Function(f) => {
134 Extern::Func(Func::from_wasmtime_function(f, store))
135 }
136 crate::runtime::vm::Export::Memory(m) => {
137 if m.memory.shared {
138 Extern::SharedMemory(SharedMemory::from_wasmtime_memory(m, store))
139 } else {
140 Extern::Memory(Memory::from_wasmtime_memory(m, store))
141 }
142 }
143 crate::runtime::vm::Export::Global(g) => {
144 Extern::Global(Global::from_wasmtime_global(g, store))
145 }
146 crate::runtime::vm::Export::Table(t) => {
147 Extern::Table(Table::from_wasmtime_table(t, store))
148 }
149 crate::runtime::vm::Export::Tag(t) => Extern::Tag(Tag::from_wasmtime_tag(t, store)),
150 }
151 }
152
153 pub(crate) fn comes_from_same_store(&self, store: &StoreOpaque) -> bool {
154 match self {
155 Extern::Func(f) => f.comes_from_same_store(store),
156 Extern::Global(g) => g.comes_from_same_store(store),
157 Extern::Memory(m) => m.comes_from_same_store(store),
158 Extern::SharedMemory(m) => Engine::same(m.engine(), store.engine()),
159 Extern::Table(t) => t.comes_from_same_store(store),
160 Extern::Tag(t) => t.comes_from_same_store(store),
161 }
162 }
163}
164
165impl From<Func> for Extern {
166 fn from(r: Func) -> Self {
167 Extern::Func(r)
168 }
169}
170
171impl From<Global> for Extern {
172 fn from(r: Global) -> Self {
173 Extern::Global(r)
174 }
175}
176
177impl From<Memory> for Extern {
178 fn from(r: Memory) -> Self {
179 Extern::Memory(r)
180 }
181}
182
183impl From<SharedMemory> for Extern {
184 fn from(r: SharedMemory) -> Self {
185 Extern::SharedMemory(r)
186 }
187}
188
189impl From<Table> for Extern {
190 fn from(r: Table) -> Self {
191 Extern::Table(r)
192 }
193}
194
195impl From<Tag> for Extern {
196 fn from(t: Tag) -> Self {
197 Extern::Tag(t)
198 }
199}
200
201#[derive(Clone)]
209pub struct Export<'instance> {
210 name: &'instance str,
212
213 definition: Extern,
215}
216
217impl<'instance> Export<'instance> {
218 pub(crate) fn new(name: &'instance str, definition: Extern) -> Export<'instance> {
221 Export { name, definition }
222 }
223
224 pub fn name(&self) -> &'instance str {
226 self.name
227 }
228
229 pub fn ty(&self, store: impl AsContext) -> ExternType {
235 self.definition.ty(store)
236 }
237
238 pub fn into_extern(self) -> Extern {
240 self.definition
241 }
242
243 pub fn into_func(self) -> Option<Func> {
246 self.definition.into_func()
247 }
248
249 pub fn into_table(self) -> Option<Table> {
252 self.definition.into_table()
253 }
254
255 pub fn into_memory(self) -> Option<Memory> {
258 self.definition.into_memory()
259 }
260
261 pub fn into_global(self) -> Option<Global> {
264 self.definition.into_global()
265 }
266
267 pub fn into_tag(self) -> Option<Tag> {
270 self.definition.into_tag()
271 }
272}