Skip to content

Commit

Permalink
wasm32: Add a wasm32_unknown_unknown_js feature to control use of w…
Browse files Browse the repository at this point in the history
…eb 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.
  • Loading branch information
briansmith committed Oct 6, 2021
1 parent d97ae1f commit 8d78cb2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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

Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 5 additions & 4 deletions src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -230,6 +230,7 @@ mod sysrand_chunk {
}

#[cfg(all(
feature = "wasm32_unknown_unknown_js",
target_arch = "wasm32",
target_vendor = "unknown",
target_os = "unknown",
Expand Down

0 comments on commit 8d78cb2

Please sign in to comment.