Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,26 @@ By default the project will find all wit files and execute wit-bindgen against e
</ItemGroup>
```

### Passing additional wit-bindgen args

[wit-bindgen](https://github.com/bytecodealliance/wit-bindgen/tree/main) for c# has some advanced settings that can be set by using `WitBindgenAddtionalArgs` property. A non-exhustive list of example args that might be useful are:

- `--features <featurename>` - turn on [wit features](https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md#feature-gates)
- `--with-wit-results` - use [wit Result types](https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md#wit-types) instead of generating exceptions
- `--skip-support-files` - don't output files like `WasmImportLinkageAttribute`

Example:

```xml
<PropertyGroup>
<WitBindgenAddtionalArgs>--with-wit-results --features tls</WitBindgenAddtionalArgs>
</PropertyGroup>
```

To learn about addtional args run `wit-bindgen c-sharp -h`

### Referencing Wit Packages from OCI Registries

Wit can be packaged into [OCI Artifacts](https://tag-runtime.cncf.io/wgs/wasm/deliverables/wasm-oci-artifact/) which can be pushed to OCI registries. To import a WIT definition from an OCI registry specify the Registry on the `Wit` Element. This will pull a WASM component binary that contains the WIT definition. wit-bindgen can use the binary format directly to generate the bindings. To view the WIT directly use [`wasm-tools component wit wit/wit.wasm`](https://github.com/bytecodealliance/wasm-tools).

```xml
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project>
<PropertyGroup>
<WitBindgenRuntime>native-aot</WitBindgenRuntime>
<WitBindgenAddtionalArgs></WitBindgenAddtionalArgs>

<!-- Keep this block all in sync manually, since URLs can be arbitrary -->
<WasiSdkVersion>24.0</WasiSdkVersion>
Expand Down Expand Up @@ -86,7 +87,7 @@

<RemoveDir Directories="$(WitGeneratedFilesRoot)" />
<MakeDir Directories="$(WitGeneratedFilesRoot)" />
<Exec Command="$(WitBindgenExe) c-sharp %(Wit.Identity) %(Wit.WitWorldArg) --runtime $(WitBindgenRuntime) --out-dir $(WitGeneratedFilesRoot)" />
<Exec Command="$(WitBindgenExe) c-sharp %(Wit.Identity) %(Wit.WitWorldArg) --runtime $(WitBindgenRuntime) $(WitBindgenAddtionalArgs) --out-dir $(WitGeneratedFilesRoot)" />
<WriteLinesToFile File="$(WitGeneratedFilesRoot)lastbuild.txt" Lines="" Overwrite="true" />

<ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions test/WitBindgenTest/WitBindgenTest/CodeGenerationTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using MyFuncsWorld;
using ProducerWorld;
using MyResultsWorld;
using MyWitResultsWorld;
using System;
using Xunit;

Expand Down Expand Up @@ -33,4 +35,16 @@ public void CanSpecifyWorld()
{
Assert.NotNull((Func<int>)SomeStuffImpl.GetNumber);
}

[Fact]
public void ShouldBeNormalResult()
{
Assert.NotNull((Func<float, float>)MyResultsWorldImpl.StringError);
}

[Fact]
public void ShouldBeNormalWitResult()
{
Assert.NotNull((Func<float, MyWitResultsWorld.Result<float, string>>)MyWitResultsWorldImpl.StringError);
}
}
2 changes: 2 additions & 0 deletions test/WitBindgenTest/WitBindgenTest/WitBindgenTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

<ItemGroup>
<ProjectReference Include="..\testapps\LibraryUsingWit\LibraryUsingWit.csproj" />
<ProjectReference Include="..\testapps\LibraryWithExceptions\LibraryWitExceptions.csproj" />
<ProjectReference Include="..\testapps\LibraryWithWitResultType\LibraryWithWitResultType.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<!-- Only needed when referencing the dependencies as projects. For package references, these are imported automatically. -->
<Import Project="..\..\..\..\src\WitBindgen\ImportInDev.proj" />

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\src\WitBindgen\WitBindgen.csproj" />
<PackageReference Include="runtime.$(NETCoreSdkPortableRuntimeIdentifier).Microsoft.DotNet.ILCompiler.LLVM"/>
</ItemGroup>

<ItemGroup>
<Wit Update="result.wit" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions test/WitBindgenTest/testapps/LibraryWithExceptions/Result.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using MyResultsWorld;

public class MyResultsWorldImpl : IMyResultsWorld
{
public static float StringError(float a)
{
return a;
}
}
5 changes: 5 additions & 0 deletions test/WitBindgenTest/testapps/LibraryWithExceptions/result.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package test:results;

world my-results {
export string-error: func(a: f32) -> result<f32, string>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<!-- Only needed when referencing the dependencies as projects. For package references, these are imported automatically. -->
<Import Project="..\..\..\..\src\WitBindgen\ImportInDev.proj" />

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<WitBindgenAddtionalArgs>--with-wit-results</WitBindgenAddtionalArgs>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\src\WitBindgen\WitBindgen.csproj" />
<PackageReference Include="runtime.$(NETCoreSdkPortableRuntimeIdentifier).Microsoft.DotNet.ILCompiler.LLVM"/>
</ItemGroup>

<ItemGroup>
<Wit Update="result.wit" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions test/WitBindgenTest/testapps/LibraryWithWitResultType/Result.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// I don't think this namespace should be so different to the one in MyFuncsWorldImpl,
// but currently that's what the codegen requires
using MyWitResultsWorld;



public class MyWitResultsWorldImpl : IMyWitResultsWorld
{
public static Result<float, string> StringError(float a)
{
return Result<float, string>.Ok(a);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package test:results;

world my-wit-results {
export string-error: func(a: f32) -> result<f32, string>;
}