@@ -143,7 +143,7 @@ pub(crate) fn encode(
143143
144144 e. custom_sections ( BeforeFirst ) ;
145145
146- e. section_list ( SectionId :: Type , Type , & types) ;
146+ e. typed_section ( & types) ;
147147 e. section_list ( SectionId :: Import , Import , & imports) ;
148148
149149 let functys = funcs. iter ( ) . map ( |f| & f. ty ) . collect :: < Vec < _ > > ( ) ;
@@ -248,6 +248,21 @@ impl Encoder<'_> {
248248 self . custom_sections ( CustomPlace :: After ( anchor) ) ;
249249 }
250250
251+ fn typed_section < T > ( & mut self , list : & [ T ] )
252+ where
253+ T : SectionItem ,
254+ {
255+ self . custom_sections ( CustomPlace :: Before ( T :: ANCHOR ) ) ;
256+ if !list. is_empty ( ) {
257+ let mut section = T :: Section :: default ( ) ;
258+ for item in list {
259+ item. encode ( & mut section) ;
260+ }
261+ self . wasm . section ( & section) ;
262+ }
263+ self . custom_sections ( CustomPlace :: After ( T :: ANCHOR ) ) ;
264+ }
265+
251266 /// Encodes the code section of a wasm module module while additionally
252267 /// handling the branch hinting proposal.
253268 ///
@@ -302,14 +317,11 @@ impl Encoder<'_> {
302317 }
303318}
304319
305- impl Encode for FunctionType < ' _ > {
306- fn encode ( & self , e : & mut Vec < u8 > ) {
307- self . params . len ( ) . encode ( e) ;
308- for ( _, _, ty) in self . params . iter ( ) {
309- ty. encode ( e) ;
310- }
311- self . results . encode ( e) ;
312- }
320+ trait SectionItem {
321+ type Section : wasm_encoder:: Section + Default ;
322+ const ANCHOR : CustomPlaceAnchor ;
323+
324+ fn encode ( & self , section : & mut Self :: Section ) ;
313325}
314326
315327impl From < & FunctionType < ' _ > > for wasm_encoder:: FuncType {
@@ -321,16 +333,6 @@ impl From<&FunctionType<'_>> for wasm_encoder::FuncType {
321333 }
322334}
323335
324- impl Encode for StructType < ' _ > {
325- fn encode ( & self , e : & mut Vec < u8 > ) {
326- self . fields . len ( ) . encode ( e) ;
327- for field in self . fields . iter ( ) {
328- field. ty . encode ( e) ;
329- ( field. mutable as i32 ) . encode ( e) ;
330- }
331- }
332- }
333-
334336impl From < & StructType < ' _ > > for wasm_encoder:: StructType {
335337 fn from ( st : & StructType ) -> wasm_encoder:: StructType {
336338 wasm_encoder:: StructType {
@@ -348,13 +350,6 @@ impl From<&StructField<'_>> for wasm_encoder::FieldType {
348350 }
349351}
350352
351- impl Encode for ArrayType < ' _ > {
352- fn encode ( & self , e : & mut Vec < u8 > ) {
353- self . ty . encode ( e) ;
354- ( self . mutable as i32 ) . encode ( e) ;
355- }
356- }
357-
358353impl From < & ArrayType < ' _ > > for wasm_encoder:: ArrayType {
359354 fn from ( at : & ArrayType ) -> Self {
360355 let field = wasm_encoder:: FieldType {
@@ -377,74 +372,38 @@ enum RecOrType<'a> {
377372 Rec ( & ' a Rec < ' a > ) ,
378373}
379374
380- impl Encode for RecOrType < ' _ > {
381- fn encode ( & self , e : & mut Vec < u8 > ) {
375+ impl SectionItem for RecOrType < ' _ > {
376+ type Section = wasm_encoder:: TypeSection ;
377+ const ANCHOR : CustomPlaceAnchor = CustomPlaceAnchor :: Type ;
378+
379+ fn encode ( & self , types : & mut wasm_encoder:: TypeSection ) {
382380 match self {
383- RecOrType :: Type ( ty) => ty . encode ( e ) ,
384- RecOrType :: Rec ( rec) => rec. encode ( e ) ,
381+ RecOrType :: Type ( ty) => types . ty ( ) . subtype ( & ty . to_subtype ( ) ) ,
382+ RecOrType :: Rec ( rec) => types . ty ( ) . rec ( rec . types . iter ( ) . map ( |t| t . to_subtype ( ) ) ) ,
385383 }
386384 }
387385}
388386
389- impl Encode for Type < ' _ > {
390- fn encode ( & self , e : & mut Vec < u8 > ) {
391- match ( & self . parent , self . final_type ) {
392- ( Some ( parent) , Some ( true ) ) => {
393- // Type is final with a supertype
394- e. push ( 0x4f ) ;
395- e. push ( 0x01 ) ;
396- parent. encode ( e) ;
397- }
398- ( Some ( parent) , Some ( false ) | None ) => {
399- // Type is not final and has a declared supertype
400- e. push ( 0x50 ) ;
401- e. push ( 0x01 ) ;
402- parent. encode ( e) ;
403- }
404- ( None , Some ( false ) ) => {
405- // Sub was used without any declared supertype
406- e. push ( 0x50 ) ;
407- e. push ( 0x00 ) ;
408- }
409- ( None , _) => { } // No supertype, sub wasn't used
410- }
411- if self . def . shared {
412- e. push ( 0x65 ) ;
413- }
414- match & self . def . kind {
415- InnerTypeKind :: Func ( func) => {
416- e. push ( 0x60 ) ;
417- func. encode ( e)
418- }
419- InnerTypeKind :: Struct ( r#struct) => {
420- e. push ( 0x5f ) ;
421- r#struct. encode ( e)
422- }
423- InnerTypeKind :: Array ( array) => {
424- e. push ( 0x5e ) ;
425- array. encode ( e)
426- }
387+ impl Type < ' _ > {
388+ pub ( crate ) fn to_subtype ( & self ) -> wasm_encoder:: SubType {
389+ wasm_encoder:: SubType {
390+ composite_type : self . def . to_composite_type ( ) ,
391+ is_final : self . final_type . unwrap_or ( true ) ,
392+ supertype_idx : self . parent . map ( |i| i. unwrap_u32 ( ) ) ,
427393 }
428394 }
429395}
430396
431- impl From < & InnerTypeKind < ' _ > > for wasm_encoder :: CompositeInnerType {
432- fn from ( kind : & InnerTypeKind ) -> Self {
397+ impl TypeDef < ' _ > {
398+ pub ( crate ) fn to_composite_type ( & self ) -> wasm_encoder :: CompositeType {
433399 use wasm_encoder:: CompositeInnerType :: * ;
434- match kind {
435- InnerTypeKind :: Func ( ft) => Func ( ft. into ( ) ) ,
436- InnerTypeKind :: Struct ( st) => Struct ( st. into ( ) ) ,
437- InnerTypeKind :: Array ( at) => Array ( at. into ( ) ) ,
438- }
439- }
440- }
441-
442- impl Encode for Rec < ' _ > {
443- fn encode ( & self , e : & mut Vec < u8 > ) {
444- e. push ( 0x4e ) ;
445- self . types . len ( ) . encode ( e) ;
446- for ty in & self . types {
447- ty. encode ( e) ;
400+ wasm_encoder:: CompositeType {
401+ inner : match & self . kind {
402+ InnerTypeKind :: Func ( ft) => Func ( ft. into ( ) ) ,
403+ InnerTypeKind :: Struct ( st) => Struct ( st. into ( ) ) ,
404+ InnerTypeKind :: Array ( at) => Array ( at. into ( ) ) ,
405+ } ,
406+ shared : self . shared ,
448407 }
449408 }
450409}
@@ -542,18 +501,6 @@ impl<'a> Encode for RefType<'a> {
542501 }
543502}
544503
545- impl < ' a > Encode for StorageType < ' a > {
546- fn encode ( & self , e : & mut Vec < u8 > ) {
547- match self {
548- StorageType :: I8 => e. push ( 0x78 ) ,
549- StorageType :: I16 => e. push ( 0x77 ) ,
550- StorageType :: Val ( ty) => {
551- ty. encode ( e) ;
552- }
553- }
554- }
555- }
556-
557504impl From < StorageType < ' _ > > for wasm_encoder:: StorageType {
558505 fn from ( st : StorageType ) -> Self {
559506 use wasm_encoder:: StorageType :: * ;
0 commit comments