wasmparser_nostd/readers/core/
types.rs1use crate::limits::{MAX_WASM_FUNCTION_PARAMS, MAX_WASM_FUNCTION_RETURNS};
17use crate::{BinaryReader, FromReader, Result, SectionLimited};
18use ::alloc::boxed::Box;
19use ::alloc::vec::Vec;
20use ::core::fmt::Debug;
21
22#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
24pub enum ValType {
25 I32,
27 I64,
29 F32,
31 F64,
33 V128,
35 FuncRef,
37 ExternRef,
39}
40
41impl ValType {
42 pub fn is_reference_type(&self) -> bool {
47 matches!(self, ValType::FuncRef | ValType::ExternRef)
48 }
49
50 pub(crate) fn from_byte(byte: u8) -> Option<ValType> {
51 match byte {
52 0x7F => Some(ValType::I32),
53 0x7E => Some(ValType::I64),
54 0x7D => Some(ValType::F32),
55 0x7C => Some(ValType::F64),
56 0x7B => Some(ValType::V128),
57 0x70 => Some(ValType::FuncRef),
58 0x6F => Some(ValType::ExternRef),
59 _ => None,
60 }
61 }
62}
63
64impl<'a> FromReader<'a> for ValType {
65 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
66 match ValType::from_byte(reader.peek()?) {
67 Some(ty) => {
68 reader.position += 1;
69 Ok(ty)
70 }
71 None => bail!(reader.original_position(), "invalid value type"),
72 }
73 }
74}
75
76#[derive(Debug, Clone)]
78pub enum Type {
79 Func(FuncType),
81}
82
83#[derive(Clone, Eq, PartialEq, Hash)]
85pub struct FuncType {
86 params_results: Box<[ValType]>,
88 len_params: usize,
90}
91
92impl Debug for FuncType {
93 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
94 f.debug_struct("FuncType")
95 .field("params", &self.params())
96 .field("returns", &self.results())
97 .finish()
98 }
99}
100
101impl FuncType {
102 pub fn new<P, R>(params: P, results: R) -> Self
104 where
105 P: IntoIterator<Item = ValType>,
106 R: IntoIterator<Item = ValType>,
107 {
108 let mut buffer = params.into_iter().collect::<Vec<_>>();
109 let len_params = buffer.len();
110 buffer.extend(results);
111 Self {
112 params_results: buffer.into(),
113 len_params,
114 }
115 }
116
117 pub(crate) fn from_raw_parts(params_results: Box<[ValType]>, len_params: usize) -> Self {
123 assert!(len_params <= params_results.len());
124 Self {
125 params_results,
126 len_params,
127 }
128 }
129
130 #[inline]
132 pub fn params(&self) -> &[ValType] {
133 &self.params_results[..self.len_params]
134 }
135
136 #[inline]
138 pub fn results(&self) -> &[ValType] {
139 &self.params_results[self.len_params..]
140 }
141}
142
143#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
145pub struct TableType {
146 pub element_type: ValType,
148 pub initial: u32,
150 pub maximum: Option<u32>,
152}
153
154#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
156pub struct MemoryType {
157 pub memory64: bool,
162
163 pub shared: bool,
169
170 pub initial: u64,
175
176 pub maximum: Option<u64>,
182}
183
184impl MemoryType {
185 pub fn index_type(&self) -> ValType {
187 if self.memory64 {
188 ValType::I64
189 } else {
190 ValType::I32
191 }
192 }
193}
194
195#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
197pub struct GlobalType {
198 pub content_type: ValType,
200 pub mutable: bool,
202}
203
204#[derive(Clone, Copy, Debug)]
206pub enum TagKind {
207 Exception,
209}
210
211#[derive(Clone, Copy, Debug)]
213pub struct TagType {
214 pub kind: TagKind,
216 pub func_type_idx: u32,
218}
219
220pub type TypeSectionReader<'a> = SectionLimited<'a, Type>;
222
223impl<'a> FromReader<'a> for Type {
224 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
225 Ok(match reader.read_u8()? {
226 0x60 => Type::Func(reader.read()?),
227 x => return reader.invalid_leading_byte(x, "type"),
228 })
229 }
230}
231
232impl<'a> FromReader<'a> for FuncType {
233 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
234 let mut params_results = reader
235 .read_iter(MAX_WASM_FUNCTION_PARAMS, "function params")?
236 .collect::<Result<Vec<_>>>()?;
237 let len_params = params_results.len();
238 let results = reader.read_iter(MAX_WASM_FUNCTION_RETURNS, "function returns")?;
239 params_results.reserve(results.size_hint().0);
240 for result in results {
241 params_results.push(result?);
242 }
243 Ok(FuncType::from_raw_parts(params_results.into(), len_params))
244 }
245}