luars 0.26.2

A library for lua 5.5 runtime implementation in Rust
Documentation
#[cfg(not(target_arch = "wasm32"))]
pub(crate) struct PlatformInstant {
    inner: std::time::Instant,
}

#[cfg(target_arch = "wasm32")]
pub(crate) struct PlatformInstant {
    start_ms: f64,
}

impl PlatformInstant {
    #[inline]
    pub(crate) fn now() -> Self {
        #[cfg(not(target_arch = "wasm32"))]
        {
            Self {
                inner: std::time::Instant::now(),
            }
        }

        #[cfg(target_arch = "wasm32")]
        {
            Self {
                start_ms: js_sys::Date::now(),
            }
        }
    }

    #[inline]
    pub(crate) fn elapsed_secs_f64(&self) -> f64 {
        #[cfg(not(target_arch = "wasm32"))]
        {
            self.inner.elapsed().as_secs_f64()
        }

        #[cfg(target_arch = "wasm32")]
        {
            (js_sys::Date::now() - self.start_ms) / 1000.0
        }
    }
}

#[inline]
pub(crate) fn unix_nanos() -> u64 {
    #[cfg(miri)]
    {
        use std::sync::atomic::{AtomicU64, Ordering};

        static MIRI_TIME: AtomicU64 = AtomicU64::new(1_700_000_000_000_000_000);

        MIRI_TIME.fetch_add(1_000_000, Ordering::Relaxed)
    }

    #[cfg(all(not(miri), not(target_arch = "wasm32")))]
    {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_nanos() as u64)
            .unwrap_or(0)
    }

    #[cfg(target_arch = "wasm32")]
    {
        (js_sys::Date::now() * 1_000_000.0) as u64
    }
}

#[inline]
pub(crate) fn unix_secs() -> u64 {
    unix_nanos() / 1_000_000_000
}