Skip to content

Commit 5dc028a

Browse files
Component type custom section
1 parent eedded8 commit 5dc028a

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

crates/d/src/lib.rs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6772
impl 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 {{

crates/d/src/wit_common.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ auto none(T)(T value) @safe @nogc nothrow {
178178

179179
/// Based on Rust's Result
180180
@mustuse
181-
struct Result(T, E) {
181+
struct Result(T = void, E = void) {
182182
private:
183183
bool _hasError;
184184
union Storage {

0 commit comments

Comments
 (0)