Repository navigation
Add crate for host-side .net component bindings #1946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ludfjig
wants to merge
7
commits into
main
Choose a base branch
from
component-wire-format
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c084005
Run hyperlight-component-util unit tests in just test
ludfjig c50e353
Name component functions by instance and encode little-endian
ludfjig b6c999c
Document the component wire format
ludfjig 4706b4e
Cover more WIT features in witguest
ludfjig 73fb2d5
Let other bindings generators reuse emit::State
ludfjig 2a91ae6
Add hyperlight-component-csharp
ludfjig 616d788
Test generated C# bindings against witguest
ludfjig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Component wire format | ||
|
|
||
| Bindings generated from WIT exchange values in this format. It is used by | ||
| `host_bindgen!`, `guest_bindgen!`, the hyperlight-wasm guest runtime, and the | ||
| C# generator in | ||
| [`hyperlight-component-csharp`](../src/hyperlight_component_csharp). The host | ||
| and guest must be built from the same Hyperlight version. | ||
|
|
||
| ## Calls | ||
|
|
||
| A component function is a Hyperlight guest or host function. Each WIT | ||
| parameter is a separate `VecBytes` argument holding the encoded value. The | ||
| result is one `VecBytes` value. A function without a result returns an empty | ||
| buffer. | ||
|
|
||
| ## Function names | ||
|
|
||
| | Function | Wire name | Example | | ||
| |---|---|---| | ||
| | Member of an imported or exported instance | `<instance>#<function>` | `wasi:http/types@0.2.0#[method]fields.get` | | ||
| | Imported or exported by the world itself | `<function>` | `run` | | ||
|
|
||
| `<instance>` and `<function>` are the import and export names in the | ||
| component type, including package versions and resource method prefixes. | ||
|
|
||
| ## Values | ||
|
|
||
| Integers, floats and lengths are little-endian. Compound values concatenate | ||
| their parts with no alignment. | ||
|
|
||
| | WIT type | Encoding | | ||
| |---|---| | ||
| | `bool` | 1 byte, `0` or `1` | | ||
| | `s8`, `u8` | 1 byte | | ||
| | `s16`, `u16` | 2 bytes | | ||
| | `s32`, `u32`, `f32` | 4 bytes | | ||
| | `s64`, `u64`, `f64` | 8 bytes | | ||
| | `char` | Unicode scalar value as `u32` | | ||
| | `string` | `u32` byte length, then UTF-8 bytes | | ||
| | `list<T>` | `u32` element count, then each element | | ||
| | `list<T, N>` | `N` elements | | ||
| | `record`, `tuple` | Fields in declaration order | | ||
| | `option<T>` | `u8` tag. `0` is `none`. `1` is `some`, followed by the payload | | ||
| | `result<T, E>` | `u8` tag. `0` is `ok` and `1` is `err`, followed by the payload if the case has a type | | ||
| | `variant` | `u32` case index, followed by the payload if the case has a type | | ||
| | `enum` | `u32` case index | | ||
| | `flags` | Bitset of `ceil(n / 8)` bytes. Flag `i` is bit `i % 8` of byte `i / 8`. Other bits are zero | | ||
| | `own<R>`, `borrow<R>` | `u32` handle | | ||
|
|
||
| Receivers read a nonzero `bool`, `option` tag or `result` tag as `true`, | ||
| `some` or `err`. They ignore unused flag bits and trailing bytes. | ||
|
|
||
| ## Resources | ||
|
|
||
| Resources are defined by the host. A handle indexes the host's table for the | ||
| resource type. Passing `own<R>` to the guest adds the value to the table, and | ||
| passing `borrow<R>` adds it until the call returns. The guest returns a handle | ||
| to give the value back or to lend it to a host method. Resources defined by the | ||
| guest are not supported. | ||
|
|
||
| ## Tests | ||
|
|
||
| The Rust and C# host bindings are both tested against | ||
| [`witguest`](../src/tests/rust_guests/witguest), which implements | ||
| [`guest.wit`](../src/tests/rust_guests/witguest/guest.wit). | ||
| [`edge.wit`](../src/tests/csharp_host/edge.wit) covers C# naming cases. After | ||
| `just guests`, `just test-csharp-bindings` runs the C# tests. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| [package] | ||
| name = "hyperlight-component-csharp" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| rust-version.workspace = true | ||
| license.workspace = true | ||
| homepage.workspace = true | ||
| repository.workspace = true | ||
| readme = "README.md" | ||
| description = """ | ||
| Experimental. Generates C# host bindings for Hyperlight components from WIT worlds | ||
| """ | ||
|
|
||
| [lib] | ||
| name = "hyperlight_component_csharp" | ||
|
|
||
| [dependencies] | ||
| hyperlight-component-util = { workspace = true } | ||
|
|
||
| [dev-dependencies] | ||
| proc-macro2 = { version = "1.0.106" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # hyperlight-component-csharp | ||
|
|
||
| > **Warning:** This crate is experimental. Its API and the generated C# can | ||
| > change in any release. | ||
|
|
||
| Generates C# host bindings for a WIT world. A .NET host uses them to call a | ||
| Hyperlight component guest and to serve its imports. | ||
|
|
||
| hyperlight-wasm's .NET packages use this crate to generate bindings at build | ||
| time. Hyperlight itself has no .NET host. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```rust | ||
| let cs = hyperlight_component_csharp::generate_from_path( | ||
| Path::new("wit/world.wit"), | ||
| None, // world name, or None for the last world | ||
| "MyApp.Guest", // root C# namespace | ||
| )?; | ||
| std::fs::write("Bindings.g.cs", cs)?; | ||
| ``` | ||
|
|
||
| `generate` takes a `hyperlight_component_util::util::WitSource`, the input | ||
| type of the bindgen macros. It reads the world through the same component type | ||
| elaboration as the macros. | ||
|
|
||
| Values use the [component wire format](../../docs/component-wire-format.md). | ||
| The generated code targets .NET 8 or later. | ||
|
|
||
| ## Generated code | ||
|
|
||
| For a world `w`, the generator emits: | ||
|
|
||
| * `WComponent`, constructed from an `IGuestCaller`. It has a property for each | ||
| exported interface and a method for each function the world exports. | ||
| * `WComponent.RegisterImports`, which registers host functions on an | ||
| `IHostFunctionRegistry` from an implementation of each imported interface. | ||
| * A C# interface `IName` for each imported interface `name`, and `IWImports` | ||
| for functions the world imports. | ||
| * Types for every record, variant, enum and flags in the world's interfaces. | ||
| * A C# interface `IR` for each resource `r`, with a method for each of its | ||
| methods. The constructor and static functions are `NewR` and `RFunction` on | ||
| the interface that defines `r`. | ||
| * `WResources` when the world uses resources. It holds the handle tables. | ||
| `RegisterImports` returns it and the `WComponent` constructor takes it. | ||
|
|
||
| | WIT type | C# type | | ||
| |---|---| | ||
| | `bool`, integers, floats | `bool`, `sbyte` to `ulong`, `float`, `double` | | ||
| | `char` | `System.Text.Rune` | | ||
| | `string` | `string` | | ||
| | `list<u8>`, `list<u8, N>` | `byte[]` | | ||
| | `list<T>`, `list<T, N>` | `IReadOnlyList<T>` | | ||
| | `tuple<...>` | Value tuple | | ||
| | `option<T>` | `T?`, or `Option<T>` when `T` is an `option` | | ||
| | `result<T, E>` | `Result<T, E>`, with `Unit` for a missing type | | ||
| | `record` | Sealed record with `required init` properties | | ||
| | `variant` | Abstract record with a nested sealed record per case | | ||
| | `enum` | `enum` with `uint` values | | ||
| | `flags` | `[Flags]` enum with `uint` values | | ||
| | `own<r>`, `borrow<r>` | `IR` | | ||
|
|
||
| Resources defined by the guest, async functions, futures, streams, maps and | ||
| flags with more than 32 members are rejected. | ||
|
|
||
| For a root namespace `Root`, types from `ns:pkg/iface` are in | ||
| `Root.Ns.Pkg.Iface`. The C# interface or exports class for `iface` is in | ||
| `Root.Ns.Pkg`. A world that uses two versions of a package gets the version | ||
| after `Pkg`, such as `Root.Ns.Pkg.V1_0_0`. | ||
|
|
||
| ## Runtime.cs | ||
|
|
||
| The generated code needs the C# types in [`src/Runtime.cs`](src/Runtime.cs). | ||
| The Rust constant `hyperlight_component_csharp::RUNTIME_SOURCE` holds the text | ||
| of this file, so tools can write it out next to the generated code. | ||
|
|
||
| A .NET host library, such as hyperlight-wasm's, includes this file in its build. | ||
| It implements the file's `IGuestCaller` and `IHostFunctionRegistry` interfaces | ||
| with its sandbox. |
37 changes: 37 additions & 0 deletions
37
src/hyperlight_component_csharp/examples/csharp_bindgen.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // Copyright 2026 The Hyperlight Authors. | ||
|
|
||
| //! Generates C# host bindings for a WIT world. | ||
| //! | ||
| //! Usage: `csharp_bindgen <wit path> <namespace> <output file>` | ||
|
|
||
| use std::process::ExitCode; | ||
|
|
||
| use hyperlight_component_csharp::generate_from_path; | ||
|
|
||
| fn main() -> ExitCode { | ||
| let args: Vec<String> = std::env::args().skip(1).collect(); | ||
| let [wit, namespace, out] = args.as_slice() else { | ||
| eprintln!("usage: csharp_bindgen <wit path> <namespace> <output file>"); | ||
| return ExitCode::FAILURE; | ||
| }; | ||
| // `cargo run` sets CARGO_MANIFEST_DIR, which would resolve a relative | ||
| // path against this crate instead of the caller's directory. | ||
| let wit = match std::path::absolute(wit) { | ||
| Ok(wit) => wit, | ||
| Err(e) => { | ||
| eprintln!("{wit}: {e}"); | ||
| return ExitCode::FAILURE; | ||
| } | ||
| }; | ||
| let result = generate_from_path(&wit, None, namespace) | ||
| .map_err(|e| e.to_string()) | ||
| .and_then(|cs| std::fs::write(out, cs).map_err(|e| format!("{out}: {e}"))); | ||
| match result { | ||
| Ok(()) => ExitCode::SUCCESS, | ||
| Err(e) => { | ||
| eprintln!("{e}"); | ||
| ExitCode::FAILURE | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.