wit-bindgen icon indicating copy to clipboard operation
wit-bindgen copied to clipboard

Go: switch CGo implementation to component model canonical ABI in pure Go implementation

Open Mossaka opened this issue 2 years ago • 7 comments

I'd like to change the implementation of the go-bindgen from using CGO and depending on the C bindings to using the canonical ABI implementation.

The biggest block, however, is there is no mechanism to export functions to Wasm. I know that //go:wasmimport will land in Go 1.21 (Augest release), and //go:wasmexport is planned and might have developed. Once these two are available, I think we will be able to re-write the go-bindgen using canonical ABI.

Mossaka avatar Jul 12 '23 22:07 Mossaka

Just found out that TinyGo has //export directive which helps to export functions to WASI.

In addition, TinyGo 0.28.0 supports //go:wasmimport compiler directive (release note).

This technically unblocks me from removing CGO and C bindings dependency on the wit-bindgen-go generator.

Mossaka avatar Jul 18 '23 00:07 Mossaka

I am happy to be able to implement a simple Go binding for the following WIT file without using CGo and C bindings!

// wit/host.wit
package example:host

world host {
  import print: func(msg: string)

  export run: func()
}

The Go binding code looks like

package host

import "unsafe"

//go:wasmimport $root print
func hostPrint(ptr0, len0 int32)

func HostPrint(msg string) {
	hostPrint(*(*int32)(unsafe.Pointer(&msg)), int32(len(msg)))
}

// Export functions from host
var host Host = nil

func SetHost(i Host) {
	host = i
}

type Host interface {
	Run()
}

//go:export run
func HostRun() {
	host.Run()
}

The Go runtime I used is TinyGo v0.28.1

To see the full repo, please refer to here.

Mossaka avatar Jul 21 '23 21:07 Mossaka

Is this going through the Canon lift and Canon lower functions of the component abi injected by one of the wasm tools (or tinygo) or is it just passing an array of bytes through the wasm export, import?

patrickhuber avatar Jul 31 '23 00:07 patrickhuber

I have problems using TinyGo. For some unknown reason the code does not work as expected. And it's impossible to debug because tinygo doesn't even support debug.Stack...

What's blocking from moving to the standard GC compiler right now? Can I help somehow?

morigs avatar Feb 15 '24 22:02 morigs

What's blocking from moving to the standard GC compiler right now? Can I help somehow?

We are working towards this goal, but there are several milestones we need to reach before moving to the "big Go" compiler.

  1. wasm32 proposal
  2. go:wasmexport proposal
  3. Go memory management for the component model

Mossaka avatar Feb 15 '24 22:02 Mossaka

This is really excellent, I want it! Is there someplace with any work in progress? How can I help?

deadprogram avatar Apr 23 '24 13:04 deadprogram

This is really excellent, I want it! Is there someplace with any work in progress? How can I help?

Check out @ydnar work on wit-bindgen-go which is currently used as the binding generator for wasip2 work in TinyGo!

Also, if you are interested, we have biweekly BytecodeAlliance Go subgroup meeting to discuss these topics! Check out agenda list!

Mossaka avatar Apr 23 '24 22:04 Mossaka