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