/build/source/src/core/reader/types/import.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use alloc::borrow::ToOwned; |
2 | | use alloc::string::String; |
3 | | |
4 | | use crate::core::indices::TypeIdx; |
5 | | use crate::core::reader::{WasmReadable, WasmReader}; |
6 | | use crate::execution::assert_validated::UnwrapValidatedExt; |
7 | | use crate::{unreachable_validated, Error, Result}; |
8 | | |
9 | | #[derive(Debug)] |
10 | | pub struct Import { |
11 | | #[allow(warnings)] |
12 | | pub module_name: String, |
13 | | #[allow(warnings)] |
14 | | pub name: String, |
15 | | #[allow(warnings)] |
16 | | pub desc: ImportDesc, |
17 | | } |
18 | | |
19 | | impl WasmReadable for Import { |
20 | 0 | fn read(wasm: &mut WasmReader) -> Result<Self> { |
21 | 0 | let module_name = wasm.read_name()?.to_owned(); |
22 | 0 | let name = wasm.read_name()?.to_owned(); |
23 | 0 | let desc = ImportDesc::read(wasm)?; |
24 | | |
25 | 0 | Ok(Self { |
26 | 0 | module_name, |
27 | 0 | name, |
28 | 0 | desc, |
29 | 0 | }) |
30 | 0 | } |
31 | | |
32 | 0 | fn read_unvalidated(wasm: &mut WasmReader) -> Self { |
33 | 0 | let module_name = wasm.read_name().unwrap_validated().to_owned(); |
34 | 0 | let name = wasm.read_name().unwrap_validated().to_owned(); |
35 | 0 | let desc = ImportDesc::read_unvalidated(wasm); |
36 | 0 |
|
37 | 0 | Self { |
38 | 0 | module_name, |
39 | 0 | name, |
40 | 0 | desc, |
41 | 0 | } |
42 | 0 | } |
43 | | } |
44 | | |
45 | | #[derive(Debug)] |
46 | | pub enum ImportDesc { |
47 | | #[allow(dead_code)] |
48 | | Func(TypeIdx), |
49 | | #[allow(dead_code)] |
50 | | Table(()), |
51 | | // TODO TableType |
52 | | #[allow(dead_code)] |
53 | | Mem(()), |
54 | | // TODO MemType |
55 | | #[allow(dead_code)] |
56 | | Global(()), // TODO GlobalType |
57 | | } |
58 | | |
59 | | impl WasmReadable for ImportDesc { |
60 | 0 | fn read(wasm: &mut WasmReader) -> Result<Self> { |
61 | 0 | let desc = match wasm.read_u8()? { |
62 | 0 | 0x00 => Self::Func(wasm.read_var_u32()? as TypeIdx), |
63 | 0 | 0x01 => todo!("read TableType"), |
64 | 0 | 0x02 => todo!("read MemType"), |
65 | 0 | 0x03 => todo!("read GlobalType"), |
66 | 0 | other => return Err(Error::InvalidImportDesc(other)), |
67 | | }; |
68 | | |
69 | 0 | Ok(desc) |
70 | 0 | } |
71 | | |
72 | 0 | fn read_unvalidated(wasm: &mut WasmReader) -> Self { |
73 | 0 | match wasm.read_u8().unwrap_validated() { |
74 | 0 | 0x00 => Self::Func(wasm.read_var_u32().unwrap_validated() as TypeIdx), |
75 | 0 | 0x01 => todo!("read TableType"), |
76 | 0 | 0x02 => todo!("read MemType"), |
77 | 0 | 0x03 => todo!("read GlobalType"), |
78 | 0 | _ => unreachable_validated!(), |
79 | | } |
80 | 0 | } |
81 | | } |