wasm32: Add a wasm32_unknown_unknown_js feature to control use of web APIs.

Planning ahead for when WASI is to be supported, require the user to opt into
using web APIs for thw wasm32-unknown-unknown target with a new feature, since
wasm32-unknown-unknown could be used for either web or non-web environments.

Don't bother updating the tests to use this new flag, since the tests aren't
part of the "public API" of *ring*. When we add support for other WebAssembly
environments (e.g. WASI) we'll update the tests then.
This commit is contained in:
Brian Smith 2021-10-06 09:38:44 -07:00
parent d97ae1fd08
commit 8d78cb2c01
4 changed files with 14 additions and 7 deletions

View File

@ -429,7 +429,8 @@ jobs:
strategy:
matrix:
features:
- # Default
- --features=wasm32_unknown_unknown_js
- --no-default-features --features=wasm32_unknown_unknown_js
host_os:
- ubuntu-18.04

View File

@ -174,7 +174,7 @@ once_cell = { version = "1.8.0", default-features = false, features=["std"], opt
once_cell = { version = "1.8.0", default-features = false, features=["std"] }
[target.'cfg(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown", target_env = ""))'.dependencies]
web-sys = { version = "0.3.51", default-features = false, features = ["Crypto", "Window"] }
web-sys = { version = "0.3.51", default-features = false, features = ["Crypto", "Window"], optional = true }
[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3.9", default-features = false, features = ["ntsecapi", "wtypesbase", "processthreadsapi"] }
@ -199,6 +199,7 @@ dev_urandom_fallback = ["once_cell"]
slow_tests = []
std = ["alloc"]
test_logging = []
wasm32_unknown_unknown_js = ["web-sys"]
# XXX: debug = false because of https://github.com/rust-lang/rust/issues/34122

View File

@ -33,7 +33,11 @@
//! <tr><td><code>std</code>
//! <td>Enable features that use libstd, in particular
//! <code>std::error::Error</code> integration. Implies `alloc`.
//!
//! <tr><td><code>wasm32_unknown_unknown_js</code>
//! <td>When this feature is enabled, for the wasm32-unknown-unknown target,
//! Web APIs will be used to implement features like `ring::rand` that
//! require an operating environment of some kind. This has no effect
//! for any other target.
//! </table>
// When running mk/package.sh, don't actually build any code.

View File

@ -134,10 +134,10 @@ impl<T> RandomlyConstructable for T where T: self::sealed::RandomlyConstructable
///
/// On macOS and iOS, `fill()` is implemented using `SecRandomCopyBytes`.
///
/// On wasm32-unknown-unknown (non-WASI), `fill()` is implemented using
/// `window.crypto.getRandomValues()`. It must be used in a context where the
/// global object is a `Window`; i.e. it must not be used in a Worker or a
/// non-browser context.
/// On wasm32-unknown-unknown when the "wasm32_unknown_unknown_js" feature is
/// enabled, `fill()` is implemented using `window.crypto.getRandomValues()`.
/// It must be used in a context where the global object is a `Window`; i.e. it
/// must not be used in a Worker or a non-browser context.
///
/// On Windows, `fill` is implemented using the platform's API for secure
/// random number generation.
@ -230,6 +230,7 @@ mod sysrand_chunk {
}
#[cfg(all(
feature = "wasm32_unknown_unknown_js",
target_arch = "wasm32",
target_vendor = "unknown",
target_os = "unknown",