math/rand 增加 GetRandomData 函数

This commit is contained in:
chai2010 2023-10-04 06:45:59 +08:00
parent 9f22163ff7
commit b08ea25c20
3 changed files with 28 additions and 0 deletions

View File

@ -0,0 +1,7 @@
// 版权 @2023 凹语言 作者。保留所有权利。
#wa:build !wasi
func getRandomData(r: []byte) => error {
return errors.New("rand.GetRandomData: unsupport")
}

View File

@ -0,0 +1,16 @@
// 版权 @2023 凹语言 作者。保留所有权利。
import (
"errors"
"runtime"
)
#wa:import wasi_snapshot_preview1 random_get
func wasi_random_get(buf: i32, bufLen: i32) => (errno: i32)
func getRandomData(r: []byte) => error {
if wasi_random_get(runtime.WaBytesToPtr(r), i32(len(r))) != 0 {
return errors.New("wasi_random_get failed")
}
return nil
}

View File

@ -0,0 +1,5 @@
// 版权 @2023 凹语言 作者。保留所有权利。
func GetRandomData(r: []byte) => error {
return getRandomData(r)
}