Skip to content
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

Temporal: attribute/property and custom calendar fixes #3639

Merged
merged 4 commits into from
Jan 31, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 33 additions & 29 deletions core/engine/src/builtins/temporal/calendar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ impl IntrinsicObject for Calendar {
.build();

BuiltInBuilder::from_standard_constructor::<Self>(realm)
.static_property(
.property(
JsSymbol::to_string_tag(),
Self::NAME,
Attribute::CONFIGURABLE,
)
.accessor(utf16!("id"), Some(get_id), None, Attribute::default())
.accessor(utf16!("id"), Some(get_id), None, Attribute::CONFIGURABLE)
.static_method(Self::from, js_string!("from"), 1)
.method(Self::date_from_fields, js_string!("dateFromFields"), 2)
.method(
Expand Down Expand Up @@ -174,8 +174,9 @@ impl Calendar {
.as_object()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("the this value of Calendar must be a Calendar object.")
JsNativeError::typ().with_message(
"the this value of Calendar.prototype.id must be a Calendar object.",
)
})?;

Ok(JsString::from(calendar.slot.identifier(context)?.as_str()).into())
Expand All @@ -193,8 +194,9 @@ impl Calendar {
.as_object()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
JsNativeError::typ().with_message(
"this value of Calendar dateFromFields must be a Calendar object.",
)
})?;

// 3. If Type(fields) is not Object, throw a TypeError exception.
Expand Down Expand Up @@ -273,8 +275,9 @@ impl Calendar {
.as_object()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
JsNativeError::typ().with_message(
"this value of Calendar yearMonthFromFields must be a Calendar object.",
)
})?;

let fields = args.get_or_undefined(0);
Expand Down Expand Up @@ -349,8 +352,9 @@ impl Calendar {
.as_object()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
JsNativeError::typ().with_message(
"this value of Calendar monthDayFromFields must be a Calendar object.",
)
})?;

// 3. If Type(fields) is not Object, throw a TypeError exception.
Expand Down Expand Up @@ -422,7 +426,7 @@ impl Calendar {
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
.with_message("this value of Calendar dateAdd must be a Calendar object.")
})?;

// 4. Set date to ? ToTemporalDate(date).
Expand Down Expand Up @@ -461,7 +465,7 @@ impl Calendar {
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
.with_message("this value of Calendar dateUntil must be a Calendar object.")
})?;

// 4. Set one to ? ToTemporalDate(one).
Expand Down Expand Up @@ -497,7 +501,7 @@ impl Calendar {
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
.with_message("this value of Calendar era must be a Calendar object.")
})?;

let date_like = to_calendar_date_like(args.get_or_undefined(0), context)?;
Expand All @@ -517,7 +521,7 @@ impl Calendar {
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
.with_message("this value of Calendar eraYear must be a Calendar object.")
})?;

let date_like = to_calendar_date_like(args.get_or_undefined(0), context)?;
Expand All @@ -537,7 +541,7 @@ impl Calendar {
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
.with_message("this value of Calendar year must be a Calendar object.")
})?;

let date_like = to_calendar_date_like(args.get_or_undefined(0), context)?;
Expand All @@ -554,7 +558,7 @@ impl Calendar {
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
.with_message("this value of Calendar month must be a Calendar object.")
})?;

let date_like = to_calendar_date_like(args.get_or_undefined(0), context)?;
Expand All @@ -576,7 +580,7 @@ impl Calendar {
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
.with_message("this value of Calendar monthCode must be a Calendar object.")
})?;

let date_like = to_calendar_date_like(args.get_or_undefined(0), context)?;
Expand All @@ -593,7 +597,7 @@ impl Calendar {
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
.with_message("this value of Calendar day must be a Calendar object.")
})?;

let date_like = to_calendar_date_like(args.get_or_undefined(0), context)?;
Expand All @@ -612,7 +616,7 @@ impl Calendar {
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
.with_message("this value of Calendar dayOfWeek must be a Calendar object.")
})?;

// 3. Let temporalDate be ? ToTemporalDate(temporalDateLike).
Expand All @@ -632,7 +636,7 @@ impl Calendar {
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
.with_message("this value of Calendar dayOfYear must be a Calendar object.")
})?;

