Skip to content

Commit 2c40116

Browse files
Fix large component type embeddings (#1704)
1 parent 8c082d0 commit 2c40116

1 file changed

Lines changed: 34 additions & 9 deletions

File tree

crates/d/src/lib.rs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,38 @@ impl WorldGenerator for D {
903903
)
904904
.unwrap();
905905

906+
world_src.push_str(&format!(
907+
"private enum componentTypeBytes = x\"{}\n\";\n",
908+
&component_type
909+
.iter()
910+
.map(|b| format!("{b:02X}"))
911+
.enumerate()
912+
.fold(String::default(), |a, (i, b)| {
913+
if (i % 39) == 0 { a + "\n" + &b } else { a + &b }
914+
})
915+
));
916+
917+
world_src.push_str(
918+
"
919+
private enum componentTypeStr = (() {
920+
immutable input = componentTypeBytes;
921+
auto result = new char[input.length*3];
922+
923+
foreach (i, b; input) {
924+
result[(i*3)+0] = '\\\\';
925+
926+
ubyte n1 = (b >> 4) & 0xF;
927+
result[(i*3)+1] = n1 < 0xA ? cast(char)('0'+n1) : cast(char)('A'+(n1-0xA));
928+
929+
ubyte n2 = b & 0xF;
930+
result[(i*3)+2] = n2 < 0xA ? cast(char)('0'+n2) : cast(char)('A'+(n2-0xA));
931+
}
932+
933+
return cast(string)result;
934+
})();
935+
",
936+
);
937+
906938
world_src.push_str(&format!(
907939
"
908940
pragma(inline, false)
@@ -914,19 +946,12 @@ impl WorldGenerator for D {
914946
\"\",
915947
\"\",
916948
`!wasm.custom_sections = !{{!0}}
917-
!0 = !{{!\"component-type:wit-bindgen:{version}:{pkg}:{world_name}:{opts_suffix}\", !\"{}\"}}`,
949+
!0 = !{{!\"component-type:wit-bindgen:{version}:{pkg}:{world_name}:{opts_suffix}\", !\"`~componentTypeStr~`\"}}`,
918950
void
919951
);
920952
}}
921953
",
922-
self.root_pkg,
923-
&component_type
924-
.iter()
925-
.map(|b| format!("\\{b:02X}"))
926-
.enumerate()
927-
.fold(String::default(), |a, (i, b)| {
928-
if (i % 24) == 0 { a + "`\n~`" + &b } else { a + &b }
929-
})
954+
self.root_pkg
930955
));
931956
}
932957

0 commit comments

Comments
 (0)