Skip to content

Commit 76898cb

Browse files
committed
webassembly: Implement MICROPY_PY_RANDOM_SEED_INIT_FUNC.
Signed-off-by: Damien George <[email protected]>
1 parent 8282bd9 commit 76898cb

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

ports/webassembly/library.h

+1
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@
2929
extern void mp_js_write(const char *str, mp_uint_t len);
3030
extern int mp_js_ticks_ms(void);
3131
extern void mp_js_hook(void);
32+
extern uint32_t mp_js_random_u32(void);

ports/webassembly/library.js

+7
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,11 @@ mergeInto(LibraryManager.library, {
6363
}
6464
}
6565
},
66+
67+
// Node prior to v19 did not expose "crypto" as a global, so make sure it exists.
68+
mp_js_random_u32__postset:
69+
"if (globalThis.crypto === undefined) { globalThis.crypto = require('crypto'); }",
70+
71+
mp_js_random_u32: () =>
72+
globalThis.crypto.getRandomValues(new Uint32Array(1))[0],
6673
});

ports/webassembly/mpconfigport.h

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE)
5050
#define MICROPY_USE_INTERNAL_ERRNO (1)
5151
#define MICROPY_USE_INTERNAL_PRINTF (0)
52+
53+
#define MICROPY_PY_RANDOM_SEED_INIT_FUNC (mp_js_random_u32())
5254
#ifndef MICROPY_VFS
5355
#define MICROPY_VFS (1)
5456
#endif
@@ -95,3 +97,5 @@ typedef long mp_off_t;
9597
// _GNU_SOURCE must be defined to get definitions of DT_xxx symbols from dirent.h.
9698
#define _GNU_SOURCE
9799
#endif
100+
101+
uint32_t mp_js_random_u32(void);

0 commit comments

Comments
 (0)