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    ///
28    /// # Errors
29    ///
30    /// This function will return an [`OutOfMemory`][crate::OutOfMemory] error when
31    /// memory allocation fails. See the `OutOfMemory` type's documentation for
32    /// details on Wasmtime's out-of-memory handling.
33    pub fn new(mut store: impl AsContextMut, ty: &TagType) -> Result<Tag> {
34        generate_tag_export(store.as_context_mut().0, ty)
35    }
36
37    /// Returns the underlying type of this `tag`.
38    ///
39    /// # Panics
40    ///
41    /// Panics if `store` does not own this tag.
42    pub fn ty(&self, store: impl AsContext) -> TagType {
43        self._ty(store.as_context().0)
44    }
45
46    pub(crate) fn _ty(&self, store: &StoreOpaque) -> TagType {
47        TagType::from_wasmtime_tag(store.engine(), self.wasmtime_ty(store))
48    }
49
50    pub(crate) fn wasmtime_ty<'a>(&self, store: &'a StoreOpaque) -> &'a wasmtime_environ::Tag {
51        let module = store[self.instance].env_module();
52        let index = module.tag_index(self.index);
53        &module.tags[index]
54    }
55
56    pub(crate) fn vmimport(&self, store: &StoreOpaque) -> crate::runtime::vm::VMTagImport {
57        let instance = &store[self.instance];
58        crate::runtime::vm::VMTagImport {
59            from: instance.tag_ptr(self.index).into(),
60            vmctx: instance.vmctx().into(),
61            index: self.index,
62        }
63    }
64
65    pub(crate) fn comes_from_same_store(&self, store: &StoreOpaque) -> bool {
66        store.id() == self.instance.store_id()
67    }
68
69    /// Returns a stable identifier for this tag within its store.
70    ///
71    /// This allows distinguishing tags when introspecting them
72    /// e.g. via debug APIs.
73    #[cfg(feature = "debug")]
74    pub fn debug_index_in_store(&self) -> u64 {
75        u64::from(self.instance.instance().as_u32()) << 32 | u64::from(self.index.as_u32())
76    }
77
78    /// Determines whether this tag is reference equal to the other
79    /// given tag in the given store.
80    ///
81    /// # Panics
82    ///
83    /// Panics if either tag do not belong to the given `store`.
84    pub fn eq(a: &Tag, b: &Tag, store: impl AsContext) -> bool {
85        // make sure both tags belong to the store
86        let store = store.as_context();
87        let _ = &store[a.instance];
88        let _ = &store[b.instance];
89
90        // then compare to see if they have the same definition
91        a.instance == b.instance && a.index == b.index
92    }
93
94    /// Get the "index coordinates" for this `Tag`: the raw instance
95    /// ID and defined-tag index within that instance. This can be
96    /// used to "serialize" the tag as safe (tamper-proof,
97    /// bounds-checked) values, e.g. within the GC store for an
98    /// exception object.
99    #[cfg(feature = "gc")]
100    pub(crate) fn to_raw_indices(&self) -> (InstanceId, DefinedTagIndex) {
101        (self.instance.instance(), self.index)
102    }
103
104    /// Create a new `Tag` from known raw indices as produced by
105    /// `to_raw_indices()`.
106    ///
107    /// # Panics
108    ///
109    /// Panics if the indices are out-of-bounds in the given store.
110    #[cfg(feature = "gc")]
111    pub(crate) fn from_raw_indices(
112        store: &StoreOpaque,
113        instance: InstanceId,
114        index: DefinedTagIndex,
115    ) -> Tag {
116        let instance = StoreInstanceId::new(store.id(), instance);
117        Tag { instance, index }
118    }
119}