Skip to content

Commit fa19e08

Browse files
authored
add component type WIT file to C# output (#1002)
This is to avoid the need to add binary .o files (which are difficult to audit from a security perspective) to the .NET runtime repository. I will open related PRs on the `wasm-tools` and `wasm-component-ld` repos to enable the latter to accept WIT files at link (and componentization) time. Note that I've taken this opportunity to remove the cabi_realloc C file from the output, since we can rely on `wasi-libc` providing an implementation. Signed-off-by: Joel Dice <[email protected]>
1 parent 1ff125d commit fa19e08

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/csharp/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ test = false
1919
wasm-encoder = { workspace = true }
2020
wit-bindgen-core = { workspace = true }
2121
wit-component = { workspace = true }
22+
wit-parser = { workspace = true }
2223
wasm-metadata = { workspace = true }
2324
heck = { workspace = true }
2425
clap = { workspace = true, optional = true }

crates/csharp/src/lib.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use wit_bindgen_core::{
2323
},
2424
Files, InterfaceGenerator as _, Ns, WorldGenerator,
2525
};
26-
use wit_component::StringEncoding;
26+
use wit_component::{StringEncoding, WitPrinter};
2727
mod csproj;
2828
pub use csproj::CSProject;
2929

@@ -733,6 +733,51 @@ impl WorldGenerator for CSharp {
733733
);
734734
}
735735

736+
// For the time being, we generate both a .wit file and a .o file to
737+
// represent the component type. Newer releases of the .NET runtime
738+
// will be able to use the former, but older ones will need the
739+
// latter.
740+
//
741+
// TODO: stop generating the .o file once a new-enough release is
742+
// available for us to test using only the .wit file.
743+
744+
{
745+
// When generating a WIT file, we first round-trip through the
746+
// binary encoding. This has the effect of flattening any
747+
// `include`d worlds into the specified world and excluding
748+
// unrelated worlds, ensuring the output WIT contains no extra
749+
// information beyond what the binary representation contains.
750+
//
751+
// This is important because including more than one world in
752+
// the output would make it ambigious, and since this file is
753+
// intended to be used non-interactively at link time, the
754+
// linker will have no additional information to resolve such
755+
// ambiguity.
756+
let (resolve, _) =
757+
wit_parser::decoding::decode_world(&wit_component::metadata::encode(
758+
&resolve,
759+
id,
760+
self.opts.string_encoding,
761+
None,
762+
)?)?;
763+
764+
files.push(
765+
&format!("{world_namespace}_component_type.wit"),
766+
WitPrinter::default()
767+
.emit_docs(false)
768+
.print(
769+
&resolve,
770+
&resolve
771+
.packages
772+
.iter()
773+
.map(|(id, _)| id)
774+
.collect::<Vec<_>>(),
775+
false,
776+
)?
777+
.as_bytes(),
778+
);
779+
}
780+
736781
files.push(
737782
&format!("{world_namespace}_component_type.o",),
738783
component_type_object::object(resolve, id, self.opts.string_encoding)

0 commit comments

Comments
 (0)