1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
use crate::limits::{MAX_WASM_FUNCTION_PARAMS, MAX_WASM_FUNCTION_RETURNS};
use crate::{BinaryReader, FromReader, Result, SectionLimited};
use std::fmt::Debug;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum ValType {
I32,
I64,
F32,
F64,
V128,
FuncRef,
ExternRef,
}
impl ValType {
pub fn is_reference_type(&self) -> bool {
matches!(self, ValType::FuncRef | ValType::ExternRef)
}
pub(crate) fn from_byte(byte: u8) -> Option<ValType> {
match byte {
0x7F => Some(ValType::I32),
0x7E => Some(ValType::I64),
0x7D => Some(ValType::F32),
0x7C => Some(ValType::F64),
0x7B => Some(ValType::V128),
0x70 => Some(ValType::FuncRef),
0x6F => Some(ValType::ExternRef),
_ => None,
}
}
}
impl<'a> FromReader<'a> for ValType {
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
match ValType::from_byte(reader.peek()?) {
Some(ty) => {
reader.position += 1;
Ok(ty)
}
None => bail!(reader.original_position(), "invalid value type"),
}
}
}
#[derive(Debug, Clone)]
pub enum Type {
Func(FuncType),
}
#[derive(Clone, Eq, PartialEq, Hash)]
pub struct FuncType {
params_results: Box<[ValType]>,
len_params: usize,
}
impl Debug for FuncType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FuncType")
.field("params", &self.params())
.field("returns", &self.results())
.finish()
}
}
impl FuncType {
pub fn new<P, R>(params: P, results: R) -> Self
where
P: IntoIterator<Item = ValType>,
R: IntoIterator<Item = ValType>,
{
let mut buffer = params.into_iter().collect::<Vec<_>>();
let len_params = buffer.len();
buffer.extend(results);
Self {
params_results: buffer.into(),
len_params,
}
}
pub(crate) fn from_raw_parts(params_results: Box<[ValType]>, len_params: usize) -> Self {
assert!(len_params <= params_results.len());
Self {
params_results,
len_params,
}
}
#[inline]
pub fn params(&self) -> &[ValType] {
&self.params_results[..self.len_params]
}
#[inline]
pub fn results(&self) -> &[ValType] {
&self.params_results[self.len_params..]
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct TableType {
pub element_type: ValType,
pub initial: u32,
pub maximum: Option<u32>,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct MemoryType {
pub memory64: bool,
pub shared: bool,
pub initial: u64,
pub maximum: Option<u64>,
}
impl MemoryType {
pub fn index_type(&self) -> ValType {
if self.memory64 {
ValType::I64
} else {
ValType::I32
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct GlobalType {
pub content_type: ValType,
pub mutable: bool,
}
#[derive(Clone, Copy, Debug)]
pub enum TagKind {
Exception,
}
#[derive(Clone, Copy, Debug)]
pub struct TagType {
pub kind: TagKind,
pub func_type_idx: u32,
}
pub type TypeSectionReader<'a> = SectionLimited<'a, Type>;
impl<'a> FromReader<'a> for Type {
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
Ok(match reader.read_u8()? {
0x60 => Type::Func(reader.read()?),
x => return reader.invalid_leading_byte(x, "type"),
})
}
}
impl<'a> FromReader<'a> for FuncType {
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
let mut params_results = reader
.read_iter(MAX_WASM_FUNCTION_PARAMS, "function params")?
.collect::<Result<Vec<_>>>()?;
let len_params = params_results.len();
let results = reader.read_iter(MAX_WASM_FUNCTION_RETURNS, "function returns")?;
params_results.reserve(results.size_hint().0);
for result in results {
params_results.push(result?);
}
Ok(FuncType::from_raw_parts(params_results.into(), len_params))
}
}