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 21, 2023
1 parent 5eb467c commit ad30241
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 34 deletions.
52 changes: 25 additions & 27 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::{Finalize, Trace, custom_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,25 +1058,17 @@ 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())
|dt| {
Ok(Some(dt.inner.calendar().clone()))
},
|_ym| {
Err(JsNativeError::range()
.with_message("Not yet implemented.")
.into())
|ym| {
Ok(Some(ym.inner.calendar().clone()))
},
|_md| {
Err(JsNativeError::range()
.with_message("Not yet implemented.")
.into())
|md| {
Ok(Some(md.inner.calendar().clone()))
},
|_zdt| {
Err(JsNativeError::range()
.with_message("Not yet implemented.")
.into())
|zdt| {
Ok(Some(zdt.inner.calendar().clone()))
},
)? {
return Ok(calendar);
Expand All @@ -1084,23 +1083,22 @@ 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 @@ -22,6 +22,7 @@ use boa_temporal::{
options::ArithmeticOverflow,
TemporalError, TemporalFields, TemporalResult, TinyAsciiStr,
};
use boa_gc::{Finalize, Trace};
use num_traits::ToPrimitive;
use plain_date::PlainDate;
use plain_month_day::PlainMonthDay;
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
5 changes: 3 additions & 2 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,11 +816,11 @@ 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,
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

0 comments on commit ad30241

Please sign in to comment.