wapc

package
v0.0.0-...-6554945 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2022 License: MIT Imports: 2 Imported by: 0

README

wapc

This is the implementation of the waPC interface for WebAssembly guest modules written in Go. It allows a wasmexec host to invoke procedures inside a Go compiled guest and similarly for the guest to invoke procedures exposed by the wasmexec host.

This implementation is built on top of the js.FuncOf() functionality and thus does not leverage the imports and exports dictated by the waPC standard. This means this is not a real waPC implementation and can not be used by a waPC-compliant host that has not implemented this wasmexec package.

Host

On the host instance you can call Invoke() on *wasmexec.Module to send something to the guest:

result, err := mod.Invoke("hello", []byte(`Hello World`))

The host can also receive events by implementing HostCall() on the instance:

func (instance *Instance) HostCall(binding, namespace, operation string, payload []byte) ([]byte, error) {
    // ...
}

For examples of host implementations, check the example in each runtime-specific directory.

Guest

On the guest, events from the host's Invoke() calls can be received as follows:

import (
    "fmt"

    "github.com/prep/wasmexec/wapc"
)

func hello(payload []byte) ([]byte, error) {
    return []byte("Hello back!"), nil
}

func main() {
    wapc.RegisterFunctions(wapc.Functions{
        "hello": hello,
    })
}

The guest can also send events to the host, which will be received by the host's HostCall() function:

resp, err := wapc.HostCall("myBinding", "sample", "hello", []byte("Guest"))

The waPC example shows a simple guest implementation that gets called by the runtime examples.

Documentation

Rendered for js/wasm

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HostCall

func HostCall(binding, namespace, operation string, payload []byte) ([]byte, error)

HostCall invokes an operation on the host. The host uses namespace and operation to route the payload to the appropriate operation. The host will return a response payload if successful.

func RegisterFunction

func RegisterFunction(name string, fn Function)

func RegisterFunctions

func RegisterFunctions(functions Functions)

Types

type Function

type Function func(payload []byte) ([]byte, error)

type Functions

type Functions map[string]Function

Jump to

Keyboard shortcuts

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