wasm/validation/modules/
imports.rs1use crate::{
2 core::{
3 decoding::reader::WasmDecoder,
4 structure::modules::{
5 imports::{Import, ImportDesc},
6 indices::{IdxVec, TypeIdx},
7 },
8 },
9 DecodingError, ExternType, FuncType, GlobalType, MemType, Module, TableType, ValidationError,
10};
11
12impl<'wasm> Import<'wasm> {
13 pub fn decode_and_validate(
14 wasm: &mut WasmDecoder<'wasm>,
15 c_types: &IdxVec<TypeIdx, FuncType>,
16 ) -> Result<Self, ValidationError> {
17 let module_name = wasm.decode_name()?;
18 let name = wasm.decode_name()?;
19 let desc = ImportDesc::decode_and_validate(wasm, c_types)?;
20
21 Ok(Self {
22 module_name,
23 name,
24 desc,
25 })
26 }
27}
28
29impl ImportDesc {
30 pub fn decode_and_validate(
31 wasm: &mut WasmDecoder,
32 c_types: &IdxVec<TypeIdx, FuncType>,
33 ) -> Result<Self, ValidationError> {
34 let desc = match wasm.decode_u8()? {
35 0x00 => Self::Func(TypeIdx::decode_and_validate(wasm, c_types)?),
36 0x01 => Self::Table(TableType::decode_and_validate(wasm)?),
38 0x02 => Self::Mem(MemType::decode_and_validate(wasm)?),
39 0x03 => Self::Global(GlobalType::decode(wasm)?),
40 other => return Err(DecodingError::MalformedImportDescDiscriminator(other).into()),
41 };
42
43 Ok(desc)
44 }
45
46 pub unsafe fn extern_type(&self, module: &Module) -> ExternType {
54 match self {
55 ImportDesc::Func(type_idx) => {
56 let func_type = unsafe { module.types.get(*type_idx) };
64 ExternType::Func(func_type.clone())
66 }
67 ImportDesc::Table(ty) => ExternType::Table(*ty),
68 ImportDesc::Mem(ty) => ExternType::Mem(*ty),
69 ImportDesc::Global(ty) => ExternType::Global(*ty),
70 }
71 }
72}