Skip to content

Commit

Permalink
Making the module both TinyGo and Go friendly for compilation/testing…
Browse files Browse the repository at this point in the history
…. Tweaking registration functions. (#3)

Co-authored-by: Phil Kedy <[email protected]>
  • Loading branch information
pkedy and Phil Kedy authored Oct 1, 2020
1 parent 49a7a40 commit f05bbfb
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 47 deletions.
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# waPC Guest Library for TinyGo

**Note:** Consider this SDK experimental. We have yet to put it through more advanced use cases than "hello world".

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.

## Example
Expand All @@ -15,7 +13,7 @@ import (
)

func main() {
wapc.Register(wapc.Functions{
wapc.RegisterFunctions(wapc.Functions{
"hello": hello,
})
}
Expand All @@ -30,9 +28,8 @@ func hello(payload []byte) ([]byte, error) {
tinygo build -o example/hello.wasm -target wasm -no-debug example/hello.go
```

## Known limitations
## Considerations

* Only go up to 1.13 is supported by TinyGo
* The `fmt` package requires `syscall/js.*` which are not imported by the waPC host
* TinyGo has limited `reflect` package support, thus libraries like protobuf will likely not work
* [No garbage collector](https://tinygo.org/compiler-internals/heap-allocation/) - memory allocation occurs in this library
* It is recommended to use the latest versions Go and TinyGo
* Avoid using the `fmt` package as it requires `syscall/js.*` which are not implemented by the waPC host libraries
* TinyGo has limited `reflect` package support, thus generated Protobuf code will likely not work without some tweaking (But we have gotten it to work!)
2 changes: 1 addition & 1 deletion example/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func main() {
wapc.Register(wapc.Functions{
wapc.RegisterFunctions(wapc.Functions{
"hello": hello,
})
}
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/wapc/wapc-guest-tinygo

go 1.15
25 changes: 25 additions & 0 deletions imports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// +build !purego,!appengine,!wasm

package wapc

func guestRequest(operationPtr uintptr, payloadPtr uintptr) {}

func guestResponse(ptr uintptr, len uint32) {}

func guestError(ptr uintptr, len uint32) {}

func hostCall(
bindingPtr uintptr, bindingLen uint32,
namespacePtr uintptr, namespaceLen uint32,
operationPtr uintptr, operationLen uint32,
payloadPtr uintptr, payloadLen uint32) bool {
return true
}

func hostResponseLen() uint32 { return 0 }

func hostResponse(ptr uintptr) {}

func hostErrorLen() uint32 { return 0 }

func hostError(ptr uintptr) {}
39 changes: 39 additions & 0 deletions imports_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// +build wasm

package wapc

//go:wasm-module wapc
//go:export __guest_request
func guestRequest(operationPtr uintptr, payloadPtr uintptr)

//go:wasm-module wapc
//go:export __guest_response
func guestResponse(ptr uintptr, len uint32)

//go:wasm-module wapc
//go:export __guest_error
func guestError(ptr uintptr, len uint32)

//go:wasm-module wapc
//go:export __host_call
func hostCall(
bindingPtr uintptr, bindingLen uint32,
namespacePtr uintptr, namespaceLen uint32,
operationPtr uintptr, operationLen uint32,
payloadPtr uintptr, payloadLen uint32) bool

//go:wasm-module wapc
//go:export __host_response_len
func hostResponseLen() uint32

//go:wasm-module wapc
//go:export __host_response
func hostResponse(ptr uintptr)

//go:wasm-module wapc
//go:export __host_error_len
func hostErrorLen() uint32

//go:wasm-module wapc
//go:export __host_error
func hostError(ptr uintptr)
46 changes: 8 additions & 38 deletions wapc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,6 @@ import (
"unsafe"
)

//go:wasm-module wapc
//go:export __guest_request
func guestRequest(operationPtr uintptr, payloadPtr uintptr)

//go:wasm-module wapc
//go:export __guest_response
func guestResponse(ptr uintptr, len uint32)

//go:wasm-module wapc
//go:export __guest_error
func guestError(ptr uintptr, len uint32)

//go:wasm-module wapc
//go:export __host_call
func hostCall(
bindingPtr uintptr, bindingLen uint32,
namespacePtr uintptr, namespaceLen uint32,
operationPtr uintptr, operationLen uint32,
payloadPtr uintptr, payloadLen uint32) bool

//go:wasm-module wapc
//go:export __host_response_len
func hostResponseLen() uint32

//go:wasm-module wapc
//go:export __host_response
func hostResponse(ptr uintptr)

//go:wasm-module wapc
//go:export __host_error_len
func hostErrorLen() uint32

//go:wasm-module wapc
//go:export __host_error
func hostError(ptr uintptr)

type (
// Function is the function to register in your waPC module.
Function func(payload []byte) ([]byte, error)
Expand All @@ -58,14 +22,20 @@ var (
allFunctions = Functions{}
)

// Register adds functions by name to the registery.
// RegisterFunctions adds functions by name to the registery.
// This should be invoked in `main()`.
func Register(functions Functions) {
func RegisterFunctions(functions Functions) {
for name, fn := range functions {
allFunctions[name] = fn
}
}

// RegisterFunction adds a single function by name to the registery.
// This should be invoked in `main()`.
func RegisterFunction(name string, fn Function) {
allFunctions[name] = fn
}

//go:export __guest_call
func guestCall(operationSize uint32, payloadSize uint32) bool {
operation := make([]byte, operationSize) // alloc
Expand Down

0 comments on commit f05bbfb

Please sign in to comment.