luaur_vm/functions/
clock_timestamp.rs1pub(crate) fn clock_timestamp() -> f64 {
2 #[cfg(windows)]
3 {
4 use core::mem::MaybeUninit;
5 #[link(name = "kernel32")]
6 extern "system" {
7 fn QueryPerformanceCounter(lpPerformanceCount: *mut i64) -> i32;
8 }
9 let mut result = 0i64;
10 unsafe {
11 QueryPerformanceCounter(&mut result);
12 }
13 result as f64
14 }
15 #[cfg(target_os = "macos")]
16 {
17 extern "C" {
18 fn mach_absolute_time() -> u64;
19 }
20 unsafe { mach_absolute_time() as f64 }
21 }
22 #[cfg(any(target_os = "linux", target_os = "freebsd"))]
23 {
24 use core::mem::MaybeUninit;
25 #[repr(C)]
26 struct timespec {
27 tv_sec: core::ffi::c_long,
28 tv_nsec: core::ffi::c_long,
29 }
30 extern "C" {
31 fn clock_gettime(clk_id: i32, tp: *mut timespec) -> i32;
32 }
33 const CLOCK_MONOTONIC: i32 = 1;
34 let mut now = MaybeUninit::<timespec>::uninit();
35 unsafe {
36 clock_gettime(CLOCK_MONOTONIC, now.as_mut_ptr());
37 let now = now.assume_init();
38 (now.tv_sec as f64) * 1e9 + (now.tv_nsec as f64)
39 }
40 }
41 #[cfg(target_os = "emscripten")]
42 {
43 extern "C" {
44 fn emscripten_get_now() -> f64;
45 }
46 unsafe { emscripten_get_now() }
47 }
48 #[cfg(not(any(
49 windows,
50 target_os = "macos",
51 target_os = "linux",
52 target_os = "freebsd",
53 target_os = "emscripten"
54 )))]
55 {
56 extern "C" {
57 fn clock() -> core::ffi::c_long;
58 }
59 unsafe { clock() as f64 }
60 }
61}