Skip to content

Commit

Permalink
WIP: Create calendar record from calendar objects
Browse files Browse the repository at this point in the history
When constructing a new Temporal object with a calendar object, eagerly
get all the calendar methods, and save them in a Record, which goes into
the Temporal object's [[CalendarRecord]] internal slot.

_For internal use of the intrinsic constructors only_, in the polyfill
constructors accept a calendar record object instead of a calendar object.
This has no observable effect on user code, because user code can never
obtain a calendar record object.

Still to do:
- spec text
- inconsistency introduced between e.g. the calendar records in the
  objects returned from pdt.toPlainDate() (same as pdt) and pdt.with()
  (different from pdt if Calendar.prototype was mutated since pdt was
  created)

See: #1294
  • Loading branch information
ptomato committed Mar 2, 2021
1 parent ca5c22d commit 05cd622
Show file tree
Hide file tree
Showing 37 changed files with 835 additions and 525 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,14 @@ overrides:
- polyfill/test/TimeZone/**/*.js
- polyfill/test/PlainYearMonth/**/*.js
- polyfill/test/ZonedDateTime/**/*.js
- polyfill/test/helpers/*.js
- polyfill/test/now/**/*.js
globals:
Temporal: readonly
Test262Error: readonly
assert: readonly
verifyProperty: readonly
MINIMAL_CALENDAR_OBJECT: readonly
# test262 code complies to a different coding style
extends: 'eslint:recommended'
rules:
Expand Down
2 changes: 2 additions & 0 deletions polyfill/ci_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ else
fi
if [ $threads -gt 8 ]; then threads=8; fi

cp ./helpers/* ../test262/harness/

test262-harness \
-t $threads \
-r json \
Expand Down
525 changes: 306 additions & 219 deletions polyfill/lib/ecmascript.mjs

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions polyfill/lib/intl.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ISO_MILLISECOND,
ISO_MICROSECOND,
ISO_NANOSECOND,
CALENDAR,
CALENDAR_RECORD,
TIME_ZONE
} from './slots.mjs';
import { TimeZone } from './timezone.mjs';
Expand Down Expand Up @@ -278,7 +278,7 @@ function extractOverrides(temporalObj, main) {
const isoYear = GetSlot(temporalObj, ISO_YEAR);
const isoMonth = GetSlot(temporalObj, ISO_MONTH);
const referenceISODay = GetSlot(temporalObj, ISO_DAY);
const calendar = ES.ToString(GetSlot(temporalObj, CALENDAR));
const calendar = ES.CalendarToString(GetSlot(temporalObj, CALENDAR_RECORD));
if (calendar !== main[CAL_ID]) {
throw new RangeError(
`cannot format PlainYearMonth with calendar ${calendar} in locale with calendar ${main[CAL_ID]}`
Expand All @@ -295,7 +295,7 @@ function extractOverrides(temporalObj, main) {
const referenceISOYear = GetSlot(temporalObj, ISO_YEAR);
const isoMonth = GetSlot(temporalObj, ISO_MONTH);
const isoDay = GetSlot(temporalObj, ISO_DAY);
const calendar = ES.ToString(GetSlot(temporalObj, CALENDAR));
const calendar = ES.CalendarToString(GetSlot(temporalObj, CALENDAR_RECORD));
if (calendar !== main[CAL_ID]) {
throw new RangeError(
`cannot format PlainMonthDay with calendar ${calendar} in locale with calendar ${main[CAL_ID]}`
Expand All @@ -312,7 +312,7 @@ function extractOverrides(temporalObj, main) {
const isoYear = GetSlot(temporalObj, ISO_YEAR);
const isoMonth = GetSlot(temporalObj, ISO_MONTH);
const isoDay = GetSlot(temporalObj, ISO_DAY);
const calendar = ES.ToString(GetSlot(temporalObj, CALENDAR));
const calendar = ES.CalendarToString(GetSlot(temporalObj, CALENDAR_RECORD));
if (calendar !== 'iso8601' && calendar !== main[CAL_ID]) {
throw new RangeError(`cannot format PlainDate with calendar ${calendar} in locale with calendar ${main[CAL_ID]}`);
}
Expand All @@ -333,7 +333,7 @@ function extractOverrides(temporalObj, main) {
const millisecond = GetSlot(temporalObj, ISO_MILLISECOND);
const microsecond = GetSlot(temporalObj, ISO_MICROSECOND);
const nanosecond = GetSlot(temporalObj, ISO_NANOSECOND);
const calendar = ES.ToString(GetSlot(temporalObj, CALENDAR));
const calendar = ES.CalendarToString(GetSlot(temporalObj, CALENDAR_RECORD));
if (calendar !== 'iso8601' && calendar !== main[CAL_ID]) {
throw new RangeError(
`cannot format PlainDateTime with calendar ${calendar} in locale with calendar ${main[CAL_ID]}`
Expand Down Expand Up @@ -361,7 +361,7 @@ function extractOverrides(temporalObj, main) {
}

if (ES.IsTemporalZonedDateTime(temporalObj)) {
const calendar = ES.ToString(GetSlot(temporalObj, CALENDAR));
const calendar = ES.CalendarToString(GetSlot(temporalObj, CALENDAR_RECORD));
if (calendar !== 'iso8601' && calendar !== main[CAL_ID]) {
throw new RangeError(
`cannot format ZonedDateTime with calendar ${calendar} in locale with calendar ${main[CAL_ID]}`
Expand Down
6 changes: 4 additions & 2 deletions polyfill/lib/now.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ function instant() {
function plainDateTime(calendarLike, temporalTimeZoneLike = timeZone()) {
const timeZone = ES.ToTemporalTimeZone(temporalTimeZoneLike);
const calendar = ES.ToTemporalCalendar(calendarLike);
const calendarRecord = ES.NewCalendarRecord(calendar);
const inst = instant();
return ES.BuiltinTimeZoneGetPlainDateTimeFor(timeZone, inst, calendar);
return ES.BuiltinTimeZoneGetPlainDateTimeFor(timeZone, inst, calendarRecord);
}
function plainDateTimeISO(temporalTimeZoneLike = timeZone()) {
const timeZone = ES.ToTemporalTimeZone(temporalTimeZoneLike);
const calendar = ES.GetISO8601Calendar();
const calendarRecord = ES.NewCalendarRecord(calendar);
const inst = instant();
return ES.BuiltinTimeZoneGetPlainDateTimeFor(timeZone, inst, calendar);
return ES.BuiltinTimeZoneGetPlainDateTimeFor(timeZone, inst, calendarRecord);
}
function zonedDateTime(calendarLike, temporalTimeZoneLike = timeZone()) {
const timeZone = ES.ToTemporalTimeZone(temporalTimeZoneLike);
Expand Down
Loading

0 comments on commit 05cd622

Please sign in to comment.