@@ -427,6 +427,16 @@ pub trait Reencode {
427427 utils:: parse_import_section ( self , imports, section)
428428 }
429429
430+ /// Parses a [`wasmparser::Imports`] and adds all of its contents to the
431+ /// `import` section.
432+ fn parse_imports (
433+ & mut self ,
434+ import_section : & mut crate :: ImportSection ,
435+ imports : wasmparser:: Imports < ' _ > ,
436+ ) -> Result < ( ) , Error < Self :: Error > > {
437+ utils:: parse_imports ( self , import_section, imports)
438+ }
439+
430440 /// Parses the single [`wasmparser::Import`] provided and adds it to the
431441 /// `import` section.
432442 fn parse_import (
@@ -648,7 +658,7 @@ impl Reencode for RoundtripReencoder {
648658#[ allow( missing_docs) ] // FIXME
649659pub mod utils {
650660 use super :: { Error , Reencode } ;
651- use crate :: { CoreTypeEncoder , Encode } ;
661+ use crate :: { CoreTypeEncoder , Encode , Imports } ;
652662 use alloc:: vec:: Vec ;
653663 use core:: ops:: Range ;
654664
@@ -1430,27 +1440,63 @@ pub mod utils {
14301440 /// all the imports to the `import` section.
14311441 pub fn parse_import_section < T : ?Sized + Reencode > (
14321442 reencoder : & mut T ,
1433- imports : & mut crate :: ImportSection ,
1443+ import_section : & mut crate :: ImportSection ,
14341444 section : wasmparser:: ImportSectionReader < ' _ > ,
14351445 ) -> Result < ( ) , Error < T :: Error > > {
1436- for import in section {
1437- reencoder. parse_import ( imports, import?) ?;
1446+ for imports in section {
1447+ let imports = imports?;
1448+ reencoder. parse_imports ( import_section, imports) ?;
14381449 }
14391450 Ok ( ( ) )
14401451 }
14411452
1453+ /// Parses a [`wasmparser::Imports`] and adds all of its contents to the
1454+ /// `import` section.
1455+ pub fn parse_imports < T : ?Sized + Reencode > (
1456+ reencoder : & mut T ,
1457+ import_section : & mut crate :: ImportSection ,
1458+ imports : wasmparser:: Imports < ' _ > ,
1459+ ) -> Result < ( ) , Error < T :: Error > > {
1460+ import_section. imports ( match imports {
1461+ wasmparser:: Imports :: Single ( _, import) => Imports :: Single ( crate :: Import {
1462+ module : import. module ,
1463+ name : import. name ,
1464+ ty : reencoder. entity_type ( import. ty ) ?,
1465+ } ) ,
1466+ wasmparser:: Imports :: Compact1 { module, items } => {
1467+ let mut new_items: Vec < crate :: ImportCompact > = Vec :: new ( ) ;
1468+ for item in items {
1469+ let item = item?;
1470+ new_items. push ( crate :: ImportCompact {
1471+ name : item. name ,
1472+ ty : reencoder. entity_type ( item. ty ) ?,
1473+ } )
1474+ }
1475+ Imports :: Compact1 {
1476+ module : module,
1477+ items : new_items. into ( ) ,
1478+ }
1479+ }
1480+ wasmparser:: Imports :: Compact2 { module, ty, names } => {
1481+ let names = names. into_iter ( ) . collect :: < wasmparser:: Result < Vec < _ > > > ( ) ?;
1482+ Imports :: Compact2 {
1483+ module : module,
1484+ ty : reencoder. entity_type ( ty) ?,
1485+ names : names. into ( ) ,
1486+ }
1487+ }
1488+ } ) ;
1489+ Ok ( ( ) )
1490+ }
1491+
14421492 /// Parses the single [`wasmparser::Import`] provided and adds it to the
14431493 /// `import` section.
14441494 pub fn parse_import < T : ?Sized + Reencode > (
14451495 reencoder : & mut T ,
14461496 imports : & mut crate :: ImportSection ,
14471497 import : wasmparser:: Import < ' _ > ,
14481498 ) -> Result < ( ) , Error < T :: Error > > {
1449- imports. import (
1450- import. module ,
1451- import. name ,
1452- reencoder. entity_type ( import. ty ) ?,
1453- ) ;
1499+ reencoder. parse_imports ( imports, wasmparser:: Imports :: Single ( 0 , import) ) ?;
14541500 Ok ( ( ) )
14551501 }
14561502
0 commit comments