wasmer/entities/tag/
mod.rs

1pub(crate) mod inner;
2pub(crate) use inner::*;
3use wasmer_types::{TagType, Type};
4
5use crate::{
6    AsStoreMut, AsStoreRef, ExportError, Exportable, Extern,
7    vm::{VMExtern, VMExternTag},
8};
9
10/// A WebAssembly `tag` instance.
11///
12/// A tag instance is the runtime representation of a tag variable.
13///
14/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#tag-instances>
15#[derive(Debug, Clone, PartialEq, Eq, derive_more::From)]
16#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
17pub struct Tag(pub(crate) BackendTag);
18
19impl Tag {
20    /// Create a new tag with event of type P -> [], that is a function that takes parameters `P`
21    /// and has no return value.
22    //
23    // Note: in the future, a tag might express other kinds of events other than just exceptions.
24    // This would imply that the signature of this function becomes
25    //
26    // `pub fn new<R: Into<Box<[Type]>>, P: Into<Box<[Type]>>>(store: &mut impl AsStoreMut, kind: TagKind, params: P, rets: R) -> Self`
27    //
28    // For now, since the only possible kind is `TagKind::Exception`, we decided to make the
29    // external API easier to use, while having the internal types in place to allow the needed
30    // changes.
31    pub fn new<P: Into<Box<[Type]>>>(store: &mut impl AsStoreMut, params: P) -> Self {
32        Self(BackendTag::new(store, params))
33    }
34
35    /// Returns the [`TagType`] of the tag.
36    pub fn ty(&self, store: &impl AsStoreRef) -> TagType {
37        self.0.ty(store)
38    }
39
40    pub(crate) fn from_vm_extern(store: &mut impl AsStoreMut, vm_extern: VMExternTag) -> Self {
41        Self(BackendTag::from_vm_extern(store, vm_extern))
42    }
43
44    /// Checks whether this tag can be used with the given context.
45    pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
46        self.0.is_from_store(store)
47    }
48
49    /// Create a [`VMExtern`] from self.
50    pub(crate) fn to_vm_extern(&self) -> VMExtern {
51        self.0.to_vm_extern()
52    }
53}
54
55impl<'a> Exportable<'a> for Tag {
56    fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
57        match _extern {
58            Extern::Tag(tag) => Ok(tag),
59            _ => Err(ExportError::IncompatibleType),
60        }
61    }
62}