diff options
| author | Damien George <damien@micropython.org> | 2023-12-13 13:10:03 +1100 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2024-03-22 13:05:54 +1100 |
| commit | 76898cbfa1b8c48ec5f2ac75bc060675895bb67a (patch) | |
| tree | 762892f7420ef30af934b97ac6ff430edbb31b57 | |
| parent | 8282bd93a2381dcc235ee89093832bb5e76e8102 (diff) | |
webassembly: Implement MICROPY_PY_RANDOM_SEED_INIT_FUNC.
Signed-off-by: Damien George <damien@micropython.org>
| -rw-r--r-- | ports/webassembly/library.h | 1 | ||||
| -rw-r--r-- | ports/webassembly/library.js | 7 | ||||
| -rw-r--r-- | ports/webassembly/mpconfigport.h | 4 |
3 files changed, 12 insertions, 0 deletions
diff --git a/ports/webassembly/library.h b/ports/webassembly/library.h index 47982e064..21027c137 100644 --- a/ports/webassembly/library.h +++ b/ports/webassembly/library.h @@ -29,3 +29,4 @@ extern void mp_js_write(const char *str, mp_uint_t len); extern int mp_js_ticks_ms(void); extern void mp_js_hook(void); +extern uint32_t mp_js_random_u32(void); diff --git a/ports/webassembly/library.js b/ports/webassembly/library.js index f554f681c..4a17942bb 100644 --- a/ports/webassembly/library.js +++ b/ports/webassembly/library.js @@ -63,4 +63,11 @@ mergeInto(LibraryManager.library, { } } }, + + // Node prior to v19 did not expose "crypto" as a global, so make sure it exists. + mp_js_random_u32__postset: + "if (globalThis.crypto === undefined) { globalThis.crypto = require('crypto'); }", + + mp_js_random_u32: () => + globalThis.crypto.getRandomValues(new Uint32Array(1))[0], }); diff --git a/ports/webassembly/mpconfigport.h b/ports/webassembly/mpconfigport.h index e0d1af0cc..43a029c53 100644 --- a/ports/webassembly/mpconfigport.h +++ b/ports/webassembly/mpconfigport.h @@ -49,6 +49,8 @@ #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE) #define MICROPY_USE_INTERNAL_ERRNO (1) #define MICROPY_USE_INTERNAL_PRINTF (0) + +#define MICROPY_PY_RANDOM_SEED_INIT_FUNC (mp_js_random_u32()) #ifndef MICROPY_VFS #define MICROPY_VFS (1) #endif @@ -95,3 +97,5 @@ typedef long mp_off_t; // _GNU_SOURCE must be defined to get definitions of DT_xxx symbols from dirent.h. #define _GNU_SOURCE #endif + +uint32_t mp_js_random_u32(void); |
