Skip to content

Commit

Permalink
fix: use global() to get performance API on wasm32-unknown-unknown (#107
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Officeyutong authored Dec 12, 2024
1 parent 64e4d80 commit d1e11f0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 32 deletions.
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 <[email protected]>"]
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") }
}
}

0 comments on commit d1e11f0

Please sign in to comment.