wasi

package module
v0.0.0-...-b868c90 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2023 License: BSD-3-Clause Imports: 8 Imported by: 1

README

go-wasi

Opinionated Go package for running WASI binaries in Go (using the tetratelabs/wazero package).

Motivation

This package is meant to be a simple wrapper to invoke a WASM (WASI) binary with zero or more string arguments returning the output of that binary as a []byte instance.

It is as much to help me understand the boundaries of what is or isn't possible with Go and WASM (WASI) right now. Under the hood it relies on the tetratelabs/wazero package for doing all the heavy lifting.

In order to compile WASI binaries derived from Go code you will need to install and build tinygo and you should consider that there is still quite a lot of the standard Go programming language that tinygo does not support yet.

Example

package main

import (
	"context"
	"flag"
	"fmt"
	"os"

	"github.com/aaronland/go-wasi"
)

func main() {

	ctx := context.Background()

	path_wasm := flag.String("wasm", "", "...")
	
	flag.Parse()
	args := flag.Args()

	wasm_r, _ := os.Open(*path_wasm)
	result, _ := wasi.Run(ctx, wasm_r, args...)

	fmt.Println(string(result))
}

Error handling omitted for the sake of brevity.

reverse

$> go run cmd/reverse/main.go foo bar baz
baz bar foo
$> tinygo build -no-debug -o wasm/reverse.wasm -target wasi ./cmd/reverse/main.go

$> wasmtime wasm/reverse.wasm foo bar baz
baz bar foo
$> go run cmd/run/main.go -wasm ./wasm/reverse.wasm foo bar baz
baz bar foo

See also

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(ctx context.Context, wasm_r io.Reader, args ...string) ([]byte, error)

Run compiles the WASI binary in 'wasm_r' and then invokes it with 'args'. The output of that function invocation is returned as a byte array. For reasons I don't understand it's not possible to wrap and reuse the underlying tetratelabs/wazero runtime and compiled code in a struct so, as of this writing, it's necessary to compile everything from scratch every time this method is invoked.

Types

This section is empty.

Directories

Path Synopsis
cmd
run

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL