@@ -401,6 +401,7 @@ pub struct EncodingState<'a> {
401401
402402 /// Imported instances and what index they were imported as.
403403 imported_instances : IndexMap < InterfaceId , u32 > ,
404+ imported_instances_by_name : HashMap < String , u32 > ,
404405 imported_funcs : IndexMap < String , u32 > ,
405406 exported_instances : IndexMap < InterfaceId , u32 > ,
406407
@@ -456,6 +457,18 @@ impl<'a> EncodingState<'a> {
456457 }
457458 }
458459
460+ fn world_import_name ( & self , key : & WorldKey ) -> String {
461+ let resolve = & self . info . encoder . metadata . resolve ;
462+ let world = & resolve. worlds [ self . info . encoder . metadata . world ] ;
463+ resolve. name_world_key_with_item ( key, & world. imports [ key] )
464+ }
465+
466+ fn world_export_name ( & self , key : & WorldKey ) -> String {
467+ let resolve = & self . info . encoder . metadata . resolve ;
468+ let world = & resolve. worlds [ self . info . encoder . metadata . world ] ;
469+ resolve. name_world_key_with_item ( key, & world. exports [ key] )
470+ }
471+
459472 fn root_import_type_encoder (
460473 & mut self ,
461474 interface : Option < InterfaceId > ,
@@ -566,8 +579,11 @@ impl<'a> EncodingState<'a> {
566579 let instance_idx = self
567580 . component
568581 . import ( name, ComponentTypeRef :: Instance ( instance_type_idx) ) ;
569- let prev = self . imported_instances . insert ( interface_id, instance_idx) ;
570- assert ! ( prev. is_none( ) ) ;
582+ self . imported_instances
583+ . entry ( interface_id)
584+ . or_insert ( instance_idx) ;
585+ self . imported_instances_by_name
586+ . insert ( name. to_string ( ) , instance_idx) ;
571587 Ok ( ( ) )
572588 }
573589
@@ -704,9 +720,9 @@ impl<'a> EncodingState<'a> {
704720 let prev = world_func_core_names. insert ( name, core_name) ;
705721 assert ! ( prev. is_none( ) ) ;
706722 }
707- Export :: InterfaceFunc ( _ , id , name, _) => {
723+ Export :: InterfaceFunc ( key , _id , name, _) => {
708724 let prev = interface_func_core_names
709- . entry ( id )
725+ . entry ( key )
710726 . or_insert ( IndexMap :: new ( ) )
711727 . insert ( name. as_str ( ) , core_name) ;
712728 assert ! ( prev. is_none( ) ) ;
@@ -731,7 +747,7 @@ impl<'a> EncodingState<'a> {
731747 let world = & resolve. worlds [ self . info . encoder . metadata . world ] ;
732748
733749 for export_name in exports {
734- let export_string = resolve . name_world_key ( export_name) ;
750+ let export_string = self . world_export_name ( export_name) ;
735751 match & world. exports [ export_name] {
736752 WorldItem :: Function ( func) => {
737753 let ty = self
@@ -743,7 +759,7 @@ impl<'a> EncodingState<'a> {
743759 . export ( & export_string, ComponentExportKind :: Func , idx, None ) ;
744760 }
745761 WorldItem :: Interface { id, .. } => {
746- let core_names = interface_func_core_names. get ( id ) ;
762+ let core_names = interface_func_core_names. get ( export_name ) ;
747763 self . encode_interface_export (
748764 & export_string,
749765 module,
@@ -966,8 +982,7 @@ impl<'a> EncodingState<'a> {
966982 instance_index,
967983 None ,
968984 ) ;
969- let prev = self . exported_instances . insert ( export, idx) ;
970- assert ! ( prev. is_none( ) ) ;
985+ self . exported_instances . entry ( export) . or_insert ( idx) ;
971986
972987 // After everything is all said and done remove all the type information
973988 // about type exports of this interface. Any entries in the map
@@ -1292,11 +1307,12 @@ impl<'a> EncodingState<'a> {
12921307 realloc,
12931308 encoding,
12941309 } => {
1295- let interface = & self . info . import_map [ interface] ;
1296- let ( ( name, _) , _) = interface. lowerings . get_index ( * index) . unwrap ( ) ;
1297- let func_index = match & interface. interface {
1298- Some ( interface_id) => {
1299- let instance_index = self . imported_instances [ interface_id] ;
1310+ let import = & self . info . import_map [ interface] ;
1311+ let ( ( name, _) , _) = import. lowerings . get_index ( * index) . unwrap ( ) ;
1312+ let func_index = match interface {
1313+ Some ( iface_key) => {
1314+ let instance_index =
1315+ self . imported_instances_by_name [ iface_key. as_str ( ) ] ;
13001316 self . component . alias_export (
13011317 instance_index,
13021318 name,
@@ -1309,7 +1325,7 @@ impl<'a> EncodingState<'a> {
13091325 let realloc = self
13101326 . info
13111327 . exports_for ( * realloc)
1312- . import_realloc_for ( interface . interface , name)
1328+ . import_realloc_for ( import . interface , name)
13131329 . map ( |name| {
13141330 let instance = self . instance_for ( * realloc) ;
13151331 self . core_alias_export (
@@ -1785,7 +1801,7 @@ impl<'a> EncodingState<'a> {
17851801 self . materialize_wit_import (
17861802 shims,
17871803 for_module,
1788- iface. map ( |_| resolve . name_world_key ( key) ) ,
1804+ iface. map ( |_| self . world_import_name ( key) ) ,
17891805 & format ! ( "{name}_drop" ) ,
17901806 key,
17911807 AbiVariant :: GuestImport ,
@@ -1955,7 +1971,7 @@ impl<'a> EncodingState<'a> {
19551971 Import :: InterfaceFunc ( key, _, name, abi) => self . materialize_wit_import (
19561972 shims,
19571973 for_module,
1958- Some ( resolve . name_world_key ( key) ) ,
1974+ Some ( self . world_import_name ( key) ) ,
19591975 name,
19601976 key,
19611977 * abi,
@@ -2072,9 +2088,9 @@ impl<'a> EncodingState<'a> {
20722088 // All direct lowerings can be `canon lower`'d here immediately
20732089 // and passed as arguments.
20742090 Lowering :: Direct => {
2075- let func_index = match & import . interface {
2076- Some ( interface ) => {
2077- let instance_index = self . imported_instances [ interface ] ;
2091+ let func_index = match & interface_key {
2092+ Some ( iface_key ) => {
2093+ let instance_index = self . imported_instances_by_name [ iface_key . as_str ( ) ] ;
20782094 self . component
20792095 . alias_export ( instance_index, name, ComponentExportKind :: Func )
20802096 }
@@ -2849,14 +2865,15 @@ impl<'a> Shims<'a> {
28492865 // metadata out of this `match` to the loop below to figure that
28502866 // out.
28512867 Import :: InterfaceFunc ( key, _, name, abi) => {
2868+ let wit_world = & resolve. worlds [ world. encoder . metadata . world ] ;
28522869 self . append_indirect_wit_func (
28532870 world,
28542871 for_module,
28552872 module,
28562873 field,
28572874 key,
28582875 name,
2859- Some ( resolve. name_world_key ( key) ) ,
2876+ Some ( resolve. name_world_key_with_item ( key, & wit_world . imports [ key ] ) ) ,
28602877 * abi,
28612878 ) ?;
28622879 }
@@ -3343,6 +3360,7 @@ impl ComponentEncoder {
33433360 import_type_encoding_maps : Default :: default ( ) ,
33443361 export_type_encoding_maps : Default :: default ( ) ,
33453362 imported_instances : Default :: default ( ) ,
3363+ imported_instances_by_name : Default :: default ( ) ,
33463364 imported_funcs : Default :: default ( ) ,
33473365 exported_instances : Default :: default ( ) ,
33483366 aliased_core_items : Default :: default ( ) ,
0 commit comments