Skip to content

Commit d1e11f0

Browse files
authored
fix: use global() to get performance API on wasm32-unknown-unknown (#107)
1 parent 64e4d80 commit d1e11f0

File tree

5 files changed

+59
-32
lines changed

5 files changed

+59
-32
lines changed

Diff for: Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "quanta"
33
version = "0.12.3"
44
authors = ["Toby Lawrence <[email protected]>"]
55
edition = "2021"
6-
rust-version = "1.60"
6+
rust-version = "1.70"
77

88
license = "MIT"
99

Diff for: src/clocks/monotonic/mod.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ mod windows;
33
#[cfg(target_os = "windows")]
44
pub use self::windows::Monotonic;
55

6-
#[cfg(target_arch = "wasm32")]
7-
mod wasm;
8-
#[cfg(target_arch = "wasm32")]
9-
pub use self::wasm::Monotonic;
6+
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
7+
mod wasm_browser;
8+
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
9+
pub use self::wasm_browser::Monotonic;
10+
11+
#[cfg(all(target_arch = "wasm32", target_os = "wasi"))]
12+
mod wasm_wasi;
13+
#[cfg(all(target_arch = "wasm32", target_os = "wasi"))]
14+
pub use self::wasm_wasi::Monotonic;
1015

1116
#[cfg(not(any(target_os = "windows", target_arch = "wasm32")))]
1217
mod unix;

Diff for: src/clocks/monotonic/wasm.rs

-27
This file was deleted.

Diff for: src/clocks/monotonic/wasm_browser.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use std::cell::OnceCell;
2+
3+
use web_sys::{
4+
js_sys::Reflect,
5+
wasm_bindgen::{JsCast, JsValue},
6+
Performance,
7+
};
8+
9+
const WASM_MISSING_GLOBAL_THIS_PERF: &str = "failed to find `globalThis.performance`";
10+
const WASM_UNABLE_TO_CAST_PERF: &str =
11+
"Unable to cast `globalThis.performance` to Performance type";
12+
13+
thread_local! {
14+
static GLOBAL_PERFORMANCE_INSTANCE: OnceCell<Performance> = const { OnceCell::new() };
15+
}
16+
17+
#[derive(Clone, Copy, Debug, Default)]
18+
pub struct Monotonic {
19+
_default: (),
20+
}
21+
22+
impl Monotonic {
23+
pub fn now(&self) -> u64 {
24+
let now = GLOBAL_PERFORMANCE_INSTANCE.with(|value| {
25+
let performance_instance = value.get_or_init(|| {
26+
Reflect::get(
27+
&web_sys::js_sys::global(),
28+
&JsValue::from_str("performance"),
29+
)
30+
.expect(WASM_MISSING_GLOBAL_THIS_PERF)
31+
.dyn_into::<Performance>()
32+
.expect(WASM_UNABLE_TO_CAST_PERF)
33+
});
34+
performance_instance.now()
35+
});
36+
// `performance.now()` returns the time in milliseconds.
37+
f64::trunc(now * 1_000_000.0) as u64
38+
}
39+
}

Diff for: src/clocks/monotonic/wasm_wasi.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[derive(Clone, Copy, Debug, Default)]
2+
pub struct Monotonic {
3+
_default: (),
4+
}
5+
6+
impl Monotonic {
7+
pub fn now(&self) -> u64 {
8+
unsafe { wasi::clock_time_get(wasi::CLOCKID_MONOTONIC, 1).expect("failed to get time") }
9+
}
10+
}

0 commit comments

Comments
 (0)