wasmer/vm/
impls.rs

1use crate::{AsStoreMut, macros::backend::match_rt};
2
3use super::*;
4
5impl VMExternToExtern for VMExtern {
6    fn to_extern(self, store: &mut impl crate::AsStoreMut) -> crate::Extern {
7        match_rt!(on self => s {
8            s.to_extern(store)
9        })
10    }
11}
12
13impl VMFunctionEnvironment {
14    #[allow(clippy::should_implement_trait)]
15    /// Returns a reference to the underlying value.
16    pub fn as_ref(&self) -> &(dyn std::any::Any + Send + 'static) {
17        match_rt!(on self => s {
18            s.as_ref()
19        })
20    }
21
22    #[allow(clippy::should_implement_trait)]
23    /// Returns a mutable reference to the underlying value.
24    pub fn as_mut(&mut self) -> &mut (dyn std::any::Any + Send + 'static) {
25        match_rt!(on self => s {
26            s.as_mut()
27        })
28    }
29
30    pub fn contents(self) -> Box<(dyn std::any::Any + Send + 'static)> {
31        match_rt!(on self => s {
32            s.contents
33        })
34    }
35}
36
37impl VMFuncRef {
38    /// Converts the `VMFuncRef` into a `RawValue`.
39    pub fn into_raw(self) -> RawValue {
40        match_rt!(on self => s {
41            s.into_raw()
42        })
43    }
44}
45
46impl VMExternRef {
47    /// Converts the `VMExternRef` into a `RawValue`.
48    pub fn into_raw(self) -> RawValue {
49        match_rt!(on self => s {
50            s.into_raw()
51        })
52    }
53}
54
55impl VMMemory {
56    /// Attempts to share this memory and return a shared detached memory.
57    pub(crate) fn as_shared(&self) -> Result<VMSharedMemory, wasmer_types::MemoryError> {
58        match self {
59            #[cfg(feature = "sys")]
60            Self::Sys(s) => s.0.as_shared().map(VMSharedMemory::Sys),
61            #[cfg(feature = "v8")]
62            Self::V8(s) => s.as_shared().map(VMSharedMemory::V8),
63            #[cfg(feature = "js")]
64            Self::Js(s) => s.try_clone().map(VMSharedMemory::Js),
65        }
66    }
67}
68
69impl VMSharedMemory {
70    /// Clones this shared memory handle.
71    pub(crate) fn clone(&self) -> Self {
72        match self {
73            #[cfg(feature = "sys")]
74            Self::Sys(s) => Self::Sys(s.clone()),
75            #[cfg(feature = "v8")]
76            Self::V8(s) => Self::V8(s.clone()),
77            #[cfg(feature = "js")]
78            Self::Js(s) => Self::Js(
79                s.try_clone()
80                    .expect("cloning JavaScript shared memory should not fail"),
81            ),
82        }
83    }
84
85    pub(crate) fn into_vm_memory(self, store: &mut impl AsStoreMut) -> VMMemory {
86        match self {
87            #[cfg(feature = "sys")]
88            Self::Sys(s) => VMMemory::Sys(s.into()),
89            #[cfg(feature = "v8")]
90            Self::V8(s) => {
91                let mut store = store.as_store_mut();
92                VMMemory::V8(s.into_vm_memory(store.inner.store.as_v8_mut()))
93            }
94            #[cfg(feature = "js")]
95            Self::Js(s) => VMMemory::Js(s),
96        }
97    }
98}
99
100impl VMExceptionRef {
101    /// Converts the `VMExternRef` into a `RawValue`.
102    pub fn into_raw(self) -> RawValue {
103        match self {
104            #[cfg(feature = "sys")]
105            Self::Sys(s) => s.into_raw(),
106
107            _ => unimplemented!("VMExceptionRef::into_raw is only implemented for the sys backend"),
108        }
109    }
110}