@@ -399,17 +399,15 @@ pub struct EncodingState<'a> {
399399 /// A map of adapter module instances and the index of their instance.
400400 adapter_instances : IndexMap < & ' a str , u32 > ,
401401
402- /// Imported instances and what index they were imported as.
403- imported_instances : IndexMap < InterfaceId , u32 > ,
402+ /// Imported/exported instances and what index they were imported as.
403+ instances : IndexMap < InterfaceId , u32 > ,
404404 imported_funcs : IndexMap < String , u32 > ,
405- exported_instances : IndexMap < InterfaceId , u32 > ,
406405
407406 /// Maps used when translating types to the component model binary format.
408407 /// Note that imports and exports are stored in separate maps since they
409408 /// need fresh hierarchies of types in case the same interface is both
410409 /// imported and exported.
411- import_type_encoding_maps : TypeEncodingMaps < ' a > ,
412- export_type_encoding_maps : TypeEncodingMaps < ' a > ,
410+ type_encoding_maps : TypeEncodingMaps < ' a > ,
413411
414412 /// Cache of items that have been aliased from core instances.
415413 ///
@@ -566,7 +564,7 @@ impl<'a> EncodingState<'a> {
566564 let instance_idx = self
567565 . component
568566 . import ( name, ComponentTypeRef :: Instance ( instance_type_idx) ) ;
569- let prev = self . imported_instances . insert ( interface_id, instance_idx) ;
567+ let prev = self . instances . insert ( interface_id, instance_idx) ;
570568 assert ! ( prev. is_none( ) ) ;
571569 Ok ( ( ) )
572570 }
@@ -600,18 +598,10 @@ impl<'a> EncodingState<'a> {
600598 Ok ( ( ) )
601599 }
602600
603- fn alias_imported_type ( & mut self , interface : InterfaceId , id : TypeId ) -> u32 {
601+ fn alias_instance_type_export ( & mut self , interface : InterfaceId , id : TypeId ) -> u32 {
604602 let ty = & self . info . encoder . metadata . resolve . types [ id] ;
605603 let name = ty. name . as_ref ( ) . expect ( "type must have a name" ) ;
606- let instance = self . imported_instances [ & interface] ;
607- self . component
608- . alias_export ( instance, name, ComponentExportKind :: Type )
609- }
610-
611- fn alias_exported_type ( & mut self , interface : InterfaceId , id : TypeId ) -> u32 {
612- let ty = & self . info . encoder . metadata . resolve . types [ id] ;
613- let name = ty. name . as_ref ( ) . expect ( "type must have a name" ) ;
614- let instance = self . exported_instances [ & interface] ;
604+ let instance = self . instances [ & interface] ;
615605 self . component
616606 . alias_export ( instance, name, ComponentExportKind :: Type )
617607 }
@@ -674,9 +664,9 @@ impl<'a> EncodingState<'a> {
674664 // If this resource is owned by a world then it's a top-level
675665 // resource which means it must have already been translated so
676666 // it's available for lookup in `import_type_map`.
677- TypeOwner :: World ( _) => self . import_type_encoding_maps . id_to_index [ & id] ,
667+ TypeOwner :: World ( _) => self . type_encoding_maps . id_to_index [ & id] ,
678668 TypeOwner :: Interface ( i) => {
679- let instance = self . imported_instances [ & i] ;
669+ let instance = self . instances [ & i] ;
680670 let name = ty. name . as_ref ( ) . expect ( "resources must be named" ) ;
681671 self . component
682672 . alias_export ( instance, name, ComponentExportKind :: Type )
@@ -886,17 +876,7 @@ impl<'a> EncodingState<'a> {
886876 nested. type_encoding_maps . def_to_index . clear ( ) ;
887877 for ( name, idx) in nested. imports . drain ( ..) {
888878 let id = reverse_map[ & idx] ;
889- let owner = match resolve. types [ id] . owner {
890- TypeOwner :: Interface ( id) => id,
891- _ => unreachable ! ( ) ,
892- } ;
893- let idx = if owner == export || exports_used. contains ( & owner) {
894- log:: trace!( "consulting exports for {id:?}" ) ;
895- nested. state . export_type_encoding_maps . id_to_index [ & id]
896- } else {
897- log:: trace!( "consulting imports for {id:?}" ) ;
898- nested. state . import_type_encoding_maps . id_to_index [ & id]
899- } ;
879+ let idx = nested. state . type_encoding_maps . id_to_index [ & id] ;
900880 imports. push ( ( name, ComponentExportKind :: Type , idx) )
901881 }
902882
@@ -966,7 +946,7 @@ impl<'a> EncodingState<'a> {
966946 instance_index,
967947 None ,
968948 ) ;
969- let prev = self . exported_instances . insert ( export, idx) ;
949+ let prev = self . instances . insert ( export, idx) ;
970950 assert ! ( prev. is_none( ) ) ;
971951
972952 // After everything is all said and done remove all the type information
@@ -977,8 +957,8 @@ impl<'a> EncodingState<'a> {
977957 // necessary via aliases from the exported instance which is the new
978958 // source of truth for all these types.
979959 for ( _name, id) in resolve. interfaces [ export] . types . iter ( ) {
980- self . export_type_encoding_maps . id_to_index . remove ( id) ;
981- self . export_type_encoding_maps
960+ self . type_encoding_maps . id_to_index . remove ( id) ;
961+ self . type_encoding_maps
982962 . def_to_index
983963 . remove ( & resolve. types [ * id] . kind ) ;
984964 }
@@ -1296,7 +1276,7 @@ impl<'a> EncodingState<'a> {
12961276 let ( ( name, _) , _) = interface. lowerings . get_index ( * index) . unwrap ( ) ;
12971277 let func_index = match & interface. interface {
12981278 Some ( interface_id) => {
1299- let instance_index = self . imported_instances [ interface_id] ;
1279+ let instance_index = self . instances [ interface_id] ;
13001280 self . component . alias_export (
13011281 instance_index,
13021282 name,
@@ -1602,7 +1582,7 @@ impl<'a> EncodingState<'a> {
16021582 dtor,
16031583 ) ;
16041584 let prev = self
1605- . export_type_encoding_maps
1585+ . type_encoding_maps
16061586 . id_to_index
16071587 . insert ( * ty, resource_idx) ;
16081588 assert ! ( prev. is_none( ) ) ;
@@ -1759,19 +1739,19 @@ impl<'a> EncodingState<'a> {
17591739 Import :: ExportedResourceDrop ( _key, id) => {
17601740 let index = self
17611741 . component
1762- . resource_drop ( self . export_type_encoding_maps . id_to_index [ id] ) ;
1742+ . resource_drop ( self . type_encoding_maps . id_to_index [ id] ) ;
17631743 Ok ( ( ExportKind :: Func , index) )
17641744 }
17651745 Import :: ExportedResourceRep ( _key, id) => {
17661746 let index = self
17671747 . component
1768- . resource_rep ( self . export_type_encoding_maps . id_to_index [ id] ) ;
1748+ . resource_rep ( self . type_encoding_maps . id_to_index [ id] ) ;
17691749 Ok ( ( ExportKind :: Func , index) )
17701750 }
17711751 Import :: ExportedResourceNew ( _key, id) => {
17721752 let index = self
17731753 . component
1774- . resource_new ( self . export_type_encoding_maps . id_to_index [ id] ) ;
1754+ . resource_new ( self . type_encoding_maps . id_to_index [ id] ) ;
17751755 Ok ( ( ExportKind :: Func , index) )
17761756 }
17771757
@@ -2078,7 +2058,7 @@ impl<'a> EncodingState<'a> {
20782058 Lowering :: Direct => {
20792059 let func_index = match & import. interface {
20802060 Some ( interface) => {
2081- let instance_index = self . imported_instances [ interface] ;
2061+ let instance_index = self . instances [ interface] ;
20822062 self . component
20832063 . alias_export ( instance_index, name, ComponentExportKind :: Func )
20842064 }
@@ -3336,6 +3316,8 @@ impl ComponentEncoder {
33363316 . merge_world_imports_based_on_semver ( self . metadata . world ) ?;
33373317 }
33383318
3319+ self . finalize_resolve_with_nominal_ids ( ) ;
3320+
33393321 let world = ComponentWorld :: new ( self ) . context ( "failed to decode world from module" ) ?;
33403322 let mut state = EncodingState {
33413323 component : ComponentBuilder :: default ( ) ,
@@ -3346,11 +3328,9 @@ impl ComponentEncoder {
33463328 fixups_module_index : None ,
33473329 adapter_modules : IndexMap :: new ( ) ,
33483330 adapter_instances : IndexMap :: new ( ) ,
3349- import_type_encoding_maps : Default :: default ( ) ,
3350- export_type_encoding_maps : Default :: default ( ) ,
3351- imported_instances : Default :: default ( ) ,
3331+ type_encoding_maps : Default :: default ( ) ,
3332+ instances : Default :: default ( ) ,
33523333 imported_funcs : Default :: default ( ) ,
3353- exported_instances : Default :: default ( ) ,
33543334 aliased_core_items : Default :: default ( ) ,
33553335 info : & world,
33563336 export_task_initialization_wrappers : HashMap :: new ( ) ,
@@ -3376,6 +3356,63 @@ impl ComponentEncoder {
33763356
33773357 Ok ( bytes)
33783358 }
3359+
3360+ /// Call the `generate_nominal_type_ids` method on the `Resolve` that we're
3361+ /// using, adjusting any preexisting keys/pointers as necessary.
3362+ ///
3363+ /// This is the final step after merging all known `Resolve`s together
3364+ /// before a component is actually created. By creating a unique
3365+ /// `InterfaceId` for all interfaces it makes the generation process easier
3366+ /// since there's no need to fret about whether an `InterfaceId` is an
3367+ /// import or an export for example.
3368+ fn finalize_resolve_with_nominal_ids ( & mut self ) {
3369+ // Before calling `generate_nominal_type_ids` we need to handle the fact
3370+ // that the exports of the world are going to be rewritten. The only
3371+ // pointers we have into those are the exports of the main module and
3372+ // adapters. To handle this, before we generate nominal ids, indices of
3373+ // exports are saved here on the stack to get restored later on.
3374+ // Effectively we're clearing out the exports and rebuilding them later.
3375+ let world = & self . metadata . resolve . worlds [ self . metadata . world ] ;
3376+ let main_module_exports = self
3377+ . main_module_exports
3378+ . iter ( )
3379+ . map ( |i| world. exports . get_index_of ( i) . unwrap ( ) )
3380+ . collect :: < Vec < _ > > ( ) ;
3381+ let adapter_exports = self
3382+ . adapters
3383+ . values ( )
3384+ . map ( |adapter| {
3385+ adapter
3386+ . required_exports
3387+ . iter ( )
3388+ . map ( |i| world. exports . get_index_of ( i) . unwrap ( ) )
3389+ . collect :: < Vec < _ > > ( )
3390+ } )
3391+ . collect :: < Vec < _ > > ( ) ;
3392+
3393+ // With everything saved this will modify `Resolve` to ensure there's a
3394+ // nominal identifier for all interfaces (e.g. not both simultaneously
3395+ // imported and exported).
3396+ self . metadata
3397+ . resolve
3398+ . generate_nominal_type_ids ( self . metadata . world ) ;
3399+
3400+ // Rebuild the sets of exports now that the world's exports have been
3401+ // clobbered.
3402+ self . main_module_exports . clear ( ) ;
3403+ let world = & self . metadata . resolve . worlds [ self . metadata . world ] ;
3404+ for index in main_module_exports {
3405+ let ( key, _) = world. exports . get_index ( index) . unwrap ( ) ;
3406+ self . main_module_exports . insert ( key. clone ( ) ) ;
3407+ }
3408+ for ( exports, adapter) in adapter_exports. into_iter ( ) . zip ( self . adapters . values_mut ( ) ) {
3409+ adapter. required_exports . clear ( ) ;
3410+ for index in exports {
3411+ let ( key, _) = world. exports . get_index ( index) . unwrap ( ) ;
3412+ adapter. required_exports . insert ( key. clone ( ) ) ;
3413+ }
3414+ }
3415+ }
33793416}
33803417
33813418impl ComponentWorld < ' _ > {
0 commit comments