Skip to content

Commit

Permalink
Apply Review
Browse files Browse the repository at this point in the history
  • Loading branch information
nekevss committed Dec 4, 2023
1 parent 928ea22 commit 1b2ae98
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 100 deletions.
2 changes: 1 addition & 1 deletion boa_engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ boa_profiler.workspace = true
boa_macros.workspace = true
boa_ast.workspace = true
boa_parser.workspace = true
boa_temporal = { workspace = true, features = ["context"] }
boa_temporal = { workspace = true }
serde = { workspace = true, features = ["derive", "rc"] }
serde_json.workspace = true
rand = "0.8.5"
Expand Down
12 changes: 8 additions & 4 deletions boa_engine/src/builtins/temporal/calendar/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use boa_temporal::{
month_day::MonthDay,
options::ArithmeticOverflow,
year_month::YearMonth,
TemporalResult, TinyStr4, TinyStr8,
TemporalResult, TinyAsciiStr,
};
use num_traits::ToPrimitive;

Expand Down Expand Up @@ -222,7 +222,11 @@ impl CalendarProtocol for CustomRuntimeCalendar {
Err(TemporalError::general("Not yet implemented."))
}

fn era(&self, _: &CalendarDateLike, _: &mut dyn Any) -> TemporalResult<Option<TinyStr8>> {
fn era(
&self,
_: &CalendarDateLike,
_: &mut dyn Any,
) -> TemporalResult<Option<TinyAsciiStr<8>>> {
// Return undefined as custom calendars do not implement -> Currently.
Ok(None)
}
Expand Down Expand Up @@ -322,7 +326,7 @@ impl CalendarProtocol for CustomRuntimeCalendar {
&self,
date_like: &CalendarDateLike,
context: &mut dyn Any,
) -> TemporalResult<TinyStr4> {
) -> TemporalResult<TinyAsciiStr<4>> {
let context = context
.downcast_mut::<Context>()
.expect("Context was not provided for a CustomCalendar.");
Expand All @@ -344,7 +348,7 @@ impl CalendarProtocol for CustomRuntimeCalendar {
return Err(TemporalError::r#type().with_message("monthCode return must be a String."));
};

let result = TinyStr4::from_str(&result.to_std_string_escaped())
let result = TinyAsciiStr::<4>::from_str(&result.to_std_string_escaped())
.map_err(|_| TemporalError::general("Unexpected monthCode value."))?;

Ok(result)
Expand Down
3 changes: 0 additions & 3 deletions boa_temporal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,5 @@ num-bigint = { workspace = true, features = ["serde"] }
bitflags.workspace = true
num-traits.workspace = true

[features]
context = []

[lints]
workspace = true
8 changes: 4 additions & 4 deletions boa_temporal/src/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
TemporalError, TemporalResult,
};

use tinystr::{TinyStr4, TinyStr8};
use tinystr::TinyAsciiStr;

use self::iso::IsoCalendar;

Expand Down Expand Up @@ -200,7 +200,7 @@ pub trait CalendarProtocol: CalendarProtocolClone {
&self,
date_like: &CalendarDateLike,
context: &mut dyn Any,
) -> TemporalResult<Option<TinyStr8>>;
) -> TemporalResult<Option<TinyAsciiStr<8>>>;
/// Returns the era year for a given `temporaldatelike`
fn era_year(
&self,
Expand All @@ -217,7 +217,7 @@ pub trait CalendarProtocol: CalendarProtocolClone {
&self,
date_like: &CalendarDateLike,
context: &mut dyn Any,
) -> TemporalResult<TinyStr4>;
) -> TemporalResult<TinyAsciiStr<4>>;
/// Returns the `day` for a given `temporaldatelike`
fn day(&self, date_like: &CalendarDateLike, context: &mut dyn Any) -> TemporalResult<u8>;
/// Returns a value representing the day of the week for a date.
Expand Down Expand Up @@ -401,7 +401,7 @@ impl CalendarSlot {
&self,
date_like: &CalendarDateLike,
context: &mut dyn Any,
) -> TemporalResult<TinyStr4> {
) -> TemporalResult<TinyAsciiStr<4>> {
match self {
Self::Identifier(id) => {
let protocol = AvailableCalendars::from_str(id)?.to_protocol();
Expand Down
10 changes: 7 additions & 3 deletions boa_temporal/src/calendar/iso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
};
use std::any::Any;

use tinystr::{TinyStr4, TinyStr8};
use tinystr::TinyAsciiStr;

use super::{CalendarDateLike, CalendarFieldsType, CalendarProtocol, CalendarSlot};

Expand Down Expand Up @@ -132,7 +132,11 @@ impl CalendarProtocol for IsoCalendar {
}

/// `Temporal.Calendar.prototype.era( dateLike )` for iso8601 calendar.
fn era(&self, _: &CalendarDateLike, _: &mut dyn Any) -> TemporalResult<Option<TinyStr8>> {
fn era(
&self,
_: &CalendarDateLike,
_: &mut dyn Any,
) -> TemporalResult<Option<TinyAsciiStr<8>>> {
// Returns undefined on iso8601.
Ok(None)
}
Expand All @@ -158,7 +162,7 @@ impl CalendarProtocol for IsoCalendar {
&self,
date_like: &CalendarDateLike,
_: &mut dyn Any,
) -> TemporalResult<TinyStr4> {
) -> TemporalResult<TinyAsciiStr<4>> {
let date = date_like.as_iso_date().as_icu4x()?;
Ok(date.month().code.0)
}
Expand Down
127 changes: 51 additions & 76 deletions boa_temporal/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,76 +27,15 @@ impl Date {
Self { iso, calendar }
}

#[inline]
/// `DifferenceDate`
pub(crate) fn diff_date(
&self,
other: &Self,
largest_unit: TemporalUnit,
context: &mut dyn Any,
) -> TemporalResult<Duration> {
if self.iso.year() == other.iso.year()
&& self.iso.month() == other.iso.month()
&& self.iso.day() == other.iso.day()
{
return Ok(Duration::default());
}

if largest_unit == TemporalUnit::Day {
let days = self.days_until(other);
return Ok(Duration::from_date_duration(DateDuration::new(
0f64,
0f64,
0f64,
f64::from(days),
)));
}

self.calendar()
.date_until(self, other, largest_unit, context)
}

#[inline]
/// Internal `AddDate` function
pub(crate) fn add_date(
&self,
duration: &Duration,
overflow: ArithmeticOverflow,
context: &mut dyn Any,
) -> TemporalResult<Self> {
// 1. If options is not present, set options to undefined.
// 2. If duration.[[Years]] ≠ 0, or duration.[[Months]] ≠ 0, or duration.[[Weeks]] ≠ 0, then
if duration.date().years() != 0.0
|| duration.date().months() != 0.0
|| duration.date().weeks() != 0.0
{
// a. If dateAdd is not present, then
// i. Set dateAdd to unused.
// ii. If calendar is an Object, set dateAdd to ? GetMethod(calendar, "dateAdd").
// b. Return ? CalendarDateAdd(calendar, plainDate, duration, options, dateAdd).
return self.calendar().date_add(self, duration, overflow, context);
}

// 3. Let overflow be ? ToTemporalOverflow(options).
// 4. Let days be ? BalanceTimeDuration(duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]], "day").[[Days]].
let (days, _) = duration.balance_time_duration(TemporalUnit::Day)?;

// 5. Let result be ? AddISODate(plainDate.[[ISOYear]], plainDate.[[ISOMonth]], plainDate.[[ISODay]], 0, 0, 0, days, overflow).
let result = self
.iso
.add_iso_date(&DateDuration::new(0f64, 0f64, 0f64, days), overflow)?;

Ok(Self::new_unchecked(result, self.calendar().clone()))
}

#[inline]
/// Returns a new moved date and the days associated with that adjustment
pub(crate) fn move_relative_date(
&self,
duration: &Duration,
context: &mut dyn Any,
) -> TemporalResult<(Self, f64)> {
let new_date = self.add_date(duration, ArithmeticOverflow::Constrain, context)?;
let new_date =
self.contextual_add_date(duration, ArithmeticOverflow::Constrain, context)?;
let days = f64::from(self.days_until(&new_date));
Ok((new_date, days))
}
Expand Down Expand Up @@ -189,57 +128,93 @@ impl IsoDateSlots for Date {
// ==== Context based API ====

impl Date {
/// Returns the date after adding the given duration to date.
/// Returns the date after adding the given duration to date with a provided context.
///
/// Temporal Equivalent: 3.5.13 `AddDate ( calendar, plainDate, duration [ , options [ , dateAdd ] ] )`
#[inline]
#[cfg(feature = "context")]
pub fn add_to_date(
pub fn contextual_add_date(
&self,
duration: &Duration,
overflow: ArithmeticOverflow,
context: &mut dyn Any,
) -> TemporalResult<Self> {
self.add_date(duration, overflow, context)
// 1. If options is not present, set options to undefined.
// 2. If duration.[[Years]] ≠ 0, or duration.[[Months]] ≠ 0, or duration.[[Weeks]] ≠ 0, then
if duration.date().years() != 0.0
|| duration.date().months() != 0.0
|| duration.date().weeks() != 0.0
{
// a. If dateAdd is not present, then
// i. Set dateAdd to unused.
// ii. If calendar is an Object, set dateAdd to ? GetMethod(calendar, "dateAdd").
// b. Return ? CalendarDateAdd(calendar, plainDate, duration, options, dateAdd).
return self.calendar().date_add(self, duration, overflow, context);
}

// 3. Let overflow be ? ToTemporalOverflow(options).
// 4. Let days be ? BalanceTimeDuration(duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]], "day").[[Days]].
let (days, _) = duration.balance_time_duration(TemporalUnit::Day)?;

// 5. Let result be ? AddISODate(plainDate.[[ISOYear]], plainDate.[[ISOMonth]], plainDate.[[ISODay]], 0, 0, 0, days, overflow).
let result = self
.iso
.add_iso_date(&DateDuration::new(0f64, 0f64, 0f64, days), overflow)?;

Ok(Self::new_unchecked(result, self.calendar().clone()))
}

/// Returns the date after adding the given duration to date.
///
/// Temporal Equivalent: 3.5.13 `AddDate ( calendar, plainDate, duration [ , options [ , dateAdd ] ] )`
#[inline]
#[cfg(not(feature = "context"))]
pub fn add_to_date(
pub fn add_date(
&self,
duration: &Duration,
overflow: ArithmeticOverflow,
) -> TemporalResult<Self> {
self.add_date(duration, overflow, &mut ())
self.contextual_add_date(duration, overflow, &mut ())
}

/// Returns a duration representing the difference between the dates one and two.
/// Returns a duration representing the difference between the dates one and two with a provided context.
///
/// Temporal Equivalent: 3.5.6 `DifferenceDate ( calendar, one, two, options )`
#[inline]
#[cfg(feature = "context")]
pub fn difference_date(
pub fn contextual_difference_date(
&self,
other: &Self,
largest_unit: TemporalUnit,
context: &mut dyn Any,
) -> TemporalResult<Duration> {
self.diff_date(other, largest_unit, context)
if self.iso.year() == other.iso.year()
&& self.iso.month() == other.iso.month()
&& self.iso.day() == other.iso.day()
{
return Ok(Duration::default());
}

if largest_unit == TemporalUnit::Day {
let days = self.days_until(other);
return Ok(Duration::from_date_duration(DateDuration::new(
0f64,
0f64,
0f64,
f64::from(days),
)));
}

self.calendar()
.date_until(self, other, largest_unit, context)
}

/// Returns a duration representing the difference between the dates one and two.
///
/// Temporal Equivalent: 3.5.6 `DifferenceDate ( calendar, one, two, options )`
#[inline]
#[cfg(not(feature = "context"))]
pub fn difference_date(
&self,
other: &Self,
largest_unit: TemporalUnit,
) -> TemporalResult<Duration> {
self.diff_date(other, largest_unit, &mut ())
self.contextual_difference_date(other, largest_unit, &mut ())
}
}
15 changes: 9 additions & 6 deletions boa_temporal/src/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,7 @@ impl Duration {
// i. Let dateAdd be unused.

// e. Let yearsLater be ? AddDate(calendar, plainRelativeTo, yearsDuration, undefined, dateAdd).
let years_later = plain_relative_to.add_date(
let years_later = plain_relative_to.contextual_add_date(
&years_duration,
ArithmeticOverflow::Constrain,
context,
Expand All @@ -1263,7 +1263,7 @@ impl Duration {
);

// g. Let yearsMonthsWeeksLater be ? AddDate(calendar, plainRelativeTo, yearsMonthsWeeks, undefined, dateAdd).
let years_months_weeks_later = plain_relative_to.add_date(
let years_months_weeks_later = plain_relative_to.contextual_add_date(
&years_months_weeks,
ArithmeticOverflow::Constrain,
context,
Expand All @@ -1290,8 +1290,11 @@ impl Duration {
// m. Let untilOptions be OrdinaryObjectCreate(null).
// n. Perform ! CreateDataPropertyOrThrow(untilOptions, "largestUnit", "year").
// o. Let timePassed be ? DifferenceDate(calendar, plainRelativeTo, wholeDaysLater, untilOptions).
let time_passed =
plain_relative_to.diff_date(&whole_days_later, TemporalUnit::Year, context)?;
let time_passed = plain_relative_to.contextual_difference_date(
&whole_days_later,
TemporalUnit::Year,
context,
)?;

// p. Let yearsPassed be timePassed.[[Years]].
let years_passed = time_passed.date.years();
Expand Down Expand Up @@ -1358,7 +1361,7 @@ impl Duration {
// i. Let dateAdd be unused.

// e. Let yearsMonthsLater be ? AddDate(calendar, plainRelativeTo, yearsMonths, undefined, dateAdd).
let years_months_later = plain_relative_to.add_date(
let years_months_later = plain_relative_to.contextual_add_date(
&years_months,
ArithmeticOverflow::Constrain,
context,
Expand All @@ -1373,7 +1376,7 @@ impl Duration {
));

// g. Let yearsMonthsWeeksLater be ? AddDate(calendar, plainRelativeTo, yearsMonthsWeeks, undefined, dateAdd).
let years_months_weeks_later = plain_relative_to.add_date(
let years_months_weeks_later = plain_relative_to.contextual_add_date(
&years_months_weeks,
ArithmeticOverflow::Constrain,
context,
Expand Down
4 changes: 2 additions & 2 deletions boa_temporal/src/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{error::TemporalError, TemporalResult};

use bitflags::bitflags;
// use rustc_hash::FxHashSet;
use tinystr::{TinyStr16, TinyStr4};
use tinystr::{TinyAsciiStr, TinyStr16, TinyStr4};

bitflags! {
/// FieldMap maps the currently active fields on the `TemporalField`
Expand Down Expand Up @@ -467,7 +467,7 @@ impl TemporalFields {
}
}

fn month_code_to_integer(mc: TinyStr4) -> TemporalResult<i32> {
fn month_code_to_integer(mc: TinyAsciiStr<4>) -> TemporalResult<i32> {
match mc.as_str() {
"M01" => Ok(1),
"M02" => Ok(2),
Expand Down
2 changes: 1 addition & 1 deletion boa_temporal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub mod zoneddatetime;

// TODO: evaluate positives and negatives of using tinystr.
// Re-exporting tinystr as a convenience, as it is currently tied into the API.
pub use tinystr::{TinyAsciiStr, TinyStr16, TinyStr4, TinyStr8};
pub use tinystr::TinyAsciiStr;

pub use error::TemporalError;

Expand Down

0 comments on commit 1b2ae98

Please sign in to comment.