wasm-tools-go icon indicating copy to clipboard operation
wasm-tools-go copied to clipboard

wit/bindgen: encapsulate exports in a package-level var Exports

Open ydnar opened this issue 9 months ago • 0 comments

This changes the way Component Model exports are represented in Go code by encapsulating them in a single anonymous struct named Exports.

  • By default, function bodies are left nil, and if called, will panic.
  • Resource destructors ([dtor]*) and post-return cleanup functions (cabi_post_*) are assigned empty function bodies by default, so the importing package can choose whether to implement them.

Examples

wasi:cli/run

//go:wasmexport wasi:cli/[email protected]#run
//export wasi:cli/[email protected]#run
func wasmexport_Run() cm.Result {
	return Exports.Run()
}

// Exports represents the caller-defined exports from "wasi:cli/[email protected]".
var Exports struct {
	// Run represents the caller-defined, exported function "run".
	//
	// Run the program.
	//
	//	run: func() -> result
	Run func() cm.Result
}

Exported Resource

Resource methods, static functions, constructor, and destructor are nested under a Exports.T struct:

// Exports represents the caller-defined exports from "example:uses/a".
var Exports struct {
	// Res represents the caller-defined exports for resource "example:uses/a#res".
	Res struct {
		// Destructor represents the caller-defined, exported destructor for resource "res".
		//
		// Resource destructor.
		Destructor func(self cm.Rep)

		// Constructor represents the caller-defined, exported constructor for resource "res".
		//
		//	constructor()
		Constructor func() ExportRes

		// Do represents the caller-defined, exported method "do".
		//
		//	do: func()
		Do func(self cm.Rep)
	}
}

ydnar avatar May 11 '24 03:05 ydnar