Skip to main content

wasmtime/runtime/externals/
tag.rs

1use crate::Result;
2use crate::runtime::types::TagType;
3use crate::trampoline::generate_tag_export;
4use crate::{
5    AsContext, AsContextMut,
6    store::{StoreInstanceId, StoreOpaque},
7};
8use wasmtime_environ::DefinedTagIndex;
9
10#[cfg(feature = "gc")]
11use crate::store::InstanceId;
12
13/// A WebAssembly `tag`.
14#[derive(Copy, Clone, Debug)]
15#[repr(C)] // here for the C API in the future
16pub struct Tag {
17    instance: StoreInstanceId,
18    index: DefinedTagIndex,
19}
20
21impl Tag {
22    pub(crate) fn from_raw(instance: StoreInstanceId, index: DefinedTagIndex) -> Tag {
23        Tag { instance, index }
24    }
25
26    /// Create a new tag instance from a given TagType.
27    pub fn new(mut store: impl AsContextMut, ty: &TagType) -> Result<Tag> {
28        generate_tag_export(store.as_context_mut().0, ty)
29    }
30
31    /// Returns the underlying type of this `tag`.
32    ///
33    /// # Panics
34    ///
35    /// Panics if `store` does not own this tag.
36    pub fn ty(&self, store: impl AsContext) -> TagType {
37        self._ty(store.as_context().0)
38    }
39
40    pub(crate) fn _ty(&self, store: &StoreOpaque) -> TagType {
41        TagType::from_wasmtime_tag(store.engine(), self.wasmtime_ty(store))
42    }
43
44    pub(crate) fn wasmtime_ty<'a>(&self, store: &'a StoreOpaque) -> &'a wasmtime_environ::Tag {
45        let module = store[self.instance].env_module();
46        let index = module.tag_index(self.index);
47        &module.tags[index]
48    }
49
50    pub(crate) fn vmimport(&self, store: &StoreOpaque) -> crate::runtime::vm::VMTagImport {
51        let instance = &store[self.instance];
52        crate::runtime::vm::VMTagImport {
53            from: instance.tag_ptr(self.index).into(),
54            vmctx: instance.vmctx().into(),
55            index: self.index,
56        }
57    }
58
59    pub(crate) fn comes_from_same_store(&self, store: &StoreOpaque) -> bool {
60        store.id() == self.instance.store_id()
61    }
62
63    /// Determines whether this tag is reference equal to the other
64    /// given tag in the given store.
65    ///
66    /// # Panics
67    ///
68    /// Panics if either tag do not belong to the given `store`.
69    pub fn eq(a: &Tag, b: &Tag, store: impl AsContext) -> bool {
70        // make sure both tags belong to the store
71        let store = store.as_context();
72        let _ = &store[a.instance];
73        let _ = &store[b.instance];
74
75        // then compare to see if they have the same definition
76        a.instance == b.instance && a.index == b.index
77    }
78
79    /// Get the "index coordinates" for this `Tag`: the raw instance
80    /// ID and defined-tag index within that instance. This can be
81    /// used to "serialize" the tag as safe (tamper-proof,
82    /// bounds-checked) values, e.g. within the GC store for an
83    /// exception object.
84    #[cfg(feature = "gc")]
85    pub(crate) fn to_raw_indices(&self) -> (InstanceId, DefinedTagIndex) {
86        (self.instance.instance(), self.index)
87    }
88
89    /// Create a new `Tag` from known raw indices as produced by
90    /// `to_raw_indices()`.
91    ///
92    /// # Panics
93    ///
94    /// Panics if the indices are out-of-bounds in the given store.
95    #[cfg(feature = "gc")]
96    pub(crate) fn from_raw_indices(
97        store: &StoreOpaque,
98        instance: InstanceId,
99        index: DefinedTagIndex,
100    ) -> Tag {
101        let instance = StoreInstanceId::new(store.id(), instance);
102        Tag { instance, index }
103    }
104}