Files
wazero/internal/gojs/crypto.go
Edoardo Vacchi 599c097603 Add -hostlogging=crypto (#1064)
This allows you to specify multiple logging scopes, both in API and the CLI.

e.g for the CLI.
```bash
$ wazero run --hostlogging=crypto --hostlogging=filesystem --mount=.:/:ro cat.wasm
```

e.g. for Go
```go
loggingCtx := context.WithValue(testCtx, experimental.FunctionListenerFactoryKey{},
	logging.NewHostLoggingListenerFactory(&log, logging.LogScopeCrypto|logging.LogScopeFilesystem))
```

Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: Adrian Cole <adrian@tetrate.io>
2023-01-28 12:51:44 +02:00

32 lines
933 B
Go

package gojs
import (
"context"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/gojs/custom"
"github.com/tetratelabs/wazero/internal/gojs/goos"
"github.com/tetratelabs/wazero/internal/wasm"
)
// jsCrypto is used by crypto/rand.Read to 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(goos.RefJsCrypto, custom.NameCrypto).
addFunction(custom.NameCryptoGetRandomValues, cryptoGetRandomValues{})
// cryptoGetRandomValues implements jsFn
type cryptoGetRandomValues struct{}
func (cryptoGetRandomValues) invoke(_ context.Context, mod api.Module, args ...interface{}) (interface{}, error) {
randSource := mod.(*wasm.CallContext).Sys.RandSource()
r := args[0].(*goos.ByteArray)
n, err := randSource.Read(r.Unwrap())
return uint32(n), err
}