github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/gojs/crypto.go (about) 1 package gojs 2 3 import ( 4 "context" 5 6 "github.com/bananabytelabs/wazero/api" 7 "github.com/bananabytelabs/wazero/internal/gojs/custom" 8 "github.com/bananabytelabs/wazero/internal/gojs/goos" 9 "github.com/bananabytelabs/wazero/internal/wasm" 10 ) 11 12 // jsCrypto is used by crypto/rand.Read to gets random values. 13 // 14 // It has only one invocation pattern: 15 // 16 // jsCrypto.Call("getRandomValues", a /* uint8Array */) 17 // 18 // This is defined as `Get("crypto")` in rand_js.go init 19 var jsCrypto = newJsVal(goos.RefJsCrypto, custom.NameCrypto). 20 addFunction(custom.NameCryptoGetRandomValues, cryptoGetRandomValues{}) 21 22 // cryptoGetRandomValues implements jsFn 23 type cryptoGetRandomValues struct{} 24 25 func (cryptoGetRandomValues) invoke(_ context.Context, mod api.Module, args ...interface{}) (interface{}, error) { 26 randSource := mod.(*wasm.ModuleInstance).Sys.RandSource() 27 28 r := args[0].(*goos.ByteArray) 29 n, err := randSource.Read(r.Unwrap()) 30 return uint32(n), err 31 }