Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Tracked by #499
Mossaka opened this issue Jul 12, 2023 · 7 comments
Labels
gen-tinygo Related to the TinyGo bindings generator

Comments

@Mossaka
Copy link
Member

Mossaka commented Jul 12, 2023

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 Mossaka added the gen-tinygo Related to the TinyGo bindings generator label Jul 13, 2023
@Mossaka
Copy link
Member Author

Mossaka commented Jul 18, 2023

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
Copy link
Member Author

Mossaka commented Jul 21, 2023

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 Mossaka changed the title Go: change the arhitecture from depending on C bindings to the component model canonical ABI Go: switch CGo implementation to component model canonical ABI in pure Go implementation Jul 24, 2023
@patrickhuber
Copy link

patrickhuber commented Jul 31, 2023

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?

@morigs
Copy link

morigs commented Feb 15, 2024

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?

@Mossaka
Copy link
Member Author

Mossaka commented Feb 15, 2024

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

@deadprogram
Copy link

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

@Mossaka
Copy link
Member Author

Mossaka commented Apr 23, 2024

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gen-tinygo Related to the TinyGo bindings generator
Projects
None yet
Development

No branches or pull requests

4 participants