This adds an experimental package gojs which implements the host side of Wasm compiled by GOARCH=wasm GOOS=js go build -o X.wasm X.go This includes heavy disclaimers, in part inherited by Go's comments https://github.com/golang/go/blob/go1.19/src/syscall/js/js.go#L10-L11 Due to this many will still use TinyGo instead. That said, this is frequently asked for and has interesting features including reflection and HTTP client support. Signed-off-by: Adrian Cole <adrian@tetrate.io>
30 lines
737 B
Go
30 lines
737 B
Go
package gojs
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/tetratelabs/wazero/api"
|
|
"github.com/tetratelabs/wazero/internal/wasm"
|
|
)
|
|
|
|
// jsCrypto gets random values.
|
|
//
|
|
// It has only one invocation pattern:
|
|
//
|
|
// jsCrypto.Call("getRandomValues", a /* uint8Array */)
|
|
//
|
|
// This is defined as `Get("crypto")` in rand_js.go init
|
|
var jsCrypto = newJsVal(refJsCrypto, "crypto").
|
|
addFunction("getRandomValues", &getRandomValues{})
|
|
|
|
type getRandomValues struct{}
|
|
|
|
// invoke implements jsFn.invoke
|
|
func (*getRandomValues) invoke(ctx context.Context, mod api.Module, args ...interface{}) (interface{}, error) {
|
|
randSource := mod.(*wasm.CallContext).Sys.RandSource()
|
|
|
|
r := args[0].(*byteArray)
|
|
n, err := randSource.Read(r.slice)
|
|
return uint32(n), err
|
|
}
|