diff options
| author | gingerBill <bill@gingerbill.org> | 2023-10-27 13:03:28 +0100 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2023-10-27 13:03:28 +0100 |
| commit | 0a7b9338f6dcf4aa05206ebbf5c8525fa4d15df3 (patch) | |
| tree | af442101fb47b3ce3097581afca229f91f4584d8 | |
| parent | bc0fa1240ba9230d6ac5c99c77a509ce7e5cfb3e (diff) | |
| parent | 292398dbe2a07ccb46023bca894f7b5f224c2a4e (diff) | |
Merge branch 'master' of https://github.com/odin-lang/Odin
| -rw-r--r-- | core/crypto/rand_generic.odin | 4 | ||||
| -rw-r--r-- | core/crypto/rand_js.odin | 20 | ||||
| -rw-r--r-- | core/math/rand/system_js.odin | 14 | ||||
| -rw-r--r-- | vendor/wasm/js/runtime.js | 5 |
4 files changed, 41 insertions, 2 deletions
diff --git a/core/crypto/rand_generic.odin b/core/crypto/rand_generic.odin index 52abfe4d7..fde91f85a 100644 --- a/core/crypto/rand_generic.odin +++ b/core/crypto/rand_generic.odin @@ -1,7 +1,7 @@ package crypto -when ODIN_OS != .Linux && ODIN_OS != .OpenBSD && ODIN_OS != .Windows { - _rand_bytes :: proc (dst: []byte) { +when ODIN_OS != .Linux && ODIN_OS != .OpenBSD && ODIN_OS != .Windows && ODIN_OS != .JS { + _rand_bytes :: proc(dst: []byte) { unimplemented("crypto: rand_bytes not supported on this OS") } } diff --git a/core/crypto/rand_js.odin b/core/crypto/rand_js.odin new file mode 100644 index 000000000..353b1e6b9 --- /dev/null +++ b/core/crypto/rand_js.odin @@ -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:] + } +} diff --git a/core/math/rand/system_js.odin b/core/math/rand/system_js.odin new file mode 100644 index 000000000..b9b71c4a6 --- /dev/null +++ b/core/math/rand/system_js.odin @@ -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 +} diff --git a/vendor/wasm/js/runtime.js b/vendor/wasm/js/runtime.js index d5ab383f0..78fdcca18 100644 --- a/vendor/wasm/js/runtime.js +++ b/vendor/wasm/js/runtime.js @@ -1349,6 +1349,11 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { ln: Math.log, exp: Math.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": { init_event_raw: (ep) => { |