oxedyne/fe2o3/fe2o3_core/src/wasm.rs
863 bytes, 1 run
created by r1870400018:13113, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Runtime shims for the `wasm32` target. |
| 2 | //! |
| 3 | //! A browser has no OS monotonic clock, no spawnable threads and no console |
| 4 | //! other than the JavaScript one. Calls such as `Instant::now`, |
| 5 | //! `SystemTime::now` and `thread::spawn` compile for `wasm32-unknown-unknown` |
| 6 | //! but panic at runtime. These helpers give `fe2o3_core`'s clock and logger a |
| 7 | //! panic-free path in the browser whilst the native code is left untouched. |
| 8 | |
| 9 | use wasm_bindgen::JsValue; |
| 10 | |
| 11 | /// Returns the current wall-clock time in milliseconds since the Unix epoch, |
| 12 | /// using the JavaScript `Date.now()` primitive. It never panics and is |
| 13 | /// available on both the main thread and web workers. |
| 14 | pub fn now_ms() -> f64 { |
| 15 | js_sys::Date::now() |
| 16 | } |
| 17 | |
| 18 | /// Emits a single line to the browser console via `console.log`. |
| 19 | pub fn console_log(s: &str) { |
| 20 | web_sys::console::log_1(&JsValue::from_str(s)); |
| 21 | } |