1use crate::{
17 Error, FuncType, GlobalType, HeapType, MemoryType, RefType, SubType, TableType, ValType,
18 WasmFeatures, types::CoreTypeId,
19};
20
21pub trait WasmModuleResources {
30 fn table_at(&self, at: u32) -> Option<TableType>;
34
35 fn memory_at(&self, at: u32) -> Option<MemoryType>;
37
38 fn tag_at(&self, at: u32) -> Option<&FuncType>;
42
43 fn global_at(&self, at: u32) -> Option<GlobalType>;
47
48 fn sub_type_at(&self, type_index: u32) -> Option<&SubType>;
52
53 fn sub_type_at_id(&self, id: CoreTypeId) -> &SubType;
55
56 fn type_id_of_function(&self, func_idx: u32) -> Option<CoreTypeId>;
58
59 fn type_index_of_function(&self, func_index: u32) -> Option<u32>;
61
62 fn element_type_at(&self, at: u32) -> Option<RefType>;
66
67 fn is_subtype(&self, a: ValType, b: ValType) -> bool;
69
70 fn is_shared(&self, ty: RefType) -> bool;
76
77 fn check_value_type(
83 &self,
84 t: &mut ValType,
85 features: &WasmFeatures,
86 offset: u64,
87 ) -> Result<(), Error> {
88 features.check_value_type(*t, offset)?;
89 match t {
90 ValType::Ref(r) => self.check_ref_type(r, offset),
91 ValType::I32 | ValType::I64 | ValType::F32 | ValType::F64 | ValType::V128 => Ok(()),
92 }
93 }
94
95 fn check_ref_type(&self, ref_type: &mut RefType, offset: u64) -> Result<(), Error> {
97 let is_nullable = ref_type.is_nullable();
98 let mut heap_ty = ref_type.heap_type();
99 self.check_heap_type(&mut heap_ty, offset)?;
100 *ref_type = RefType::new(is_nullable, heap_ty).unwrap();
101 Ok(())
102 }
103
104 fn check_heap_type(&self, heap_type: &mut HeapType, offset: u64) -> Result<(), Error>;
109
110 fn top_type(&self, heap_type: &HeapType) -> HeapType;
112
113 fn element_count(&self) -> u32;
115
116 fn data_count(&self) -> Option<u32>;
118
119 fn is_function_referenced(&self, idx: u32) -> bool;
122
123 fn has_function_exact_type(&self, idx: u32) -> bool;
125}
126
127impl<T> WasmModuleResources for &'_ T
128where
129 T: ?Sized + WasmModuleResources,
130{
131 fn table_at(&self, at: u32) -> Option<TableType> {
132 T::table_at(self, at)
133 }
134 fn memory_at(&self, at: u32) -> Option<MemoryType> {
135 T::memory_at(self, at)
136 }
137 fn tag_at(&self, at: u32) -> Option<&FuncType> {
138 T::tag_at(self, at)
139 }
140 fn global_at(&self, at: u32) -> Option<GlobalType> {
141 T::global_at(self, at)
142 }
143 fn sub_type_at(&self, at: u32) -> Option<&SubType> {
144 T::sub_type_at(self, at)
145 }
146 fn sub_type_at_id(&self, at: CoreTypeId) -> &SubType {
147 T::sub_type_at_id(self, at)
148 }
149 fn type_id_of_function(&self, func_idx: u32) -> Option<CoreTypeId> {
150 T::type_id_of_function(self, func_idx)
151 }
152 fn type_index_of_function(&self, func_idx: u32) -> Option<u32> {
153 T::type_index_of_function(self, func_idx)
154 }
155 fn check_heap_type(&self, t: &mut HeapType, offset: u64) -> Result<(), Error> {
156 T::check_heap_type(self, t, offset)
157 }
158 fn top_type(&self, heap_type: &HeapType) -> HeapType {
159 T::top_type(self, heap_type)
160 }
161 fn element_type_at(&self, at: u32) -> Option<RefType> {
162 T::element_type_at(self, at)
163 }
164 fn is_subtype(&self, a: ValType, b: ValType) -> bool {
165 T::is_subtype(self, a, b)
166 }
167 fn is_shared(&self, ty: RefType) -> bool {
168 T::is_shared(self, ty)
169 }
170 fn element_count(&self) -> u32 {
171 T::element_count(self)
172 }
173 fn data_count(&self) -> Option<u32> {
174 T::data_count(self)
175 }
176 fn is_function_referenced(&self, idx: u32) -> bool {
177 T::is_function_referenced(self, idx)
178 }
179 fn has_function_exact_type(&self, idx: u32) -> bool {
180 T::has_function_exact_type(self, idx)
181 }
182}
183
184impl<T> WasmModuleResources for alloc::sync::Arc<T>
185where
186 T: WasmModuleResources,
187{
188 fn table_at(&self, at: u32) -> Option<TableType> {
189 T::table_at(self, at)
190 }
191
192 fn memory_at(&self, at: u32) -> Option<MemoryType> {
193 T::memory_at(self, at)
194 }
195
196 fn tag_at(&self, at: u32) -> Option<&FuncType> {
197 T::tag_at(self, at)
198 }
199
200 fn global_at(&self, at: u32) -> Option<GlobalType> {
201 T::global_at(self, at)
202 }
203
204 fn sub_type_at(&self, type_idx: u32) -> Option<&SubType> {
205 T::sub_type_at(self, type_idx)
206 }
207
208 fn sub_type_at_id(&self, id: CoreTypeId) -> &SubType {
209 T::sub_type_at_id(self, id)
210 }
211
212 fn type_id_of_function(&self, func_idx: u32) -> Option<CoreTypeId> {
213 T::type_id_of_function(self, func_idx)
214 }
215
216 fn type_index_of_function(&self, func_idx: u32) -> Option<u32> {
217 T::type_index_of_function(self, func_idx)
218 }
219
220 fn check_heap_type(&self, t: &mut HeapType, offset: u64) -> Result<(), Error> {
221 T::check_heap_type(self, t, offset)
222 }
223
224 fn top_type(&self, heap_type: &HeapType) -> HeapType {
225 T::top_type(self, heap_type)
226 }
227
228 fn element_type_at(&self, at: u32) -> Option<RefType> {
229 T::element_type_at(self, at)
230 }
231
232 fn is_subtype(&self, a: ValType, b: ValType) -> bool {
233 T::is_subtype(self, a, b)
234 }
235
236 fn is_shared(&self, ty: RefType) -> bool {
237 T::is_shared(self, ty)
238 }
239
240 fn element_count(&self) -> u32 {
241 T::element_count(self)
242 }
243
244 fn data_count(&self) -> Option<u32> {
245 T::data_count(self)
246 }
247
248 fn is_function_referenced(&self, idx: u32) -> bool {
249 T::is_function_referenced(self, idx)
250 }
251
252 fn has_function_exact_type(&self, idx: u32) -> bool {
253 T::has_function_exact_type(self, idx)
254 }
255}