Skip to content

Commit

Permalink
TemporalCalendar trace & general cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nekevss committed Dec 22, 2023
1 parent 5eb467c commit 75f5183
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 112 deletions.
54 changes: 23 additions & 31 deletions core/engine/src/builtins/temporal/calendar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
string::{common::StaticJsStrings, utf16},
Context, JsArgs, JsData, JsNativeError, JsObject, JsResult, JsString, JsSymbol, JsValue,
};
use boa_gc::{Finalize, Trace};
use boa_gc::{custom_trace, Finalize, Trace};
use boa_profiler::Profiler;
use boa_temporal::{
components::calendar::{
Expand All @@ -38,15 +38,22 @@ pub(crate) use object::JsCustomCalendar;

#[cfg(test)]
mod tests;

/// The `Temporal.Calendar` object.
#[derive(Debug, Trace, Finalize, JsData)]
// SAFETY: `Calendar` doesn't contain traceable types.
#[boa_gc(unsafe_empty_trace)]
#[derive(Debug, Finalize, JsData)]
pub struct Calendar {
slot: CalendarSlot<JsCustomCalendar>,
}

unsafe impl Trace for Calendar {
custom_trace!(this, mark, {
match &this.slot {
CalendarSlot::Protocol(custom) => mark(custom),
// SAFETY: CalendarSlot::Builtin does not contain any JsValues for the gc to trace.
CalendarSlot::Builtin(_) => {}
}
});
}

impl Calendar {
pub(crate) fn new(slot: CalendarSlot<JsCustomCalendar>) -> Self {
Self { slot }
Expand Down Expand Up @@ -1051,26 +1058,10 @@ pub(crate) fn to_temporal_calendar_slot_value(
if let Some(calendar) = extract_from_temporal_type(
calendar_like,
|d| Ok(Some(d.inner.calendar().clone())),
|_dt| {
Err(JsNativeError::range()
.with_message("Not yet implemented.")
.into())
},
|_ym| {
Err(JsNativeError::range()
.with_message("Not yet implemented.")
.into())
},
|_md| {
Err(JsNativeError::range()
.with_message("Not yet implemented.")
.into())
},
|_zdt| {
Err(JsNativeError::range()
.with_message("Not yet implemented.")
.into())
},
|dt| Ok(Some(dt.inner.calendar().clone())),
|ym| Ok(Some(ym.inner.calendar().clone())),
|md| Ok(Some(md.inner.calendar().clone())),
|zdt| Ok(Some(zdt.inner.calendar().clone())),
)? {
return Ok(calendar);
}
Expand All @@ -1084,23 +1075,24 @@ pub(crate) fn to_temporal_calendar_slot_value(
}

// Types: Box<dyn CalendarProtocol> <- UserCalendar
let protocol = JsCustomCalendar::new(calendar_like);
let custom = JsCustomCalendar::new(calendar_like);
// c. Return temporalCalendarLike.
return Ok(CalendarSlot::Protocol(protocol));
return Ok(CalendarSlot::Protocol(custom));
}

// 3. If temporalCalendarLike is not a String, throw a TypeError exception.
if !calendar_like.is_string() {
let JsValue::String(calendar_id) = calendar_like else {
return Err(JsNativeError::typ()
.with_message("temporalCalendarLike is not a string.")
.into());
}
};

// TODO: 4-6
// 4. Let identifier be ? ParseTemporalCalendarString(temporalCalendarLike).
// 5. If IsBuiltinCalendar(identifier) is false, throw a RangeError exception.
// 6. Return the ASCII-lowercase of identifier.
Ok(CalendarSlot::default())
Ok(CalendarSlot::<JsCustomCalendar>::from_str(
&calendar_id.to_std_string_escaped(),
)?)
}

fn object_implements_calendar_protocol(calendar_like: &JsObject, context: &mut Context) -> bool {
Expand Down
3 changes: 2 additions & 1 deletion core/engine/src/builtins/temporal/calendar/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
};
use std::any::Any;

use boa_gc::{Finalize, Trace};
use boa_macros::utf16;
use boa_temporal::{
components::{
Expand All @@ -32,7 +33,7 @@ use plain_year_month::PlainYearMonth;
///
/// A user-defined calendar implements all of the `CalendarProtocolMethods`
/// and therefore satisfies the requirements to be used as a calendar.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Trace, Finalize)]
pub(crate) struct JsCustomCalendar {
calendar: JsObject,
}
Expand Down
2 changes: 1 addition & 1 deletion core/engine/src/builtins/temporal/zoned_date_time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use super::JsCustomCalendar;
// SAFETY: ZonedDateTime does not contain any traceable types.
#[boa_gc(unsafe_empty_trace)]
pub struct ZonedDateTime {
inner: InnerZdt<JsCustomCalendar>,
pub(crate) inner: InnerZdt<JsCustomCalendar>,
}

impl BuiltInObject for ZonedDateTime {
Expand Down
94 changes: 21 additions & 73 deletions core/temporal/src/components/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ impl<C: CalendarProtocol> FromStr for CalendarSlot<C> {
type Err = TemporalError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
// NOTE(nekesss): Catch the iso identifier here, as `iso8601` is not a valid ID below.
if s == "iso8601" {
return Ok(CalendarSlot::Builtin(AnyCalendar::Iso(Iso)));
}
Expand Down Expand Up @@ -815,22 +816,21 @@ impl<C: CalendarProtocol> CalendarSlot<C> {
}
}

/// An empty `CalendarProtocol` impl that will panic if treated as a valid calendar.
/// An empty `CalendarProtocol` implementation on `()`.
///
/// # Panics
///
/// Attempting to use this calendar as a valid calendar is an error and will cause a panic.
/// Attempting to use this empty calendar implementation as a valid calendar is an error and will cause a panic.
impl CalendarProtocol for () {
fn date_from_fields(
&self,
_: &mut TemporalFields,
_: ArithmeticOverflow,
_: &mut dyn Any,
) -> TemporalResult<Date<Self>> {
&self,
_: &mut TemporalFields,
_: ArithmeticOverflow,
_: &mut dyn Any,
) -> TemporalResult<Date<Self>> {
unreachable!();
}


fn month_day_from_fields(
&self,
_: &mut TemporalFields,
Expand Down Expand Up @@ -877,27 +877,15 @@ impl CalendarProtocol for () {
unreachable!();
}

fn era_year(
&self,
_: &CalendarDateLike<()>,
_: &mut dyn Any,
) -> TemporalResult<Option<i32>> {
fn era_year(&self, _: &CalendarDateLike<()>, _: &mut dyn Any) -> TemporalResult<Option<i32>> {
unreachable!();
}

fn year(
&self,
_: &CalendarDateLike<()>,
_: &mut dyn Any,
) -> TemporalResult<i32> {
fn year(&self, _: &CalendarDateLike<()>, _: &mut dyn Any) -> TemporalResult<i32> {
unreachable!();
}

fn month(
&self,
_: &CalendarDateLike<()>,
_: &mut dyn Any,
) -> TemporalResult<u8> {
fn month(&self, _: &CalendarDateLike<()>, _: &mut dyn Any) -> TemporalResult<u8> {
unreachable!();
}

Expand All @@ -909,83 +897,43 @@ impl CalendarProtocol for () {
unreachable!();
}

fn day(
&self,
_: &CalendarDateLike<()>,
_: &mut dyn Any,
) -> TemporalResult<u8> {
fn day(&self, _: &CalendarDateLike<()>, _: &mut dyn Any) -> TemporalResult<u8> {
unreachable!();
}

fn day_of_week(
&self,
_: &CalendarDateLike<()>,
_: &mut dyn Any,
) -> TemporalResult<u16> {
fn day_of_week(&self, _: &CalendarDateLike<()>, _: &mut dyn Any) -> TemporalResult<u16> {
unreachable!();
}

fn day_of_year(
&self,
_: &CalendarDateLike<()>,
_: &mut dyn Any,
) -> TemporalResult<u16> {
fn day_of_year(&self, _: &CalendarDateLike<()>, _: &mut dyn Any) -> TemporalResult<u16> {
unreachable!();
}

fn week_of_year(
&self,
_: &CalendarDateLike<()>,
_: &mut dyn Any,
) -> TemporalResult<u16> {
fn week_of_year(&self, _: &CalendarDateLike<()>, _: &mut dyn Any) -> TemporalResult<u16> {
unreachable!();
}

fn year_of_week(
&self,
_: &CalendarDateLike<()>,
_: &mut dyn Any,
) -> TemporalResult<i32> {
fn year_of_week(&self, _: &CalendarDateLike<()>, _: &mut dyn Any) -> TemporalResult<i32> {
unreachable!();
}

fn days_in_week(
&self,
_: &CalendarDateLike<()>,
_: &mut dyn Any,
) -> TemporalResult<u16> {
fn days_in_week(&self, _: &CalendarDateLike<()>, _: &mut dyn Any) -> TemporalResult<u16> {
unreachable!();
}

fn days_in_month(
&self,
_: &CalendarDateLike<()>,
_: &mut dyn Any,
) -> TemporalResult<u16> {
fn days_in_month(&self, _: &CalendarDateLike<()>, _: &mut dyn Any) -> TemporalResult<u16> {
unreachable!();
}

fn days_in_year(
&self,
_: &CalendarDateLike<()>,
_: &mut dyn Any,
) -> TemporalResult<u16> {
fn days_in_year(&self, _: &CalendarDateLike<()>, _: &mut dyn Any) -> TemporalResult<u16> {
unreachable!();
}

fn months_in_year(
&self,
_: &CalendarDateLike<()>,
_: &mut dyn Any,
) -> TemporalResult<u16> {
fn months_in_year(&self, _: &CalendarDateLike<()>, _: &mut dyn Any) -> TemporalResult<u16> {
unreachable!();
}

fn in_leap_year(
&self,
_: &CalendarDateLike<()>,
_: &mut dyn Any,
) -> TemporalResult<bool> {
fn in_leap_year(&self, _: &CalendarDateLike<()>, _: &mut dyn Any) -> TemporalResult<bool> {
unreachable!();
}

Expand Down
5 changes: 2 additions & 3 deletions core/temporal/src/components/zoneddatetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ impl<C: CalendarProtocol> ZonedDateTime<C> {
/// Returns the `ZonedDateTime`'s Calendar identifier.
#[inline]
#[must_use]
pub fn calendar_id(&self) -> String {
// TODO: Implement Identifier method on `CalendarSlot`
String::from("Not yet implemented.")
pub fn calendar(&self) -> &CalendarSlot<C> {
&self.calendar
}

/// Returns the `epochSeconds` value of this `ZonedDateTime`.
Expand Down
4 changes: 1 addition & 3 deletions core/temporal/src/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ fn temporal_parser_basic() {

let basic_result = basic.parse::<DateTime<()>>().unwrap();

let sep_result = basic_separated
.parse::<DateTime<()>>()
.unwrap();
let sep_result = basic_separated.parse::<DateTime<()>>().unwrap();

assert_eq!(basic_result.iso_date().year(), 2020);
assert_eq!(basic_result.iso_date().month(), 11);
Expand Down

0 comments on commit 75f5183

Please sign in to comment.