Skip to content

Commit f97c9d2

Browse files
committed
fix(csharp): force-link component-type object for NativeAOT
Signed-off-by: Yordis Prieto <[email protected]>
1 parent af8b7cb commit f97c9d2

2 files changed

Lines changed: 11 additions & 32 deletions

File tree

crates/csharp/src/csproj.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ impl CSProjectLLVMBuilder {
9494
</ItemGroup>
9595
9696
<ItemGroup>
97-
<CustomLinkerArg Include=\"-Wl,--component-type,{camel}_component_type.wasm\" />
97+
<NativeFileReference Include=\"{camel}_component_type.o\" />
98+
<CustomLinkerArg Include=\"-Wl,--undefined=__component_type_object_force_link_{world}\" />
9899
</ItemGroup>
99100
"
100101
);

crates/csharp/src/world_generator.rs

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -813,37 +813,15 @@ impl WorldGenerator for CSharp {
813813
);
814814
}
815815

816-
// For the time being, we generate both a .wit file and a .o file to
817-
// represent the component type. Newer releases of the .NET runtime
818-
// will be able to use the former, but older ones will need the
819-
// latter.
820-
//
821-
// TODO: stop generating the .o file once a new-enough release is
822-
// available for us to test using only the .wit file.
823-
824-
{
825-
// When generating a WIT file, we first round-trip through the
826-
// binary encoding. This has the effect of flattening any
827-
// `include`d worlds into the specified world and excluding
828-
// unrelated worlds, ensuring the output WIT contains no extra
829-
// information beyond what the binary representation contains.
830-
//
831-
// This is important because including more than one world in
832-
// the output would make it ambigious, and since this file is
833-
// intended to be used non-interactively at link time, the
834-
// linker will have no additional information to resolve such
835-
// ambiguity.
836-
let (resolve, world) = wit_parser::decoding::decode_world(
837-
&wit_component::metadata::encode(resolve, id, self.opts.string_encoding, None)?,
838-
)?;
839-
let pkg = resolve.worlds[world].package.unwrap();
840-
841-
files.push(
842-
&format!("{world_namespace}_component_type.wasm"),
843-
wit_component::encode(&resolve, pkg)?.as_slice(),
844-
);
845-
}
846-
816+
// Embed component-type metadata as a wasm object file with a
817+
// `component-type:<world>` custom section and a linking symbol.
818+
// The symbol is force-linked via a `--undefined` linker arg in the
819+
// generated csproj so the object is retained after GC, which makes
820+
// the custom section available to `wasm-component-ld` for
821+
// component synthesis. This path is independent of the bundled
822+
// `wasm-component-ld`'s WIT parser and therefore works for WIT
823+
// features (e.g. `map`) that the bundled version does not yet
824+
// understand when fed a textual or binary WIT package.
847825
files.push(
848826
&format!("{world_namespace}_component_type.o"),
849827
wit_bindgen_c::component_type_object::object(

0 commit comments

Comments
 (0)