Skip to content

feat(ecmascript): implement Date constructor and prototype methods #586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nova_vm/src/builtin_strings
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ unregister
unscopables
unshift
URIError
utc
UTC
value
valueOf
values
Expand Down
20 changes: 20 additions & 0 deletions nova_vm/src/ecmascript/abstract_operations/type_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,26 @@ impl IntegerOrInfinity {
}
}

/// ### [7.1.5 ToIntegerOrInfinity ( argument )](https://tc39.es/ecma262/#sec-tointegerorinfinity)
pub(crate) fn to_integer_or_infinity_f64(number: f64) -> f64 {
// `ToIntegerOrInfinity ( argument )`
if number.is_nan() || number == 0.0 {
// 2. If number is NaN, +0𝔽, or -0𝔽, return 0.
0.0
} else if number == f64::INFINITY {
// 3. If number is +∞𝔽, return +∞.
f64::INFINITY
} else if number == f64::NEG_INFINITY {
// 4. If number is -∞𝔽, return -∞.
f64::NEG_INFINITY
} else {
// 5. Let integer be floor(abs(ℝ(number))).
// 6. If number < +0𝔽, set integer to -integer.
// 7. Return integer.
number.trunc()
}
}

/// ### [7.1.5 ToIntegerOrInfinity ( argument )](https://tc39.es/ecma262/#sec-tointegerorinfinity)
pub(crate) fn to_integer_or_infinity(
agent: &mut Agent,
Expand Down
14 changes: 14 additions & 0 deletions nova_vm/src/ecmascript/builtins/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub(crate) mod data;

use core::ops::{Index, IndexMut};

use data::DateValue;

use crate::{
ecmascript::{
execution::{Agent, ProtoIntrinsics},
Expand Down Expand Up @@ -38,6 +40,18 @@ impl Date<'_> {
Scoped::new(agent, self.unbind(), gc)
}

/// ### get [[DateValue]]
#[inline]
pub(crate) fn date_value(self, agent: &Agent) -> DateValue {
agent[self].date
}

/// ### set [[DateValue]]
#[inline]
pub(crate) fn set_date_value(self, agent: &mut Agent, date: DateValue) {
agent[self].date = date;
}

pub(crate) const fn _def() -> Self {
Self(DateIndex::from_u32_index(0))
}
Expand Down
82 changes: 78 additions & 4 deletions nova_vm/src/ecmascript/builtins/date/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,97 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::time::SystemTime;

use crate::{
ecmascript::types::OrdinaryObject,
SmallInteger,
ecmascript::types::{IntoValue, OrdinaryObject, Value},
heap::{CompactionLists, HeapMarkAndSweep, WorkQueues},
};
use std::time::SystemTime;

/// ### [21.4.1.1 Time Values and Time Range](https://tc39.es/ecma262/#sec-time-values-and-time-range)
///
/// A Number can exactly represent all integers from -9,007,199,254,740,992
/// to 9,007,199,254,740,992 (21.1.2.8 and 21.1.2.6). A time value supports
/// a slightly smaller range of -8,640,000,000,000,000 to 8,640,000,000,000,000 milliseconds.
/// This yields a supported time value range of exactly -100,000,000 days
/// to 100,000,000 days relative to midnight at the beginning of 1 January 1970 UTC.
///
/// In that case, the time value can be either:
///
/// - Invalid, which is presented as `i64::MAX`
/// - An integer in the range of -8,640,000,000,000,000 to 8,640,000,000,000,000,
/// which is represented as a non-max `i64`, and can also fit in `SmallInteger`
#[derive(Debug, Clone, Copy)]
#[repr(transparent)]
pub(crate) struct DateValue(i64);

impl DateValue {
pub const NAN: Self = Self(i64::MAX);

pub fn get_i64(self) -> Option<i64> {
if self.0 == i64::MAX {
None
} else {
Some(self.0)
}
}

pub fn get_f64(self) -> Option<f64> {
self.get_i64().map(|v| v as f64)
}

pub fn now() -> Self {
let now = SystemTime::now();
let now = now
.duration_since(SystemTime::UNIX_EPOCH)
.expect("Time went backwards")
.as_millis();
Self(now as i64)
}
}

/// ### [21.4.1.31 TimeClip ( time )](https://tc39.es/ecma262/#sec-timeclip)
///
/// The abstract operation TimeClip takes argument time (a Number) and returns
/// a Number. It calculates a number of milliseconds.
pub(crate) fn time_clip(time: f64) -> DateValue {
// 1. If time is not finite, return NaN.
if !time.is_finite() {
return DateValue::NAN;
}

// 2. If abs(ℝ(time)) > 8.64 × 10**15, return NaN.
if time.abs() > 8.64e15 {
return DateValue::NAN;
}

// 3. Return 𝔽(! ToIntegerOrInfinity(time)).
DateValue(time.trunc() as i64)
}

impl<'a> IntoValue<'a> for DateValue {
fn into_value(self) -> Value<'a> {
if let Some(value) = self.get_f64() {
// SAFETY: `value` is guaranteed to be in the range of `SmallInteger`.
Value::Integer(SmallInteger::try_from(value).unwrap())
} else {
Value::nan()
}
}
}

#[derive(Debug, Clone, Copy)]
pub struct DateHeapData {
pub(crate) object_index: Option<OrdinaryObject<'static>>,
pub(crate) date: Option<SystemTime>,
pub(crate) date: DateValue,
}

impl DateHeapData {
pub(crate) fn new_invalid() -> Self {
Self {
object_index: None,
date: None,
date: DateValue::NAN,
}
}
}
Expand Down
Loading