@@ -13,25 +13,18 @@ use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
1313// See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
1414const MAX_BUFFER_SIZE : usize = 65536 ;
1515
16- pub fn fill_inner ( dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
17- CRYPTO . with ( |crypto| {
18- let crypto = crypto. as_ref ( ) . ok_or ( Error :: WEB_CRYPTO ) ?;
19- inner ( crypto, dest)
20- } )
21- }
22-
2316#[ cfg( not( target_feature = "atomics" ) ) ]
24- fn inner ( crypto : & Crypto , dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
17+ fn fill_inner ( dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
2518 for chunk in dest. chunks_mut ( MAX_BUFFER_SIZE ) {
26- if crypto . get_random_values ( chunk) . is_err ( ) {
27- return Err ( Error :: WEB_GET_RANDOM_VALUES ) ;
19+ if get_random_values ( chunk) . is_err ( ) {
20+ return Err ( Error :: WEB_CRYPTO ) ;
2821 }
2922 }
3023 Ok ( ( ) )
3124}
3225
3326#[ cfg( target_feature = "atomics" ) ]
34- fn inner ( crypto : & Crypto , dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
27+ fn fill_inner ( dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
3528 // getRandomValues does not work with all types of WASM memory,
3629 // so we initially write to browser memory to avoid exceptions.
3730 let buf_len = usize:: min ( dest. len ( ) , MAX_BUFFER_SIZE ) ;
@@ -52,8 +45,8 @@ fn inner(crypto: &Crypto, dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
5245 & buf. subarray ( 0 , chunk_len)
5346 } ;
5447
55- if crypto . get_random_values ( sub_buf) . is_err ( ) {
56- return Err ( Error :: WEB_GET_RANDOM_VALUES ) ;
48+ if get_random_values ( sub_buf) . is_err ( ) {
49+ return Err ( Error :: WEB_CRYPTO ) ;
5750 }
5851
5952 // SAFETY: `sub_buf`'s length is the same length as `chunk`
@@ -64,16 +57,11 @@ fn inner(crypto: &Crypto, dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
6457
6558#[ wasm_bindgen]
6659extern "C" {
67- // Web Crypto API: Crypto interface (https://www.w3.org/TR/WebCryptoAPI/)
68- type Crypto ;
69- // Holds the global `Crypto` object.
70- #[ wasm_bindgen( thread_local_v2, js_namespace = globalThis, js_name = crypto) ]
71- static CRYPTO : Option < Crypto > ;
7260 // Crypto.getRandomValues()
7361 #[ cfg( not( target_feature = "atomics" ) ) ]
74- #[ wasm_bindgen( method , js_name = getRandomValues, catch) ]
75- fn get_random_values ( this : & Crypto , buf : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , JsValue > ;
62+ #[ wasm_bindgen( js_namespace = [ "globalThis" , "crypto" ] , js_name = getRandomValues, catch) ]
63+ fn get_random_values ( buf : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , JsValue > ;
7664 #[ cfg( target_feature = "atomics" ) ]
77- #[ wasm_bindgen( method , js_name = getRandomValues, catch) ]
78- fn get_random_values ( this : & Crypto , buf : & js_sys:: Uint8Array ) -> Result < ( ) , JsValue > ;
65+ #[ wasm_bindgen( js_namespace = [ "globalThis" , "crypto" ] , js_name = getRandomValues, catch) ]
66+ fn get_random_values ( buf : & js_sys:: Uint8Array ) -> Result < ( ) , JsValue > ;
7967}
0 commit comments