wasmtime/runtime/externals/
tag.rs

1use crate::runtime::types::TagType;
2use crate::{
3    store::{StoreData, StoreOpaque, Stored},
4    AsContext,
5};
6use wasmtime_environ::VMSharedTypeIndex;
7
8/// A WebAssembly `tag`.
9#[derive(Copy, Clone, Debug)]
10#[repr(transparent)] // here for the C API
11pub struct Tag(pub(super) Stored<crate::runtime::vm::ExportTag>);
12
13impl Tag {
14    pub(crate) unsafe fn from_wasmtime_tag(
15        wasmtime_export: crate::runtime::vm::ExportTag,
16        store: &mut StoreOpaque,
17    ) -> Self {
18        debug_assert!(
19            wasmtime_export.tag.signature.unwrap_engine_type_index()
20                != VMSharedTypeIndex::default()
21        );
22        Tag(store.store_data_mut().insert(wasmtime_export))
23    }
24
25    /// Returns the underlying type of this `tag`.
26    ///
27    /// # Panics
28    ///
29    /// Panics if `store` does not own this tag.
30    pub fn ty(&self, store: impl AsContext) -> TagType {
31        self._ty(store.as_context().0)
32    }
33
34    pub(crate) fn _ty(&self, store: &StoreOpaque) -> TagType {
35        let ty = &store[self.0].tag;
36        TagType::from_wasmtime_tag(store.engine(), &ty)
37    }
38
39    pub(crate) fn wasmtime_ty<'a>(&self, data: &'a StoreData) -> &'a wasmtime_environ::Tag {
40        &data[self.0].tag
41    }
42
43    pub(crate) fn vmimport(&self, store: &StoreOpaque) -> crate::runtime::vm::VMTagImport {
44        let export = &store[self.0];
45        crate::runtime::vm::VMTagImport {
46            from: export.definition.into(),
47        }
48    }
49
50    /// Determines whether this tag is reference equal to the other
51    /// given tag in the given store.
52    ///
53    /// # Panics
54    ///
55    /// Panics if either tag do not belong to the given `store`.
56    pub fn eq(a: &Tag, b: &Tag, store: impl AsContext) -> bool {
57        let store = store.as_context().0;
58        let a = &store[a.0];
59        let b = &store[b.0];
60        a.definition.eq(&b.definition)
61    }
62}