Skip to content

Commit

Permalink
webassembly: Implement MICROPY_PY_RANDOM_SEED_INIT_FUNC.
Browse files Browse the repository at this point in the history
Signed-off-by: Damien George <[email protected]>
  • Loading branch information
dpgeorge committed Mar 22, 2024
1 parent 8282bd9 commit 76898cb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions ports/webassembly/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
7 changes: 7 additions & 0 deletions ports/webassembly/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
});
4 changes: 4 additions & 0 deletions ports/webassembly/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

0 comments on commit 76898cb

Please sign in to comment.