11use alloc:: { boxed:: Box , format, string:: ToString , vec:: Vec } ;
22use log:: info;
33use tinywasm_types:: {
4- BlockArgs , ConstInstruction , Export , ExternalKind , FuncType , Global , Instruction , MemArg , MemoryArch , MemoryType ,
5- TableType , ValType ,
4+ BlockArgs , ConstInstruction , Export , ExternalKind , FuncType , Global , GlobalType , Import , ImportKind , Instruction ,
5+ MemArg , MemoryArch , MemoryType , TableType , ValType ,
66} ;
77use wasmparser:: { FuncValidator , ValidatorResources } ;
88
99use crate :: { module:: CodeSection , Result } ;
1010
11+ pub ( crate ) fn convert_module_imports < ' a , T : IntoIterator < Item = wasmparser:: Result < wasmparser:: Import < ' a > > > > (
12+ imports : T ,
13+ ) -> Result < Vec < Import > > {
14+ let imports = imports
15+ . into_iter ( )
16+ . map ( |import| convert_module_import ( import?) )
17+ . collect :: < Result < Vec < _ > > > ( ) ?;
18+ Ok ( imports)
19+ }
20+
21+ pub ( crate ) fn convert_module_import < ' a > ( import : wasmparser:: Import < ' a > ) -> Result < Import > {
22+ Ok ( Import {
23+ module : import. module . to_string ( ) ,
24+ name : import. name . to_string ( ) ,
25+ kind : match import. ty {
26+ wasmparser:: TypeRef :: Func ( ty) => ImportKind :: Func ( ty) ,
27+ wasmparser:: TypeRef :: Table ( ty) => ImportKind :: Table ( convert_module_table ( ty) ?) ,
28+ wasmparser:: TypeRef :: Memory ( ty) => ImportKind :: Mem ( convert_module_memory ( ty) ?) ,
29+ wasmparser:: TypeRef :: Global ( ty) => ImportKind :: Global ( GlobalType {
30+ mutable : ty. mutable ,
31+ ty : convert_valtype ( & ty. content_type ) ,
32+ } ) ,
33+ wasmparser:: TypeRef :: Tag ( ty) => {
34+ return Err ( crate :: ParseError :: UnsupportedOperator ( format ! (
35+ "Unsupported import kind: {:?}" ,
36+ ty
37+ ) ) )
38+ }
39+ } ,
40+ } )
41+ }
42+
1143pub ( crate ) fn convert_module_memories < T : IntoIterator < Item = wasmparser:: Result < wasmparser:: MemoryType > > > (
1244 memory_types : T ,
1345) -> Result < Vec < MemoryType > > {
1446 let memory_type = memory_types
1547 . into_iter ( )
16- . map ( |memory| {
17- let memory = memory?;
18- Ok ( MemoryType {
19- arch : match memory. memory64 {
20- true => MemoryArch :: I64 ,
21- false => MemoryArch :: I32 ,
22- } ,
23- page_count_initial : memory. initial ,
24- page_count_max : memory. maximum ,
25- } )
26- } )
48+ . map ( |memory| convert_module_memory ( memory?) )
2749 . collect :: < Result < Vec < _ > > > ( ) ?;
2850
2951 Ok ( memory_type)
3052}
3153
54+ pub ( crate ) fn convert_module_memory ( memory : wasmparser:: MemoryType ) -> Result < MemoryType > {
55+ Ok ( MemoryType {
56+ arch : match memory. memory64 {
57+ true => MemoryArch :: I64 ,
58+ false => MemoryArch :: I32 ,
59+ } ,
60+ page_count_initial : memory. initial ,
61+ page_count_max : memory. maximum ,
62+ } )
63+ }
64+
3265pub ( crate ) fn convert_module_tables < T : IntoIterator < Item = wasmparser:: Result < wasmparser:: TableType > > > (
3366 table_types : T ,
3467) -> Result < Vec < TableType > > {
3568 let table_type = table_types
3669 . into_iter ( )
37- . map ( |table| {
38- let table = table?;
39- let ty = convert_valtype ( & table. element_type ) ;
40- Ok ( TableType {
41- element_type : ty,
42- size_initial : table. initial ,
43- size_max : table. maximum ,
44- } )
45- } )
70+ . map ( |table| convert_module_table ( table?) )
4671 . collect :: < Result < Vec < _ > > > ( ) ?;
4772
4873 Ok ( table_type)
4974}
5075
76+ pub ( crate ) fn convert_module_table ( table : wasmparser:: TableType ) -> Result < TableType > {
77+ let ty = convert_valtype ( & table. element_type ) ;
78+ Ok ( TableType {
79+ element_type : ty,
80+ size_initial : table. initial ,
81+ size_max : table. maximum ,
82+ } )
83+ }
84+
5185pub ( crate ) fn convert_module_globals < ' a , T : IntoIterator < Item = wasmparser:: Result < wasmparser:: Global < ' a > > > > (
5286 globals : T ,
5387) -> Result < Vec < Global > > {
@@ -70,9 +104,11 @@ pub(crate) fn convert_module_globals<'a, T: IntoIterator<Item = wasmparser::Resu
70104 assert ! ( matches!( ops[ ops. len( ) - 1 ] , wasmparser:: Operator :: End ) ) ;
71105
72106 Ok ( Global {
73- ty,
74107 init : process_const_operator ( ops[ ops. len ( ) - 2 ] . clone ( ) ) ?,
75- mutable : global. ty . mutable ,
108+ ty : GlobalType {
109+ mutable : global. ty . mutable ,
110+ ty,
111+ } ,
76112 } )
77113 } )
78114 . collect :: < Result < Vec < _ > > > ( ) ?;
0 commit comments