11use crate :: log:: debug;
2+ use crate :: { conversion, ParseError , Result } ;
23use alloc:: { boxed:: Box , format, vec:: Vec } ;
34use core:: fmt:: Debug ;
4- use tinywasm_types:: { Export , FuncType , Global , Import , Instruction , MemoryType , TableType , ValType } ;
5+ use tinywasm_types:: { Data , Export , FuncType , Global , Import , Instruction , MemoryType , TableType , ValType } ;
56use wasmparser:: { Payload , Validator } ;
67
7- use crate :: { conversion, ParseError , Result } ;
8-
98#[ derive( Debug , Clone , PartialEq ) ]
109pub struct CodeSection {
1110 pub locals : Box < [ ValType ] > ,
@@ -25,9 +24,9 @@ pub struct ModuleReader {
2524 pub table_types : Vec < TableType > ,
2625 pub memory_types : Vec < MemoryType > ,
2726 pub imports : Vec < Import > ,
27+ pub data : Vec < Data > ,
2828
2929 // pub element_section: Option<ElementSectionReader<'a>>,
30- // pub data_section: Option<DataSectionReader<'a>>,
3130 pub end_reached : bool ,
3231}
3332
@@ -67,11 +66,19 @@ impl ModuleReader {
6766 }
6867 }
6968 StartSection { func, range } => {
69+ if self . start_func . is_some ( ) {
70+ return Err ( ParseError :: DuplicateSection ( "Start section" . into ( ) ) ) ;
71+ }
72+
7073 debug ! ( "Found start section" ) ;
7174 validator. start_section ( func, & range) ?;
7275 self . start_func = Some ( func) ;
7376 }
7477 TypeSection ( reader) => {
78+ if !self . func_types . is_empty ( ) {
79+ return Err ( ParseError :: DuplicateSection ( "Type section" . into ( ) ) ) ;
80+ }
81+
7582 debug ! ( "Found type section" ) ;
7683 validator. type_section ( & reader) ?;
7784 self . func_types = reader
@@ -80,21 +87,37 @@ impl ModuleReader {
8087 . collect :: < Result < Vec < FuncType > > > ( ) ?;
8188 }
8289 FunctionSection ( reader) => {
90+ if !self . func_addrs . is_empty ( ) {
91+ return Err ( ParseError :: DuplicateSection ( "Function section" . into ( ) ) ) ;
92+ }
93+
8394 debug ! ( "Found function section" ) ;
8495 validator. function_section ( & reader) ?;
8596 self . func_addrs = reader. into_iter ( ) . map ( |f| Ok ( f?) ) . collect :: < Result < Vec < _ > > > ( ) ?;
8697 }
8798 GlobalSection ( reader) => {
99+ if !self . globals . is_empty ( ) {
100+ return Err ( ParseError :: DuplicateSection ( "Global section" . into ( ) ) ) ;
101+ }
102+
88103 debug ! ( "Found global section" ) ;
89104 validator. global_section ( & reader) ?;
90105 self . globals = conversion:: convert_module_globals ( reader) ?;
91106 }
92107 TableSection ( reader) => {
108+ if !self . table_types . is_empty ( ) {
109+ return Err ( ParseError :: DuplicateSection ( "Table section" . into ( ) ) ) ;
110+ }
111+
93112 debug ! ( "Found table section" ) ;
94113 validator. table_section ( & reader) ?;
95114 self . table_types = conversion:: convert_module_tables ( reader) ?;
96115 }
97116 MemorySection ( reader) => {
117+ if !self . memory_types . is_empty ( ) {
118+ return Err ( ParseError :: DuplicateSection ( "Memory section" . into ( ) ) ) ;
119+ }
120+
98121 debug ! ( "Found memory section" ) ;
99122 validator. memory_section ( & reader) ?;
100123 self . memory_types = conversion:: convert_module_memories ( reader) ?;
@@ -105,11 +128,14 @@ impl ModuleReader {
105128 // validator.element_section(&reader)?;
106129 // self.element_section = Some(reader);
107130 }
108- DataSection ( _reader) => {
109- return Err ( ParseError :: UnsupportedSection ( "Data section" . into ( ) ) ) ;
110- // debug!("Found data section");
111- // validator.data_section(&reader)?;
112- // self.data_section = Some(reader);
131+ DataSection ( reader) => {
132+ if !self . data . is_empty ( ) {
133+ return Err ( ParseError :: DuplicateSection ( "Data section" . into ( ) ) ) ;
134+ }
135+
136+ debug ! ( "Found data section" ) ;
137+ validator. data_section ( & reader) ?;
138+ self . data = conversion:: convert_module_data_sections ( reader) ?;
113139 }
114140 CodeSectionStart { count, range, .. } => {
115141 debug ! ( "Found code section ({} functions)" , count) ;
@@ -127,11 +153,19 @@ impl ModuleReader {
127153 . push ( conversion:: convert_module_code ( function, func_validator) ?) ;
128154 }
129155 ImportSection ( reader) => {
156+ if !self . imports . is_empty ( ) {
157+ return Err ( ParseError :: DuplicateSection ( "Import section" . into ( ) ) ) ;
158+ }
159+
130160 debug ! ( "Found import section" ) ;
131161 validator. import_section ( & reader) ?;
132162 self . imports = conversion:: convert_module_imports ( reader) ?;
133163 }
134164 ExportSection ( reader) => {
165+ if !self . exports . is_empty ( ) {
166+ return Err ( ParseError :: DuplicateSection ( "Export section" . into ( ) ) ) ;
167+ }
168+
135169 debug ! ( "Found export section" ) ;
136170 validator. export_section ( & reader) ?;
137171 self . exports = reader
0 commit comments