-
Notifications
You must be signed in to change notification settings - Fork 166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix build for wasm32/64-unknown-unknown without std #395
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
//! Implementation for WASM based on Web and Node.js | ||
use crate::Error; | ||
|
||
extern crate std; | ||
use std::{mem::MaybeUninit, thread_local}; | ||
use core::mem::MaybeUninit; | ||
|
||
#[cfg(not(std))] | ||
use core::cell::RefCell; | ||
|
||
use js_sys::{global, Function, Uint8Array}; | ||
use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue}; | ||
|
@@ -20,14 +22,17 @@ enum RngSource { | |
|
||
// JsValues are always per-thread, so we initialize RngSource for each thread. | ||
// See: https://github.com/rustwasm/wasm-bindgen/pull/955 | ||
thread_local!( | ||
#[cfg(std)] | ||
std::thread_local!( | ||
static RNG_SOURCE: Result<RngSource, Error> = getrandom_init(); | ||
); | ||
|
||
pub(crate) fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
RNG_SOURCE.with(|result| { | ||
let source = result.as_ref().map_err(|&e| e)?; | ||
#[cfg(not(std))] | ||
#[thread_local] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, you're right however if I'm not mistaken this is the only alternative for no_std envs |
||
static RNG_SOURCE: RefCell<Option<RngSource>> = RefCell::new(None); | ||
|
||
pub(crate) fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
let mut getrandom_impl = |source: &RngSource| { | ||
match source { | ||
RngSource::Node(n) => { | ||
for chunk in dest.chunks_mut(NODE_MAX_BUFFER_SIZE) { | ||
|
@@ -64,8 +69,23 @@ pub(crate) fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> | |
} | ||
} | ||
}; | ||
|
||
Ok(()) | ||
}) | ||
}; | ||
|
||
#[cfg(not(std))] | ||
{ | ||
if RNG_SOURCE.borrow().is_none() { | ||
let rng_source = getrandom_init()?; | ||
*RNG_SOURCE.borrow_mut() = Some(rng_source); | ||
} | ||
|
||
let binding = RNG_SOURCE.borrow(); | ||
getrandom_impl(binding.as_ref().expect("initialized above")) | ||
} | ||
|
||
#[cfg(std)] | ||
RNG_SOURCE.with(|result| getrandom_impl(result.as_ref().map_err(|&e| e)?)) | ||
} | ||
|
||
fn getrandom_init() -> Result<RngSource, Error> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a breaking change.