@@ -11,19 +11,13 @@ use crate::{module::CodeSection, Result};
11
11
pub ( crate ) fn convert_module_elements < ' a , T : IntoIterator < Item = wasmparser:: Result < wasmparser:: Element < ' a > > > > (
12
12
elements : T ,
13
13
) -> Result < Vec < tinywasm_types:: Element > > {
14
- let elements = elements
15
- . into_iter ( )
16
- . map ( |element| convert_module_element ( element?) )
17
- . collect :: < Result < Vec < _ > > > ( ) ?;
14
+ let elements = elements. into_iter ( ) . map ( |element| convert_module_element ( element?) ) . collect :: < Result < Vec < _ > > > ( ) ?;
18
15
Ok ( elements)
19
16
}
20
17
21
18
pub ( crate ) fn convert_module_element ( element : wasmparser:: Element < ' _ > ) -> Result < tinywasm_types:: Element > {
22
19
let kind = match element. kind {
23
- wasmparser:: ElementKind :: Active {
24
- table_index,
25
- offset_expr,
26
- } => tinywasm_types:: ElementKind :: Active {
20
+ wasmparser:: ElementKind :: Active { table_index, offset_expr } => tinywasm_types:: ElementKind :: Active {
27
21
table : table_index,
28
22
offset : process_const_operators ( offset_expr. get_operators_reader ( ) ) ?,
29
23
} ,
@@ -32,38 +26,24 @@ pub(crate) fn convert_module_element(element: wasmparser::Element<'_>) -> Result
32
26
} ;
33
27
34
28
let items = match element. items {
35
- wasmparser:: ElementItems :: Functions ( funcs) => funcs
36
- . into_iter ( )
37
- . map ( |func| Ok ( ElementItem :: Func ( func?) ) )
38
- . collect :: < Result < Vec < _ > > > ( ) ?
39
- . into_boxed_slice ( ) ,
29
+ wasmparser:: ElementItems :: Functions ( funcs) => {
30
+ funcs. into_iter ( ) . map ( |func| Ok ( ElementItem :: Func ( func?) ) ) . collect :: < Result < Vec < _ > > > ( ) ?. into_boxed_slice ( )
31
+ }
40
32
41
33
wasmparser:: ElementItems :: Expressions ( exprs) => exprs
42
34
. into_iter ( )
43
- . map ( |expr| {
44
- Ok ( ElementItem :: Expr ( process_const_operators (
45
- expr?. get_operators_reader ( ) ,
46
- ) ?) )
47
- } )
35
+ . map ( |expr| Ok ( ElementItem :: Expr ( process_const_operators ( expr?. get_operators_reader ( ) ) ?) ) )
48
36
. collect :: < Result < Vec < _ > > > ( ) ?
49
37
. into_boxed_slice ( ) ,
50
38
} ;
51
39
52
- Ok ( tinywasm_types:: Element {
53
- kind,
54
- items,
55
- ty : convert_valtype ( & element. ty ) ,
56
- range : element. range ,
57
- } )
40
+ Ok ( tinywasm_types:: Element { kind, items, ty : convert_valtype ( & element. ty ) , range : element. range } )
58
41
}
59
42
60
43
pub ( crate ) fn convert_module_data_sections < ' a , T : IntoIterator < Item = wasmparser:: Result < wasmparser:: Data < ' a > > > > (
61
44
data_sections : T ,
62
45
) -> Result < Vec < tinywasm_types:: Data > > {
63
- let data_sections = data_sections
64
- . into_iter ( )
65
- . map ( |data| convert_module_data ( data?) )
66
- . collect :: < Result < Vec < _ > > > ( ) ?;
46
+ let data_sections = data_sections. into_iter ( ) . map ( |data| convert_module_data ( data?) ) . collect :: < Result < Vec < _ > > > ( ) ?;
67
47
Ok ( data_sections)
68
48
}
69
49
@@ -72,15 +52,9 @@ pub(crate) fn convert_module_data(data: wasmparser::Data<'_>) -> Result<tinywasm
72
52
data : data. data . to_vec ( ) . into_boxed_slice ( ) ,
73
53
range : data. range ,
74
54
kind : match data. kind {
75
- wasmparser:: DataKind :: Active {
76
- memory_index,
77
- offset_expr,
78
- } => {
55
+ wasmparser:: DataKind :: Active { memory_index, offset_expr } => {
79
56
let offset = process_const_operators ( offset_expr. get_operators_reader ( ) ) ?;
80
- tinywasm_types:: DataKind :: Active {
81
- mem : memory_index,
82
- offset,
83
- }
57
+ tinywasm_types:: DataKind :: Active { mem : memory_index, offset }
84
58
}
85
59
wasmparser:: DataKind :: Passive => tinywasm_types:: DataKind :: Passive ,
86
60
} ,
@@ -90,10 +64,7 @@ pub(crate) fn convert_module_data(data: wasmparser::Data<'_>) -> Result<tinywasm
90
64
pub ( crate ) fn convert_module_imports < ' a , T : IntoIterator < Item = wasmparser:: Result < wasmparser:: Import < ' a > > > > (
91
65
imports : T ,
92
66
) -> Result < Vec < Import > > {
93
- let imports = imports
94
- . into_iter ( )
95
- . map ( |import| convert_module_import ( import?) )
96
- . collect :: < Result < Vec < _ > > > ( ) ?;
67
+ let imports = imports. into_iter ( ) . map ( |import| convert_module_import ( import?) ) . collect :: < Result < Vec < _ > > > ( ) ?;
97
68
Ok ( imports)
98
69
}
99
70
@@ -105,15 +76,11 @@ pub(crate) fn convert_module_import(import: wasmparser::Import<'_>) -> Result<Im
105
76
wasmparser:: TypeRef :: Func ( ty) => ImportKind :: Func ( ty) ,
106
77
wasmparser:: TypeRef :: Table ( ty) => ImportKind :: Table ( convert_module_table ( ty) ?) ,
107
78
wasmparser:: TypeRef :: Memory ( ty) => ImportKind :: Mem ( convert_module_memory ( ty) ?) ,
108
- wasmparser:: TypeRef :: Global ( ty) => ImportKind :: Global ( GlobalType {
109
- mutable : ty. mutable ,
110
- ty : convert_valtype ( & ty. content_type ) ,
111
- } ) ,
79
+ wasmparser:: TypeRef :: Global ( ty) => {
80
+ ImportKind :: Global ( GlobalType { mutable : ty. mutable , ty : convert_valtype ( & ty. content_type ) } )
81
+ }
112
82
wasmparser:: TypeRef :: Tag ( ty) => {
113
- return Err ( crate :: ParseError :: UnsupportedOperator ( format ! (
114
- "Unsupported import kind: {:?}" ,
115
- ty
116
- ) ) )
83
+ return Err ( crate :: ParseError :: UnsupportedOperator ( format ! ( "Unsupported import kind: {:?}" , ty) ) )
117
84
}
118
85
} ,
119
86
} )
@@ -122,10 +89,8 @@ pub(crate) fn convert_module_import(import: wasmparser::Import<'_>) -> Result<Im
122
89
pub ( crate ) fn convert_module_memories < T : IntoIterator < Item = wasmparser:: Result < wasmparser:: MemoryType > > > (
123
90
memory_types : T ,
124
91
) -> Result < Vec < MemoryType > > {
125
- let memory_type = memory_types
126
- . into_iter ( )
127
- . map ( |memory| convert_module_memory ( memory?) )
128
- . collect :: < Result < Vec < _ > > > ( ) ?;
92
+ let memory_type =
93
+ memory_types. into_iter ( ) . map ( |memory| convert_module_memory ( memory?) ) . collect :: < Result < Vec < _ > > > ( ) ?;
129
94
130
95
Ok ( memory_type)
131
96
}
@@ -144,21 +109,14 @@ pub(crate) fn convert_module_memory(memory: wasmparser::MemoryType) -> Result<Me
144
109
pub ( crate ) fn convert_module_tables < T : IntoIterator < Item = wasmparser:: Result < wasmparser:: TableType > > > (
145
110
table_types : T ,
146
111
) -> Result < Vec < TableType > > {
147
- let table_type = table_types
148
- . into_iter ( )
149
- . map ( |table| convert_module_table ( table?) )
150
- . collect :: < Result < Vec < _ > > > ( ) ?;
112
+ let table_type = table_types. into_iter ( ) . map ( |table| convert_module_table ( table?) ) . collect :: < Result < Vec < _ > > > ( ) ?;
151
113
152
114
Ok ( table_type)
153
115
}
154
116
155
117
pub ( crate ) fn convert_module_table ( table : wasmparser:: TableType ) -> Result < TableType > {
156
118
let ty = convert_valtype ( & table. element_type ) ;
157
- Ok ( TableType {
158
- element_type : ty,
159
- size_initial : table. initial ,
160
- size_max : table. maximum ,
161
- } )
119
+ Ok ( TableType { element_type : ty, size_initial : table. initial , size_max : table. maximum } )
162
120
}
163
121
164
122
pub ( crate ) fn convert_module_globals < ' a , T : IntoIterator < Item = wasmparser:: Result < wasmparser:: Global < ' a > > > > (
@@ -171,13 +129,7 @@ pub(crate) fn convert_module_globals<'a, T: IntoIterator<Item = wasmparser::Resu
171
129
let ty = convert_valtype ( & global. ty . content_type ) ;
172
130
let ops = global. init_expr . get_operators_reader ( ) ;
173
131
174
- Ok ( Global {
175
- init : process_const_operators ( ops) ?,
176
- ty : GlobalType {
177
- mutable : global. ty . mutable ,
178
- ty,
179
- } ,
180
- } )
132
+ Ok ( Global { init : process_const_operators ( ops) ?, ty : GlobalType { mutable : global. ty . mutable , ty } } )
181
133
} )
182
134
. collect :: < Result < Vec < _ > > > ( ) ?;
183
135
Ok ( globals)
@@ -190,18 +142,11 @@ pub(crate) fn convert_module_export(export: wasmparser::Export) -> Result<Export
190
142
wasmparser:: ExternalKind :: Memory => ExternalKind :: Memory ,
191
143
wasmparser:: ExternalKind :: Global => ExternalKind :: Global ,
192
144
wasmparser:: ExternalKind :: Tag => {
193
- return Err ( crate :: ParseError :: UnsupportedOperator ( format ! (
194
- "Unsupported export kind: {:?}" ,
195
- export. kind
196
- ) ) )
145
+ return Err ( crate :: ParseError :: UnsupportedOperator ( format ! ( "Unsupported export kind: {:?}" , export. kind) ) )
197
146
}
198
147
} ;
199
148
200
- Ok ( Export {
201
- index : export. index ,
202
- name : Box :: from ( export. name ) ,
203
- kind,
204
- } )
149
+ Ok ( Export { index : export. index , name : Box :: from ( export. name ) , kind } )
205
150
}
206
151
207
152
pub ( crate ) fn convert_module_code (
@@ -224,27 +169,16 @@ pub(crate) fn convert_module_code(
224
169
let body_reader = func. get_operators_reader ( ) ?;
225
170
let body = process_operators ( body_reader. original_position ( ) , body_reader. into_iter ( ) , validator) ?;
226
171
227
- Ok ( CodeSection {
228
- locals : locals. into_boxed_slice ( ) ,
229
- body,
230
- } )
172
+ Ok ( CodeSection { locals : locals. into_boxed_slice ( ) , body } )
231
173
}
232
174
233
175
pub ( crate ) fn convert_module_type ( ty : wasmparser:: Type ) -> Result < FuncType > {
234
176
let wasmparser:: Type :: Func ( ty) = ty;
235
- let params = ty
236
- . params ( )
237
- . iter ( )
238
- . map ( |p| Ok ( convert_valtype ( p) ) )
239
- . collect :: < Result < Vec < ValType > > > ( ) ?
240
- . into_boxed_slice ( ) ;
241
-
242
- let results = ty
243
- . results ( )
244
- . iter ( )
245
- . map ( |p| Ok ( convert_valtype ( p) ) )
246
- . collect :: < Result < Vec < ValType > > > ( ) ?
247
- . into_boxed_slice ( ) ;
177
+ let params =
178
+ ty. params ( ) . iter ( ) . map ( |p| Ok ( convert_valtype ( p) ) ) . collect :: < Result < Vec < ValType > > > ( ) ?. into_boxed_slice ( ) ;
179
+
180
+ let results =
181
+ ty. results ( ) . iter ( ) . map ( |p| Ok ( convert_valtype ( p) ) ) . collect :: < Result < Vec < ValType > > > ( ) ?. into_boxed_slice ( ) ;
248
182
249
183
Ok ( FuncType { params, results } )
250
184
}
@@ -270,19 +204,14 @@ pub(crate) fn convert_valtype(valtype: &wasmparser::ValType) -> ValType {
270
204
I64 => ValType :: I64 ,
271
205
F32 => ValType :: F32 ,
272
206
F64 => ValType :: F64 ,
273
- V128 => ValType :: V128 ,
207
+ V128 => unimplemented ! ( "128-bit values are not supported yet" ) ,
274
208
FuncRef => ValType :: FuncRef ,
275
209
ExternRef => ValType :: ExternRef ,
276
210
}
277
211
}
278
212
279
213
pub ( crate ) fn convert_memarg ( memarg : wasmparser:: MemArg ) -> MemArg {
280
- MemArg {
281
- offset : memarg. offset ,
282
- align : memarg. align ,
283
- align_max : memarg. max_align ,
284
- mem_addr : memarg. memory ,
285
- }
214
+ MemArg { offset : memarg. offset , align : memarg. align , align_max : memarg. max_align , mem_addr : memarg. memory }
286
215
}
287
216
288
217
pub ( crate ) fn process_const_operators ( ops : OperatorsReader ) -> Result < ConstInstruction > {
@@ -306,10 +235,7 @@ pub fn process_const_operator(op: wasmparser::Operator) -> Result<ConstInstructi
306
235
wasmparser:: Operator :: F32Const { value } => Ok ( ConstInstruction :: F32Const ( f32:: from_bits ( value. bits ( ) ) ) ) , // TODO: check if this is correct
307
236
wasmparser:: Operator :: F64Const { value } => Ok ( ConstInstruction :: F64Const ( f64:: from_bits ( value. bits ( ) ) ) ) , // TODO: check if this is correct
308
237
wasmparser:: Operator :: GlobalGet { global_index } => Ok ( ConstInstruction :: GlobalGet ( global_index) ) ,
309
- op => Err ( crate :: ParseError :: UnsupportedOperator ( format ! (
310
- "Unsupported const instruction: {:?}" ,
311
- op
312
- ) ) ) ,
238
+ op => Err ( crate :: ParseError :: UnsupportedOperator ( format ! ( "Unsupported const instruction: {:?}" , op) ) ) ,
313
239
}
314
240
}
315
241
@@ -332,9 +258,7 @@ pub fn process_operators<'a>(
332
258
let res = match op {
333
259
BrTable { targets } => {
334
260
let def = targets. default ( ) ;
335
- let targets = targets
336
- . targets ( )
337
- . collect :: < Result < Vec < u32 > , wasmparser:: BinaryReaderError > > ( ) ?;
261
+ let targets = targets. targets ( ) . collect :: < Result < Vec < u32 > , wasmparser:: BinaryReaderError > > ( ) ?;
338
262
instructions. push ( Instruction :: BrTable ( def, targets. len ( ) ) ) ;
339
263
instructions. extend ( targets. into_iter ( ) . map ( Instruction :: BrLabel ) ) ;
340
264
continue ;
@@ -406,11 +330,7 @@ pub fn process_operators<'a>(
406
330
BrIf { relative_depth } => Instruction :: BrIf ( relative_depth) ,
407
331
Return => Instruction :: Return ,
408
332
Call { function_index } => Instruction :: Call ( function_index) ,
409
- CallIndirect {
410
- type_index,
411
- table_index,
412
- ..
413
- } => Instruction :: CallIndirect ( type_index, table_index) ,
333
+ CallIndirect { type_index, table_index, .. } => Instruction :: CallIndirect ( type_index, table_index) ,
414
334
Drop => Instruction :: Drop ,
415
335
Select => Instruction :: Select ( None ) ,
416
336
TypedSelect { ty } => Instruction :: Select ( Some ( convert_valtype ( & ty) ) ) ,
@@ -590,30 +510,21 @@ pub fn process_operators<'a>(
590
510
TableGet { table } => Instruction :: TableGet ( table) ,
591
511
TableSet { table } => Instruction :: TableSet ( table) ,
592
512
TableInit { table, elem_index } => Instruction :: TableInit ( table, elem_index) ,
593
- TableCopy { src_table, dst_table } => Instruction :: TableCopy {
594
- from : src_table,
595
- to : dst_table,
596
- } ,
513
+ TableCopy { src_table, dst_table } => Instruction :: TableCopy { from : src_table, to : dst_table } ,
597
514
TableGrow { table } => Instruction :: TableGrow ( table) ,
598
515
TableSize { table } => Instruction :: TableSize ( table) ,
599
516
TableFill { table } => Instruction :: TableFill ( table) ,
600
517
op => {
601
518
log:: error!( "Unsupported instruction: {:?}" , op) ;
602
- return Err ( crate :: ParseError :: UnsupportedOperator ( format ! (
603
- "Unsupported instruction: {:?}" ,
604
- op
605
- ) ) ) ;
519
+ return Err ( crate :: ParseError :: UnsupportedOperator ( format ! ( "Unsupported instruction: {:?}" , op) ) ) ;
606
520
}
607
521
} ;
608
522
609
523
instructions. push ( res) ;
610
524
}
611
525
612
526
if !labels_ptrs. is_empty ( ) {
613
- panic ! (
614
- "last_label_pointer should be None after processing all instructions: {:?}" ,
615
- labels_ptrs
616
- ) ;
527
+ panic ! ( "last_label_pointer should be None after processing all instructions: {:?}" , labels_ptrs) ;
617
528
}
618
529
619
530
validator. finish ( offset) ?;
0 commit comments