MCPcopy Create free account
hub / github.com/bytecodealliance/wit-bindgen / emit_custom_section

Method emit_custom_section

crates/rust/src/lib.rs:954–1050  ·  view source on GitHub ↗

Generates a `#[link_section]` custom section to get smuggled through `wasm-ld`. This custom section is an encoding of the component metadata and will be used as part of the `wit-component`-based componentization process. The `section_suffix` here is used to distinguish the multiple sections that this generator emits, and `func_name` is an optional function to generate next to this which is used

(
        &mut self,
        resolve: &Resolve,
        world_id: WorldId,
        section_suffix: &str,
        func_name: Option<&str>,
    )

Source from the content-addressed store, hash-verified

952 /// generate next to this which is used to force rustc to at least visit
953 /// this `static` and codegen it.
954 fn emit_custom_section(
955 &mut self,
956 resolve: &Resolve,
957 world_id: WorldId,
958 section_suffix: &str,
959 func_name: Option<&str>,
960 ) {
961 // As above, disable rustfmt, as we use prettyplease.
962 if self.opts.format {
963 uwriteln!(self.src, "#[rustfmt::skip]");
964 }
965 self.src.push_str("\n#[cfg(target_arch = \"wasm32\")]\n");
966
967 // The custom section name here must start with "component-type" but
968 // otherwise is attempted to be unique here to ensure that this doesn't get
969 // concatenated to other custom sections by LLD by accident since LLD will
970 // concatenate custom sections of the same name.
971 let opts_suffix = self.opts.type_section_suffix.as_deref().unwrap_or("");
972 let world = &resolve.worlds[world_id];
973 let world_name = &world.name;
974 let pkg = &resolve.packages[world.package.unwrap()].name;
975 let version = env!("CARGO_PKG_VERSION");
976 self.src.push_str(&format!(
977 "#[unsafe(link_section = \"component-type:wit-bindgen:{version}:\
978 {pkg}:{world_name}:{section_suffix}{opts_suffix}\")]\n"
979 ));
980
981 let mut producers = wasm_metadata::Producers::empty();
982 producers.add(
983 "processed-by",
984 env!("CARGO_PKG_NAME"),
985 env!("CARGO_PKG_VERSION"),
986 );
987
988 let component_type = wit_component::metadata::encode(
989 resolve,
990 world_id,
991 wit_component::StringEncoding::UTF8,
992 Some(&producers),
993 )
994 .unwrap();
995
996 self.src.push_str("#[doc(hidden)]\n");
997 self.src.push_str("#[allow(clippy::octal_escapes)]\n");
998 self.src.push_str(&format!(
999 "pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; {}] = *b\"\\\n",
1000 component_type.len()
1001 ));
1002 let old_indent = self.src.set_indent(0);
1003 let mut line_length = 0;
1004 let s = self.src.as_mut_string();
1005 for byte in component_type.iter() {
1006 if line_length >= 80 {
1007 s.push_str("\\\n");
1008 line_length = 0;
1009 }
1010 match byte {
1011 b'\\' => {

Callers 2

finish_export_macroMethod · 0.80
finishMethod · 0.80

Calls 8

set_indentMethod · 0.80
as_mut_stringMethod · 0.80
runtime_pathMethod · 0.80
push_strMethod · 0.45
addMethod · 0.45
iterMethod · 0.45
pushMethod · 0.45
to_stringMethod · 0.45

Tested by

no test coverage detected