2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-04-14 15:10:48 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
//! This module helps deno implement timers and performance APIs.
|
2021-04-14 15:10:48 -04:00
|
|
|
|
2023-10-05 08:34:38 -04:00
|
|
|
use deno_core::op2;
|
2021-04-14 15:10:48 -04:00
|
|
|
use deno_core::OpState;
|
2024-11-08 17:20:24 -05:00
|
|
|
use std::time::Duration;
|
2021-04-14 15:10:48 -04:00
|
|
|
use std::time::Instant;
|
2024-11-08 17:20:24 -05:00
|
|
|
use std::time::SystemTime;
|
|
|
|
use std::time::UNIX_EPOCH;
|
2021-04-14 15:10:48 -04:00
|
|
|
|
|
|
|
pub trait TimersPermission {
|
|
|
|
fn allow_hrtime(&mut self) -> bool;
|
|
|
|
}
|
|
|
|
|
2024-06-06 23:37:53 -04:00
|
|
|
impl TimersPermission for deno_permissions::PermissionsContainer {
|
|
|
|
#[inline(always)]
|
|
|
|
fn allow_hrtime(&mut self) -> bool {
|
2024-09-03 05:24:25 -04:00
|
|
|
true
|
2024-06-06 23:37:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-08 17:20:24 -05:00
|
|
|
pub struct StartTime(Instant);
|
2021-04-14 15:10:48 -04:00
|
|
|
|
2024-11-08 17:20:24 -05:00
|
|
|
impl Default for StartTime {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(Instant::now())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::Deref for StartTime {
|
|
|
|
type Target = Instant;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expose_time<TP>(state: &mut OpState, duration: Duration, out: &mut [u8])
|
2021-04-14 15:10:48 -04:00
|
|
|
where
|
|
|
|
TP: TimersPermission + 'static,
|
|
|
|
{
|
2024-11-08 17:20:24 -05:00
|
|
|
let seconds = duration.as_secs() as u32;
|
|
|
|
let mut subsec_nanos = duration.subsec_nanos();
|
2021-04-14 15:10:48 -04:00
|
|
|
|
|
|
|
// If the permission is not enabled
|
|
|
|
// Round the nano result on 2 milliseconds
|
|
|
|
// see: https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp#Reduced_time_precision
|
|
|
|
if !state.borrow_mut::<TP>().allow_hrtime() {
|
2022-08-28 02:51:49 -04:00
|
|
|
let reduced_time_precision = 2_000_000; // 2ms in nanoseconds
|
2021-04-14 15:10:48 -04:00
|
|
|
subsec_nanos -= subsec_nanos % reduced_time_precision;
|
|
|
|
}
|
2024-11-08 17:20:24 -05:00
|
|
|
|
|
|
|
if out.len() >= 8 {
|
|
|
|
out[0..4].copy_from_slice(&seconds.to_ne_bytes());
|
|
|
|
out[4..8].copy_from_slice(&subsec_nanos.to_ne_bytes());
|
2022-08-28 02:51:49 -04:00
|
|
|
}
|
2024-11-08 17:20:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[op2(fast)]
|
|
|
|
pub fn op_now<TP>(state: &mut OpState, #[buffer] buf: &mut [u8])
|
|
|
|
where
|
|
|
|
TP: TimersPermission + 'static,
|
|
|
|
{
|
|
|
|
let start_time = state.borrow::<StartTime>();
|
|
|
|
let elapsed = start_time.elapsed();
|
|
|
|
expose_time::<TP>(state, elapsed, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op2(fast)]
|
|
|
|
pub fn op_time_origin<TP>(state: &mut OpState, #[buffer] buf: &mut [u8])
|
|
|
|
where
|
|
|
|
TP: TimersPermission + 'static,
|
|
|
|
{
|
|
|
|
// https://w3c.github.io/hr-time/#dfn-estimated-monotonic-time-of-the-unix-epoch
|
|
|
|
let wall_time = SystemTime::now();
|
|
|
|
let monotonic_time = state.borrow::<StartTime>().elapsed();
|
|
|
|
let epoch = wall_time.duration_since(UNIX_EPOCH).unwrap() - monotonic_time;
|
|
|
|
expose_time::<TP>(state, epoch, buf);
|
2021-04-14 15:10:48 -04:00
|
|
|
}
|
|
|
|
|
2024-03-11 23:48:00 -04:00
|
|
|
#[allow(clippy::unused_async)]
|
2024-02-16 10:35:51 -05:00
|
|
|
#[op2(async(lazy), fast)]
|
2024-03-01 13:15:18 -05:00
|
|
|
pub async fn op_defer() {}
|