wa-lang.org/wazero@v1.0.2/internal/gojs/crypto.go (about)

     1  package gojs
     2  
     3  import (
     4  	"context"
     5  
     6  	"wa-lang.org/wazero/api"
     7  	"wa-lang.org/wazero/internal/wasm"
     8  )
     9  
    10  // jsCrypto gets random values.
    11  //
    12  // It has only one invocation pattern:
    13  //
    14  //	jsCrypto.Call("getRandomValues", a /* uint8Array */)
    15  //
    16  // This is defined as `Get("crypto")` in rand_js.go init
    17  var jsCrypto = newJsVal(refJsCrypto, "crypto").
    18  	addFunction("getRandomValues", &getRandomValues{})
    19  
    20  type getRandomValues struct{}
    21  
    22  // invoke implements jsFn.invoke
    23  func (*getRandomValues) invoke(ctx context.Context, mod api.Module, args ...interface{}) (interface{}, error) {
    24  	randSource := mod.(*wasm.CallContext).Sys.RandSource()
    25  
    26  	r := args[0].(*byteArray)
    27  	n, err := randSource.Read(r.slice)
    28  	return uint32(n), err
    29  }