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

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

Merged
merged 5 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 10 additions & 4 deletions src/clocks/monotonic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ 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.

54 changes: 54 additions & 0 deletions src/clocks/monotonic/wasm_browser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/// Since threads (WebWorkers) in browser don't shared the same memory space, so no objects would have a chance to be sended to other threads, thus it's safe to implement Send for objects here
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";

struct PerformanceClassWrapper(Performance);

unsafe impl Send for PerformanceClassWrapper {}
unsafe impl Sync for PerformanceClassWrapper {}

static GLOBAL_PERFORMANCE_INSTANCE: std::sync::OnceLock<PerformanceClassWrapper> =
std::sync::OnceLock::new();

#[derive(Clone, Copy, Debug)]
pub struct Monotonic {
performance: &'static Performance,
}

unsafe impl Send for Monotonic {}
unsafe impl Sync for Monotonic {}

impl Default for Monotonic {
fn default() -> Self {
let performance = GLOBAL_PERFORMANCE_INSTANCE.get_or_init(|| {
PerformanceClassWrapper(
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),
)
});

Self {
performance: &performance.0,
}
}
}

impl Monotonic {
pub fn now(&self) -> u64 {
let now = self.performance.now();
// `performance.now()` returns the time in milliseconds.
return 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") }
}
}