├── example ├── go.sum ├── go.mod └── hello.go ├── .gitignore ├── internal ├── Makefile ├── e2e │ ├── go.mod │ ├── __console_log │ │ └── main.go │ ├── Makefile │ └── README.md ├── go.mod ├── go.sum ├── e2e_test.go └── host_test.go ├── go.mod ├── go.work.sum ├── go.work ├── Makefile ├── imports.go ├── README.md ├── imports_webassembly.go ├── .github └── workflows │ └── go-test.yaml ├── wapc.go └── LICENSE.txt /example/go.sum: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.wasm 2 | -------------------------------------------------------------------------------- /internal/Makefile: -------------------------------------------------------------------------------- 1 | test: 2 | go test -v ./... 3 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/wapc/wapc-guest-tinygo 2 | 3 | go 1.23.0 4 | -------------------------------------------------------------------------------- /go.work.sum: -------------------------------------------------------------------------------- 1 | github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= 2 | -------------------------------------------------------------------------------- /go.work: -------------------------------------------------------------------------------- 1 | go 1.23.0 2 | 3 | use ( 4 | . 5 | ./example 6 | ./internal 7 | ./internal/e2e 8 | ) -------------------------------------------------------------------------------- /internal/e2e/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/wapc/wapc-guest-tinygo/internal/e2e 2 | 3 | // Match min version in go-test.yaml 4 | go 1.23 5 | 6 | replace github.com/wapc/wapc-guest-tinygo => ../../ 7 | -------------------------------------------------------------------------------- /internal/e2e/__console_log/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "github.com/wapc/wapc-guest-tinygo" 4 | 5 | func main() { 6 | wapc.ConsoleLog("msg") 7 | wapc.ConsoleLog("msg1") 8 | wapc.ConsoleLog("msg") 9 | } 10 | -------------------------------------------------------------------------------- /example/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/wapc/wapc-go/testdata/go 2 | 3 | go 1.23.0 4 | 5 | toolchain go1.23.4 6 | 7 | require github.com/wapc/wapc-guest-tinygo v0.0.0 8 | 9 | replace github.com/wapc/wapc-guest-tinygo => ../ 10 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | build: 2 | tinygo build -o example/hello.wasm -scheduler=none --no-debug -target=wasip1 -buildmode=c-shared example/hello.go 3 | $(MAKE) -C internal/e2e build 4 | 5 | tests: 6 | go test -v ./... 7 | $(MAKE) -C internal test 8 | -------------------------------------------------------------------------------- /internal/e2e/Makefile: -------------------------------------------------------------------------------- 1 | tinygo_sources := $(wildcard */*.go) 2 | build: $(tinygo_sources) 3 | @echo "Building End-to-End Wasm" 4 | @for f in $^; do \ 5 | tinygo build -o $$(echo $$f | sed -e 's/\.go/\.wasm/') -scheduler=none --no-debug -target=wasi $$f; \ 6 | done 7 | -------------------------------------------------------------------------------- /example/hello.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "github.com/wapc/wapc-guest-tinygo" 4 | 5 | //go:wasmexport wapc_init 6 | func Initialize() { 7 | wapc.RegisterFunctions(wapc.Functions{ 8 | "hello": hello, 9 | }) 10 | } 11 | 12 | func hello(payload []byte) ([]byte, error) { 13 | wapc.HostCall("myBinding", "sample", "hello", []byte("Simon")) 14 | return []byte("Hello"), nil 15 | } 16 | -------------------------------------------------------------------------------- /internal/e2e/README.md: -------------------------------------------------------------------------------- 1 | ## waPC Guest Library for TinyGo End-to-End Tests 2 | 3 | This is an internal project holding source for various test guest wasm used to 4 | cover corner cases. This has its own [go.mod](go.mod) and [Makefile](Makefile) 5 | as the guest source is compiled with TinyGo, not Go. The `%.wasm` binaries here 6 | are checked in, to ensure end-to-end tests run without any prerequisite setup. 7 | -------------------------------------------------------------------------------- /internal/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/wapc/wapc-guest-tinygo/internal 2 | 3 | go 1.23 4 | 5 | require ( 6 | github.com/stretchr/testify v1.10.0 7 | github.com/tetratelabs/wazero v1.8.2 8 | ) 9 | 10 | require ( 11 | github.com/davecgh/go-spew v1.1.1 // indirect 12 | github.com/pmezard/go-difflib v1.0.0 // indirect 13 | gopkg.in/yaml.v3 v3.0.1 // indirect 14 | ) 15 | 16 | replace github.com/wapc/wapc-guest-tinygo => ../ 17 | -------------------------------------------------------------------------------- /imports.go: -------------------------------------------------------------------------------- 1 | //go:build !purego && !appengine && !wasm && !tinygo.wasm && !wasi 2 | // +build !purego,!appengine,!wasm,!tinygo.wasm,!wasi 3 | 4 | package wapc 5 | 6 | func guestRequest(operationPtr uintptr, payloadPtr uintptr) {} 7 | 8 | func guestResponse(ptr uintptr, len uint32) {} 9 | 10 | func guestError(ptr uintptr, len uint32) {} 11 | 12 | func hostCall( 13 | bindingPtr uintptr, bindingLen uint32, 14 | namespacePtr uintptr, namespaceLen uint32, 15 | operationPtr uintptr, operationLen uint32, 16 | payloadPtr uintptr, payloadLen uint32, 17 | ) bool { 18 | return true 19 | } 20 | 21 | func hostResponseLen() uint32 { return 0 } 22 | 23 | func hostResponse(ptr uintptr) {} 24 | 25 | func hostErrorLen() uint32 { return 0 } 26 | 27 | func hostError(ptr uintptr) {} 28 | 29 | func consoleLog(ptr uintptr, size uint32) {} 30 | -------------------------------------------------------------------------------- /internal/go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 2 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 4 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 5 | github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= 6 | github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= 7 | github.com/tetratelabs/wazero v1.8.2 h1:yIgLR/b2bN31bjxwXHD8a3d+BogigR952csSDdLYEv4= 8 | github.com/tetratelabs/wazero v1.8.2/go.mod h1:yAI0XTsMBhREkM/YDAK/zNou3GoiAce1P6+rp/wQhjs= 9 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 10 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 11 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 12 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # waPC Guest Library for TinyGo 2 | 3 | This is the TinyGo implementation of the **waPC** standard for WebAssembly guest modules. It allows any waPC-compliant WebAssembly host to invoke to procedures inside a TinyGo compiled guest and similarly for the guest to invoke procedures exposed by the host. 4 | 5 | ## Example 6 | The following is a simple example of synchronous, bi-directional procedure calls between a WebAssembly host runtime and the guest module. 7 | 8 | > It is recommended to use the latest versions Go and TinyGo 9 | 10 | For TinyGo 0.35+ 11 | 12 | ```go 13 | package main 14 | 15 | import ( 16 | wapc "github.com/wapc/wapc-guest-tinygo" 17 | ) 18 | 19 | //go:wasmexport wapc_init 20 | func Initialize() { 21 | wapc.RegisterFunctions(wapc.Functions{ 22 | "hello": hello, 23 | }) 24 | } 25 | 26 | func hello(payload []byte) ([]byte, error) { 27 | wapc.HostCall("myBinding", "sample", "hello", []byte("Simon")) 28 | return []byte("Hello"), nil 29 | } 30 | ``` 31 | 32 | ```sh 33 | tinygo build -o example/hello.wasm -scheduler=none --no-debug -target=wasip1 -buildmode=c-shared example/hello.go 34 | ``` 35 | -------------------------------------------------------------------------------- /imports_webassembly.go: -------------------------------------------------------------------------------- 1 | //go:build wasm || tinygo.wasm || wasi 2 | // +build wasm tinygo.wasm wasi 3 | 4 | package wapc 5 | 6 | //go:wasm-module wapc 7 | //go:export __guest_request 8 | func guestRequest(operationPtr uintptr, payloadPtr uintptr) 9 | 10 | //go:wasm-module wapc 11 | //go:export __guest_response 12 | func guestResponse(ptr uintptr, len uint32) 13 | 14 | //go:wasm-module wapc 15 | //go:export __guest_error 16 | func guestError(ptr uintptr, len uint32) 17 | 18 | //go:wasm-module wapc 19 | //go:export __host_call 20 | func hostCall( 21 | bindingPtr uintptr, bindingLen uint32, 22 | namespacePtr uintptr, namespaceLen uint32, 23 | operationPtr uintptr, operationLen uint32, 24 | payloadPtr uintptr, payloadLen uint32) bool 25 | 26 | //go:wasm-module wapc 27 | //go:export __host_response_len 28 | func hostResponseLen() uint32 29 | 30 | //go:wasm-module wapc 31 | //go:export __host_response 32 | func hostResponse(ptr uintptr) 33 | 34 | //go:wasm-module wapc 35 | //go:export __host_error_len 36 | func hostErrorLen() uint32 37 | 38 | //go:wasm-module wapc 39 | //go:export __host_error 40 | func hostError(ptr uintptr) 41 | 42 | //go:wasm-module wapc 43 | //go:export __console_log 44 | func consoleLog(ptr uintptr, size uint32) 45 | -------------------------------------------------------------------------------- /.github/workflows/go-test.yaml: -------------------------------------------------------------------------------- 1 | name: go tests 2 | 3 | on: 4 | push: 5 | tags: 6 | - v* 7 | branches: 8 | - master 9 | - main 10 | pull_request: 11 | 12 | jobs: 13 | # Note: TinyGo is not idempotent when generating wasm, so we don't check in 14 | # %.wasm as a part of this job. 15 | build: 16 | runs-on: ubuntu-latest 17 | strategy: 18 | # To simplify setup, we use one Go version, even if it is out of the official version range. 19 | # This version must be <= max version of earliest TinyGo supported and >= min version of latest. 20 | matrix: 21 | go-version: # Note: Go only supports 2 versions: https://go.dev/doc/devel/release#policy 22 | - "1.23" 23 | - "1.24" 24 | tinygo-version: # Note: TinyGo only supports latest: https://github.com/tinygo-org/tinygo/releases 25 | - "0.37.0" 26 | - "0.38.0" 27 | 28 | steps: 29 | - name: Set up Go 30 | uses: actions/setup-go@v3 31 | with: 32 | go-version: ${{ matrix.go-version }} 33 | 34 | - name: Install TinyGo 35 | run: | # Installing via curl so commands are similar on OS/x 36 | tinygo_version=${{ matrix.tinygo-version }} 37 | curl -sSL https://github.com/tinygo-org/tinygo/releases/download/v${tinygo_version}/tinygo${tinygo_version}.linux-amd64.tar.gz | sudo tar -C /usr/local -xzf - 38 | echo "TINYGOROOT=/usr/local/tinygo" >> $GITHUB_ENV 39 | echo "/usr/local/tinygo/bin" >> $GITHUB_PATH 40 | 41 | - name: Checkout 42 | uses: actions/checkout@v3 43 | 44 | - name: Build example 45 | run: make build 46 | 47 | - name: Build test wasm 48 | run: cd internal/e2e; make 49 | 50 | - name: Test 51 | run: make tests 52 | -------------------------------------------------------------------------------- /internal/e2e_test.go: -------------------------------------------------------------------------------- 1 | package internal_test 2 | 3 | import ( 4 | "context" 5 | _ "embed" 6 | "log" 7 | "os" 8 | "path" 9 | "testing" 10 | 11 | "github.com/stretchr/testify/require" 12 | "github.com/tetratelabs/wazero/api" 13 | 14 | "github.com/tetratelabs/wazero" 15 | "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" 16 | ) 17 | 18 | type testKey struct{} 19 | 20 | // testCtx is an arbitrary, non-default context. Non-nil also prevents linter errors. 21 | var testCtx = context.WithValue(context.Background(), testKey{}, "arbitrary") 22 | 23 | var guestWasm map[string][]byte 24 | 25 | const ( 26 | guestWasmConsoleLog = "__console_log" 27 | ) 28 | 29 | // TestMain ensures we can read the test wasm prior to running e2e tests. 30 | func TestMain(m *testing.M) { 31 | wasms := []string{guestWasmConsoleLog} 32 | guestWasm = make(map[string][]byte, len(wasms)) 33 | for _, name := range wasms { 34 | if wasm, err := os.ReadFile(path.Join("e2e", name, "main.wasm")); err != nil { 35 | log.Panicln(err) 36 | } else { 37 | guestWasm[name] = wasm 38 | } 39 | } 40 | os.Exit(m.Run()) 41 | } 42 | 43 | func Test_EndToEnd(t *testing.T) { 44 | type testCase struct { 45 | name string 46 | guest []byte 47 | test func(t *testing.T, guest api.Module, host *wapcHost) 48 | } 49 | 50 | tests := []testCase{ 51 | { 52 | name: "ConsoleLog", 53 | guest: guestWasm[guestWasmConsoleLog], 54 | test: func(t *testing.T, guest api.Module, host *wapcHost) { 55 | // main invokes ConsoleLog 56 | require.Equal(t, []string{"msg", "msg1", "msg"}, host.consoleLogMessages) 57 | }, 58 | }, 59 | } 60 | 61 | // Create a new WebAssembly Runtime. 62 | r := wazero.NewRuntime(testCtx) 63 | defer r.Close(testCtx) // This closes everything this Runtime created. 64 | 65 | // Instantiate WASI, which implements system I/O such as console output and 66 | // is required for `tinygo build -target=wasi` 67 | if _, err := wasi_snapshot_preview1.Instantiate(testCtx, r); err != nil { 68 | t.Errorf("Error instantiating WASI - %v", err) 69 | } 70 | 71 | for _, tt := range tests { 72 | tc := tt 73 | t.Run(tc.name, func(t *testing.T) { 74 | h, host := instantiateWapcHost(t, r) 75 | defer host.Close(testCtx) 76 | 77 | g, err := r.Instantiate(testCtx, tc.guest) 78 | if err != nil { 79 | t.Errorf("Error instantiating waPC guest - %v", err) 80 | } 81 | defer g.Close(testCtx) 82 | 83 | tc.test(t, g, h) 84 | }) 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /wapc.go: -------------------------------------------------------------------------------- 1 | package wapc 2 | 3 | import ( 4 | "reflect" 5 | "unsafe" 6 | ) 7 | 8 | type ( 9 | // Function is the function to register in your waPC module. 10 | Function func(payload []byte) ([]byte, error) 11 | 12 | // Functions is a map of function name to `Function`. 13 | Functions map[string]Function 14 | 15 | // HostError indicates an error when invoking a host operation. 16 | HostError struct { 17 | message string 18 | } 19 | ) 20 | 21 | var allFunctions = Functions{} 22 | 23 | // RegisterFunctions adds functions by name to the registry. 24 | // This should be invoked in `main()`. 25 | func RegisterFunctions(functions Functions) { 26 | for name, fn := range functions { 27 | allFunctions[name] = fn 28 | } 29 | } 30 | 31 | // RegisterFunction adds a single function by name to the registry. 32 | // This should be invoked in `main()`. 33 | func RegisterFunction(name string, fn Function) { 34 | allFunctions[name] = fn 35 | } 36 | 37 | //go:export __guest_call 38 | func guestCall(operationSize uint32, payloadSize uint32) bool { 39 | operation := make([]byte, operationSize) // alloc 40 | payload := make([]byte, payloadSize) // alloc 41 | guestRequest(bytesToPointer(operation), bytesToPointer(payload)) 42 | 43 | if f, ok := allFunctions[string(operation)]; ok { 44 | response, err := f(payload) 45 | if err != nil { 46 | message := err.Error() 47 | guestError(stringToPointer(message), uint32(len(message))) 48 | 49 | return false 50 | } 51 | 52 | guestResponse(bytesToPointer(response), uint32(len(response))) 53 | 54 | return true 55 | } 56 | 57 | message := `Could not find function "` + string(operation) + `"` 58 | guestError(stringToPointer(message), uint32(len(message))) 59 | 60 | return false 61 | } 62 | 63 | // ConsoleLog writes the message the underlying waPC console logger. 64 | func ConsoleLog(msg string) { 65 | consoleLog(stringToPointer(msg), uint32(len(msg))) 66 | } 67 | 68 | // HostCall invokes an operation on the host. The host uses `namespace` and `operation` 69 | // to route to the `payload` to the appropriate operation. The host will return 70 | // a response payload if successful. 71 | func HostCall(binding, namespace, operation string, payload []byte) ([]byte, error) { 72 | result := hostCall( 73 | stringToPointer(binding), uint32(len(binding)), 74 | stringToPointer(namespace), uint32(len(namespace)), 75 | stringToPointer(operation), uint32(len(operation)), 76 | bytesToPointer(payload), uint32(len(payload)), 77 | ) 78 | if !result { 79 | errorLen := hostErrorLen() 80 | message := make([]byte, errorLen) // alloc 81 | hostError(bytesToPointer(message)) 82 | 83 | return nil, &HostError{message: string(message)} // alloc 84 | } 85 | 86 | responseLen := hostResponseLen() 87 | response := make([]byte, responseLen) // alloc 88 | hostResponse(bytesToPointer(response)) 89 | 90 | return response, nil 91 | } 92 | 93 | //go:inline 94 | func bytesToPointer(s []byte) uintptr { 95 | return (*(*reflect.SliceHeader)(unsafe.Pointer(&s))).Data 96 | } 97 | 98 | //go:inline 99 | func stringToPointer(s string) uintptr { 100 | return (*(*reflect.StringHeader)(unsafe.Pointer(&s))).Data 101 | } 102 | 103 | func (e *HostError) Error() string { 104 | return "Host error: " + e.message // alloc 105 | } 106 | -------------------------------------------------------------------------------- /internal/host_test.go: -------------------------------------------------------------------------------- 1 | package internal_test 2 | 3 | import ( 4 | "context" 5 | "testing" 6 | 7 | "github.com/tetratelabs/wazero" 8 | "github.com/tetratelabs/wazero/api" 9 | ) 10 | 11 | // instantiateWapcHost instantiates a test waPC host and returns it and a cleanup function. 12 | func instantiateWapcHost(t *testing.T, r wazero.Runtime) (*wapcHost, api.Closer) { 13 | h := &wapcHost{t: t} 14 | // Export host functions (in the order defined in https://wapc.io/docs/spec/#required-host-exports) 15 | if host, err := r.NewHostModuleBuilder("wapc"). 16 | NewFunctionBuilder(). 17 | WithFunc(h.hostCall). 18 | WithParameterNames("bind_ptr", "bind_len", "ns_ptr", "ns_len", "cmd_ptr", "cmd_len", "payload_ptr", "payload_len"). 19 | Export("__host_call"). 20 | NewFunctionBuilder(). 21 | WithFunc(h.consoleLog). 22 | WithParameterNames("ptr", "len"). 23 | Export("__console_log"). 24 | NewFunctionBuilder(). 25 | WithFunc(h.guestRequest). 26 | WithParameterNames("op_ptr", "ptr"). 27 | Export("__guest_request"). 28 | NewFunctionBuilder(). 29 | WithFunc(h.hostResponse). 30 | WithParameterNames("ptr"). 31 | Export("__host_response"). 32 | NewFunctionBuilder(). 33 | WithFunc(h.hostResponseLen). 34 | Export("__host_response_len"). 35 | NewFunctionBuilder(). 36 | WithFunc(h.guestResponse). 37 | WithParameterNames("ptr", "len"). 38 | Export("__guest_response"). 39 | NewFunctionBuilder(). 40 | WithFunc(h.guestError). 41 | WithParameterNames("ptr", "len"). 42 | Export("__guest_error"). 43 | NewFunctionBuilder(). 44 | WithFunc(h.hostError). 45 | WithParameterNames("ptr"). 46 | Export("__host_error"). 47 | NewFunctionBuilder(). 48 | WithFunc(h.hostErrorLen). 49 | Export("__host_error_len"). 50 | Instantiate(testCtx); err != nil { 51 | t.Errorf("Error instantiating waPC host - %v", err) 52 | return h, nil 53 | } else { 54 | return h, host 55 | } 56 | } 57 | 58 | type wapcHost struct { 59 | t *testing.T 60 | consoleLogMessages []string 61 | } 62 | 63 | // hostCall is the WebAssembly function export "__host_call", which initiates a host using the callHandler using 64 | // parameters read from linear memory (wasm.Memory). 65 | func (w *wapcHost) hostCall(ctx context.Context, m api.Module, bindPtr, bindLen, nsPtr, nsLen, cmdPtr, cmdLen, payloadPtr, payloadLen uint32) int32 { 66 | panic("TODO") 67 | } 68 | 69 | // consoleLog is the WebAssembly function export "__console_log", which logs the message stored by the guest at the 70 | // given offset (ptr) and length (len) in linear memory (wasm.Memory). 71 | func (w *wapcHost) consoleLog(ctx context.Context, m api.Module, ptr, len uint32) { 72 | msg := w.requireReadString(ctx, m.Memory(), "msg", ptr, len) 73 | w.consoleLogMessages = append(w.consoleLogMessages, msg) 74 | } 75 | 76 | // guestRequest is the WebAssembly function export "__guest_request", which writes the invokeContext.operation and 77 | // invokeContext.guestReq to the given offsets (opPtr, ptr) in linear memory (wasm.Memory). 78 | func (w *wapcHost) guestRequest(ctx context.Context, m api.Module, opPtr, ptr uint32) { 79 | panic("TODO") 80 | } 81 | 82 | // hostResponse is the WebAssembly function export "__host_response", which writes the invokeContext.hostResp to the 83 | // given offset (ptr) in linear memory (wasm.Memory). 84 | func (w *wapcHost) hostResponse(ctx context.Context, m api.Module, ptr uint32) { 85 | panic("TODO") 86 | } 87 | 88 | // hostResponse is the WebAssembly function export "__host_response_len", which returns the length of the current host 89 | // response from invokeContext.hostResp. 90 | func (w *wapcHost) hostResponseLen(ctx context.Context) uint32 { 91 | panic("TODO") 92 | } 93 | 94 | // guestResponse is the WebAssembly function export "__guest_response", which reads invokeContext.guestResp from the 95 | // given offset (ptr) and length (len) in linear memory (wasm.Memory). 96 | func (w *wapcHost) guestResponse(ctx context.Context, m api.Module, ptr, len uint32) { 97 | panic("TODO") 98 | } 99 | 100 | // guestError is the WebAssembly function export "__guest_error", which reads invokeContext.guestErr from the given 101 | // offset (ptr) and length (len) in linear memory (wasm.Memory). 102 | func (w *wapcHost) guestError(ctx context.Context, m api.Module, ptr, len uint32) { 103 | panic("TODO") 104 | } 105 | 106 | // hostError is the WebAssembly function export "__host_error", which writes the invokeContext.hostErr to the given 107 | // offset (ptr) in linear memory (wasm.Memory). 108 | func (w *wapcHost) hostError(ctx context.Context, m api.Module, ptr uint32) { 109 | panic("TODO") 110 | } 111 | 112 | // hostError is the WebAssembly function export "__host_error_len", which returns the length of the current host error 113 | // from invokeContext.hostErr. 114 | func (w *wapcHost) hostErrorLen(ctx context.Context) uint32 { 115 | panic("TODO") 116 | } 117 | 118 | // requireReadString is a convenience function that casts requireRead 119 | func (w *wapcHost) requireReadString(ctx context.Context, mem api.Memory, fieldName string, offset, byteCount uint32) string { 120 | return string(w.requireRead(ctx, mem, fieldName, offset, byteCount)) 121 | } 122 | 123 | // requireRead is like api.Memory except that it panics if the offset and byteCount are out of range. 124 | func (w *wapcHost) requireRead(ctx context.Context, mem api.Memory, fieldName string, offset, byteCount uint32) []byte { 125 | buf, ok := mem.Read(offset, byteCount) 126 | if !ok { 127 | w.t.Fatalf("out of memory reading %s", fieldName) 128 | } 129 | return buf 130 | } 131 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. --------------------------------------------------------------------------------