wasmer/entities/tag/
inner.rs

1use wasmer_types::{TagType, Type};
2
3use crate::{
4    AsStoreMut, AsStoreRef, ExportError, Exportable, Extern,
5    macros::backend::{gen_rt_ty, match_rt},
6    vm::{VMExtern, VMExternTag},
7};
8
9/// A WebAssembly `global` instance.
10///
11/// A global instance is the runtime representation of a global variable.
12/// It consists of an individual value and a flag indicating whether it is mutable.
13///
14/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#global-instances>
15gen_rt_ty!(Tag
16    @cfg feature = "artifact-size" => derive(loupe::MemoryUsage)
17    @derives Debug, Clone, PartialEq, Eq, derive_more::From
18);
19
20impl BackendTag {
21    /// Create a new tag with event of type P -> [], that is a function that takes parameters `P`
22    /// and has no return value.
23    #[inline]
24    pub fn new<P: Into<Box<[Type]>>>(store: &mut impl AsStoreMut, params: P) -> Self {
25        match &store.as_store_mut().inner.store {
26            #[cfg(feature = "sys")]
27            crate::BackendStore::Sys(_) => {
28                Self::Sys(crate::backend::sys::tag::Tag::new(store, params))
29            }
30            #[cfg(feature = "wamr")]
31            crate::BackendStore::Wamr(_) => {
32                Self::Wamr(crate::backend::wamr::tag::Tag::new(store, params))
33            }
34            #[cfg(feature = "wasmi")]
35            crate::BackendStore::Wasmi(_) => {
36                Self::Wasmi(crate::backend::wasmi::tag::Tag::new(store, params))
37            }
38            #[cfg(feature = "v8")]
39            crate::BackendStore::V8(_) => {
40                Self::V8(crate::backend::v8::entities::tag::Tag::new(store, params))
41            }
42            #[cfg(feature = "js")]
43            crate::BackendStore::Js(_) => {
44                Self::Js(crate::backend::js::tag::Tag::new(store, params))
45            }
46            #[cfg(feature = "jsc")]
47            crate::BackendStore::Jsc(_) => {
48                Self::Jsc(crate::backend::jsc::tag::Tag::new(store, params))
49            }
50        }
51    }
52
53    /// Returns the [`TagType`] of the tag.
54    #[inline]
55    pub fn ty(&self, store: &impl AsStoreRef) -> TagType {
56        match_rt!(on self => f {
57            f.ty(store)
58        })
59    }
60
61    #[inline]
62    pub(crate) fn from_vm_extern(store: &mut impl AsStoreMut, vm_extern: VMExternTag) -> Self {
63        match &store.as_store_mut().inner.store {
64            #[cfg(feature = "sys")]
65            crate::BackendStore::Sys(_) => Self::Sys(
66                crate::backend::sys::tag::Tag::from_vm_extern(store, vm_extern),
67            ),
68            #[cfg(feature = "wamr")]
69            crate::BackendStore::Wamr(_) => Self::Wamr(
70                crate::backend::wamr::tag::Tag::from_vm_extern(store, vm_extern),
71            ),
72            #[cfg(feature = "wasmi")]
73            crate::BackendStore::Wasmi(_) => Self::Wasmi(
74                crate::backend::wasmi::tag::Tag::from_vm_extern(store, vm_extern),
75            ),
76            #[cfg(feature = "v8")]
77            crate::BackendStore::V8(_) => Self::V8(
78                crate::backend::v8::entities::tag::Tag::from_vm_extern(store, vm_extern),
79            ),
80            #[cfg(feature = "js")]
81            crate::BackendStore::Js(_) => Self::Js(
82                crate::backend::js::entities::tag::Tag::from_vm_extern(store, vm_extern),
83            ),
84            #[cfg(feature = "jsc")]
85            crate::BackendStore::Jsc(_) => Self::Jsc(
86                crate::backend::jsc::entities::tag::Tag::from_vm_extern(store, vm_extern),
87            ),
88        }
89    }
90
91    /// Checks whether this tag can be used with the given context.
92    #[inline]
93    pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
94        match_rt!(on self => f {
95            f.is_from_store(store)
96        })
97    }
98
99    /// Create a [`VMExtern`] from self.
100    #[inline]
101    pub(crate) fn to_vm_extern(&self) -> VMExtern {
102        match_rt!(on self => f {
103            f.to_vm_extern()
104        })
105    }
106}
107
108impl<'a> Exportable<'a> for BackendTag {
109    fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
110        match _extern {
111            Extern::Tag(func) => Ok(&func.0),
112            _ => Err(ExportError::IncompatibleType),
113        }
114    }
115}