wasmtime/runtime/externals/
tag.rs1use 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#[cfg(feature = "gc")]
13use wasmtime_environ::EntityRef;
14
15#[derive(Copy, Clone, Debug)]
17#[repr(C)] pub struct Tag {
19 instance: StoreInstanceId,
20 index: DefinedTagIndex,
21}
22
23impl Tag {
24 pub(crate) fn from_raw(instance: StoreInstanceId, index: DefinedTagIndex) -> Tag {
25 Tag { instance, index }
26 }
27
28 pub fn new(mut store: impl AsContextMut, ty: &TagType) -> Result<Tag> {
39 generate_tag_export(store.as_context_mut().0, ty)
40 }
41
42 pub fn ty(&self, store: impl AsContext) -> TagType {
48 self._ty(store.as_context().0)
49 }
50
51 pub(crate) fn _ty(&self, store: &StoreOpaque) -> TagType {
52 TagType::from_wasmtime_tag(store.engine(), self.wasmtime_ty(store))
53 }
54
55 pub(crate) fn wasmtime_ty<'a>(&self, store: &'a StoreOpaque) -> &'a wasmtime_environ::Tag {
56 let module = store[self.instance].env_module();
57 let index = module.tag_index(self.index);
58 &module.tags[index]
59 }
60
61 pub(crate) fn vmimport(&self, store: &StoreOpaque) -> crate::runtime::vm::VMTagImport {
62 let instance = &store[self.instance];
63 crate::runtime::vm::VMTagImport {
64 from: instance.tag_ptr(self.index).into(),
65 vmctx: instance.vmctx().into(),
66 index: self.index,
67 }
68 }
69
70 pub(crate) fn comes_from_same_store(&self, store: &StoreOpaque) -> bool {
71 store.id() == self.instance.store_id()
72 }
73
74 #[cfg(feature = "debug")]
79 pub fn debug_index_in_store(&self) -> u64 {
80 u64::from(self.instance.instance().as_u32()) << 32 | u64::from(self.index.as_u32())
81 }
82
83 pub fn eq(a: &Tag, b: &Tag, store: impl AsContext) -> bool {
90 let store = store.as_context();
92 let _ = &store[a.instance];
93 let _ = &store[b.instance];
94
95 a.instance == b.instance && a.index == b.index
97 }
98
99 #[cfg(feature = "gc")]
106 pub(crate) fn to_raw_indices(&self) -> (InstanceId, DefinedTagIndex) {
107 (self.instance.instance(), self.index)
108 }
109
110 #[cfg(feature = "gc")]
117 pub(crate) fn from_raw_indices(
118 store: &StoreOpaque,
119 instance: InstanceId,
120 index: DefinedTagIndex,
121 ) -> Option<Tag> {
122 let num_defined_tags = store
123 .try_instance(instance)?
124 .env_module()
125 .num_defined_tags();
126 if index.index() >= num_defined_tags {
127 return None;
128 }
129
130 let instance = StoreInstanceId::new(store.id(), instance);
131 Some(Tag { instance, index })
132 }
133}