@@ -58,54 +58,38 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
58
58
}
59
59
60
60
fn getrandom_init ( ) -> Result < RngSource , Error > {
61
- // First up we need to detect if we're running in node.js or a
62
- // browser. To do this we get ahold of the `this` object (in a bit
63
- // of a roundabout fashion).
64
- //
65
- // Once we have `this` we look at its `self` property, which is
66
- // only defined on the web (either a main window or web worker).
67
- let this = Function :: new ( "return this" ) . call ( & JsValue :: undefined ( ) ) ;
68
- assert ! ( this != JsValue :: undefined( ) ) ;
69
- let this = This :: from ( this) ;
70
- let is_browser = this. self_ ( ) != JsValue :: undefined ( ) ;
71
-
72
- if !is_browser {
73
- return Ok ( RngSource :: Node ( node_require ( "crypto" ) ) ) ;
74
- }
61
+ if let Ok ( self_) = Global :: get_self ( ) {
62
+ // If `self` is defined then we're in a browser somehow (main window
63
+ // or web worker). Here we want to try to use
64
+ // `crypto.getRandomValues`, but if `crypto` isn't defined we assume
65
+ // we're in an older web browser and the OS RNG isn't available.
66
+
67
+ let crypto = self_. crypto ( ) ;
68
+ if crypto. is_undefined ( ) {
69
+ return Err ( BINDGEN_CRYPTO_UNDEF ) ;
70
+ }
75
71
76
- // If `self` is defined then we're in a browser somehow (main window
77
- // or web worker). Here we want to try to use
78
- // `crypto.getRandomValues`, but if `crypto` isn't defined we assume
79
- // we're in an older web browser and the OS RNG isn't available.
80
- let crypto = this. crypto ( ) ;
81
- if crypto. is_undefined ( ) {
82
- return Err ( BINDGEN_CRYPTO_UNDEF ) ;
83
- }
72
+ // Test if `crypto.getRandomValues` is undefined as well
73
+ let crypto: BrowserCrypto = crypto. into ( ) ;
74
+ if crypto. get_random_values_fn ( ) . is_undefined ( ) {
75
+ return Err ( BINDGEN_GRV_UNDEF ) ;
76
+ }
84
77
85
- // Test if `crypto.getRandomValues` is undefined as well
86
- let crypto: BrowserCrypto = crypto. into ( ) ;
87
- if crypto. get_random_values_fn ( ) . is_undefined ( ) {
88
- return Err ( BINDGEN_GRV_UNDEF ) ;
78
+ return Ok ( RngSource :: Browser ( crypto) ) ;
89
79
}
90
80
91
- // Ok! `self.crypto.getRandomValues` is a defined value, so let's
92
- // assume we can do browser crypto.
93
- Ok ( RngSource :: Browser ( crypto) )
81
+ return Ok ( RngSource :: Node ( node_require ( "crypto" ) ) ) ;
94
82
}
95
83
96
84
#[ wasm_bindgen]
97
85
extern "C" {
98
- type Function ;
99
- #[ wasm_bindgen( constructor) ]
100
- fn new ( s : & str ) -> Function ;
101
- #[ wasm_bindgen( method) ]
102
- fn call ( this : & Function , self_ : & JsValue ) -> JsValue ;
103
-
104
- type This ;
105
- #[ wasm_bindgen( method, getter, structural, js_name = self ) ]
106
- fn self_ ( me : & This ) -> JsValue ;
86
+ type Global ;
87
+ #[ wasm_bindgen( getter, catch, static_method_of = Global , js_class = self , js_name = self ) ]
88
+ fn get_self ( ) -> Result < Self_ , JsValue > ;
89
+
90
+ type Self_ ;
107
91
#[ wasm_bindgen( method, getter, structural) ]
108
- fn crypto ( me : & This ) -> JsValue ;
92
+ fn crypto ( me : & Self_ ) -> JsValue ;
109
93
110
94
#[ derive( Clone , Debug ) ]
111
95
type BrowserCrypto ;
0 commit comments