@@ -165,14 +165,7 @@ struct State {
165165 core : CoreState ,
166166 #[ cfg( feature = "component-model" ) ]
167167 component : ComponentState ,
168- custom_section_place : Option < ( & ' static str , usize ) > ,
169- // `custom_section_place` stores the text representation of the location where
170- // a custom section should be serialized in the binary format.
171- // The tuple elements are a str (e.g. "after elem") and the line number
172- // where the custom section place was set. `update_custom_section_place` won't
173- // update the custom section place unless the line number changes; this prevents
174- // printing a place "after xxx" where the xxx section doesn't appear in the text format
175- // (e.g. because it was present but empty in the binary format).
168+ custom_section_place : Option < & ' static str > ,
176169}
177170
178171impl State {
@@ -485,8 +478,7 @@ impl Printer<'_, '_> {
485478 match encoding {
486479 Encoding :: Module => {
487480 states. push ( State :: new ( Encoding :: Module ) ) ;
488- states. last_mut ( ) . unwrap ( ) . custom_section_place =
489- Some ( ( "before first" , self . line ) ) ;
481+ states. last_mut ( ) . unwrap ( ) . custom_section_place = Some ( "before first" ) ;
490482 if states. len ( ) > 1 {
491483 self . start_group ( "core module" ) ?;
492484 } else {
@@ -546,7 +538,6 @@ impl Printer<'_, '_> {
546538 self . result
547539 . print_custom_section ( c. name ( ) , c. data_offset ( ) , c. data ( ) ) ?;
548540 if printed {
549- self . update_custom_section_line ( & mut states) ;
550541 continue ;
551542 }
552543
@@ -589,16 +580,19 @@ impl Printer<'_, '_> {
589580 }
590581 }
591582 assert ! ( self . nesting == start) ;
592- self . update_custom_section_line ( & mut states) ;
593583 }
594584 Payload :: TypeSection ( s) => {
585+ if s. count ( ) > 0 {
586+ self . update_custom_section_place ( & mut states, "after type" ) ;
587+ }
595588 self . print_types ( states. last_mut ( ) . unwrap ( ) , s) ?;
596- self . update_custom_section_place ( & mut states, "after type" ) ;
597589 }
598590 Payload :: ImportSection ( s) => {
599591 Self :: ensure_module ( & states) ?;
592+ if s. count ( ) > 0 {
593+ self . update_custom_section_place ( & mut states, "after import" ) ;
594+ }
600595 self . print_imports ( states. last_mut ( ) . unwrap ( ) , s) ?;
601- self . update_custom_section_place ( & mut states, "after import" ) ;
602596 }
603597 Payload :: FunctionSection ( reader) => {
604598 Self :: ensure_module ( & states) ?;
@@ -609,35 +603,47 @@ impl Printer<'_, '_> {
609603 MAX_WASM_FUNCTIONS
610604 ) ;
611605 }
606+ if reader. count ( ) > 0 {
607+ self . update_custom_section_place ( & mut states, "after func" ) ;
608+ }
612609 for ty in reader {
613610 states. last_mut ( ) . unwrap ( ) . core . func_to_type . push ( Some ( ty?) )
614611 }
615- self . update_custom_section_place ( & mut states, "after func" ) ;
616612 }
617613 Payload :: TableSection ( s) => {
618614 Self :: ensure_module ( & states) ?;
615+ if s. count ( ) > 0 {
616+ self . update_custom_section_place ( & mut states, "after table" ) ;
617+ }
619618 self . print_tables ( states. last_mut ( ) . unwrap ( ) , s) ?;
620- self . update_custom_section_place ( & mut states, "after table" ) ;
621619 }
622620 Payload :: MemorySection ( s) => {
623621 Self :: ensure_module ( & states) ?;
622+ if s. count ( ) > 0 {
623+ self . update_custom_section_place ( & mut states, "after memory" ) ;
624+ }
624625 self . print_memories ( states. last_mut ( ) . unwrap ( ) , s) ?;
625- self . update_custom_section_place ( & mut states, "after memory" ) ;
626626 }
627627 Payload :: TagSection ( s) => {
628628 Self :: ensure_module ( & states) ?;
629+ if s. count ( ) > 0 {
630+ self . update_custom_section_place ( & mut states, "after tag" ) ;
631+ }
629632 self . print_tags ( states. last_mut ( ) . unwrap ( ) , s) ?;
630- self . update_custom_section_place ( & mut states, "after tag" ) ;
631633 }
632634 Payload :: GlobalSection ( s) => {
633635 Self :: ensure_module ( & states) ?;
636+ if s. count ( ) > 0 {
637+ self . update_custom_section_place ( & mut states, "after global" ) ;
638+ }
634639 self . print_globals ( states. last_mut ( ) . unwrap ( ) , s) ?;
635- self . update_custom_section_place ( & mut states, "after global" ) ;
636640 }
637641 Payload :: ExportSection ( s) => {
638642 Self :: ensure_module ( & states) ?;
643+ if s. count ( ) > 0 {
644+ self . update_custom_section_place ( & mut states, "after export" ) ;
645+ }
639646 self . print_exports ( states. last ( ) . unwrap ( ) , s) ?;
640- self . update_custom_section_place ( & mut states, "after export" ) ;
641647 }
642648 Payload :: StartSection { func, range } => {
643649 Self :: ensure_module ( & states) ?;
@@ -649,8 +655,10 @@ impl Printer<'_, '_> {
649655 }
650656 Payload :: ElementSection ( s) => {
651657 Self :: ensure_module ( & states) ?;
658+ if s. count ( ) > 0 {
659+ self . update_custom_section_place ( & mut states, "after elem" ) ;
660+ }
652661 self . print_elems ( states. last_mut ( ) . unwrap ( ) , s) ?;
653- self . update_custom_section_place ( & mut states, "after elem" ) ;
654662 }
655663 Payload :: CodeSectionStart { .. } => {
656664 Self :: ensure_module ( & states) ?;
@@ -669,8 +677,10 @@ impl Printer<'_, '_> {
669677 }
670678 Payload :: DataSection ( s) => {
671679 Self :: ensure_module ( & states) ?;
680+ if s. count ( ) > 0 {
681+ self . update_custom_section_place ( & mut states, "after data" ) ;
682+ }
672683 self . print_data ( states. last_mut ( ) . unwrap ( ) , s) ?;
673- self . update_custom_section_place ( & mut states, "after data" ) ;
674684 }
675685
676686 #[ cfg( feature = "component-model" ) ]
@@ -774,19 +784,8 @@ impl Printer<'_, '_> {
774784
775785 fn update_custom_section_place ( & self , states : & mut Vec < State > , place : & ' static str ) {
776786 if let Some ( last) = states. last_mut ( ) {
777- if let Some ( ( prev, prev_line) ) = & mut last. custom_section_place {
778- if * prev_line != self . line {
779- * prev = place;
780- * prev_line = self . line ;
781- }
782- }
783- }
784- }
785-
786- fn update_custom_section_line ( & self , states : & mut Vec < State > ) {
787- if let Some ( last) = states. last_mut ( ) {
788- if let Some ( ( _, prev_line) ) = & mut last. custom_section_place {
789- * prev_line = self . line ;
787+ if let Some ( prev) = & mut last. custom_section_place {
788+ * prev = place;
790789 }
791790 }
792791 }
@@ -1997,7 +1996,7 @@ impl Printer<'_, '_> {
19971996 self . newline ( section. range ( ) . start ) ?;
19981997 self . start_group ( "@custom " ) ?;
19991998 self . print_str ( section. name ( ) ) ?;
2000- if let Some ( ( place, _ ) ) = state. custom_section_place {
1999+ if let Some ( place) = state. custom_section_place {
20012000 write ! ( self . result, " ({place})" ) ?;
20022001 }
20032002 self . result . write_str ( " " ) ?;
0 commit comments