From 17f6efb36dfc93bd15fe5934f1c8b12b0a3a716e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 7 Dec 2020 04:44:58 +0000 Subject: [PATCH] remove direct stdweb support --- Cargo.toml | 8 ++-- src/{wasm-bindgen.rs => js.rs} | 0 src/lib.rs | 9 ++-- src/stdweb.rs | 86 ---------------------------------- 4 files changed, 6 insertions(+), 97 deletions(-) rename src/{wasm-bindgen.rs => js.rs} (100%) delete mode 100644 src/stdweb.rs diff --git a/Cargo.toml b/Cargo.toml index 8f43e16b..912a6243 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,12 +27,10 @@ libc = { version = "0.2.64", default-features = false } [target.'cfg(target_os = "wasi")'.dependencies] wasi = "0.10" -[target.'cfg(all(target_arch = "wasm32", target_os = "unknown", cargo_web))'.dependencies] -stdweb = { version = "0.4.18", default-features = false, optional = true } -[target.'cfg(all(target_arch = "wasm32", target_os = "unknown", not(cargo_web)))'.dependencies] +[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies] wasm-bindgen = { version = "0.2.62", default-features = false, optional = true } js-sys = { version = "0.3", optional = true } -[target.'cfg(all(target_arch = "wasm32", target_os = "unknown", not(cargo_web)))'.dev-dependencies] +[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dev-dependencies] wasm-bindgen-test = "0.3.18" [features] @@ -41,7 +39,7 @@ std = [] # Feature to enable fallback RDRAND-based implementation on x86/x86_64 rdrand = [] # Feature to enable JavaScript bindings on wasm32-unknown-unknown -js = ["stdweb", "wasm-bindgen", "js-sys"] +js = ["wasm-bindgen", "js-sys"] # Feature to enable custom RNG implementations custom = [] # Unstable feature to support being a libstd dependency diff --git a/src/wasm-bindgen.rs b/src/js.rs similarity index 100% rename from src/wasm-bindgen.rs rename to src/js.rs diff --git a/src/lib.rs b/src/lib.rs index d19717e0..053c5778 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,9 +69,8 @@ //! that you are building for an environment containing JavaScript, and will //! call the appropriate methods. Both web browser (main window and Web Workers) //! and Node.js environments are supported, invoking the methods -//! [described above](#supported-targets). This crate can be built with either -//! the [wasm-bindgen](https://github.com/rust-lang/rust-bindgen) or -//! [cargo-web](https://github.com/koute/cargo-web) toolchains. +//! [described above](#supported-targets) using the +//! [wasm-bindgen](https://github.com/rust-lang/rust-bindgen) toolchain. //! //! This feature has no effect on targets other than `wasm32-unknown-unknown`. //! @@ -215,9 +214,7 @@ cfg_if! { #[path = "rdrand.rs"] mod imp; } else if #[cfg(all(feature = "js", target_arch = "wasm32", target_os = "unknown"))] { - #[cfg_attr(cargo_web, path = "stdweb.rs")] - #[cfg_attr(not(cargo_web), path = "wasm-bindgen.rs")] - mod imp; + #[path = "js.rs"] mod imp; } else if #[cfg(feature = "custom")] { use custom as imp; } else { diff --git a/src/stdweb.rs b/src/stdweb.rs deleted file mode 100644 index 5a50b68d..00000000 --- a/src/stdweb.rs +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright 2018 Developers of the Rand project. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. -use crate::Error; - -extern crate std; -use std::thread_local; - -use stdweb::js; - -#[derive(Clone, Copy, PartialEq)] -enum RngSource { - Browser, - Node, -} - -thread_local!( - static RNG_SOURCE: Result = getrandom_init(); -); - -pub(crate) fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { - RNG_SOURCE.with(|&source| getrandom_fill(source?, dest)) -} - -fn getrandom_init() -> Result { - if js! { return typeof self === "object"; } == true { - // We are in a Browser or WebWorker - let supported = js! { return typeof self.crypto === "object"; }; - if supported == true { - Ok(RngSource::Browser) - } else { - Err(Error::WEB_CRYPTO) - } - } else { - // We are in Node.js - let supported = js! { - try { - require("crypto"); - return true; - } catch(err) { - return false; - } - }; - if supported == true { - Ok(RngSource::Node) - } else { - Err(Error::NODE_CRYPTO) - } - } -} - -fn getrandom_fill(source: RngSource, dest: &mut [u8]) -> Result<(), Error> { - for chunk in dest.chunks_mut(65536) { - let len = chunk.len() as u32; - let ptr = chunk.as_mut_ptr() as i32; - - let success = js! { - try { - let array = new Uint8Array(@{ len }); - - if @{ source == RngSource::Browser } { - self.crypto.getRandomValues(array); - } else { - require("crypto").randomFillSync(array); - } - - HEAPU8.set(array, @{ ptr }); - return true; - } catch(err) { - return false; - } - }; - - if success != true { - return match source { - RngSource::Browser => Err(Error::WEB_GET_RANDOM_VALUES), - RngSource::Node => Err(Error::NODE_RANDOM_FILL_SYNC), - }; - } - } - Ok(()) -}