Skip to content

fix: use global() to get performance API on wasm32-unknown-unknown #107

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

Merged
merged 5 commits into from
Dec 12, 2024
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "quanta"
version = "0.12.3"
authors = ["Toby Lawrence <toby@nuclearfurnace.com>"]
edition = "2021"
rust-version = "1.60"
rust-version = "1.70"

license = "MIT"

Expand Down
13 changes: 9 additions & 4 deletions src/clocks/monotonic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ mod windows;
#[cfg(target_os = "windows")]
pub use self::windows::Monotonic;

#[cfg(target_arch = "wasm32")]
mod wasm;
#[cfg(target_arch = "wasm32")]
pub use self::wasm::Monotonic;
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
mod wasm_browser;
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
pub use self::wasm_browser::Monotonic;

#[cfg(all(target_arch = "wasm32", target_os = "wasi"))]
mod wasm_wasi;
#[cfg(all(target_arch = "wasm32", target_os = "wasi"))]
pub use self::wasm_wasi::Monotonic;

#[cfg(not(any(target_os = "windows", target_arch = "wasm32")))]
mod unix;
Expand Down
27 changes: 0 additions & 27 deletions src/clocks/monotonic/wasm.rs

This file was deleted.

39 changes: 39 additions & 0 deletions src/clocks/monotonic/wasm_browser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::cell::OnceCell;

use web_sys::{
js_sys::Reflect,
wasm_bindgen::{JsCast, JsValue},
Performance,
};

const WASM_MISSING_GLOBAL_THIS_PERF: &str = "failed to find `globalThis.performance`";
const WASM_UNABLE_TO_CAST_PERF: &str =
"Unable to cast `globalThis.performance` to Performance type";

thread_local! {
static GLOBAL_PERFORMANCE_INSTANCE: OnceCell<Performance> = const { OnceCell::new() };
}

#[derive(Clone, Copy, Debug, Default)]
pub struct Monotonic {
_default: (),
}

impl Monotonic {
pub fn now(&self) -> u64 {
let now = GLOBAL_PERFORMANCE_INSTANCE.with(|value| {
let performance_instance = value.get_or_init(|| {
Reflect::get(
&web_sys::js_sys::global(),
&JsValue::from_str("performance"),
)
.expect(WASM_MISSING_GLOBAL_THIS_PERF)
.dyn_into::<Performance>()
.expect(WASM_UNABLE_TO_CAST_PERF)
});
performance_instance.now()
});
// `performance.now()` returns the time in milliseconds.
f64::trunc(now * 1_000_000.0) as u64
}
}
10 changes: 10 additions & 0 deletions src/clocks/monotonic/wasm_wasi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[derive(Clone, Copy, Debug, Default)]
pub struct Monotonic {
_default: (),
}

impl Monotonic {
pub fn now(&self) -> u64 {
unsafe { wasi::clock_time_get(wasi::CLOCKID_MONOTONIC, 1).expect("failed to get time") }
}
}
Loading