Skip to content

Commit

Permalink
Fast-path accesses to calendar property
Browse files Browse the repository at this point in the history
There are a few places where the `calendar` property of a property bag is
read. If the property bag is actually a Temporal object with a
[[Calendar]] internal slot, then read this slot instead.

See: #1428
  • Loading branch information
ptomato committed Apr 16, 2021
1 parent 32b1588 commit 2122f4c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
50 changes: 24 additions & 26 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -876,9 +876,7 @@ export const ES = ObjectAssign({}, ES2020, {
GetSlot(relativeTo, CALENDAR)
);
}
calendar = relativeTo.calendar;
if (calendar === undefined) calendar = ES.GetISO8601Calendar();
calendar = ES.ToTemporalCalendar(calendar);
calendar = ES.GetOptionalTemporalCalendar(relativeTo);
const fieldNames = ES.CalendarFields(calendar, ['day', 'month', 'monthCode', 'year']);
const fields = ES.ToTemporalDateTimeFields(relativeTo, fieldNames);
({
Expand Down Expand Up @@ -1164,9 +1162,7 @@ export const ES = ObjectAssign({}, ES2020, {
GetSlot(item, CALENDAR)
);
}
let calendar = item.calendar;
if (calendar === undefined) calendar = ES.GetISO8601Calendar();
calendar = ES.ToTemporalCalendar(calendar);
const calendar = ES.GetOptionalTemporalCalendar(item);
const fieldNames = ES.CalendarFields(calendar, ['day', 'month', 'monthCode', 'year']);
const fields = ES.ToTemporalDateFields(item, fieldNames);
return ES.DateFromFields(calendar, fields, options);
Expand Down Expand Up @@ -1217,10 +1213,7 @@ export const ES = ObjectAssign({}, ES2020, {
);
}

calendar = item.calendar;
if (calendar === undefined) calendar = ES.GetISO8601Calendar();
calendar = ES.ToTemporalCalendar(calendar);

calendar = ES.GetOptionalTemporalCalendar(item);
const fieldNames = ES.CalendarFields(calendar, ['day', 'month', 'monthCode', 'year']);
const fields = ES.ToTemporalDateTimeFields(item, fieldNames);
({
Expand Down Expand Up @@ -1323,10 +1316,16 @@ export const ES = ObjectAssign({}, ES2020, {
ToTemporalMonthDay: (item, options = {}) => {
if (ES.Type(item) === 'Object') {
if (ES.IsTemporalMonthDay(item)) return item;
let calendar = item.calendar;
let calendarAbsent = calendar === undefined;
if (calendar === undefined) calendar = ES.GetISO8601Calendar();
calendar = ES.ToTemporalCalendar(calendar);
let calendar, calendarAbsent;
if (HasSlot(item, CALENDAR)) {
calendar = GetSlot(item, CALENDAR);
calendarAbsent = false;
} else {
calendar = item.calendar;
calendarAbsent = calendar === undefined;
if (calendar === undefined) calendar = ES.GetISO8601Calendar();
calendar = ES.ToTemporalCalendar(calendar);
}
const fieldNames = ES.CalendarFields(calendar, ['day', 'month', 'monthCode', 'year']);
const fields = ES.ToTemporalMonthDayFields(item, fieldNames);
// Callers who omit the calendar are not writing calendar-independent
Expand Down Expand Up @@ -1366,12 +1365,9 @@ export const ES = ObjectAssign({}, ES2020, {
GetSlot(item, ISO_NANOSECOND)
);
}
calendar = item.calendar;
if (calendar) {
calendar = ES.ToTemporalCalendar(calendar);
if (ES.ToString(calendar) !== 'iso8601') {
throw new RangeError('PlainTime can only have iso8601 calendar');
}
calendar = ES.GetOptionalTemporalCalendar(item);
if (ES.ToString(calendar) !== 'iso8601') {
throw new RangeError('PlainTime can only have iso8601 calendar');
}
({ hour, minute, second, millisecond, microsecond, nanosecond } = ES.ToTemporalTimeRecord(item));
({ hour, minute, second, millisecond, microsecond, nanosecond } = ES.RegulateTime(
Expand All @@ -1398,9 +1394,7 @@ export const ES = ObjectAssign({}, ES2020, {
ToTemporalYearMonth: (item, options = {}) => {
if (ES.Type(item) === 'Object') {
if (ES.IsTemporalYearMonth(item)) return item;
let calendar = item.calendar;
if (calendar === undefined) calendar = ES.GetISO8601Calendar();
calendar = ES.ToTemporalCalendar(calendar);
const calendar = ES.GetOptionalTemporalCalendar(item);
const fieldNames = ES.CalendarFields(calendar, ['month', 'monthCode', 'year']);
const fields = ES.ToTemporalYearMonthFields(item, fieldNames);
return ES.YearMonthFromFields(calendar, fields, options);
Expand Down Expand Up @@ -1487,9 +1481,7 @@ export const ES = ObjectAssign({}, ES2020, {
let year, month, day, hour, minute, second, millisecond, microsecond, nanosecond, timeZone, offset, calendar;
if (ES.Type(item) === 'Object') {
if (ES.IsTemporalZonedDateTime(item)) return item;
calendar = item.calendar;
if (calendar === undefined) calendar = ES.GetISO8601Calendar();
calendar = ES.ToTemporalCalendar(calendar);
calendar = ES.GetOptionalTemporalCalendar(item);
const fieldNames = ES.CalendarFields(calendar, ['day', 'month', 'year']);
const fields = ES.ToTemporalZonedDateTimeFields(item, fieldNames);
({
Expand Down Expand Up @@ -1678,6 +1670,12 @@ export const ES = ObjectAssign({}, ES2020, {
if (!calendar) calendar = 'iso8601';
return new TemporalCalendar(calendar);
},
GetOptionalTemporalCalendar: (item) => {
if (HasSlot(item, CALENDAR)) return GetSlot(item, CALENDAR);
const { calendar } = item;
if (calendar === undefined) return ES.GetISO8601Calendar();
return ES.ToTemporalCalendar(calendar);
},
CalendarCompare: (one, two) => {
const cal1 = ES.ToString(one);
const cal2 = ES.ToString(two);
Expand Down
2 changes: 2 additions & 0 deletions spec/calendar.html
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ <h1>GetOptionalTemporalCalendar ( _item_ )</h1>
If no such property is present, the ISO 8601 calendar is returned.
</p>
<emu-alg>
1. If _item_ has an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]], [[InitializedTemporalMonthDay]], [[InitializedTemporalTime]], [[InitializedTemporalYearMonth]], or [[InitializedTemporalZonedDateTime]] internal slot, then
1. Return _item_.[[Calendar]].
1. Let _calendar_ be ? Get(_item_, *"calendar"*).
1. Return ? ToOptionalTemporalCalendar(_calendar_).
</emu-alg>
Expand Down
14 changes: 9 additions & 5 deletions spec/plainmonthday.html
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,16 @@ <h1>ToTemporalMonthDay ( _item_ [ , _options_ ] )</h1>
1. If Type(_item_) is Object, then
1. If _item_ has an [[InitializedTemporalMonthDay]] internal slot, then
1. Return _item_.
1. Let _calendar_ be ? Get(_item_, *"calendar"*).
1. If _calendar_ is *undefined*, then
1. Let _calendarAbsent_ be *true*.
1. Else,
1. If _item_ has an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]], [[InitializedTemporalTime]], [[InitializedTemporalYearMonth]], or [[InitializedTemporalZonedDateTime]] internal slot, then
1. Let _calendar_ be _item_.[[Calendar]].
1. Let _calendarAbsent_ be *false*.
1. Set _calendar_ to ? ToOptionalTemporalCalendar(_calendar_).
1. Else,
1. Let _calendar_ be ? Get(_item_, *"calendar"*).
1. If _calendar_ is *undefined*, then
1. Let _calendarAbsent_ be *true*.
1. Else,
1. Let _calendarAbsent_ be *false*.
1. Set _calendar_ to ? ToOptionalTemporalCalendar(_calendar_).
1. Let _fieldNames_ be ? CalendarFields(_calendar_, « *"day"*, *"month"*, *"monthCode"*, *"year"* »).
1. Let _fields_ be ? PrepareTemporalFields(_item_, _fieldNames_, «»).
1. Let _month_ be ? Get(_fields_, *"month"*).
Expand Down
8 changes: 3 additions & 5 deletions spec/plaintime.html
Original file line number Diff line number Diff line change
Expand Up @@ -612,11 +612,9 @@ <h1>ToTemporalTime ( _item_ [ , _overflow_ ] )</h1>
1. Return _item_.
1. If _item_ has an [[InitializedTemporalDateTime]] internal slot, then
1. Return ! CreateTemporalTime(_item_.[[ISOHour]], _item_.[[ISOMinute]], _item_.[[ISOSecond]], _item_.[[ISOMillisecond]], _item_.[[ISOMicrosecond]], _item_.[[ISONanosecond]]).
1. Let _calendar_ be ? Get(_item_, *"calendar"*).
1. If _calendar_ is not *undefined*, then
1. Set _calendar_ to ? ToTemporalCalendar(_calendar_).
1. If ? ToString(_calendar_) is not *"iso8601"*, then
1. Throw a *RangeError* exception.
1. Let _calendar_ be ? GetOptionalTemporalCalendar(_item_).
1. If ? ToString(_calendar_) is not *"iso8601"*, then
1. Throw a *RangeError* exception.
1. Let _result_ be ? ToTemporalTimeRecord(_item_).
1. Set _result_ to ? RegulateTime(_result_.[[Hour]], _result_.[[Minute]], _result_.[[Second]], _result_.[[Millisecond]], _result_.[[Microsecond]], _result_.[[Nanosecond]], _overflow_).
1. Else,
Expand Down

0 comments on commit 2122f4c

Please sign in to comment.