Commit 5354d8a1 authored by Frank Tang's avatar Frank Tang Committed by V8 LUCI CQ

[Temporal] Add PlainYearMonth.prototype.toJSON

Also add AOs: TemporalYearMonthToString

Spec Text:
https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.tojson
https://tc39.es/proposal-temporal/#sec-temporal-temporalyearmonthtostring

Bug: v8:11544
Change-Id: Ibe8bd20ae5eb5b7721e50cf5386c20d8d23e18e6
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3437894
Commit-Queue: Frank Tang <ftang@chromium.org>
Reviewed-by: 's avatarAdam Klein <adamk@chromium.org>
Cr-Commit-Position: refs/heads/main@{#80627}
parent 04359996
......@@ -250,8 +250,6 @@ TO_BE_IMPLEMENTED(TemporalPlainYearMonthPrototypeSince)
TO_BE_IMPLEMENTED(TemporalPlainYearMonthPrototypeEquals)
/* Temporal #sec-temporal.plainyearmonth.tostring */
TO_BE_IMPLEMENTED(TemporalPlainYearMonthPrototypeToString)
/* Temporal #sec-temporal.plainyearmonth.tojson */
TO_BE_IMPLEMENTED(TemporalPlainYearMonthPrototypeToJSON)
/* Temporal #sec-temporal.plainyearmonth.prototype.toplaindate */
TO_BE_IMPLEMENTED(TemporalPlainYearMonthPrototypeToPlainDate)
......@@ -582,6 +580,7 @@ TEMPORAL_GET_BY_INVOKE_CALENDAR_METHOD(PlainYearMonth, MonthsInYear,
TEMPORAL_GET_BY_INVOKE_CALENDAR_METHOD(PlainYearMonth, InLeapYear, inLeapYear)
TEMPORAL_PROTOTYPE_METHOD0(PlainYearMonth, GetISOFields, getISOFields)
TEMPORAL_VALUE_OF(PlainYearMonth)
TEMPORAL_PROTOTYPE_METHOD0(PlainYearMonth, ToJSON, toJSON)
// PlainMonthDay
BUILTIN(TemporalPlainMonthDayConstructor) {
......
......@@ -2567,6 +2567,7 @@ int32_t DecimalLength(int32_t n) {
return i;
}
// #sec-tozeropaddeddecimalstring
void ToZeroPaddedDecimalString(IncrementalStringBuilder* builder, int32_t n,
int32_t min_length) {
for (int32_t pad = min_length - DecimalLength(n); pad > 0; pad--) {
......@@ -2626,19 +2627,11 @@ MaybeHandle<String> TemporalDateToString(
// 3. Let year be ! PadISOYear(temporalDate.[[ISOYear]]).
PadISOYear(&builder, temporal_date->iso_year());
// 4. Let month be ToZeroPaddedDecimalString(temporalDate.[[ISOMonth]], 2).
int32_t month = temporal_date->iso_month();
builder.AppendCharacter('-');
if (month < 10) {
builder.AppendCharacter('0');
}
builder.AppendInt(month);
ToZeroPaddedDecimalString(&builder, temporal_date->iso_month(), 2);
// 5. Let day be ToZeroPaddedDecimalString(temporalDate.[[ISODay]], 2).
int32_t day = temporal_date->iso_day();
builder.AppendCharacter('-');
if (day < 10) {
builder.AppendCharacter('0');
}
builder.AppendInt(day);
ToZeroPaddedDecimalString(&builder, temporal_date->iso_day(), 2);
// 6. Let calendarID be ? ToString(temporalDate.[[Calendar]]).
Handle<String> calendar_id;
ASSIGN_RETURN_ON_EXCEPTION(
......@@ -2681,25 +2674,56 @@ MaybeHandle<String> TemporalMonthDayToString(
builder.AppendCharacter('-');
}
// 3. Let month be ToZeroPaddedDecimalString(monthDay.[[ISOMonth]], 2).
int32_t month = month_day->iso_month();
if (month < 10) {
builder.AppendCharacter('0');
}
builder.AppendInt(month);
ToZeroPaddedDecimalString(&builder, month_day->iso_month(), 2);
// 5. Let result be the string-concatenation of month, the code unit 0x002D
// (HYPHEN-MINUS), and day.
builder.AppendCharacter('-');
// 4. Let day be ToZeroPaddedDecimalString(monthDay.[[ISODay]], 2).
int32_t day = month_day->iso_day();
if (day < 10) {
builder.AppendCharacter('0');
}
builder.AppendInt(day);
ToZeroPaddedDecimalString(&builder, month_day->iso_day(), 2);
// 8. Let calendarString be ! FormatCalendarAnnotation(calendarID,
// showCalendar).
Handle<String> calendar_string =
FormatCalendarAnnotation(isolate, calendar_id, show_calendar);
// 9. Set result to the string-concatenation of result and calendarString.
builder.AppendString(calendar_string);
// 10. Return result.
return builder.Finish().ToHandleChecked();
}
// #sec-temporal-temporalyearmonthtostring
MaybeHandle<String> TemporalYearMonthToString(
Isolate* isolate, Handle<JSTemporalPlainYearMonth> year_month,
ShowCalendar show_calendar) {
// 1. Assert: Type(yearMonth) is Object.
// 2. Assert: yearMonth has an [[InitializedTemporalYearMonth]] internal slot.
IncrementalStringBuilder builder(isolate);
// 3. Let year be ! PadISOYear(yearMonth.[[ISOYear]]).
PadISOYear(&builder, year_month->iso_year());
// 4. Let month be ToZeroPaddedDecimalString(yearMonth.[[ISOMonth]], 2).
// 5. Let result be the string-concatenation of year, the code unit 0x002D
// (HYPHEN-MINUS), and month.
builder.AppendCharacter('-');
ToZeroPaddedDecimalString(&builder, year_month->iso_month(), 2);
// 6. Let calendarID be ? ToString(yearMonth.[[Calendar]]).
Handle<String> calendar_id;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, calendar_id,
Object::ToString(isolate, handle(year_month->calendar(), isolate)),
String);
// 7. If showCalendar is "always" or if *_calendarID_ is not *"iso8601", then
if (show_calendar == ShowCalendar::kAlways ||
!String::Equals(isolate, calendar_id,
isolate->factory()->iso8601_string())) {
// a. Let day be ToZeroPaddedDecimalString(yearMonth.[[ISODay]], 2).
// b. Set result to the string-concatenation of result, the code unit 0x002D
// (HYPHEN-MINUS), and day.
builder.AppendCharacter('-');
ToZeroPaddedDecimalString(&builder, year_month->iso_day(), 2);
}
// 8. Let calendarString be ! FormatCalendarAnnotation(calendarID,
// showCalendar).
Handle<String> calendar_string =
FormatCalendarAnnotation(isolate, calendar_id, show_calendar);
// 9. Set result to the string-concatenation of result and calendarString.
builder.AppendString(calendar_string);
// 10. Return result.
......@@ -8026,6 +8050,12 @@ MaybeHandle<JSReceiver> JSTemporalPlainYearMonth::GetISOFields(
return fields;
}
// #sec-temporal.plainyearmonth.prototype.tojson
MaybeHandle<String> JSTemporalPlainYearMonth::ToJSON(
Isolate* isolate, Handle<JSTemporalPlainYearMonth> year_month) {
return TemporalYearMonthToString(isolate, year_month, ShowCalendar::kAuto);
}
// #sec-temporal-plaintime-constructor
MaybeHandle<JSTemporalPlainTime> JSTemporalPlainTime::Constructor(
Isolate* isolate, Handle<JSFunction> target, Handle<HeapObject> new_target,
......
......@@ -398,6 +398,10 @@ class JSTemporalPlainYearMonth
V8_WARN_UNUSED_RESULT static MaybeHandle<JSReceiver> GetISOFields(
Isolate* isolate, Handle<JSTemporalPlainYearMonth> year_month);
// #sec-temporal.plainyearmonth.prototype.tojson
V8_WARN_UNUSED_RESULT static MaybeHandle<String> ToJSON(
Isolate* isolate, Handle<JSTemporalPlainYearMonth> year_month);
// Abstract Operations
DECL_PRINTER(JSTemporalPlainYearMonth)
......
......@@ -47,7 +47,6 @@
# https://crbug.com/v8/11544
'temporal/calendar-date-until': [FAIL],
'temporal/calendar-week-of-year': [FAIL],
'temporal/calendar-year-month-from-fields': [FAIL],
'temporal/duration-add': [FAIL],
'temporal/duration-to-json': [FAIL],
'temporal/duration-with': [FAIL],
......
......@@ -1454,8 +1454,6 @@
'built-ins/Temporal/PlainYearMonth/prototype/subtract/overflow-undefined': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/subtract/overflow-wrong-type': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/subtract/subclassing-ignored': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/toJSON/branding': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/toJSON/year-format': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/toLocaleString/branding': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/toPlainDate/argument-not-object': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/toPlainDate/branding': [FAIL],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment