Skip to content

Commit

Permalink
Bug 1840374 - Part 9: Implement changes for ToTemporalCalendarSlotVal…
Browse files Browse the repository at this point in the history
…ue. r=spidermonkey-reviewers,sfink

Implement the changes from <tc39/proposal-temporal#2485>.

The function name hasn't yet been updated to reflect the new name.

Depends on D182026

Differential Revision: https://phabricator.services.mozilla.com/D182027

UltraBlame original commit: 12e79d0c699efde090bcded208f9585c35dfd856
  • Loading branch information
marco-c committed Jul 21, 2023
1 parent 95d8417 commit f88d79d
Showing 1 changed file with 53 additions and 43 deletions.
96 changes: 53 additions & 43 deletions js/src/builtin/temporal/Calendar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,46 @@ CalendarObject* js::temporal::GetISO8601Calendar(JSContext* cx) {
return CreateTemporalCalendar(cx, id);
}




static bool ObjectImplementsTemporalCalendarProtocol(JSContext* cx,
Handle<JSObject*> object,
bool* result) {

MOZ_ASSERT(!object->canUnwrapAs<CalendarObject>(),
"Calendar objects handled in the caller");


for (auto key : {
&JSAtomState::dateAdd, &JSAtomState::dateFromFields,
&JSAtomState::dateUntil, &JSAtomState::day,
&JSAtomState::dayOfWeek, &JSAtomState::dayOfYear,
&JSAtomState::daysInMonth, &JSAtomState::daysInWeek,
&JSAtomState::daysInYear, &JSAtomState::fields,
&JSAtomState::id, &JSAtomState::inLeapYear,
&JSAtomState::mergeFields, &JSAtomState::month,
&JSAtomState::monthCode, &JSAtomState::monthDayFromFields,
&JSAtomState::monthsInYear, &JSAtomState::weekOfYear,
&JSAtomState::year, &JSAtomState::yearMonthFromFields,
&JSAtomState::yearOfWeek,
}) {

bool has;
if (!HasProperty(cx, object, cx->names().*key, &has)) {
return false;
}
if (!has) {
*result = false;
return true;
}
}


*result = true;
return true;
}

template <typename T, typename... Ts>
static bool ToTemporalCalendar(JSContext* cx, Handle<JSObject*> object,
MutableHandle<CalendarValue> result) {
Expand All @@ -614,6 +654,8 @@ bool js::temporal::ToTemporalCalendar(JSContext* cx,
Handle<Value> temporalCalendarLike,
MutableHandle<CalendarValue> result) {



Rooted<Value> calendarLike(cx, temporalCalendarLike);
if (calendarLike.isObject()) {
Rooted<JSObject*> obj(cx, &calendarLike.toObject());
Expand All @@ -637,55 +679,21 @@ bool js::temporal::ToTemporalCalendar(JSContext* cx,
}


if (obj->canUnwrapAs<TimeZoneObject>()) {
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr,
JSMSG_TEMPORAL_INVALID_OBJECT,
"Temporal.Calendar", "Temporal.TimeZone");
return false;
}


bool hasCalendar;
if (!HasProperty(cx, obj, cx->names().calendar, &hasCalendar)) {
bool implementsCalendarProtocol;
if (!ObjectImplementsTemporalCalendarProtocol(
cx, obj, &implementsCalendarProtocol)) {
return false;
}
if (!hasCalendar) {
result.set(obj);
return true;
}


if (!GetProperty(cx, obj, obj, cx->names().calendar, &calendarLike)) {
if (!implementsCalendarProtocol) {
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr,
JSMSG_TEMPORAL_INVALID_OBJECT,
"Temporal.Calendar", obj->getClass()->name);
return false;
}


if (calendarLike.isObject()) {
obj = &calendarLike.toObject();





if (obj->canUnwrapAs<TimeZoneObject>()) {
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr,
JSMSG_TEMPORAL_INVALID_OBJECT,
"Temporal.Calendar", "Temporal.TimeZone");
return false;
}





if (!HasProperty(cx, obj, cx->names().calendar, &hasCalendar)) {
return false;
}
if (!hasCalendar) {
result.set(obj);
return true;
}
}
result.set(obj);
return true;
}


Expand Down Expand Up @@ -714,6 +722,8 @@ bool js::temporal::ToTemporalCalendar(JSContext* cx,





bool js::temporal::ToTemporalCalendarWithISODefault(
JSContext* cx, Handle<Value> temporalCalendarLike,
MutableHandle<CalendarValue> result) {
Expand Down

0 comments on commit f88d79d

Please sign in to comment.