Skip to content
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

Add support for WASM targets running in a custom runtime #83

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/zxcvbn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,6 @@ jobs:
run: cargo build --target wasm32-unknown-unknown --tests --benches

- name: Run tests (wasm, all features)
run: wasm-pack test --node --all-features
env:
ALL_WASM_BINDGEN_FEATURES: "default,ser,builder"
run: wasm-pack test --node --features $ALL_WASM_BINDGEN_FEATURES
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ regex = "1"
time = { version = "0.3" }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"] }
chrono = "0.4.38"
wasm-bindgen = "0.2"
web-sys = { version = "0.3", features = ["Performance"] }

Expand All @@ -41,12 +41,14 @@ serde_json = "1"

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
criterion = { version = "0.5", default-features = false }
getrandom = { version = "0.2", features = ["js"] }
wasm-bindgen-test = "0.3"

[features]
default = ["builder"]
ser = ["serde"]
builder = ["derive_builder"]
custom_wasm_env = []

[profile.test]
opt-level = 2
Expand Down
22 changes: 19 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![doc = include_str!("../README.md")]
#![recursion_limit = "128"]
#![warn(missing_docs)]
#![forbid(unsafe_code)]

#[macro_use]
#[cfg(feature = "builder")]
Expand All @@ -18,7 +17,7 @@ extern crate quickcheck;

pub use scoring::Score;
use time_estimates::CrackTimes;
#[cfg(target_arch = "wasm32")]
#[cfg(all(target_arch = "wasm32", not(feature = "custom_wasm_env")))]
use wasm_bindgen::prelude::wasm_bindgen;

pub use crate::matching::Match;
Expand All @@ -42,7 +41,7 @@ where
(result, calc_time)
}

#[cfg(target_arch = "wasm32")]
#[cfg(all(target_arch = "wasm32", not(feature = "custom_wasm_env")))]
#[allow(non_upper_case_globals)]
fn time_scoped<F, R>(f: F) -> (R, Duration)
where
Expand All @@ -61,6 +60,23 @@ where
(result, calc_time)
}

#[cfg(all(target_arch = "wasm32", feature = "custom_wasm_env"))]
fn time_scoped<F, R>(f: F) -> (R, Duration)
where
F: FnOnce() -> R,
{
#[link(wasm_import_module = "zxcvbn")]
extern "C" {
fn unix_time_milliseconds_imported() -> u64;
}
let start_time = unsafe { unix_time_milliseconds_imported() };
let result = f();
let end_time = unsafe { unix_time_milliseconds_imported() };

let duration = std::time::Duration::from_millis(end_time - start_time);
(result, duration)
}

/// Contains the results of an entropy calculation
#[derive(Debug, Clone)]
#[cfg_attr(feature = "ser", derive(serde::Serialize))]
Expand Down
19 changes: 18 additions & 1 deletion src/scoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,31 @@
pub(crate) static ref REFERENCE_YEAR: i32 = time::OffsetDateTime::now_utc().year();
}

#[cfg(target_arch = "wasm32")]
#[cfg(all(target_arch = "wasm32", not(feature = "custom_wasm_env")))]
lazy_static! {
pub(crate) static ref REFERENCE_YEAR: i32 = web_sys::js_sys::Date::new_0()
.get_full_year()
.try_into()
.unwrap();
}

#[cfg(all(target_arch = "wasm32", feature = "custom_wasm_env"))]
lazy_static! {
pub(crate) static ref REFERENCE_YEAR: i32 = {
#[link(wasm_import_module = "zxcvbn")]
extern "C" {
fn unix_time_milliseconds_imported() -> u64;
}
let unix_millis = unsafe { unix_time_milliseconds_imported() };

use chrono::Datelike;
chrono::DateTime::<chrono::Utc>::from(
std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_millis(unix_millis),
)
.year()
};
}

const MIN_YEAR_SPACE: i32 = 20;
const BRUTEFORCE_CARDINALITY: u64 = 10;
const MIN_GUESSES_BEFORE_GROWING_SEQUENCE: u64 = 10_000;
Expand Down Expand Up @@ -544,7 +561,7 @@

quickcheck! {
fn test_n_ck_mul_overflow(n: usize, k: usize) -> TestResult {
if n >= 63 && n <= 100 {

Check warning on line 564 in src/scoring.rs

View workflow job for this annotation

GitHub Actions / clippy-rustfmt

manual `RangeInclusive::contains` implementation
scoring::n_ck(n, k); // Must not panic
TestResult::from_bool(true)
} else {
Expand Down Expand Up @@ -837,7 +854,7 @@
};
assert_eq!(
p.estimate(token),
(*scoring::REFERENCE_YEAR - 1972).abs() as u64

Check warning on line 857 in src/scoring.rs

View workflow job for this annotation

GitHub Actions / clippy-rustfmt

casting the result of `i32::abs()` to u64
);
}

Expand Down Expand Up @@ -872,7 +889,7 @@
let token = "1123";
assert_eq!(
p.estimate(token),
365 * (*scoring::REFERENCE_YEAR - p.year).abs() as u64

Check warning on line 892 in src/scoring.rs

View workflow job for this annotation

GitHub Actions / clippy-rustfmt

casting the result of `i32::abs()` to u64
);
}

Expand Down Expand Up @@ -900,7 +917,7 @@
let base_guesses = *scoring::KEYBOARD_STARTING_POSITIONS
* *scoring::KEYBOARD_AVERAGE_DEGREE
* (token.len() - 1) as u64;
assert_eq!(p.estimate(token), base_guesses as u64);

Check warning on line 920 in src/scoring.rs

View workflow job for this annotation

GitHub Actions / clippy-rustfmt

casting to the same type is unnecessary (`u64` -> `u64`)
}

#[test]
Expand Down
Loading