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! {
16    #[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
17    #[derive(Debug, Clone, PartialEq, Eq, derive_more::From)]
18    pub(crate) BackendTag(entities::tag::Tag);
19}
20
21impl BackendTag {
22    /// Create a new tag with event of type P -> [], that is a function that takes parameters `P`
23    /// and has no return value.
24    #[inline]
25    pub fn new<P: Into<Box<[Type]>>>(store: &mut impl AsStoreMut, params: P) -> Self {
26        match &store.as_store_mut().inner.store {
27            #[cfg(feature = "sys")]
28            crate::BackendStore::Sys(_) => {
29                Self::Sys(crate::backend::sys::tag::Tag::new(store, params))
30            }
31            #[cfg(feature = "v8")]
32            crate::BackendStore::V8(_) => {
33                Self::V8(crate::backend::v8::entities::tag::Tag::new(store, params))
34            }
35            #[cfg(feature = "js")]
36            crate::BackendStore::Js(_) => {
37                Self::Js(crate::backend::js::tag::Tag::new(store, params))
38            }
39        }
40    }
41
42    /// Returns the [`TagType`] of the tag.
43    #[inline]
44    pub fn ty(&self, store: &impl AsStoreRef) -> TagType {
45        match_rt!(on self => f {
46            f.ty(store)
47        })
48    }
49
50    #[inline]
51    pub(crate) fn from_vm_extern(store: &mut impl AsStoreMut, vm_extern: VMExternTag) -> Self {
52        match &store.as_store_mut().inner.store {
53            #[cfg(feature = "sys")]
54            crate::BackendStore::Sys(_) => Self::Sys(
55                crate::backend::sys::tag::Tag::from_vm_extern(store, vm_extern),
56            ),
57            #[cfg(feature = "v8")]
58            crate::BackendStore::V8(_) => Self::V8(
59                crate::backend::v8::entities::tag::Tag::from_vm_extern(store, vm_extern),
60            ),
61            #[cfg(feature = "js")]
62            crate::BackendStore::Js(_) => Self::Js(
63                crate::backend::js::entities::tag::Tag::from_vm_extern(store, vm_extern),
64            ),
65        }
66    }
67
68    /// Checks whether this tag can be used with the given context.
69    #[inline]
70    pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
71        match_rt!(on self => f {
72            f.is_from_store(store)
73        })
74    }
75
76    /// Create a [`VMExtern`] from self.
77    #[inline]
78    pub(crate) fn to_vm_extern(&self) -> VMExtern {
79        match_rt!(on self => f {
80            f.to_vm_extern()
81        })
82    }
83}
84
85impl<'a> Exportable<'a> for BackendTag {
86    fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
87        match _extern {
88            Extern::Tag(func) => Ok(&func.0),
89            _ => Err(ExportError::IncompatibleType),
90        }
91    }
92}