Skip to content

Commit

Permalink
Review feedback and get_option changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nekevss committed Oct 10, 2023
1 parent dc311e6 commit c127bb1
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 120 deletions.
8 changes: 4 additions & 4 deletions boa_engine/src/builtins/temporal/calendar/iso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl BuiltinCalendar for IsoCalendar {
// NOTE: we are in ISO by default here.
// a. Perform ? ISOResolveMonth(fields).
// b. Let result be ? ISODateFromFields(fields, overflow).
fields.resolve_month()?;
fields.iso_resolve_month()?;

// Extra: handle reulating/overflow until implemented on `icu_calendar`
fields.regulate(overflow)?;
Expand Down Expand Up @@ -69,7 +69,7 @@ impl BuiltinCalendar for IsoCalendar {
) -> JsResult<JsValue> {
// 9. If calendar.[[Identifier]] is "iso8601", then
// a. Perform ? ISOResolveMonth(fields).
fields.resolve_month()?;
fields.iso_resolve_month()?;

// b. Let result be ? ISOYearMonthFromFields(fields, overflow).
fields.regulate_year_month(overflow);
Expand Down Expand Up @@ -100,7 +100,7 @@ impl BuiltinCalendar for IsoCalendar {
context: &mut Context<'_>,
) -> JsResult<JsValue> {
// 8. Perform ? ISOResolveMonth(fields).
fields.resolve_month()?;
fields.iso_resolve_month()?;

fields.regulate(overflow)?;

Expand Down Expand Up @@ -331,7 +331,7 @@ impl BuiltinCalendar for IsoCalendar {

// Resolve the fields for the iso calendar.
fn resolve_fields(&self, fields: &mut temporal::TemporalFields, _: &str) -> JsResult<()> {
fields.resolve_month()?;
fields.iso_resolve_month()?;
Ok(())
}

Expand Down
31 changes: 10 additions & 21 deletions boa_engine/src/builtins/temporal/calendar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,8 @@ impl Calendar {
};

// 8. Let overflow be ? ToTemporalOverflow(options).
let overflow =
get_option::<ArithmeticOverflow>(&options, utf16!("overflow"), false, context)?
.unwrap_or(ArithmeticOverflow::Constrain);
let overflow = get_option(&options, utf16!("overflow"), context)?
.unwrap_or(ArithmeticOverflow::Constrain);

// NOTE: implement the below on the calenar itself
// 9. If calendar.[[Identifier]] is "iso8601", then
Expand Down Expand Up @@ -453,9 +452,8 @@ impl Calendar {
};

// 7. Let overflow be ? ToTemporalOverflow(options).
let overflow =
get_option::<ArithmeticOverflow>(&options, utf16!("overflow"), false, context)?
.unwrap_or(ArithmeticOverflow::Constrain);
let overflow = get_option::<ArithmeticOverflow>(&options, utf16!("overflow"), context)?
.unwrap_or(ArithmeticOverflow::Constrain);

this_calendar.year_month_from_fields(&mut fields, overflow, context)
}
Expand Down Expand Up @@ -530,9 +528,8 @@ impl Calendar {
};

// 8. Let overflow be ? ToTemporalOverflow(options).
let overflow =
get_option::<ArithmeticOverflow>(&options, utf16!("overflow"), false, context)?
.unwrap_or(ArithmeticOverflow::Constrain);
let overflow = get_option(&options, utf16!("overflow"), context)?
.unwrap_or(ArithmeticOverflow::Constrain);

this_calendar.month_day_from_fields(&mut fields, overflow, context)
}
Expand Down Expand Up @@ -569,9 +566,8 @@ impl Calendar {
let options_obj = get_options_object(options)?;

// 7. Let overflow be ? ToTemporalOverflow(options).
let overflow =
get_option::<ArithmeticOverflow>(&options_obj, utf16!("overflow"), false, context)?
.unwrap_or(ArithmeticOverflow::Constrain);
let overflow = get_option(&options_obj, utf16!("overflow"), context)?
.unwrap_or(ArithmeticOverflow::Constrain);

// 8. Let balanceResult be ? BalanceTimeDuration(duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]], "day").
duration.balance_time_duration(TemporalUnit::Day, None)?;
Expand Down Expand Up @@ -616,17 +612,10 @@ impl Calendar {
&options,
utf16!("largestUnit"),
TemporalUnitGroup::Date,
false,
Some(TemporalUnit::Day),
None,
context,
)?;

let Some(largest_unit) = largest_unit else {
return Err(JsNativeError::range()
.with_message("largestUnit cannot be undefined in this context.")
.into());
};
)?
.unwrap_or(TemporalUnit::Day);

this_calendar.date_until(&one, &two, largest_unit, context)
}
Expand Down
9 changes: 8 additions & 1 deletion boa_engine/src/builtins/temporal/date_equations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,16 @@ pub(crate) fn mathematical_in_leap_year(t: f64) -> i32 {

pub(crate) fn epoch_time_to_month_in_year(t: f64) -> i32 {
const DAYS: [i32; 11] = [30, 58, 89, 120, 150, 181, 212, 242, 272, 303, 333];
const LEAP_DAYS: [i32; 11] = [30, 59, 90, 121, 151, 182, 213, 242, 272, 303, 334];

let in_leap_year = mathematical_in_leap_year(t) == 1;
let day = epoch_time_to_day_in_year(t);

let result = DAYS.binary_search(&day);
let result = if in_leap_year {
LEAP_DAYS.binary_search(&day)
} else {
DAYS.binary_search(&day)
};

match result {
Ok(i) | Err(i) => i as i32,
Expand Down
45 changes: 16 additions & 29 deletions boa_engine/src/builtins/temporal/duration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,10 +610,10 @@ impl Duration {
}
};

// NOTE: 6 & 7 unused in favor of `is_none()`.
// 6. Let smallestUnitPresent be true.
let mut smallest_unit_present = true;
// 7. Let largestUnitPresent be true.
let mut largest_unit_present = true;

// 8. NOTE: The following steps read options and perform independent validation in alphabetical order
// (ToRelativeTemporalObject reads "relativeTo", ToTemporalRoundingIncrement reads "roundingIncrement" and ToTemporalRoundingMode reads "roundingMode").

Expand All @@ -622,8 +622,6 @@ impl Duration {
&round_to,
utf16!("largestUnit"),
TemporalUnitGroup::DateTime,
false,
None,
Some([TemporalUnit::Auto].into()),
context,
)?;
Expand All @@ -635,27 +633,32 @@ impl Duration {
let rounding_increment = get_temporal_rounding_increment(&round_to, context)?;

// 12. Let roundingMode be ? ToTemporalRoundingMode(roundTo, "halfExpand").
let rounding_mode =
get_option::<RoundingMode>(&round_to, utf16!("roundingMode"), false, context)?
.unwrap_or(RoundingMode::HalfExpand);
let rounding_mode = get_option(&round_to, utf16!("roundingMode"), context)?
.unwrap_or(RoundingMode::HalfExpand);

// 13. Let smallestUnit be ? GetTemporalUnit(roundTo, "smallestUnit", datetime, undefined).
let smallest_unit = get_temporal_unit(
&round_to,
utf16!("smallestUnit"),
TemporalUnitGroup::DateTime,
false,
None,
None,
context,
)?;

// NOTE: execute step 19 earlier before initial values are shadowed.
// 19. If smallestUnitPresent is false and largestUnitPresent is false, then
if smallest_unit.is_none() && largest_unit.is_none() {
// a. Throw a RangeError exception.
return Err(JsNativeError::range()
.with_message("smallestUnit or largestUnit must be present.")
.into());
}

// 14. If smallestUnit is undefined, then
let smallest_unit = if let Some(unit) = smallest_unit {
unit
} else {
// a. Set smallestUnitPresent to false.
smallest_unit_present = false;
// b. Set smallestUnit to "nanosecond".
TemporalUnit::Nanosecond
};
Expand All @@ -668,24 +671,15 @@ impl Duration {

// 17. If largestUnit is undefined, then
let largest_unit = match largest_unit {
Some(u) if u == TemporalUnit::Auto => default_largest_unit,
Some(TemporalUnit::Auto) => default_largest_unit,
Some(u) => u,
None => {
// a. Set largestUnitPresent to false.
largest_unit_present = false;
// b. Set largestUnit to defaultLargestUnit.
default_largest_unit
}
};

// 19. If smallestUnitPresent is false and largestUnitPresent is false, then
if !smallest_unit_present && !largest_unit_present {
// a. Throw a RangeError exception.
return Err(JsNativeError::range()
.with_message("smallestUnit or largestUnit must be present.")
.into());
}

// 20. If LargerOfTwoTemporalUnits(largestUnit, smallestUnit) is not largestUnit, throw a RangeError exception.
if core::cmp::max(largest_unit, smallest_unit) != largest_unit {
return Err(JsNativeError::range()
Expand Down Expand Up @@ -799,17 +793,10 @@ impl Duration {
&total_of,
utf16!("unit"),
TemporalUnitGroup::DateTime,
true,
None,
None,
context,
)?;

let Some(unit) = unit else {
return Err(JsNativeError::range()
.with_message("TemporalUnit cannot be undefined in this context.")
.into());
};
)?
.ok_or_else(|| JsNativeError::range().with_message("unit cannot be undefined."))?;

let mut unbalance_duration = DurationRecord::from_date_duration(duration.inner.date());

Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/builtins/temporal/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ impl TemporalFields {
}

/// Resolve the month and monthCode on this `TemporalFields`.
pub(crate) fn resolve_month(&mut self) -> JsResult<()> {
pub(crate) fn iso_resolve_month(&mut self) -> JsResult<()> {
if self.month_code.is_none() {
if self.month.is_some() {
return Ok(());
Expand Down
14 changes: 3 additions & 11 deletions boa_engine/src/builtins/temporal/instant/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,25 +366,17 @@ impl Instant {

// 8. Let roundingMode be ? ToTemporalRoundingMode(roundTo, "halfExpand").
let rounding_mode =
get_option::<RoundingMode>(&round_to, utf16!("roundingMode"), false, context)?
.unwrap_or_default();
get_option(&round_to, utf16!("roundingMode"), context)?.unwrap_or_default();

// 9. Let smallestUnit be ? GetTemporalUnit(roundTo, "smallestUnit"), time, required).
let smallest_unit = get_temporal_unit(
&round_to,
utf16!("smallestUnit"),
TemporalUnitGroup::Time,
true,
None,
None,
context,
)?;

let Some(smallest_unit) = smallest_unit else {
return Err(JsNativeError::range()
.with_message("smallestUnit cannot be undefined.")
.into());
};
)?
.ok_or_else(|| JsNativeError::range().with_message("smallestUnit cannot be undefined."))?;

let maximum = match smallest_unit {
// 10. If smallestUnit is "hour"), then
Expand Down
40 changes: 8 additions & 32 deletions boa_engine/src/builtins/temporal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,21 +500,9 @@ pub(crate) fn get_diff_settings(
) -> JsResult<(TemporalUnit, TemporalUnit, RoundingMode, f64)> {
// 1. NOTE: The following steps read options and perform independent validation in alphabetical order (ToTemporalRoundingIncrement reads "roundingIncrement" and ToTemporalRoundingMode reads "roundingMode").
// 2. Let largestUnit be ? GetTemporalUnit(options, "largestUnit", unitGroup, "auto").
let largest_unit = get_temporal_unit(
options,
utf16!("largestUnit"),
unit_group,
false,
Some(TemporalUnit::Auto),
None,
context,
)?;

let Some(mut largest_unit) = largest_unit else {
return Err(JsNativeError::range()
.with_message("largestUnit cannot be undefined in this context.")
.into());
};
let mut largest_unit =
get_temporal_unit(options, utf16!("largestUnit"), unit_group, None, context)?
.unwrap_or(TemporalUnit::Auto);

// 3. If disallowedUnits contains largestUnit, throw a RangeError exception.
if disallowed_units.contains(&largest_unit) {
Expand All @@ -525,10 +513,10 @@ pub(crate) fn get_diff_settings(

// 4. Let roundingIncrement be ? ToTemporalRoundingIncrement(options).
let rounding_increment = get_temporal_rounding_increment(options, context)?;

// 5. Let roundingMode be ? ToTemporalRoundingMode(options, "trunc").
let mut rounding_mode =
get_option::<RoundingMode>(options, utf16!("roundingMode"), false, context)?
.unwrap_or(RoundingMode::Trunc);
get_option(options, utf16!("roundingMode"), context)?.unwrap_or(RoundingMode::Trunc);

// 6. If operation is since, then
if !op {
Expand All @@ -537,21 +525,9 @@ pub(crate) fn get_diff_settings(
}

// 7. Let smallestUnit be ? GetTemporalUnit(options, "smallestUnit", unitGroup, fallbackSmallestUnit).
let smallest_unit = get_temporal_unit(
options,
utf16!("smallestUnit"),
unit_group,
false,
Some(fallback_smallest_unit),
None,
context,
)?;

let Some(smallest_unit) = smallest_unit else {
return Err(JsNativeError::range()
.with_message("smallestUnit cannot be undefined in this context.")
.into());
};
let smallest_unit =
get_temporal_unit(options, utf16!("smallestUnit"), unit_group, None, context)?
.unwrap_or(fallback_smallest_unit);

// 8. If disallowedUnits contains smallestUnit, throw a RangeError exception.
if disallowed_units.contains(&smallest_unit) {
Expand Down
8 changes: 1 addition & 7 deletions boa_engine/src/builtins/temporal/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,21 @@ pub(crate) fn get_temporal_unit(
options: &JsObject,
key: &[u16],
unit_group: TemporalUnitGroup,
required: bool,
default: Option<TemporalUnit>,
extra_values: Option<Vec<TemporalUnit>>,
context: &mut Context<'_>,
) -> JsResult<Option<TemporalUnit>> {
let extra = extra_values.unwrap_or_default();
let mut unit_values = unit_group.group();
unit_values.extend(extra);

let unit = get_option::<TemporalUnit>(options, key, required, context)?.map_or(default, Some);
let unit = get_option(options, key, context)?;

if let Some(u) = &unit {
if !unit_values.contains(u) {
return Err(JsNativeError::range()
.with_message("TemporalUnit was not part of the valid UnitGroup.")
.into());
}
} else if unit.is_none() && required {
return Err(JsNativeError::range()
.with_message("TemporalUnit cannot be undefined when required.")
.into());
}

Ok(unit)
Expand Down
10 changes: 4 additions & 6 deletions boa_engine/src/builtins/temporal/plain_date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,8 @@ pub(crate) fn to_temporal_date(
// c. If item has an [[InitializedTemporalDateTime]] internal slot, then
} else if object.is_plain_date_time() {
// i. Perform ? ToTemporalOverflow(options).
let _o =
get_option::<ArithmeticOverflow>(&options_obj, utf16!("overflow"), false, context)?
.unwrap_or(ArithmeticOverflow::Constrain);
let _o = get_option(&options_obj, utf16!("overflow"), context)?
.unwrap_or(ArithmeticOverflow::Constrain);

let obj = object.borrow();
let date_time = obj
Expand Down Expand Up @@ -537,9 +536,8 @@ pub(crate) fn to_temporal_date(
// 11. Set calendar to the ASCII-lowercase of calendar.

// 12. Perform ? ToTemporalOverflow(options).
let _result =
get_option::<ArithmeticOverflow>(&options_obj, utf16!("overflow"), false, context)?
.unwrap_or(ArithmeticOverflow::Constrain);
let _result = get_option(&options_obj, utf16!("overflow"), context)?
.unwrap_or(ArithmeticOverflow::Constrain);

// 13. Return ? CreateTemporalDate(result.[[Year]], result.[[Month]], result.[[Day]], calendar).
Ok(PlainDate {
Expand Down
Loading

0 comments on commit c127bb1

Please sign in to comment.