// 3. Let temporalDate be ? ToTemporalDate(temporalDateLike).
Expand All @@ -652,7 +656,7 @@ impl Calendar {
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
.with_message("this value of Calendar weekOfYear must be a Calendar object.")
})?;

// 3. Let temporalDate be ? ToTemporalDate(temporalDateLike).
Expand All @@ -672,7 +676,7 @@ impl Calendar {
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
.with_message("this value of Calendar yearOfWeek must be a Calendar object.")
})?;

// 3. Let temporalDate be ? ToTemporalDate(temporalDateLike).
Expand All @@ -692,7 +696,7 @@ impl Calendar {
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
.with_message("this value of Calendar daysInWeek must be a Calendar object.")
})?;

// 3. Let temporalDate be ? ToTemporalDate(temporalDateLike).
Expand All @@ -712,7 +716,7 @@ impl Calendar {
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
.with_message("this value of Calendar daysInMonth must be a Calendar object.")
})?;

let date_like = to_calendar_date_like(args.get_or_undefined(0), context)?;
Expand All @@ -729,7 +733,7 @@ impl Calendar {
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
.with_message("this value of Calendar daysInYear must be a Calendar object.")
})?;

let date_like = to_calendar_date_like(args.get_or_undefined(0), context)?;
Expand All @@ -749,7 +753,7 @@ impl Calendar {
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
.with_message("this value of Calendar monthsInYear must be a Calendar object.")
})?;

let date_like = to_calendar_date_like(args.get_or_undefined(0), context)?;
Expand All @@ -766,7 +770,7 @@ impl Calendar {
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
.with_message("this value of Calendar inLeapYear must be a Calendar object.")
})?;

let date_like = to_calendar_date_like(args.get_or_undefined(0), context)?;
Expand All @@ -785,7 +789,7 @@ impl Calendar {
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
.with_message("this value of Calendar Fields must be a Calendar object.")
})?;

// Custom Calendars override the `fields` method.
Expand Down Expand Up @@ -902,7 +906,7 @@ impl Calendar {
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("this value of Calendar must be a Calendar object.")
.with_message("this value of Calendar mergeFields must be a Calendar object.")
})?;

