-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use global() to get performance API on wasm32-unknown-unknown (#107
- Loading branch information
1 parent
64e4d80
commit d1e11f0
Showing
5 changed files
with
59 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") } | ||
} | ||
} |