1
1
use crate :: log:: debug;
2
2
use alloc:: { boxed:: Box , format, vec:: Vec } ;
3
3
use core:: fmt:: Debug ;
4
- use tinywasm_types:: { Export , FuncType , Global , Instruction , ValType } ;
4
+ use tinywasm_types:: { Export , FuncType , Global , Instruction , MemoryType , TableType , ValType } ;
5
5
use wasmparser:: { Payload , Validator } ;
6
6
7
7
use crate :: { conversion, ParseError , Result } ;
@@ -17,14 +17,14 @@ pub struct ModuleReader {
17
17
pub version : Option < u16 > ,
18
18
pub start_func : Option < u32 > ,
19
19
20
- pub type_section : Vec < FuncType > ,
21
- pub function_section : Vec < u32 > ,
22
- pub export_section : Vec < Export > ,
23
- pub code_section : Vec < CodeSection > ,
24
- pub global_section : Vec < Global > ,
20
+ pub func_types : Vec < FuncType > ,
21
+ pub func_addrs : Vec < u32 > ,
22
+ pub exports : Vec < Export > ,
23
+ pub code : Vec < CodeSection > ,
24
+ pub globals : Vec < Global > ,
25
+ pub table_types : Vec < TableType > ,
26
+ pub memory_types : Vec < MemoryType > ,
25
27
26
- // pub table_section: Option<TableSectionReader<'a>>,
27
- // pub memory_section: Option<MemorySectionReader<'a>>,
28
28
// pub element_section: Option<ElementSectionReader<'a>>,
29
29
// pub data_section: Option<DataSectionReader<'a>>,
30
30
// pub import_section: Option<ImportSectionReader<'a>>,
@@ -35,13 +35,13 @@ impl Debug for ModuleReader {
35
35
fn fmt ( & self , f : & mut core:: fmt:: Formatter ) -> core:: fmt:: Result {
36
36
f. debug_struct ( "ModuleReader" )
37
37
. field ( "version" , & self . version )
38
- . field ( "type_section " , & self . type_section )
39
- . field ( "function_section " , & self . function_section )
40
- . field ( "code_section " , & self . code_section )
41
- . field ( "export_section " , & self . export_section )
42
- . field ( "global_section " , & self . global_section )
43
- // .field("table_section ", &self.table_section )
44
- // .field("memory_section ", &self.memory_section )
38
+ . field ( "func_types " , & self . func_types )
39
+ . field ( "func_addrs " , & self . func_addrs )
40
+ . field ( "code " , & self . code )
41
+ . field ( "exports " , & self . exports )
42
+ . field ( "globals " , & self . globals )
43
+ . field ( "table_types " , & self . table_types )
44
+ . field ( "memory_types " , & self . memory_types )
45
45
// .field("element_section", &self.element_section)
46
46
// .field("data_section", &self.data_section)
47
47
// .field("import_section", &self.import_section)
@@ -74,34 +74,31 @@ impl ModuleReader {
74
74
TypeSection ( reader) => {
75
75
debug ! ( "Found type section" ) ;
76
76
validator. type_section ( & reader) ?;
77
- self . type_section = reader
77
+ self . func_types = reader
78
78
. into_iter ( )
79
79
. map ( |t| conversion:: convert_module_type ( t?) )
80
80
. collect :: < Result < Vec < FuncType > > > ( ) ?;
81
81
}
82
82
FunctionSection ( reader) => {
83
83
debug ! ( "Found function section" ) ;
84
84
validator. function_section ( & reader) ?;
85
- self . function_section = reader. into_iter ( ) . map ( |f| Ok ( f?) ) . collect :: < Result < Vec < _ > > > ( ) ?;
85
+ self . func_addrs = reader. into_iter ( ) . map ( |f| Ok ( f?) ) . collect :: < Result < Vec < _ > > > ( ) ?;
86
86
}
87
87
GlobalSection ( reader) => {
88
88
debug ! ( "Found global section" ) ;
89
89
validator. global_section ( & reader) ?;
90
- self . global_section = conversion:: convert_module_globals ( reader) ?;
90
+ self . globals = conversion:: convert_module_globals ( reader) ?;
91
91
}
92
- TableSection ( _reader) => {
93
- return Err ( ParseError :: UnsupportedSection ( "Table section" . into ( ) ) ) ;
94
- // debug!("Found table section");
95
- // validator.table_section(&reader)?;
96
- // self.table_section = Some(reader);
92
+ TableSection ( reader) => {
93
+ debug ! ( "Found table section" ) ;
94
+ validator. table_section ( & reader) ?;
95
+ self . table_types = conversion:: convert_module_tables ( reader) ?;
97
96
}
98
- MemorySection ( _reader) => {
99
- return Err ( ParseError :: UnsupportedSection ( "Memory section" . into ( ) ) ) ;
100
- // debug!("Found memory section");
101
- // validator.memory_section(&reader)?;
102
- // self.memory_section = Some(reader);
97
+ MemorySection ( reader) => {
98
+ debug ! ( "Found memory section" ) ;
99
+ validator. memory_section ( & reader) ?;
100
+ self . memory_types = conversion:: convert_module_memories ( reader) ?;
103
101
}
104
-
105
102
ElementSection ( _reader) => {
106
103
return Err ( ParseError :: UnsupportedSection ( "Element section" . into ( ) ) ) ;
107
104
// debug!("Found element section");
@@ -116,7 +113,7 @@ impl ModuleReader {
116
113
}
117
114
CodeSectionStart { count, range, .. } => {
118
115
debug ! ( "Found code section ({} functions)" , count) ;
119
- if !self . code_section . is_empty ( ) {
116
+ if !self . code . is_empty ( ) {
120
117
return Err ( ParseError :: DuplicateSection ( "Code section" . into ( ) ) ) ;
121
118
}
122
119
@@ -127,7 +124,7 @@ impl ModuleReader {
127
124
let v = validator. code_section_entry ( & function) ?;
128
125
let func_validator = v. into_validator ( Default :: default ( ) ) ;
129
126
130
- self . code_section
127
+ self . code
131
128
. push ( conversion:: convert_module_code ( function, func_validator) ?) ;
132
129
}
133
130
ImportSection ( _reader) => {
@@ -140,7 +137,7 @@ impl ModuleReader {
140
137
ExportSection ( reader) => {
141
138
debug ! ( "Found export section" ) ;
142
139
validator. export_section ( & reader) ?;
143
- self . export_section = reader
140
+ self . exports = reader
144
141
. into_iter ( )
145
142
. map ( |e| conversion:: convert_module_export ( e?) )
146
143
. collect :: < Result < Vec < _ > > > ( ) ?;
@@ -158,6 +155,10 @@ impl ModuleReader {
158
155
debug ! ( "Found custom section" ) ;
159
156
debug ! ( "Skipping custom section: {:?}" , reader. name( ) ) ;
160
157
}
158
+ // TagSection(tag) => {
159
+ // debug!("Found tag section");
160
+ // validator.tag_section(&tag)?;
161
+ // }
161
162
UnknownSection { .. } => return Err ( ParseError :: UnsupportedSection ( "Unknown section" . into ( ) ) ) ,
162
163
section => {
163
164
return Err ( ParseError :: UnsupportedSection ( format ! (
0 commit comments