@@ -3,13 +3,10 @@ use alloc::sync::Arc;
33use crate :: { Result , module:: FunctionCode , visit:: process_operators_and_validate} ;
44use alloc:: { boxed:: Box , format, string:: ToString , vec:: Vec } ;
55use tinywasm_types:: * ;
6- use wasmparser:: { CompositeInnerType , FuncValidator , FuncValidatorAllocations , OperatorsReader , ValidatorResources } ;
7-
8- pub ( crate ) fn convert_module_elements < ' a , T : IntoIterator < Item = wasmparser:: Result < wasmparser:: Element < ' a > > > > (
9- elements : T ,
10- ) -> Result < Vec < tinywasm_types:: Element > > {
11- elements. into_iter ( ) . map ( |element| convert_module_element ( element?) ) . collect :: < Result < Vec < _ > > > ( )
12- }
6+ use wasmparser:: {
7+ CompositeInnerType , FuncValidator , FuncValidatorAllocations , OperatorsReader , OperatorsReaderAllocations ,
8+ ValidatorResources ,
9+ } ;
1310
1411pub ( crate ) fn convert_module_element ( element : wasmparser:: Element < ' _ > ) -> Result < tinywasm_types:: Element > {
1512 let kind = match element. kind {
@@ -44,12 +41,6 @@ pub(crate) fn convert_module_element(element: wasmparser::Element<'_>) -> Result
4441 }
4542}
4643
47- pub ( crate ) fn convert_module_data_sections < ' a , T : IntoIterator < Item = wasmparser:: Result < wasmparser:: Data < ' a > > > > (
48- data_sections : T ,
49- ) -> Result < Vec < tinywasm_types:: Data > > {
50- data_sections. into_iter ( ) . map ( |data| convert_module_data ( data?) ) . collect :: < Result < Vec < _ > > > ( )
51- }
52-
5344pub ( crate ) fn convert_module_data ( data : wasmparser:: Data < ' _ > ) -> Result < tinywasm_types:: Data > {
5445 Ok ( tinywasm_types:: Data {
5546 data : data. data . to_vec ( ) . into_boxed_slice ( ) ,
@@ -64,12 +55,6 @@ pub(crate) fn convert_module_data(data: wasmparser::Data<'_>) -> Result<tinywasm
6455 } )
6556}
6657
67- pub ( crate ) fn convert_module_imports < ' a , T : IntoIterator < Item = wasmparser:: Result < wasmparser:: Import < ' a > > > > (
68- imports : T ,
69- ) -> Result < Vec < Import > > {
70- imports. into_iter ( ) . map ( |import| convert_module_import ( import?) ) . collect :: < Result < Vec < _ > > > ( )
71- }
72-
7358pub ( crate ) fn convert_module_import ( import : wasmparser:: Import < ' _ > ) -> Result < Import > {
7459 let kind = match import. ty {
7560 wasmparser:: TypeRef :: Func ( ty) => ImportKind :: Function ( ty) ,
@@ -100,12 +85,6 @@ pub(crate) fn convert_module_import(import: wasmparser::Import<'_>) -> Result<Im
10085 Ok ( Import { module : import. module . into ( ) , name : import. name . into ( ) , kind } )
10186}
10287
103- pub ( crate ) fn convert_module_memories < T : IntoIterator < Item = wasmparser:: Result < wasmparser:: MemoryType > > > (
104- memory_types : T ,
105- ) -> Result < Vec < MemoryType > > {
106- memory_types. into_iter ( ) . map ( |memory| Ok ( convert_module_memory ( memory?) ) ) . collect :: < Result < Vec < _ > > > ( )
107- }
108-
10988pub ( crate ) fn convert_module_memory ( memory : wasmparser:: MemoryType ) -> MemoryType {
11089 MemoryType :: new (
11190 if memory. memory64 { MemoryArch :: I64 } else { MemoryArch :: I32 } ,
@@ -115,12 +94,6 @@ pub(crate) fn convert_module_memory(memory: wasmparser::MemoryType) -> MemoryTyp
11594 )
11695}
11796
118- pub ( crate ) fn convert_module_tables < ' a , T : IntoIterator < Item = wasmparser:: Result < wasmparser:: Table < ' a > > > > (
119- table_types : T ,
120- ) -> Result < Vec < TableType > > {
121- table_types. into_iter ( ) . map ( |table| convert_module_table ( table?) ) . collect :: < Result < Vec < _ > > > ( )
122- }
123-
12497pub ( crate ) fn convert_module_table ( table : wasmparser:: Table < ' _ > ) -> Result < TableType > {
12598 let size_initial = table. ty . initial . try_into ( ) . map_err ( |_| {
12699 crate :: ParseError :: UnsupportedOperator ( format ! ( "Table size initial is too large: {}" , table. ty. initial) )
@@ -134,7 +107,7 @@ pub(crate) fn convert_module_table(table: wasmparser::Table<'_>) -> Result<Table
134107
135108pub ( crate ) fn convert_module_globals (
136109 globals : wasmparser:: SectionLimited < ' _ , wasmparser:: Global < ' _ > > ,
137- ) -> Result < Vec < Global > > {
110+ ) -> Result < Box < [ Global ] > > {
138111 globals
139112 . into_iter ( )
140113 . map ( |global| {
@@ -143,7 +116,7 @@ pub(crate) fn convert_module_globals(
143116 let ops = global. init_expr . get_operators_reader ( ) ;
144117 Ok ( Global { init : process_const_operators ( ops) ?, ty : GlobalType :: new ( ty, global. ty . mutable ) } )
145118 } )
146- . collect :: < Result < Vec < _ > > > ( )
119+ . collect :: < Result < Box < _ > > > ( )
147120}
148121
149122pub ( crate ) fn convert_module_export ( export : wasmparser:: Export < ' _ > ) -> Result < Export > {
@@ -163,7 +136,8 @@ pub(crate) fn convert_module_export(export: wasmparser::Export<'_>) -> Result<Ex
163136pub ( crate ) fn convert_module_code (
164137 func : wasmparser:: FunctionBody < ' _ > ,
165138 mut validator : FuncValidator < ValidatorResources > ,
166- ) -> Result < ( FunctionCode , FuncValidatorAllocations ) > {
139+ reader_allocs : OperatorsReaderAllocations ,
140+ ) -> Result < ( FunctionCode , FuncValidatorAllocations , OperatorsReaderAllocations ) > {
167141 let locals_reader = func. get_locals_reader ( ) ?;
168142 let count = locals_reader. get_count ( ) ;
169143 let pos = locals_reader. original_position ( ) ;
@@ -199,8 +173,13 @@ pub(crate) fn convert_module_code(
199173 }
200174 }
201175
202- let ( body, data, allocations) = process_operators_and_validate ( validator, func, local_addr_map) ?;
203- Ok ( ( FunctionCode { instructions : body, data, locals : local_counts, uses_local_memory : false } , allocations) )
176+ let ( body, data, validator_allocs, reader_allocs) =
177+ process_operators_and_validate ( validator, func, local_addr_map, reader_allocs) ?;
178+ Ok ( (
179+ FunctionCode { instructions : body, data, locals : local_counts, uses_local_memory : false } ,
180+ validator_allocs,
181+ reader_allocs,
182+ ) )
204183}
205184
206185pub ( crate ) fn convert_module_type ( ty : wasmparser:: RecGroup ) -> Result < Arc < FuncType > > {
0 commit comments