Stream: wit-bindgen

Topic: [C#] wit-bindgen as a source generator


view this post on Zulip Jesse Wellenberg (Yeseh) (Feb 13 2024 at 15:15):

Straight up generating the C# code as it is now is obviously a good first step, but I would love wit-bindgen to be more integrated with the .NET ecosystem and I believe that (incremental) source generators might be a direction worth exploring. A source generator would create a dev experience more similar to the bindgen! macros in Rust where feedback about WIT files can be communicated directly through Roslyn.

It is still early, but I would love to hack a bit on this idea to see if it is possible and practical. I need a little bit of help to get started though. So I have a few questions:

1. Has there already been thinking/work done with using c# source generators for WIT files?
2. Could we reuse/integrate existing tooling such as wit-parser or even wit-bindgen directly? It might be a bit wasteful to implement a WIT parser in c# itself, but as far as I can see the existing tools don't extern their interface so creating direct bindings seems out of the question for now.

Any further thoughts, ideas, or directions are welcome :)

view this post on Zulip Lann Martin (Feb 13 2024 at 15:30):

There has been some work done on generating Go bindings from Go via a JSON-serialized form of parsed WIT: https://github.com/ydnar/wasm-tools-go which @Randy Reddig may be able to give insight on

WebAssembly + WASI tools for Go. Contribute to ydnar/wasm-tools-go development by creating an account on GitHub.

view this post on Zulip Lann Martin (Feb 13 2024 at 15:31):

That serialization isn't very friendly though; it is basically a dump of wit-parser's internal representation.

view this post on Zulip Lann Martin (Feb 13 2024 at 15:33):

I have personally always liked the idea of a WIT interface for bindings generation so that generators could be written in any language, compiled to wasm, and executed by wit-bindgen as a harness, but I also haven't done any real work to make that dream a reality :smile:

view this post on Zulip Randy Reddig (Feb 13 2024 at 15:34):

The JSON representation isn’t simple, but it works well enough to recreate the original WIT syntax, and generate Go

view this post on Zulip Randy Reddig (Feb 13 2024 at 15:35):

The Go implementation might be a good place to start if you want to do a C# implementation

view this post on Zulip Randy Reddig (Feb 13 2024 at 15:36):

I’m contemplating running wasm-tools inside Go as a Wasm module to generate the JSON from WIT implicitly

view this post on Zulip Jesse Wellenberg (Yeseh) (Feb 13 2024 at 16:37):

Lann Martin said:

I have personally always liked the idea of a WIT interface for bindings generation so that generators could be written in any language, compiled to wasm, and executed by wit-bindgen as a harness, but I also haven't done any real work to make that dream a reality :smile:

I have had that thought as well haha.

Thanks for the directions, will give that go implementation a look.

view this post on Zulip Scott Waye (Feb 26 2024 at 00:41):

C# source generators for WIT if it involves call outs to processes, e.g. wit-bindgen is not allowed, it was discussed here https://github.com/dotnet/runtimelab/issues/2377

Hi, Some time back in the ByteAlliance zulip chat there was mention of some interest from Microsoft in helping with the c# bindings for wit-bindgen https://bytecodealliance.zulipchat.com/#narrow/st...

view this post on Zulip Randy Reddig (Feb 26 2024 at 01:24):

As a small step forward, wit-bindgen-go can now exec wasm-tools to perform the WIT to JSON conversion under the hood: https://github.com/ydnar/wasm-tools-go/pull/51

wit: add LoadJSON and LoadWIT functions cmd/wit-bindgen-go .github/workflows/test: setup wasm-tools README: update with additional context

Last updated: Oct 23 2024 at 20:03 UTC