@@ -27,6 +27,7 @@ struct InterfaceName {
27
27
#[ derive( Default ) ]
28
28
struct RustWasm {
29
29
types : Types ,
30
+ src_preamble : Source ,
30
31
src : Source ,
31
32
opts : Opts ,
32
33
import_modules : Vec < ( String , Vec < String > ) > ,
@@ -837,44 +838,90 @@ macro_rules! __export_{world_name}_impl {{
837
838
838
839
impl WorldGenerator for RustWasm {
839
840
fn preprocess ( & mut self , resolve : & Resolve , world : WorldId ) {
840
- wit_bindgen_core:: generated_preamble ( & mut self . src , env ! ( "CARGO_PKG_VERSION" ) ) ;
841
+ wit_bindgen_core:: generated_preamble ( & mut self . src_preamble , env ! ( "CARGO_PKG_VERSION" ) ) ;
841
842
842
843
// Render some generator options to assist with debugging and/or to help
843
844
// recreate it if the original generation command is lost.
844
- uwriteln ! ( self . src , "// Options used:" ) ;
845
+ uwriteln ! ( self . src_preamble , "// Options used:" ) ;
845
846
if self . opts . std_feature {
846
- uwriteln ! ( self . src , "// * std_feature" ) ;
847
+ uwriteln ! ( self . src_preamble , "// * std_feature" ) ;
847
848
}
848
849
if self . opts . raw_strings {
849
- uwriteln ! ( self . src , "// * raw_strings" ) ;
850
+ uwriteln ! ( self . src_preamble , "// * raw_strings" ) ;
850
851
}
851
852
if !self . opts . skip . is_empty ( ) {
852
- uwriteln ! ( self . src, "// * skip: {:?}" , self . opts. skip) ;
853
+ uwriteln ! ( self . src_preamble, "// * skip: {:?}" , self . opts. skip) ;
854
+ }
855
+ if self . opts . stubs {
856
+ uwriteln ! ( self . src_preamble, "// * stubs" ) ;
857
+ }
858
+ if let Some ( export_prefix) = & self . opts . export_prefix {
859
+ uwriteln ! (
860
+ self . src_preamble,
861
+ "// * export_prefix: {:?}" ,
862
+ export_prefix
863
+ ) ;
864
+ }
865
+ if let Some ( runtime_path) = & self . opts . runtime_path {
866
+ uwriteln ! ( self . src_preamble, "// * runtime_path: {:?}" , runtime_path) ;
867
+ }
868
+ if let Some ( bitflags_path) = & self . opts . bitflags_path {
869
+ uwriteln ! (
870
+ self . src_preamble,
871
+ "// * bitflags_path: {:?}" ,
872
+ bitflags_path
873
+ ) ;
853
874
}
854
875
if !matches ! ( self . opts. ownership, Ownership :: Owning ) {
855
- uwriteln ! ( self . src, "// * ownership: {:?}" , self . opts. ownership) ;
876
+ uwriteln ! (
877
+ self . src_preamble,
878
+ "// * ownership: {:?}" ,
879
+ self . opts. ownership
880
+ ) ;
856
881
}
857
882
if !self . opts . additional_derive_attributes . is_empty ( ) {
858
883
uwriteln ! (
859
- self . src ,
884
+ self . src_preamble ,
860
885
"// * additional derives {:?}" ,
861
886
self . opts. additional_derive_attributes
862
887
) ;
863
888
}
864
889
for ( k, v) in self . opts . with . iter ( ) {
865
- uwriteln ! ( self . src, "// * with {k:?} = {v}" ) ;
890
+ uwriteln ! ( self . src_preamble, "// * with {k:?} = {v}" ) ;
891
+ }
892
+ if let Some ( type_section_suffix) = & self . opts . type_section_suffix {
893
+ uwriteln ! (
894
+ self . src_preamble,
895
+ "// * type_section_suffix: {:?}" ,
896
+ type_section_suffix
897
+ ) ;
866
898
}
867
899
if let Some ( default) = & self . opts . default_bindings_module {
868
- uwriteln ! ( self . src, "// * default-bindings-module: {default:?}" ) ;
900
+ uwriteln ! (
901
+ self . src_preamble,
902
+ "// * default-bindings-module: {default:?}"
903
+ ) ;
869
904
}
870
905
if self . opts . disable_run_ctors_once_workaround {
871
- uwriteln ! ( self . src, "// * disable-run-ctors-once-workaround" ) ;
906
+ uwriteln ! (
907
+ self . src_preamble,
908
+ "// * disable-run-ctors-once-workaround"
909
+ ) ;
872
910
}
873
911
if let Some ( s) = & self . opts . export_macro_name {
874
- uwriteln ! ( self . src , "// * export-macro-name: {s}" ) ;
912
+ uwriteln ! ( self . src_preamble , "// * export-macro-name: {s}" ) ;
875
913
}
876
914
if self . opts . pub_export_macro {
877
- uwriteln ! ( self . src, "// * pub-export-macro" ) ;
915
+ uwriteln ! ( self . src_preamble, "// * pub-export-macro" ) ;
916
+ }
917
+ if self . opts . generate_unused_types {
918
+ uwriteln ! ( self . src_preamble, "// * generate_unused_types" ) ;
919
+ }
920
+ if self . opts . disable_custom_section_link_helpers {
921
+ uwriteln ! (
922
+ self . src_preamble,
923
+ "// * disable_custom_section_link_helpers"
924
+ ) ;
878
925
}
879
926
self . types . analyze ( resolve) ;
880
927
self . world = Some ( world) ;
@@ -1101,6 +1148,11 @@ impl WorldGenerator for RustWasm {
1101
1148
* src. as_mut_string ( ) = prettyplease:: unparse ( & syntax_tree) ;
1102
1149
}
1103
1150
1151
+ // Prepend the preamble. We do this after formatting because
1152
+ // `syn::parse_file` + `prettyplease::unparse` does not preserve comments.
1153
+ let src_preamble = mem:: take ( & mut self . src_preamble ) ;
1154
+ * src. as_mut_string ( ) = format ! ( "{}{}" , src_preamble. as_str( ) , src. as_str( ) ) ;
1155
+
1104
1156
let module_name = name. to_snake_case ( ) ;
1105
1157
files. push ( & format ! ( "{module_name}.rs" ) , src. as_bytes ( ) ) ;
1106
1158
0 commit comments