Files
Odin/base/runtime/os_specific_js.odin
Yawning Angel e1ba69ea51 base/runtime: Add rand_bytes and HAS_RAND_BYTES
Having the OS/runtime provide a cryptographic entropy source is the
right thing to do, and we need it to initialize the default random
number generator.
2025-11-29 10:45:53 +09:00

36 lines
677 B
Odin

#+build js
#+private
package runtime
foreign import "odin_env"
_HAS_RAND_BYTES :: true
_stderr_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) {
foreign odin_env {
write :: proc "contextless" (fd: u32, p: []byte) ---
}
write(1, data)
return len(data), 0
}
_rand_bytes :: proc "contextless" (dst: []byte) {
foreign odin_env {
@(link_name = "rand_bytes")
env_rand_bytes :: proc "contextless" (buf: []byte) ---
}
MAX_PER_CALL_BYTES :: 65536 // 64kiB
dst := dst
for len(dst) > 0 {
to_read := min(len(dst), MAX_PER_CALL_BYTES)
env_rand_bytes(dst[:to_read])
dst = dst[to_read:]
}
}
_exit :: proc "contextless" (code: int) -> ! {
trap()
}