/rust/registry/src/index.crates.io-6f17d22bba15001f/wasmparser-0.121.2/src/readers/component/imports.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use crate::{ |
2 | | BinaryReader, ComponentExternalKind, ComponentValType, FromReader, Result, SectionLimited, |
3 | | }; |
4 | | |
5 | | /// Represents the type bounds for imports and exports. |
6 | | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
7 | | pub enum TypeBounds { |
8 | | /// The type is bounded by equality. |
9 | | Eq(u32), |
10 | | /// A fresh resource type, |
11 | | SubResource, |
12 | | } |
13 | | |
14 | | impl<'a> FromReader<'a> for TypeBounds { |
15 | 0 | fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> { |
16 | 0 | Ok(match reader.read_u8()? { |
17 | 0 | 0x00 => TypeBounds::Eq(reader.read()?), |
18 | 0 | 0x01 => TypeBounds::SubResource, |
19 | 0 | x => return reader.invalid_leading_byte(x, "type bound"), |
20 | | }) |
21 | 0 | } |
22 | | } |
23 | | |
24 | | /// Represents a reference to a component type. |
25 | | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
26 | | pub enum ComponentTypeRef { |
27 | | /// The reference is to a core module type. |
28 | | /// |
29 | | /// The index is expected to be core type index to a core module type. |
30 | | Module(u32), |
31 | | /// The reference is to a function type. |
32 | | /// |
33 | | /// The index is expected to be a type index to a function type. |
34 | | Func(u32), |
35 | | /// The reference is to a value type. |
36 | | Value(ComponentValType), |
37 | | /// The reference is to a bounded type. |
38 | | /// |
39 | | /// The index is expected to be a type index. |
40 | | Type(TypeBounds), |
41 | | /// The reference is to an instance type. |
42 | | /// |
43 | | /// The index is a type index to an instance type. |
44 | | Instance(u32), |
45 | | /// The reference is to a component type. |
46 | | /// |
47 | | /// The index is a type index to a component type. |
48 | | Component(u32), |
49 | | } |
50 | | |
51 | | impl ComponentTypeRef { |
52 | | /// Returns the corresponding [`ComponentExternalKind`] for this reference. |
53 | 0 | pub fn kind(&self) -> ComponentExternalKind { |
54 | 0 | match self { |
55 | 0 | ComponentTypeRef::Module(_) => ComponentExternalKind::Module, |
56 | 0 | ComponentTypeRef::Func(_) => ComponentExternalKind::Func, |
57 | 0 | ComponentTypeRef::Value(_) => ComponentExternalKind::Value, |
58 | 0 | ComponentTypeRef::Type(..) => ComponentExternalKind::Type, |
59 | 0 | ComponentTypeRef::Instance(_) => ComponentExternalKind::Instance, |
60 | 0 | ComponentTypeRef::Component(_) => ComponentExternalKind::Component, |
61 | | } |
62 | 0 | } Unexecuted instantiation: <wasmparser::readers::component::imports::ComponentTypeRef>::kind Unexecuted instantiation: <wasmparser::readers::component::imports::ComponentTypeRef>::kind |
63 | | } |
64 | | |
65 | | impl<'a> FromReader<'a> for ComponentTypeRef { |
66 | 0 | fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> { |
67 | 0 | Ok(match reader.read()? { |
68 | 0 | ComponentExternalKind::Module => ComponentTypeRef::Module(reader.read()?), |
69 | 0 | ComponentExternalKind::Func => ComponentTypeRef::Func(reader.read()?), |
70 | 0 | ComponentExternalKind::Value => ComponentTypeRef::Value(reader.read()?), |
71 | 0 | ComponentExternalKind::Type => ComponentTypeRef::Type(reader.read()?), |
72 | 0 | ComponentExternalKind::Instance => ComponentTypeRef::Instance(reader.read()?), |
73 | 0 | ComponentExternalKind::Component => ComponentTypeRef::Component(reader.read()?), |
74 | | }) |
75 | 0 | } |
76 | | } |
77 | | |
78 | | /// Represents an import in a WebAssembly component |
79 | | #[derive(Debug, Copy, Clone)] |
80 | | pub struct ComponentImport<'a> { |
81 | | /// The name of the imported item. |
82 | | pub name: ComponentImportName<'a>, |
83 | | /// The type reference for the import. |
84 | | pub ty: ComponentTypeRef, |
85 | | } |
86 | | |
87 | | impl<'a> FromReader<'a> for ComponentImport<'a> { |
88 | 0 | fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> { |
89 | 0 | Ok(ComponentImport { |
90 | 0 | name: reader.read()?, |
91 | 0 | ty: reader.read()?, |
92 | | }) |
93 | 0 | } |
94 | | } |
95 | | |
96 | | /// A reader for the import section of a WebAssembly component. |
97 | | /// |
98 | | /// # Examples |
99 | | /// |
100 | | /// ``` |
101 | | /// use wasmparser::ComponentImportSectionReader; |
102 | | /// let data: &[u8] = &[0x01, 0x00, 0x01, 0x41, 0x01, 0x66]; |
103 | | /// let reader = ComponentImportSectionReader::new(data, 0).unwrap(); |
104 | | /// for import in reader { |
105 | | /// let import = import.expect("import"); |
106 | | /// println!("Import: {:?}", import); |
107 | | /// } |
108 | | /// ``` |
109 | | pub type ComponentImportSectionReader<'a> = SectionLimited<'a, ComponentImport<'a>>; |
110 | | |
111 | | /// Represents the name of a component import. |
112 | | #[derive(Debug, Copy, Clone)] |
113 | | #[allow(missing_docs)] |
114 | | pub struct ComponentImportName<'a>(pub &'a str); |
115 | | |
116 | | impl<'a> FromReader<'a> for ComponentImportName<'a> { |
117 | 0 | fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> { |
118 | 0 | match reader.read_u8()? { |
119 | 0 | 0x00 => {} |
120 | | // Historically export names used a discriminator byte of 0x01 to |
121 | | // indicate an "interface" of the form `a:b/c` but nowadays that's |
122 | | // inferred from string syntax. Ignore 0-vs-1 to continue to parse |
123 | | // older binaries. Eventually this will go away. |
124 | 0 | 0x01 => {} |
125 | 0 | x => return reader.invalid_leading_byte(x, "import name"), |
126 | | } |
127 | 0 | Ok(ComponentImportName(reader.read_string()?)) |
128 | 0 | } |
129 | | } |