Skip to content

Commit

Permalink
Bug 1920955 - Part 1: Ensure date-time separators are consistent. r=s…
Browse files Browse the repository at this point in the history
…fink

Request for test262 coverage: tc39/test262#4237

Differential Revision: https://phabricator.services.mozilla.com/D223506
  • Loading branch information
anba committed Oct 3, 2024
1 parent 17e9349 commit 496d9a6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
2 changes: 2 additions & 0 deletions js/public/friend/ErrorNumbers.msg
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,8 @@ MSG_DEF(JSMSG_TEMPORAL_PARSER_INVALID_UTC_DESIGNATOR_WITHOUT_NAME, 0, JSEXN_RANG
MSG_DEF(JSMSG_TEMPORAL_PARSER_MONTH_DAY_CALENDAR_NOT_ISO8601, 0, JSEXN_RANGEERR, "Month-Day formats only support the \"iso8601\" calendar")
MSG_DEF(JSMSG_TEMPORAL_PARSER_YEAR_MONTH_CALENDAR_NOT_ISO8601, 0, JSEXN_RANGEERR, "Year-Month formats only support the \"iso8601\" calendar")
MSG_DEF(JSMSG_TEMPORAL_PARSER_INVALID_SUBMINUTE_TIMEZONE, 0, JSEXN_RANGEERR, "time zone offset must not contain seconds precision")
MSG_DEF(JSMSG_TEMPORAL_PARSER_INCONSISTENT_DATE_SEPARATOR, 0, JSEXN_RANGEERR, "date separator '-' must be consistent")
MSG_DEF(JSMSG_TEMPORAL_PARSER_INCONSISTENT_TIME_SEPARATOR, 0, JSEXN_RANGEERR, "time separator ':' must be consistent")

// Explicit Resource Management

Expand Down
35 changes: 25 additions & 10 deletions js/src/builtin/temporal/TemporalParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ mozilla::Result<PlainDate, ParserError> TemporalParser<CharT>::date() {
}

// Optional: -
character('-');
bool hasMonthSeparator = character('-');

// DateMonth :::
// 0 NonzeroDigit
Expand All @@ -1003,7 +1003,12 @@ mozilla::Result<PlainDate, ParserError> TemporalParser<CharT>::date() {
}

// Optional: -
character('-');
bool hasDaySeparator = character('-');

// Date separators must be consistent.
if (hasMonthSeparator != hasDaySeparator) {
return mozilla::Err(JSMSG_TEMPORAL_PARSER_INCONSISTENT_DATE_SEPARATOR);
}

// DateDay :::
// 0 NonzeroDigit
Expand Down Expand Up @@ -1053,7 +1058,7 @@ mozilla::Result<PlainTime, ParserError> TemporalParser<CharT>::timeSpec() {
}

// Optional: :
bool needsMinutes = character(':');
bool hasMinuteSeparator = character(':');

// TimeMinute :::
// MinuteSecond
Expand All @@ -1072,7 +1077,7 @@ mozilla::Result<PlainTime, ParserError> TemporalParser<CharT>::timeSpec() {
}

// Optional: :
bool needsSeconds = needsMinutes && character(':');
bool hasSecondSeparator = character(':');

// TimeSecond :::
// MinuteSecond
Expand All @@ -1083,6 +1088,11 @@ mozilla::Result<PlainTime, ParserError> TemporalParser<CharT>::timeSpec() {
return mozilla::Err(JSMSG_TEMPORAL_PARSER_INVALID_LEAPSECOND);
}

// Time separators must be consistent.
if (hasMinuteSeparator != hasSecondSeparator) {
return mozilla::Err(JSMSG_TEMPORAL_PARSER_INCONSISTENT_TIME_SEPARATOR);
}

// TimeFraction :::
// Fraction
if (auto f = fraction()) {
Expand All @@ -1091,10 +1101,10 @@ mozilla::Result<PlainTime, ParserError> TemporalParser<CharT>::timeSpec() {
result.microsecond = (fractionalPart % 1'000'000) / 1'000;
result.nanosecond = fractionalPart % 1'000;
}
} else if (needsSeconds) {
} else if (hasSecondSeparator) {
return mozilla::Err(JSMSG_TEMPORAL_PARSER_MISSING_SECOND);
}
} else if (needsMinutes) {
} else if (hasMinuteSeparator) {
return mozilla::Err(JSMSG_TEMPORAL_PARSER_MISSING_MINUTE);
}

Expand Down Expand Up @@ -1231,7 +1241,7 @@ TemporalParser<CharT>::utcOffsetSubMinutePrecision() {
// TimeSeparator[Extended] :::
// [+Extended] :
// [~Extended] [empty]
bool needsMinutes = character(':');
bool hasMinuteSeparator = character(':');

// MinuteSecond :::
// 0 DecimalDigit
Expand All @@ -1249,7 +1259,7 @@ TemporalParser<CharT>::utcOffsetSubMinutePrecision() {
// TimeSeparator[Extended] :::
// [+Extended] :
// [~Extended] [empty]
bool needsSeconds = needsMinutes && character(':');
bool hasSecondSeparator = character(':');

// MinuteSecond :::
// 0 DecimalDigit
Expand All @@ -1264,15 +1274,20 @@ TemporalParser<CharT>::utcOffsetSubMinutePrecision() {
return mozilla::Err(JSMSG_TEMPORAL_PARSER_INVALID_SECOND);
}

// Time separators must be consistent.
if (hasMinuteSeparator != hasSecondSeparator) {
return mozilla::Err(JSMSG_TEMPORAL_PARSER_INCONSISTENT_TIME_SEPARATOR);
}

if (auto fractionalPart = fraction()) {
result.fractionalPart = fractionalPart.value();
}

result.subMinutePrecision = true;
} else if (needsSeconds) {
} else if (hasSecondSeparator) {
return mozilla::Err(JSMSG_TEMPORAL_PARSER_MISSING_SECOND);
}
} else if (needsMinutes) {
} else if (hasMinuteSeparator) {
return mozilla::Err(JSMSG_TEMPORAL_PARSER_MISSING_MINUTE);
}

Expand Down

0 comments on commit 496d9a6

Please sign in to comment.