diff --git a/src/library_uuid.js b/src/library_uuid.js index 14429bea6e1a1..ef1cf4929cbc2 100644 --- a/src/library_uuid.js +++ b/src/library_uuid.js @@ -24,37 +24,11 @@ addToLibrary({ // Write a RFC4122 version 4 compliant UUID largely based on the method found in // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript // tweaked slightly in order to use the 'compact' UUID form used by libuuid. - uuid_generate__deps: ['$writeArrayToMemory'], + uuid_generate__deps: ['$writeArrayToMemory', '$randomFill'], uuid_generate: (out) => { // void uuid_generate(uuid_t out); - var uuid = null; - - if (ENVIRONMENT_IS_NODE) { -#if ENVIRONMENT_MAY_BE_NODE - // If Node.js try to use crypto.randomBytes - try { - var rb = require('crypto')['randomBytes']; - uuid = rb(16); - } catch(e) {} -#endif // ENVIRONMENT_MAY_BE_NODE - } else if (ENVIRONMENT_IS_WEB && - typeof window.crypto != 'undefined' && - typeof window.crypto.getRandomValues != 'undefined') { - // If crypto.getRandomValues is available try to use it. - uuid = new Uint8Array(16); - window.crypto.getRandomValues(uuid); - } - - // Fall back to Math.random if a higher quality random number generator is not available. - if (!uuid) { - uuid = new Array(16); - var d = new Date().getTime(); - for (var i = 0; i < 16; i++) { - var r = ((d + Math.random() * 256) % 256)|0; - d = (d / 256)|0; - uuid[i] = r; - } - } + var uuid = new Uint8Array(16); + randomFill(uuid); // Makes uuid compliant to RFC-4122 uuid[6] = (uuid[6] & 0x0F) | 0x40; // uuid version diff --git a/src/library_wasi.js b/src/library_wasi.js index ceca145428409..820b5bb67bf62 100644 --- a/src/library_wasi.js +++ b/src/library_wasi.js @@ -586,9 +586,8 @@ var WasiLibrary = { return (view) => crypto_module['randomFillSync'](view); } // very old nodejs with the original crypto API - var randomBytes = crypto_module['randomBytes']; return (view) => ( - view.set(randomBytes(view.byteLength)), + view.set(crypto_module['randomBytes'](view.byteLength)), // Return the original view to match modern native implementations. view ); diff --git a/test/other/codesize/test_codesize_cxx_ctors1.gzsize b/test/other/codesize/test_codesize_cxx_ctors1.gzsize index b2fd8c7b992c7..1c8e7ca38f8d2 100644 --- a/test/other/codesize/test_codesize_cxx_ctors1.gzsize +++ b/test/other/codesize/test_codesize_cxx_ctors1.gzsize @@ -1 +1 @@ -8408 +8405 diff --git a/test/other/codesize/test_codesize_cxx_ctors1.jssize b/test/other/codesize/test_codesize_cxx_ctors1.jssize index a20e18243e7cb..1bdf007f3f5c0 100644 --- a/test/other/codesize/test_codesize_cxx_ctors1.jssize +++ b/test/other/codesize/test_codesize_cxx_ctors1.jssize @@ -1 +1 @@ -20486 +20468 diff --git a/test/other/codesize/test_codesize_cxx_ctors2.gzsize b/test/other/codesize/test_codesize_cxx_ctors2.gzsize index cdc91cd424110..a559385da7f91 100644 --- a/test/other/codesize/test_codesize_cxx_ctors2.gzsize +++ b/test/other/codesize/test_codesize_cxx_ctors2.gzsize @@ -1 +1 @@ -8392 +8390 diff --git a/test/other/codesize/test_codesize_cxx_ctors2.jssize b/test/other/codesize/test_codesize_cxx_ctors2.jssize index 0ca3697c3144a..223fd65b313b1 100644 --- a/test/other/codesize/test_codesize_cxx_ctors2.jssize +++ b/test/other/codesize/test_codesize_cxx_ctors2.jssize @@ -1 +1 @@ -20454 +20436 diff --git a/test/other/codesize/test_codesize_cxx_except.gzsize b/test/other/codesize/test_codesize_cxx_except.gzsize index 199a0b963f1ab..9d058193a3182 100644 --- a/test/other/codesize/test_codesize_cxx_except.gzsize +++ b/test/other/codesize/test_codesize_cxx_except.gzsize @@ -1 +1 @@ -9413 +9412 diff --git a/test/other/codesize/test_codesize_cxx_except.jssize b/test/other/codesize/test_codesize_cxx_except.jssize index d80dc789e6880..9da24a411ba03 100644 --- a/test/other/codesize/test_codesize_cxx_except.jssize +++ b/test/other/codesize/test_codesize_cxx_except.jssize @@ -1 +1 @@ -24255 +24237 diff --git a/test/other/codesize/test_codesize_cxx_except_wasm.gzsize b/test/other/codesize/test_codesize_cxx_except_wasm.gzsize index 0b2a5d20da7ef..baf7c81d38c48 100644 --- a/test/other/codesize/test_codesize_cxx_except_wasm.gzsize +++ b/test/other/codesize/test_codesize_cxx_except_wasm.gzsize @@ -1 +1 @@ -8374 +8370 diff --git a/test/other/codesize/test_codesize_cxx_except_wasm.jssize b/test/other/codesize/test_codesize_cxx_except_wasm.jssize index 903c819cfe0ac..8d36e4f399dbe 100644 --- a/test/other/codesize/test_codesize_cxx_except_wasm.jssize +++ b/test/other/codesize/test_codesize_cxx_except_wasm.jssize @@ -1 +1 @@ -20379 +20361 diff --git a/test/other/codesize/test_codesize_cxx_except_wasm_exnref.gzsize b/test/other/codesize/test_codesize_cxx_except_wasm_exnref.gzsize index 0b2a5d20da7ef..baf7c81d38c48 100644 --- a/test/other/codesize/test_codesize_cxx_except_wasm_exnref.gzsize +++ b/test/other/codesize/test_codesize_cxx_except_wasm_exnref.gzsize @@ -1 +1 @@ -8374 +8370 diff --git a/test/other/codesize/test_codesize_cxx_except_wasm_exnref.jssize b/test/other/codesize/test_codesize_cxx_except_wasm_exnref.jssize index 903c819cfe0ac..8d36e4f399dbe 100644 --- a/test/other/codesize/test_codesize_cxx_except_wasm_exnref.jssize +++ b/test/other/codesize/test_codesize_cxx_except_wasm_exnref.jssize @@ -1 +1 @@ -20379 +20361 diff --git a/test/other/codesize/test_codesize_cxx_lto.gzsize b/test/other/codesize/test_codesize_cxx_lto.gzsize index 9fa65500fcae8..5f41444ba9dcc 100644 --- a/test/other/codesize/test_codesize_cxx_lto.gzsize +++ b/test/other/codesize/test_codesize_cxx_lto.gzsize @@ -1 +1 @@ -8406 +8404 diff --git a/test/other/codesize/test_codesize_cxx_lto.jssize b/test/other/codesize/test_codesize_cxx_lto.jssize index c7e497cb94f12..356db77432d95 100644 --- a/test/other/codesize/test_codesize_cxx_lto.jssize +++ b/test/other/codesize/test_codesize_cxx_lto.jssize @@ -1 +1 @@ -20510 +20492 diff --git a/test/other/codesize/test_codesize_cxx_mangle.gzsize b/test/other/codesize/test_codesize_cxx_mangle.gzsize index b92fe18e229cc..54e18a6e2baed 100644 --- a/test/other/codesize/test_codesize_cxx_mangle.gzsize +++ b/test/other/codesize/test_codesize_cxx_mangle.gzsize @@ -1 +1 @@ -9417 +9416 diff --git a/test/other/codesize/test_codesize_cxx_mangle.jssize b/test/other/codesize/test_codesize_cxx_mangle.jssize index d80dc789e6880..9da24a411ba03 100644 --- a/test/other/codesize/test_codesize_cxx_mangle.jssize +++ b/test/other/codesize/test_codesize_cxx_mangle.jssize @@ -1 +1 @@ -24255 +24237 diff --git a/test/other/codesize/test_codesize_cxx_noexcept.gzsize b/test/other/codesize/test_codesize_cxx_noexcept.gzsize index b2fd8c7b992c7..1c8e7ca38f8d2 100644 --- a/test/other/codesize/test_codesize_cxx_noexcept.gzsize +++ b/test/other/codesize/test_codesize_cxx_noexcept.gzsize @@ -1 +1 @@ -8408 +8405 diff --git a/test/other/codesize/test_codesize_cxx_noexcept.jssize b/test/other/codesize/test_codesize_cxx_noexcept.jssize index a20e18243e7cb..1bdf007f3f5c0 100644 --- a/test/other/codesize/test_codesize_cxx_noexcept.jssize +++ b/test/other/codesize/test_codesize_cxx_noexcept.jssize @@ -1 +1 @@ -20486 +20468 diff --git a/test/other/codesize/test_codesize_cxx_wasmfs.gzsize b/test/other/codesize/test_codesize_cxx_wasmfs.gzsize index 384632eff7fe6..03c58a00d4d8e 100644 --- a/test/other/codesize/test_codesize_cxx_wasmfs.gzsize +++ b/test/other/codesize/test_codesize_cxx_wasmfs.gzsize @@ -1 +1 @@ -3651 +3646 diff --git a/test/other/codesize/test_codesize_cxx_wasmfs.jssize b/test/other/codesize/test_codesize_cxx_wasmfs.jssize index 122c645d8cd89..dbce50b5ba14f 100644 --- a/test/other/codesize/test_codesize_cxx_wasmfs.jssize +++ b/test/other/codesize/test_codesize_cxx_wasmfs.jssize @@ -1 +1 @@ -7910 +7892 diff --git a/test/other/codesize/test_codesize_files_js_fs.gzsize b/test/other/codesize/test_codesize_files_js_fs.gzsize index 3eb330568eb87..c4d3fc86b6c91 100644 --- a/test/other/codesize/test_codesize_files_js_fs.gzsize +++ b/test/other/codesize/test_codesize_files_js_fs.gzsize @@ -1 +1 @@ -7704 +7699 diff --git a/test/other/codesize/test_codesize_files_js_fs.jssize b/test/other/codesize/test_codesize_files_js_fs.jssize index 33dab3f6f464f..08e5419ab0b97 100644 --- a/test/other/codesize/test_codesize_files_js_fs.jssize +++ b/test/other/codesize/test_codesize_files_js_fs.jssize @@ -1 +1 @@ -18980 +18962 diff --git a/test/other/codesize/test_codesize_files_wasmfs.gzsize b/test/other/codesize/test_codesize_files_wasmfs.gzsize index a17ea6fb4828b..693832e1f702b 100644 --- a/test/other/codesize/test_codesize_files_wasmfs.gzsize +++ b/test/other/codesize/test_codesize_files_wasmfs.gzsize @@ -1 +1 @@ -2882 +2880 diff --git a/test/other/codesize/test_codesize_files_wasmfs.jssize b/test/other/codesize/test_codesize_files_wasmfs.jssize index d665f60e5a1bc..a1c8982c1d6cd 100644 --- a/test/other/codesize/test_codesize_files_wasmfs.jssize +++ b/test/other/codesize/test_codesize_files_wasmfs.jssize @@ -1 +1 @@ -6198 +6180 diff --git a/test/test_other.py b/test/test_other.py index 036a0709b5ea3..59ef936fcbaea 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -15243,10 +15243,10 @@ def test_uuid(self): js_out = read_file('test_uuid.js') - # Check that test.js compiled with --closure 1 contains ").randomBytes" and - # "window.crypto.getRandomValues" - self.assertContained(").randomBytes", js_out) - self.assertContained("window.crypto.getRandomValues", js_out) + # Check that test.js compiled with --closure 1 contains ".randomBytes(" and + # ".getRandomValues(" + self.assertContained(".randomBytes(", js_out) + self.assertContained(".getRandomValues(", js_out) def test_wasm64_no_asan(self): err = self.expect_fail([EMCC, test_file('hello_world.c'), '-sMEMORY64', '-fsanitize=address'])