@@ -62,6 +62,11 @@ pub struct Opts {
6262 /// Whether stubs/declarations for exports should be emitted
6363 /// Only for testing purposes.
6464 emit_export_stubs : bool ,
65+
66+ /// Add the specified suffix to the name of the custome section containing
67+ /// the component type.
68+ #[ cfg_attr( feature = "clap" , arg( long, value_name = "STRING" ) ) ]
69+ pub type_section_suffix : Option < String > ,
6570}
6671
6772impl Opts {
@@ -814,6 +819,55 @@ impl WorldGenerator for D {
814819 world_src. append_src ( & self . export_stubs_src ) ;
815820 }
816821
822+ // Linker `component-type` section
823+ {
824+ let opts_suffix = self . opts . type_section_suffix . as_deref ( ) . unwrap_or ( "" ) ;
825+ let world = & resolve. worlds [ world_id] ;
826+ let world_name = & world. name ;
827+ let pkg = & resolve. packages [ world. package . unwrap ( ) ] . name ;
828+ let version = env ! ( "CARGO_PKG_VERSION" ) ;
829+ world_src. push_str ( & format ! (
830+ "\n @(imported!\" ldc.attributes\" .section(\" component-type:wit-bindgen:{version}:\
831+ {pkg}:{world_name}:{opts_suffix}\" ))\n "
832+ ) ) ;
833+
834+ let mut producers = wasm_metadata:: Producers :: empty ( ) ;
835+ producers. add (
836+ "processed-by" ,
837+ env ! ( "CARGO_PKG_NAME" ) ,
838+ env ! ( "CARGO_PKG_VERSION" ) ,
839+ ) ;
840+
841+ let component_type = wit_component:: metadata:: encode (
842+ resolve,
843+ world_id,
844+ wit_component:: StringEncoding :: UTF8 ,
845+ Some ( & producers) ,
846+ )
847+ . unwrap ( ) ;
848+
849+ world_src. push_str ( & format ! (
850+ "
851+ void __wit_bindgen_component_types() {{
852+ imported!\" ldc.llvmasm\" .__irEx!(
853+ \" \" ,
854+ \" \" ,
855+ `!wasm.custom_sections = !{{!0}}
856+ !0 = !{{!\" component-type:wit-bindgen:{version}:{pkg}:{world_name}:{opts_suffix}\" , !\" {}\" }}`,
857+ void
858+ );
859+ }}
860+ " ,
861+ & component_type
862+ . iter( )
863+ . map( |b| format!( "\\ {b:02X}" ) )
864+ . enumerate( )
865+ . fold( String :: default ( ) , |a, ( i, b) | {
866+ if ( i % 24 ) == 0 { a + "`\n ~`" + & b } else { a + & b }
867+ } )
868+ ) ) ;
869+ }
870+
817871 let mut world_filepath = PathBuf :: from_iter ( get_world_fqn ( world_id, resolve) . split ( "." ) ) ;
818872 world_filepath. push ( "package.d" ) ;
819873
@@ -1658,7 +1712,7 @@ impl<'a> InterfaceGenerator<'a> for DInterfaceGenerator<'a> {
16581712 self . src . push_str ( & format ! (
16591713 "// TODO: make RAII? disable copy for the own
16601714
1661- auto borrow() => Borrow(__handle);
1715+ Borrow borrow() => Borrow(__handle);
16621716 alias borrow this;
16631717
16641718 struct Borrow {{
@@ -1810,7 +1864,7 @@ impl<'a> InterfaceGenerator<'a> for DInterfaceGenerator<'a> {
18101864
18111865 self . src . push_str ( & format ! (
18121866 "// TODO: make RAII? disable copy for the own
1813- auto borrow() => Borrow(__handle);
1867+ Borrow borrow() => Borrow(__handle);
18141868 alias borrow this;
18151869
18161870 struct Borrow {{
0 commit comments