mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-04 22:58:46 +00:00
Merge pull request #2896 from thetarnav/js-rand
Add system_random and random_bytes for js target
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
package crypto
|
package crypto
|
||||||
|
|
||||||
when ODIN_OS != .Linux && ODIN_OS != .OpenBSD && ODIN_OS != .Windows {
|
when ODIN_OS != .Linux && ODIN_OS != .OpenBSD && ODIN_OS != .Windows && ODIN_OS != .JS {
|
||||||
_rand_bytes :: proc (dst: []byte) {
|
_rand_bytes :: proc(dst: []byte) {
|
||||||
unimplemented("crypto: rand_bytes not supported on this OS")
|
unimplemented("crypto: rand_bytes not supported on this OS")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package crypto
|
||||||
|
|
||||||
|
foreign import "odin_env"
|
||||||
|
foreign odin_env {
|
||||||
|
@(link_name = "rand_bytes")
|
||||||
|
env_rand_bytes :: proc "contextless" (buf: []byte) ---
|
||||||
|
}
|
||||||
|
|
||||||
|
_MAX_PER_CALL_BYTES :: 65536 // 64kiB
|
||||||
|
|
||||||
|
_rand_bytes :: proc(dst: []byte) {
|
||||||
|
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:]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package rand
|
||||||
|
|
||||||
|
foreign import "odin_env"
|
||||||
|
foreign odin_env {
|
||||||
|
@(link_name = "rand_bytes")
|
||||||
|
env_rand_bytes :: proc "contextless" (buf: []byte) ---
|
||||||
|
}
|
||||||
|
|
||||||
|
@(require_results)
|
||||||
|
_system_random :: proc() -> u64 {
|
||||||
|
buf: [8]u8
|
||||||
|
env_rand_bytes(buf[:])
|
||||||
|
return transmute(u64)buf
|
||||||
|
}
|
||||||
Vendored
+5
@@ -1349,6 +1349,11 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) {
|
|||||||
ln: Math.log,
|
ln: Math.log,
|
||||||
exp: Math.exp,
|
exp: Math.exp,
|
||||||
ldexp: (x, exp) => x * Math.pow(2, exp),
|
ldexp: (x, exp) => x * Math.pow(2, exp),
|
||||||
|
|
||||||
|
rand_bytes: (ptr, len) => {
|
||||||
|
const view = new Uint8Array(wasmMemoryInterface.memory.buffer, ptr, len)
|
||||||
|
crypto.getRandomValues(view)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
"odin_dom": {
|
"odin_dom": {
|
||||||
init_event_raw: (ep) => {
|
init_event_raw: (ep) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user