let fields = args.get_or_undefined(0).to_object(context)?;
Expand Down
36 changes: 20 additions & 16 deletions core/engine/src/builtins/temporal/calendar/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ impl CalendarProtocol for JsCustomCalendar {
let val = method
.as_callable()
.expect("is method")
.call(&method, &[date_like], context)
.call(&self.calendar.clone().into(), &[date_like], context)
.map_err(|err| TemporalError::general(err.to_string()))?;

// Validate the return value.
Expand Down Expand Up @@ -314,7 +314,7 @@ impl CalendarProtocol for JsCustomCalendar {
let val = method
.as_callable()
.expect("is method")
.call(&method, &[date_like], context)
.call(&self.calendar.clone().into(), &[date_like], context)
.map_err(|err| TemporalError::general(err.to_string()))?;

// Validate the return value.
Expand Down Expand Up @@ -361,7 +361,7 @@ impl CalendarProtocol for JsCustomCalendar {
let val = method
.as_callable()
.expect("is method")
.call(&method, &[date_like], context)
.call(&self.calendar.clone().into(), &[date_like], context)
.map_err(|err| TemporalError::general(err.to_string()))?;

let JsValue::String(result) = val else {
Expand Down Expand Up @@ -393,7 +393,7 @@ impl CalendarProtocol for JsCustomCalendar {
let val = method
.as_callable()
.expect("is method")
.call(&method, &[date_like], context)
.call(&self.calendar.clone().into(), &[date_like], context)
.map_err(|err| TemporalError::general(err.to_string()))?;

// Validate the return value.
Expand Down Expand Up @@ -440,7 +440,7 @@ impl CalendarProtocol for JsCustomCalendar {
let val = method
.as_callable()
.expect("is method")
.call(&method, &[date_like], context)
.call(&self.calendar.clone().into(), &[date_like], context)
.map_err(|err| TemporalError::general(err.to_string()))?;

// Validate the return value.
Expand Down Expand Up @@ -489,7 +489,7 @@ impl CalendarProtocol for JsCustomCalendar {
let val = method
.as_callable()
.expect("is method")
.call(&method, &[date_like], context)
.call(&self.calendar.clone().into(), &[date_like], context)
.map_err(|err| TemporalError::general(err.to_string()))?;

// Validate the return value.
Expand Down Expand Up @@ -538,7 +538,7 @@ impl CalendarProtocol for JsCustomCalendar {
let val = method
.as_callable()
.expect("is method")
.call(&method, &[date_like], context)
.call(&self.calendar.clone().into(), &[date_like], context)
.map_err(|err| TemporalError::general(err.to_string()))?;

// Validate the return value.
Expand Down Expand Up @@ -587,7 +587,7 @@ impl CalendarProtocol for JsCustomCalendar {
let val = method
.as_callable()
.expect("is method")
.call(&method, &[date_like], context)
.call(&self.calendar.clone().into(), &[date_like], context)
.map_err(|err| TemporalError::general(err.to_string()))?;

// Validate the return value.
Expand Down Expand Up @@ -629,7 +629,7 @@ impl CalendarProtocol for JsCustomCalendar {
let val = method
.as_callable()
.expect("is method")
.call(&method, &[date_like], context)
.call(&self.calendar.clone().into(), &[date_like], context)
.map_err(|err| TemporalError::general(err.to_string()))?;

// Validate the return value.
Expand Down Expand Up @@ -677,7 +677,7 @@ impl CalendarProtocol for JsCustomCalendar {
let val = method
.as_callable()
.expect("is method")
.call(&method, &[date_like], context)
.call(&self.calendar.clone().into(), &[date_like], context)
.map_err(|err| TemporalError::general(err.to_string()))?;

// Validate the return value.
Expand Down Expand Up @@ -728,7 +728,7 @@ impl CalendarProtocol for JsCustomCalendar {
let val = method
.as_callable()
.expect("is method")
.call(&method, &[date_like], context)
.call(&self.calendar.clone().into(), &[date_like], context)
.map_err(|err| TemporalError::general(err.to_string()))?;

// Validate the return value.
Expand Down Expand Up @@ -777,7 +777,7 @@ impl CalendarProtocol for JsCustomCalendar {
let val = method
.as_callable()
.expect("is method")
.call(&method, &[date_like], context)
.call(&self.calendar.clone().into(), &[date_like], context)
.map_err(|err| TemporalError::general(err.to_string()))?;

// Validate the return value.
Expand Down Expand Up @@ -828,7 +828,7 @@ impl CalendarProtocol for JsCustomCalendar {
let val = method
.as_callable()
.expect("is method")
.call(&method, &[date_like], context)
.call(&self.calendar.clone().into(), &[date_like], context)
.map_err(|err| TemporalError::general(err.to_string()))?;

let JsValue::Boolean(result) = val else {
Expand Down Expand Up @@ -858,7 +858,7 @@ impl CalendarProtocol for JsCustomCalendar {
let result = method
.as_callable()
.expect("is method")
.call(&method, &[fields_js.into()], context)
.call(&self.calendar.clone().into(), &[fields_js.into()], context)
.map_err(|e| TemporalError::general(e.to_string()))?;

// validate result and map to a `Vec<String>`
Expand Down Expand Up @@ -909,7 +909,11 @@ impl CalendarProtocol for JsCustomCalendar {
let value = method
.as_callable()
.expect("is method")
.call(&method, &[fields.into(), add_fields.into()], context)
.call(
&self.calendar.clone().into(),
&[fields.into(), add_fields.into()],
context,
)
.map_err(|e| TemporalError::general(e.to_string()))?;

let JsValue::Object(o) = value else {
Expand All @@ -930,7 +934,7 @@ impl CalendarProtocol for JsCustomCalendar {
.calendar
.__get__(
&PropertyKey::from(utf16!("id")),
JsValue::undefined(),
self.calendar.clone().into(),
&mut context.into(),
)
.expect("method must exist on a object that implements the CalendarProtocol.");
Expand Down