Skip to content
Open
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
[submodule "tests/codegen/wasi-clocks"]
path = tests/codegen/wasi-clocks
url = https://github.com/WebAssembly/wasi-clocks
[submodule "crates/go/src/pkg"]
path = crates/go/src/pkg
url = https://github.com/bytecodealliance/go-pkg
31 changes: 22 additions & 9 deletions crates/go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@

This tool generates Go bindings for a chosen WIT world.

## Contributing

If changes need to be made to `wit-bindgen-go`, here are the steps that need to be taken:
- Make the required changes to the [bytecodealliance/go-pkg](https://github.com/bytecodealliance/go-pkg) Go files and tag a release.
- Update the `crates/go/src/pkg` git submodule to reflect the most-recent release of `go-pkg`.
- Update the `REMOTE_PKG_VERSION` constant in [lib.rs](./src/lib.rs) to reflect the most-recent release of `go-pkg`.
- Make the required changes to `wit-bindgen-go`.

## Usage

The easiest way to use `wit-bindgen-go` is through the [componentize-go](https://github.com/bytecodealliance/componentize-go) tool. See below for using `wit-bindgen-go` directly.

To generate bindings with this crate, issue the `go` subcommand to `wit-bindgen`:

```bash
Expand All @@ -19,19 +29,20 @@ world provided:
- You can replace this with your own version (e.g. referencing third party dependencies) if desired
- `wit_bindings.go`: defines the `main` package for the module, including low-level, `//go:export`-annotated entrypoint functions corresponding to exported functions
- These entrypoint functions in turn call high-level functions which must be provided by the application developer
- `wit_runtime/wit_runtime.go`: defines low-level functions for supporting the component model ABI
- `go.bytecodealliance.org/pkg/wit/runtime`: defines low-level functions for supporting the component model ABI
- `<name>/wit_bindings.go`: defines any types generated for the interface named `<name>` (or `wit_world` for WIT types defined at the world level), plus any imported functions
- Note that the types placed in these files include all types for both imported and exported interfaces, except for exported resource types and any types which depend on exported resource types
- `export_<name>/wit_bindings.go`: defines intrinsics for use with any exported resource types generated for the interface named `<name>` (or `wit_world` for WIT types defined at the world level), plus any types which depend on those exported resource types, plus any exported functions
- The exported resource type definitions must be provided by the application developer
- The `export_<name>` package is also the place to define any exported functions
- (if needed) `wit_types/wit_tuples.go`: defines `Tuple<N>` types as required by the WIT world
- (if needed) `wit_types/wit_async.go`: defines low-level functions for integrating the Go scheduler with the component model async ABI
- (if needed) `wit_types/wit_option.go`: defines an `Option` type if required by the WIT world
- (if needed) `wit_types/wit_result.go`: defines an `Result` type if required by the WIT world
- (if needed) `wit_types/wit_unit.go`: defines an `Unit` type if required by the WIT world
- (if needed) `wit_types/wit_stream.go`: defines a `StreamReader` and `StreamWriter` types if required by the WIT world
- (if needed) `wit_types/wit_future.go`: defines a `FutureReader` and `FutureWriter` types if required by the WIT world
- (if needed) `go.bytecodealliance.org/pkg/wit/types`:
- defines `Tuple<N>` types as required by the WIT world
- defines an `Option` type as required by the WIT world
- defines a `Result` type as required by the WIT world
- defines a `Unit` type as required by the WIT world
- defines a `StreamReader` and `StreamWriter` types as required by the WIT world
- defines a `FutureReader` and `FutureWriter` types as required by the WIT world
- (if needed) `go.bytecodealliance.org/pkg/wit/async`: defines low-level functions for integrating the Go scheduler with the component model async ABI

Note that async support currently requires [a patched version of
Go](https://github.com/dicej/go/releases/tag/go1.25.5-wasi-on-idle). Code
Expand Down Expand Up @@ -60,7 +71,7 @@ interface foo {
a: s32,
b: string,
}

echo: func(x: thing) -> tuple<s32, string>;
}

Expand Down Expand Up @@ -89,6 +100,7 @@ func Echo(x Thing) (int32, string) {
return x.A, x.B
}
EOF
go mod tidy
GOARCH="wasm" GOOS="wasip1" go build -o core.wasm -buildmode=c-shared -ldflags=-checklinkname=0
wasm-tools component embed -w test ../world.wit core.wasm -o core-with-wit.wasm
wasm-tools component new --adapt ../wasi_snapshot_preview1.reactor.wasm core-with-wit.wasm -o component.wasm
Expand All @@ -106,6 +118,7 @@ func Run() (int32, string) {
return Echo(Thing{42, "hello, world!"})
}
EOF
go mod tidy
GOARCH="wasm" GOOS="wasip1" go build -o core.wasm -buildmode=c-shared -ldflags=-checklinkname=0
wasm-tools component embed -w runner ../world.wit core.wasm -o core-with-wit.wasm
wasm-tools component new --adapt ../wasi_snapshot_preview1.reactor.wasm core-with-wit.wasm -o component.wasm
Expand Down
Loading
Loading