diff --git a/README.md b/README.md index 98aec9e..6a2e75f 100644 --- a/README.md +++ b/README.md @@ -232,7 +232,26 @@ By default the project will find all wit files and execute wit-bindgen against e ``` +### 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 ` - 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 + + --with-wit-results --features tls + +``` + +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 diff --git a/src/WitBindgen/build/BytecodeAlliance.Componentize.DotNet.WitBindgen.targets b/src/WitBindgen/build/BytecodeAlliance.Componentize.DotNet.WitBindgen.targets index c4641e7..715b1b4 100644 --- a/src/WitBindgen/build/BytecodeAlliance.Componentize.DotNet.WitBindgen.targets +++ b/src/WitBindgen/build/BytecodeAlliance.Componentize.DotNet.WitBindgen.targets @@ -1,6 +1,7 @@  native-aot + 24.0 @@ -86,7 +87,7 @@ - + diff --git a/test/WitBindgenTest/WitBindgenTest/CodeGenerationTest.cs b/test/WitBindgenTest/WitBindgenTest/CodeGenerationTest.cs index 576dc10..54f16fb 100644 --- a/test/WitBindgenTest/WitBindgenTest/CodeGenerationTest.cs +++ b/test/WitBindgenTest/WitBindgenTest/CodeGenerationTest.cs @@ -1,5 +1,7 @@ using MyFuncsWorld; using ProducerWorld; +using MyResultsWorld; +using MyWitResultsWorld; using System; using Xunit; @@ -33,4 +35,16 @@ public void CanSpecifyWorld() { Assert.NotNull((Func)SomeStuffImpl.GetNumber); } + + [Fact] + public void ShouldBeNormalResult() + { + Assert.NotNull((Func)MyResultsWorldImpl.StringError); + } + + [Fact] + public void ShouldBeNormalWitResult() + { + Assert.NotNull((Func>)MyWitResultsWorldImpl.StringError); + } } diff --git a/test/WitBindgenTest/WitBindgenTest/WitBindgenTest.csproj b/test/WitBindgenTest/WitBindgenTest/WitBindgenTest.csproj index 199b69a..316dc09 100644 --- a/test/WitBindgenTest/WitBindgenTest/WitBindgenTest.csproj +++ b/test/WitBindgenTest/WitBindgenTest/WitBindgenTest.csproj @@ -20,6 +20,8 @@ + + diff --git a/test/WitBindgenTest/testapps/LibraryWithExceptions/LibraryWitExceptions.csproj b/test/WitBindgenTest/testapps/LibraryWithExceptions/LibraryWitExceptions.csproj new file mode 100644 index 0000000..2068f3c --- /dev/null +++ b/test/WitBindgenTest/testapps/LibraryWithExceptions/LibraryWitExceptions.csproj @@ -0,0 +1,20 @@ + + + + + + net9.0 + enable + enable + + + + + + + + + + + + diff --git a/test/WitBindgenTest/testapps/LibraryWithExceptions/Result.cs b/test/WitBindgenTest/testapps/LibraryWithExceptions/Result.cs new file mode 100644 index 0000000..d473cb8 --- /dev/null +++ b/test/WitBindgenTest/testapps/LibraryWithExceptions/Result.cs @@ -0,0 +1,9 @@ +using MyResultsWorld; + +public class MyResultsWorldImpl : IMyResultsWorld +{ + public static float StringError(float a) + { + return a; + } +} diff --git a/test/WitBindgenTest/testapps/LibraryWithExceptions/result.wit b/test/WitBindgenTest/testapps/LibraryWithExceptions/result.wit new file mode 100644 index 0000000..34158a0 --- /dev/null +++ b/test/WitBindgenTest/testapps/LibraryWithExceptions/result.wit @@ -0,0 +1,5 @@ +package test:results; + +world my-results { + export string-error: func(a: f32) -> result; +} diff --git a/test/WitBindgenTest/testapps/LibraryWithWitResultType/LibraryWithWitResultType.csproj b/test/WitBindgenTest/testapps/LibraryWithWitResultType/LibraryWithWitResultType.csproj new file mode 100644 index 0000000..ce7a703 --- /dev/null +++ b/test/WitBindgenTest/testapps/LibraryWithWitResultType/LibraryWithWitResultType.csproj @@ -0,0 +1,21 @@ + + + + + + net9.0 + enable + enable + --with-wit-results + + + + + + + + + + + + diff --git a/test/WitBindgenTest/testapps/LibraryWithWitResultType/Result.cs b/test/WitBindgenTest/testapps/LibraryWithWitResultType/Result.cs new file mode 100644 index 0000000..cb01ccf --- /dev/null +++ b/test/WitBindgenTest/testapps/LibraryWithWitResultType/Result.cs @@ -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 StringError(float a) + { + return Result.Ok(a); + } +} diff --git a/test/WitBindgenTest/testapps/LibraryWithWitResultType/result.wit b/test/WitBindgenTest/testapps/LibraryWithWitResultType/result.wit new file mode 100644 index 0000000..7a98a8c --- /dev/null +++ b/test/WitBindgenTest/testapps/LibraryWithWitResultType/result.wit @@ -0,0 +1,5 @@ +package test:results; + +world my-wit-results { + export string-error: func(a: f32) -> result; +}