wasmtime/runtime/
externals.rs1use crate::store::StoreOpaque;
2use crate::{AsContext, Engine, ExternType, Func, Memory, SharedMemory};
3
4mod global;
5mod table;
6
7pub use global::Global;
8pub use table::Table;
9
10#[derive(Clone, Debug)]
20pub enum Extern {
21 Func(Func),
23 Global(Global),
26 Table(Table),
28 Memory(Memory),
30 SharedMemory(SharedMemory),
33}
34
35impl Extern {
36 pub fn into_func(self) -> Option<Func> {
40 match self {
41 Extern::Func(func) => Some(func),
42 _ => None,
43 }
44 }
45
46 pub fn into_global(self) -> Option<Global> {
50 match self {
51 Extern::Global(global) => Some(global),
52 _ => None,
53 }
54 }
55
56 pub fn into_table(self) -> Option<Table> {
60 match self {
61 Extern::Table(table) => Some(table),
62 _ => None,
63 }
64 }
65
66 pub fn into_memory(self) -> Option<Memory> {
70 match self {
71 Extern::Memory(memory) => Some(memory),
72 _ => None,
73 }
74 }
75
76 pub fn into_shared_memory(self) -> Option<SharedMemory> {
81 match self {
82 Extern::SharedMemory(memory) => Some(memory),
83 _ => None,
84 }
85 }
86
87 pub fn ty(&self, store: impl AsContext) -> ExternType {
96 let store = store.as_context();
97 match self {
98 Extern::Func(ft) => ExternType::Func(ft.ty(store)),
99 Extern::Memory(ft) => ExternType::Memory(ft.ty(store)),
100 Extern::SharedMemory(ft) => ExternType::Memory(ft.ty()),
101 Extern::Table(tt) => ExternType::Table(tt.ty(store)),
102 Extern::Global(gt) => ExternType::Global(gt.ty(store)),
103 }
104 }
105
106 pub(crate) unsafe fn from_wasmtime_export(
107 wasmtime_export: crate::runtime::vm::Export,
108 store: &mut StoreOpaque,
109 ) -> Extern {
110 match wasmtime_export {
111 crate::runtime::vm::Export::Function(f) => {
112 Extern::Func(Func::from_wasmtime_function(f, store))
113 }
114 crate::runtime::vm::Export::Memory(m) => {
115 if m.memory.memory.shared {
116 Extern::SharedMemory(SharedMemory::from_wasmtime_memory(m, store))
117 } else {
118 Extern::Memory(Memory::from_wasmtime_memory(m, store))
119 }
120 }
121 crate::runtime::vm::Export::Global(g) => {
122 Extern::Global(Global::from_wasmtime_global(g, store))
123 }
124 crate::runtime::vm::Export::Table(t) => {
125 Extern::Table(Table::from_wasmtime_table(t, store))
126 }
127 }
128 }
129
130 pub(crate) fn comes_from_same_store(&self, store: &StoreOpaque) -> bool {
131 match self {
132 Extern::Func(f) => f.comes_from_same_store(store),
133 Extern::Global(g) => store.store_data().contains(g.0),
134 Extern::Memory(m) => m.comes_from_same_store(store),
135 Extern::SharedMemory(m) => Engine::same(m.engine(), store.engine()),
136 Extern::Table(t) => store.store_data().contains(t.0),
137 }
138 }
139}
140
141impl From<Func> for Extern {
142 fn from(r: Func) -> Self {
143 Extern::Func(r)
144 }
145}
146
147impl From<Global> for Extern {
148 fn from(r: Global) -> Self {
149 Extern::Global(r)
150 }
151}
152
153impl From<Memory> for Extern {
154 fn from(r: Memory) -> Self {
155 Extern::Memory(r)
156 }
157}
158
159impl From<SharedMemory> for Extern {
160 fn from(r: SharedMemory) -> Self {
161 Extern::SharedMemory(r)
162 }
163}
164
165impl From<Table> for Extern {
166 fn from(r: Table) -> Self {
167 Extern::Table(r)
168 }
169}
170
171#[derive(Clone)]
179pub struct Export<'instance> {
180 name: &'instance str,
182
183 definition: Extern,
185}
186
187impl<'instance> Export<'instance> {
188 pub(crate) fn new(name: &'instance str, definition: Extern) -> Export<'instance> {
191 Export { name, definition }
192 }
193
194 pub fn name(&self) -> &'instance str {
196 self.name
197 }
198
199 pub fn ty(&self, store: impl AsContext) -> ExternType {
205 self.definition.ty(store)
206 }
207
208 pub fn into_extern(self) -> Extern {
210 self.definition
211 }
212
213 pub fn into_func(self) -> Option<Func> {
216 self.definition.into_func()
217 }
218
219 pub fn into_table(self) -> Option<Table> {
222 self.definition.into_table()
223 }
224
225 pub fn into_memory(self) -> Option<Memory> {
228 self.definition.into_memory()
229 }
230
231 pub fn into_global(self) -> Option<Global> {
234 self.definition.into_global()
235 }
236}