@@ -72,6 +72,7 @@ struct CoreState {
7272 local_names : NamingMap < ( u32 , u32 ) , NameLocal > ,
7373 label_names : NamingMap < ( u32 , u32 ) , NameLabel > ,
7474 type_names : NamingMap < u32 , NameType > ,
75+ field_names : NamingMap < ( u32 , u32 ) , NameField > ,
7576 tag_names : NamingMap < u32 , NameTag > ,
7677 table_names : NamingMap < u32 , NameTable > ,
7778 memory_names : NamingMap < u32 , NameMemory > ,
@@ -639,7 +640,7 @@ impl Printer {
639640 let mut used = match name {
640641 // labels can be shadowed, so maintaining the used names is not useful.
641642 "label" => None ,
642- "local" => Some ( HashSet :: new ( ) ) ,
643+ "local" | "field" => Some ( HashSet :: new ( ) ) ,
643644 _ => unimplemented ! ( "{name} is an unknown type of indirect names" ) ,
644645 } ;
645646 for naming in indirect. names {
@@ -668,6 +669,7 @@ impl Printer {
668669 Name :: Global ( n) => name_map ( & mut state. core . global_names , n, "global" ) ?,
669670 Name :: Element ( n) => name_map ( & mut state. core . element_names , n, "elem" ) ?,
670671 Name :: Data ( n) => name_map ( & mut state. core . data_names , n, "data" ) ?,
672+ Name :: Field ( n) => indirect_name_map ( & mut state. core . field_names , n, "field" ) ?,
671673 Name :: Tag ( n) => name_map ( & mut state. core . tag_names , n, "tag" ) ?,
672674 Name :: Unknown { .. } => ( ) ,
673675 }
@@ -774,37 +776,33 @@ impl Printer {
774776
775777 fn print_type ( & mut self , state : & mut State , ty : SubType ) -> Result < ( ) > {
776778 self . start_group ( "type " ) ;
777- self . print_name ( & state. core . type_names , state. core . types . len ( ) as u32 ) ?;
779+ let ty_idx = state. core . types . len ( ) as u32 ;
780+ self . print_name ( & state. core . type_names , ty_idx) ?;
778781 self . result . push ( ' ' ) ;
779- self . print_sub ( state, & ty, None ) ?;
782+ self . print_sub ( state, & ty, ty_idx ) ?;
780783 self . end_group ( ) ; // `type`
781784 state. core . types . push ( Some ( ty) ) ;
782785 Ok ( ( ) )
783786 }
784787
785- fn print_sub ( & mut self , state : & State , ty : & SubType , names_for : Option < u32 > ) -> Result < u32 > {
788+ fn print_sub ( & mut self , state : & State , ty : & SubType , ty_idx : u32 ) -> Result < u32 > {
786789 let r = if !ty. is_final || !ty. supertype_idx . is_none ( ) {
787790 self . start_group ( "sub" ) ;
788791 self . print_sub_type ( state, ty) ?;
789- let r = self . print_composite ( state, & ty. composite_type , names_for ) ?;
792+ let r = self . print_composite ( state, & ty. composite_type , ty_idx ) ?;
790793 self . end_group ( ) ; // `sub`
791794 r
792795 } else {
793- self . print_composite ( state, & ty. composite_type , names_for ) ?
796+ self . print_composite ( state, & ty. composite_type , ty_idx ) ?
794797 } ;
795798 Ok ( r)
796799 }
797800
798- fn print_composite (
799- & mut self ,
800- state : & State ,
801- ty : & CompositeType ,
802- names_for : Option < u32 > ,
803- ) -> Result < u32 > {
801+ fn print_composite ( & mut self , state : & State , ty : & CompositeType , ty_idx : u32 ) -> Result < u32 > {
804802 let r = match & ty {
805803 CompositeType :: Func ( ty) => {
806804 self . start_group ( "func" ) ;
807- let r = self . print_func_type ( state, ty, names_for ) ?;
805+ let r = self . print_func_type ( state, ty, None ) ?;
808806 self . end_group ( ) ; // `func`
809807 r
810808 }
@@ -816,7 +814,7 @@ impl Printer {
816814 }
817815 CompositeType :: Struct ( ty) => {
818816 self . start_group ( "struct" ) ;
819- let r = self . print_struct_type ( state, ty) ?;
817+ let r = self . print_struct_type ( state, ty, ty_idx ) ?;
820818 self . end_group ( ) ; // `struct`
821819 r
822820 }
@@ -904,8 +902,20 @@ impl Printer {
904902 Ok ( ty. params ( ) . len ( ) as u32 )
905903 }
906904
907- fn print_field_type ( & mut self , state : & State , ty : & FieldType ) -> Result < u32 > {
905+ fn print_field_type (
906+ & mut self ,
907+ state : & State ,
908+ ty : & FieldType ,
909+ ty_field_idx : Option < ( u32 , u32 ) > ,
910+ ) -> Result < u32 > {
908911 self . result . push ( ' ' ) ;
912+ if let Some ( idxs @ ( _, field_idx) ) = ty_field_idx {
913+ match state. core . field_names . index_to_name . get ( & idxs) {
914+ Some ( name) => write ! ( self . result, "${} " , name. identifier( ) ) ?,
915+ None if self . name_unnamed => write ! ( self . result, "$#field{field_idx} " ) ?,
916+ None => { }
917+ }
918+ }
909919 if ty. mutable {
910920 self . result . push_str ( "(mut " ) ;
911921 }
@@ -917,13 +927,13 @@ impl Printer {
917927 }
918928
919929 fn print_array_type ( & mut self , state : & State , ty : & ArrayType ) -> Result < u32 > {
920- self . print_field_type ( state, & ty. 0 )
930+ self . print_field_type ( state, & ty. 0 , None )
921931 }
922932
923- fn print_struct_type ( & mut self , state : & State , ty : & StructType ) -> Result < u32 > {
924- for field in ty. fields . iter ( ) {
933+ fn print_struct_type ( & mut self , state : & State , ty : & StructType , ty_idx : u32 ) -> Result < u32 > {
934+ for ( field_index , field) in ty. fields . iter ( ) . enumerate ( ) {
925935 self . result . push_str ( " (field" ) ;
926- self . print_field_type ( state, field) ?;
936+ self . print_field_type ( state, field, Some ( ( ty_idx , field_index as u32 ) ) ) ?;
927937 self . result . push ( ')' ) ;
928938 }
929939 Ok ( 0 )
@@ -1459,6 +1469,15 @@ impl Printer {
14591469 Ok ( ( ) )
14601470 }
14611471
1472+ fn print_field_idx ( & mut self , state : & State , ty : u32 , idx : u32 ) -> Result < ( ) > {
1473+ match state. core . field_names . index_to_name . get ( & ( ty, idx) ) {
1474+ Some ( name) => write ! ( self . result, "${}" , name. identifier( ) ) ?,
1475+ None if self . name_unnamed => write ! ( self . result, "$#field{idx}" ) ?,
1476+ None => write ! ( self . result, "{}" , idx) ?,
1477+ }
1478+ Ok ( ( ) )
1479+ }
1480+
14621481 fn print_name < K > ( & mut self , names : & NamingMap < u32 , K > , cur_idx : u32 ) -> Result < ( ) >
14631482 where
14641483 K : NamingNamespace ,
@@ -3184,6 +3203,7 @@ naming_namespaces! {
31843203 struct NameTable => "table"
31853204 struct NameValue => "value"
31863205 struct NameType => "type"
3206+ struct NameField => "field"
31873207 struct NameData => "data"
31883208 struct NameElem => "elem"
31893209 struct NameComponent => "component"
0 commit comments