mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
Replace Math.random with crypto.getRandomValues for _system_number
This commit is contained in:
@@ -6,7 +6,7 @@ foreign odin_env {
|
|||||||
env_rand_bytes :: proc "contextless" (buf: []byte) ---
|
env_rand_bytes :: proc "contextless" (buf: []byte) ---
|
||||||
}
|
}
|
||||||
|
|
||||||
_MAX_PER_CALL_BYTES :: 65536
|
_MAX_PER_CALL_BYTES :: 65536 // 64kiB
|
||||||
|
|
||||||
_rand_bytes :: proc(dst: []byte) {
|
_rand_bytes :: proc(dst: []byte) {
|
||||||
dst := dst
|
dst := dst
|
||||||
|
|||||||
@@ -2,11 +2,13 @@ package rand
|
|||||||
|
|
||||||
foreign import "odin_env"
|
foreign import "odin_env"
|
||||||
foreign odin_env {
|
foreign odin_env {
|
||||||
@(link_name = "rand")
|
@(link_name = "rand_bytes")
|
||||||
rand_f64 :: proc "contextless" () -> f64 ---
|
env_rand_bytes :: proc "contextless" (buf: []byte) ---
|
||||||
}
|
}
|
||||||
|
|
||||||
@(require_results)
|
@(require_results)
|
||||||
_system_random :: proc() -> u64 {
|
_system_random :: proc() -> u64 {
|
||||||
return u64(rand_f64() * 0x1fffffffffffff)
|
buf: [8]u8
|
||||||
|
env_rand_bytes(buf[:])
|
||||||
|
return transmute(u64)buf
|
||||||
}
|
}
|
||||||
|
|||||||
Vendored
-1
@@ -1350,7 +1350,6 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) {
|
|||||||
exp: Math.exp,
|
exp: Math.exp,
|
||||||
ldexp: (x, exp) => x * Math.pow(2, exp),
|
ldexp: (x, exp) => x * Math.pow(2, exp),
|
||||||
|
|
||||||
rand: Math.random,
|
|
||||||
rand_bytes: (ptr, len) => {
|
rand_bytes: (ptr, len) => {
|
||||||
const view = new Uint8Array(wasmMemoryInterface.memory.buffer, ptr, len)
|
const view = new Uint8Array(wasmMemoryInterface.memory.buffer, ptr, len)
|
||||||
crypto.getRandomValues(view)
|
crypto.getRandomValues(view)
|
||||||
|
|||||||
Reference in New Issue
Block a user