wasmer/vm/
mod.rs

1//! This module defines traits to handle abstractions created by the runtimes.
2
3mod impls;
4
5use crate::VMExternToExtern;
6use wasmer_types::RawValue;
7
8macro_rules! define_vm_like {
9    ($name: ident $(, $derive:ident)*) => {
10        paste::paste! {
11        /// The enum for all those VM values of this kind.
12        $(#[derive($derive)])*
13        #[repr(C)]
14        pub enum [<VM $name>] {
15            #[cfg(feature = "sys")]
16            Sys(crate::backend::sys::vm::[<VM $name>]),
17            #[cfg(feature = "wamr")]
18            Wamr(crate::backend::wamr::vm::[<VM $name>]),
19            #[cfg(feature = "wasmi")]
20            Wasmi(crate::backend::wasmi::vm::[<VM $name>]),
21            #[cfg(feature = "v8")]
22            V8(crate::backend::v8::vm::[<VM $name>]),
23            #[cfg(feature = "js")]
24            Js(crate::backend::js::vm::[<VM $name>]),
25            #[cfg(feature = "jsc")]
26            Jsc(crate::backend::jsc::vm::[<VM $name>]),
27        }
28
29        impl [<VM $name>] {
30            #[cfg(feature = "sys")]
31            /// Consume `self` into a `sys` VM kind.
32            pub fn into_sys(self) -> crate::backend::sys::vm::[<VM $name>] {
33                match self {
34                    [<VM $name>]::Sys(s) => s,
35                    _ => panic!("Not a `sys` value!")
36                }
37            }
38
39            #[cfg(feature = "sys")]
40            /// Convert a reference to [`self`] into a reference to the same `sys` VM kind.
41            pub fn as_sys(&self) -> &crate::backend::sys::vm::[<VM $name>] {
42                match self {
43                    [<VM $name>]::Sys(s) => s,
44                    _ => panic!("Not a `sys` value!")
45                }
46            }
47
48            #[cfg(feature = "sys")]
49            /// Convert a mutable reference to [`self`] into a mutable reference to the same `sys` VM kind.
50            pub fn as_sys_mut(&mut self) -> &mut crate::backend::sys::vm::[<VM $name>] {
51                match self {
52                    [<VM $name>]::Sys(s) => s,
53                    _ => panic!("Not a `sys` value!")
54                }
55            }
56
57            #[cfg(feature = "wamr")]
58            /// Consume `self` into a `wamr` VM kind.
59            pub fn into_wamr(self) -> crate::backend::wamr::vm::[<VM $name>] {
60                match self {
61                    [<VM $name>]::Wamr(s) => s,
62                    _ => panic!("Not a `wamr` value!")
63                }
64            }
65
66            #[cfg(feature = "wamr")]
67            /// Convert a reference to [`self`] into a reference to the same `wamr` VM kind.
68            pub fn as_wamr(&self) -> &crate::backend::wamr::vm::[<VM $name>] {
69                match self {
70                    [<VM $name>]::Wamr(s) => s,
71                    _ => panic!("Not a `wamr` value!")
72                }
73            }
74
75            #[cfg(feature = "wamr")]
76            /// Convert a mutable reference to [`self`] into a mutable reference to the same `wamr` VM kind.
77            pub fn as_wamr_mut(&mut self) -> &mut crate::backend::wamr::vm::[<VM $name>] {
78                match self {
79                    [<VM $name>]::Wamr(s) => s,
80                    _ => panic!("Not a `wamr` value!")
81                }
82            }
83
84            #[cfg(feature = "wasmi")]
85            /// Consume `self` into a `wasmi` VM kind.
86            pub fn into_wasmi(self) -> crate::backend::wasmi::vm::[<VM $name>] {
87                match self {
88                    [<VM $name>]::Wasmi(s) => s,
89                    _ => panic!("Not a `wasmi` value!")
90                }
91            }
92
93            #[cfg(feature = "wasmi")]
94            /// Convert a reference to [`self`] into a reference to the same `wasmi` VM kind.
95            pub fn as_wasmi(&self) -> &crate::backend::wasmi::vm::[<VM $name>] {
96                match self {
97                    [<VM $name>]::Wasmi(s) => s,
98                    _ => panic!("Not a `wasmi` value!")
99                }
100            }
101
102            #[cfg(feature = "wasmi")]
103            /// Convert a mutable reference to [`self`] into a mutable reference to the same `wasmi` VM kind.
104            pub fn as_wasmi_mut(&mut self) -> &mut crate::backend::wasmi::vm::[<VM $name>] {
105                match self {
106                    [<VM $name>]::Wasmi(s) => s,
107                    _ => panic!("Not a `wasmi` value!")
108                }
109            }
110
111            #[cfg(feature = "v8")]
112            /// Consume `self` into a `v8` VM kind.
113            pub fn into_v8(self) -> crate::backend::v8::vm::[<VM $name>] {
114                match self {
115                    [<VM $name>]::V8(s) => s,
116                    _ => panic!("Not a `v8` value!")
117                }
118            }
119
120            #[cfg(feature = "v8")]
121            /// Convert a reference to [`self`] into a reference to the same `v8` VM kind.
122            pub fn as_v8(&self) -> &crate::backend::v8::vm::[<VM $name>] {
123                match self {
124                    [<VM $name>]::V8(s) => s,
125                    _ => panic!("Not a `v8` value!")
126                }
127            }
128
129            #[cfg(feature = "v8")]
130            /// Convert a mutable reference to [`self`] into a mutable reference to the same `v8` VM kind.
131            pub fn as_v8_mut(&mut self) -> &mut crate::backend::v8::vm::[<VM $name>] {
132                match self {
133                    [<VM $name>]::V8(s) => s,
134                    _ => panic!("Not a `v8` value!")
135                }
136            }
137
138            #[cfg(feature = "js")]
139            /// Consume `self` into a `js` VM kind.
140            pub fn into_js(self) -> crate::backend::js::vm::[<VM $name>] {
141                match self {
142                    [<VM $name>]::Js(s) => s,
143                    _ => panic!("Not a `js` value!")
144                }
145            }
146
147            #[cfg(feature = "js")]
148            /// Convert a reference to [`self`] into a reference to the same `js` VM kind.
149            pub fn as_js(&self) -> &crate::backend::js::vm::[<VM $name>] {
150                match self {
151                    [<VM $name>]::Js(s) => s,
152                    _ => panic!("Not a `js` value!")
153                }
154            }
155
156            #[cfg(feature = "js")]
157            /// Convert a mutable reference to [`self`] into a mutable reference to the same `js` VM kind.
158            pub fn as_js_mut(&mut self) -> &mut crate::backend::js::vm::[<VM $name>] {
159                match self {
160                    [<VM $name>]::Js(s) => s,
161                    _ => panic!("Not a `js` value!")
162                }
163            }
164
165            #[cfg(feature = "jsc")]
166            /// Consume `self` into a `jsc` VM kind.
167            pub fn into_jsc(self) -> crate::backend::jsc::vm::[<VM $name>] {
168                match self {
169                    [<VM $name>]::Jsc(s) => s,
170                    _ => panic!("Not a `jsc` value!")
171                }
172            }
173
174            #[cfg(feature = "jsc")]
175            /// Convert a reference to [`self`] into a reference to the same `jsc` VM kind.
176            pub fn as_jsc(&self) -> &crate::backend::jsc::vm::[<VM $name>] {
177                match self {
178                    [<VM $name>]::Jsc(s) => s,
179                    _ => panic!("Not a `jsc` value!")
180                }
181            }
182
183            #[cfg(feature = "jsc")]
184            /// Convert a mutable reference to [`self`] into a mutable reference to the same `jsc` VM kind.
185            pub fn as_jsc_mut(&mut self) -> &mut crate::backend::jsc::vm::[<VM $name>] {
186                match self {
187                    [<VM $name>]::Jsc(s) => s,
188                    _ => panic!("Not a `jsc` value!")
189                }
190            }
191        }
192        }
193    };
194
195    ($name: ident $(, $derive:ident)*, @From) => {
196        paste::paste! {
197        /// The enum for all those VM values of this kind.
198        $(#[derive($derive)])*
199        #[repr(C)]
200        pub enum [<VM $name>] {
201            #[cfg(feature = "sys")]
202            Sys(crate::backend::sys::vm::[<VM $name>]),
203            #[cfg(feature = "wamr")]
204            Wamr(crate::backend::wamr::vm::[<VM $name>]),
205            #[cfg(feature = "wasmi")]
206            Wasmi(crate::backend::wasmi::vm::[<VM $name>]),
207            #[cfg(feature = "v8")]
208            V8(crate::backend::v8::vm::[<VM $name>]),
209            #[cfg(feature = "js")]
210            Js(crate::backend::js::vm::[<VM $name>]),
211            #[cfg(feature = "jsc")]
212            Jsc(crate::backend::jsc::vm::[<VM $name>]),
213        }
214
215        #[cfg(feature = "sys")]
216        impl From<crate::backend::sys::vm::[<VM $name>]> for [<VM $name>] {
217            fn from(value: crate::backend::sys::vm::[<VM $name>]) -> Self {
218                [<VM $name>]::Sys(value)
219            }
220        }
221
222        #[cfg(feature = "wamr")]
223        impl From<crate::backend::wamr::vm::[<VM $name>]> for [<VM $name>] {
224            fn from(value: crate::backend::wamr::vm::[<VM $name>]) -> Self {
225                [<VM $name>]::Wamr(value)
226            }
227        }
228
229        #[cfg(feature = "wasmi")]
230        impl From<crate::backend::wasmi::vm::[<VM $name>]> for [<VM $name>] {
231            fn from(value: crate::backend::wasmi::vm::[<VM $name>]) -> Self {
232                [<VM $name>]::Wasmi(value)
233            }
234        }
235
236        #[cfg(feature = "v8")]
237        impl From<crate::backend::v8::vm::[<VM $name>]> for [<VM $name>] {
238            fn from(value: crate::backend::v8::vm::[<VM $name>]) -> Self {
239                [<VM $name>]::V8(value)
240            }
241        }
242
243        #[cfg(feature = "js")]
244        impl From<crate::backend::js::vm::[<VM $name>]> for [<VM $name>] {
245            fn from(value: crate::backend::js::vm::[<VM $name>]) -> Self {
246                [<VM $name>]::Js(value)
247            }
248        }
249
250        #[cfg(feature = "jsc")]
251        impl From<crate::backend::jsc::vm::[<VM $name>]> for [<VM $name>] {
252            fn from(value: crate::backend::jsc::vm::[<VM $name>]) -> Self {
253                [<VM $name>]::Jsc(value)
254            }
255        }
256
257        impl [<VM $name>] {
258            #[cfg(feature = "sys")]
259            /// Consume `self` into a `sys` VM kind.
260            pub fn into_sys(self) -> crate::backend::sys::vm::[<VM $name>] {
261                match self {
262                    [<VM $name>]::Sys(s) => s,
263                    _ => panic!("Not a `sys` value!")
264                }
265            }
266
267            #[cfg(feature = "sys")]
268            /// Convert a reference to [`self`] into a reference to the same `sys` VM kind.
269            pub fn as_sys(&self) -> &crate::backend::sys::vm::[<VM $name>] {
270                match self {
271                    [<VM $name>]::Sys(s) => s,
272                    _ => panic!("Not a `sys` value!")
273                }
274            }
275
276            #[cfg(feature = "sys")]
277            /// Convert a mutable reference to [`self`] into a mutable reference to the same `sys` VM kind.
278            pub fn as_sys_mut(&mut self) -> &mut crate::backend::sys::vm::[<VM $name>] {
279                match self {
280                    [<VM $name>]::Sys(s) => s,
281                    _ => panic!("Not a `sys` value!")
282                }
283            }
284
285            #[cfg(feature = "wamr")]
286            /// Consume `self` into a `wamr` VM kind.
287            pub fn into_wamr(self) -> crate::backend::wamr::vm::[<VM $name>] {
288                match self {
289                    [<VM $name>]::Wamr(s) => s,
290                    _ => panic!("Not a `wamr` value!")
291                }
292            }
293
294            #[cfg(feature = "wamr")]
295            /// Convert a reference to [`self`] into a reference to the same `wamr` VM kind.
296            pub fn as_wamr(&self) -> &crate::backend::wamr::vm::[<VM $name>] {
297                match self {
298                    [<VM $name>]::Wamr(s) => s,
299                    _ => panic!("Not a `wamr` value!")
300                }
301            }
302
303            #[cfg(feature = "wamr")]
304            /// Convert a mutable reference to [`self`] into a mutable reference to the same `wamr` VM kind.
305            pub fn as_wamr_mut(&mut self) -> &mut crate::backend::wamr::vm::[<VM $name>] {
306                match self {
307                    [<VM $name>]::Wamr(s) => s,
308                    _ => panic!("Not a `wamr` value!")
309                }
310            }
311
312            #[cfg(feature = "wasmi")]
313            /// Consume `self` into a `wasmi` VM kind.
314            pub fn into_wasmi(self) -> crate::backend::wasmi::vm::[<VM $name>] {
315                match self {
316                    [<VM $name>]::Wasmi(s) => s,
317                    _ => panic!("Not a `wasmi` value!")
318                }
319            }
320
321            #[cfg(feature = "wasmi")]
322            /// Convert a reference to [`self`] into a reference to the same `wasmi` VM kind.
323            pub fn as_wasmi(&self) -> &crate::backend::wasmi::vm::[<VM $name>] {
324                match self {
325                    [<VM $name>]::Wasmi(s) => s,
326                    _ => panic!("Not a `wasmi` value!")
327                }
328            }
329
330            #[cfg(feature = "wasmi")]
331            /// Convert a mutable reference to [`self`] into a mutable reference to the same `wasmi` VM kind.
332            pub fn as_wasmi_mut(&mut self) -> &mut crate::backend::wasmi::vm::[<VM $name>] {
333                match self {
334                    [<VM $name>]::Wasmi(s) => s,
335                    _ => panic!("Not a `wasmi` value!")
336                }
337            }
338
339            #[cfg(feature = "v8")]
340            /// Consume `self` into a `v8` VM kind.
341            pub fn into_v8(self) -> crate::backend::v8::vm::[<VM $name>] {
342                match self {
343                    [<VM $name>]::V8(s) => s,
344                    _ => panic!("Not a `v8` value!")
345                }
346            }
347
348            #[cfg(feature = "v8")]
349            /// Convert a reference to [`self`] into a reference to the same `v8` VM kind.
350            pub fn as_v8(&self) -> &crate::backend::v8::vm::[<VM $name>] {
351                match self {
352                    [<VM $name>]::V8(s) => s,
353                    _ => panic!("Not a `v8` value!")
354                }
355            }
356
357            #[cfg(feature = "v8")]
358            /// Convert a mutable reference to [`self`] into a mutable reference to the same `v8` VM kind.
359            pub fn as_v8_mut(&mut self) -> &mut crate::backend::v8::vm::[<VM $name>] {
360                match self {
361                    [<VM $name>]::V8(s) => s,
362                    _ => panic!("Not a `v8` value!")
363                }
364            }
365
366            #[cfg(feature = "js")]
367            /// Consume `self` into a `js` VM kind.
368            pub fn into_js(self) -> crate::backend::js::vm::[<VM $name>] {
369                match self {
370                    [<VM $name>]::Js(s) => s,
371                    _ => panic!("Not a `js` value!")
372                }
373            }
374
375            #[cfg(feature = "js")]
376            /// Convert a reference to [`self`] into a reference to the same `js` VM kind.
377            pub fn as_js(&self) -> &crate::backend::js::vm::[<VM $name>] {
378                match self {
379                    [<VM $name>]::Js(s) => s,
380                    _ => panic!("Not a `js` value!")
381                }
382            }
383
384            #[cfg(feature = "js")]
385            /// Convert a mutable reference to [`self`] into a mutable reference to the same `js` VM kind.
386            pub fn as_js_mut(&mut self) -> &mut crate::backend::js::vm::[<VM $name>] {
387                match self {
388                    [<VM $name>]::Js(s) => s,
389                    _ => panic!("Not a `js` value!")
390                }
391            }
392
393            #[cfg(feature = "jsc")]
394            /// Consume `self` into a `jsc` VM kind.
395            pub fn into_jsc(self) -> crate::backend::jsc::vm::[<VM $name>] {
396                match self {
397                    [<VM $name>]::Jsc(s) => s,
398                    _ => panic!("Not a `jsc` value!")
399                }
400            }
401
402            #[cfg(feature = "jsc")]
403            /// Convert a reference to [`self`] into a reference to the same `jsc` VM kind.
404            pub fn as_jsc(&self) -> &crate::backend::jsc::vm::[<VM $name>] {
405                match self {
406                    [<VM $name>]::Jsc(s) => s,
407                    _ => panic!("Not a `jsc` value!")
408                }
409            }
410
411            #[cfg(feature = "jsc")]
412            /// Convert a mutable reference to [`self`] into a mutable reference to the same `jsc` VM kind.
413            pub fn as_jsc_mut(&mut self) -> &mut crate::backend::jsc::vm::[<VM $name>] {
414                match self {
415                    [<VM $name>]::Jsc(s) => s,
416                    _ => panic!("Not a `jsc` value!")
417                }
418            }
419        }
420        }
421    };
422}
423
424define_vm_like!(Extern);
425define_vm_like!(ExternFunction);
426define_vm_like!(ExternGlobal);
427define_vm_like!(ExternTag);
428define_vm_like!(ExternMemory);
429define_vm_like!(ExternTable);
430//define_vm_like!(ExternObj, Debug);
431define_vm_like!(FunctionCallback);
432define_vm_like!(FunctionBody);
433define_vm_like!(FunctionEnvironment, Debug);
434define_vm_like!(Instance, Debug);
435define_vm_like!(Trampoline);
436
437//define_vm_like!(Config);
438define_vm_like!(Function, Debug);
439define_vm_like!(Global, Debug);
440define_vm_like!(Tag, Debug);
441define_vm_like!(Memory, Debug, @From);
442define_vm_like!(SharedMemory);
443define_vm_like!(Table, Debug);
444
445define_vm_like!(ExceptionRef);
446define_vm_like!(ExternRef);
447define_vm_like!(FuncRef);