From 0b44a86009d7114e45129da59a6227c48064329b Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Wed, 18 May 2022 17:45:39 -0700 Subject: [PATCH] [Temporal] Add PlainMonthDay.prototype.toJSON Also add AO: TemporalMonthDayToString Spec Text: https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.tojson https://tc39.es/proposal-temporal/#sec-temporal-temporalmonthdaytostring Bug: v8:11544 Change-Id: Ibbc5b28a9c73474f7edc1b67c9beabf5bca54dbc Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3437891 Reviewed-by: Adam Klein Commit-Queue: Frank Tang Cr-Commit-Position: refs/heads/main@{#80624} --- src/builtins/builtins-temporal.cc | 3 +- src/objects/js-temporal-objects.cc | 60 +++++++++++++++++++++++++++++- src/objects/js-temporal-objects.h | 4 ++ test/mjsunit/mjsunit.status | 1 - test/test262/test262.status | 3 -- 5 files changed, 64 insertions(+), 7 deletions(-) diff --git a/src/builtins/builtins-temporal.cc b/src/builtins/builtins-temporal.cc index 85c0596e55..96fd9fbf17 100644 --- a/src/builtins/builtins-temporal.cc +++ b/src/builtins/builtins-temporal.cc @@ -268,8 +268,6 @@ TO_BE_IMPLEMENTED(TemporalPlainMonthDayPrototypeWith) TO_BE_IMPLEMENTED(TemporalPlainMonthDayPrototypeEquals) /* Temporal #sec-temporal.plainmonthday.prototype.tostring */ TO_BE_IMPLEMENTED(TemporalPlainMonthDayPrototypeToString) -/* Temporal #sec-temporal.plainmonthday.tojson */ -TO_BE_IMPLEMENTED(TemporalPlainMonthDayPrototypeToJSON) /* Temporal #sec-temporal.plainmonthday.prototype.toplaindate */ TO_BE_IMPLEMENTED(TemporalPlainMonthDayPrototypeToPlainDate) @@ -601,6 +599,7 @@ TEMPORAL_GET_BY_FORWARD_CALENDAR(PlainMonthDay, MonthCode, monthCode) TEMPORAL_GET_BY_FORWARD_CALENDAR(PlainMonthDay, Day, day) TEMPORAL_PROTOTYPE_METHOD0(PlainMonthDay, GetISOFields, getISOFields) TEMPORAL_VALUE_OF(PlainMonthDay) +TEMPORAL_PROTOTYPE_METHOD0(PlainMonthDay, ToJSON, toJSON) // ZonedDateTime diff --git a/src/objects/js-temporal-objects.cc b/src/objects/js-temporal-objects.cc index a5851fadfa..1082fe98d5 100644 --- a/src/objects/js-temporal-objects.cc +++ b/src/objects/js-temporal-objects.cc @@ -2657,6 +2657,55 @@ MaybeHandle TemporalDateToString( return builder.Finish().ToHandleChecked(); } +// #sec-temporal-temporalmonthdaytostring +MaybeHandle TemporalMonthDayToString( + Isolate* isolate, Handle month_day, + ShowCalendar show_calendar) { + // 1. Assert: Type(monthDay) is Object. + // 2. Assert: monthDay has an [[InitializedTemporalMonthDay]] internal slot. + IncrementalStringBuilder builder(isolate); + // 6. Let calendarID be ? ToString(monthDay.[[Calendar]]). + Handle calendar_id; + ASSIGN_RETURN_ON_EXCEPTION( + isolate, calendar_id, + Object::ToString(isolate, handle(month_day->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 year be ! PadISOYear(monthDay.[[ISOYear]]). + PadISOYear(&builder, month_day->iso_year()); + // b. Set result to the string-concatenation of year, the code unit + // 0x002D (HYPHEN-MINUS), and result. + 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); + // 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); + // 8. Let calendarString be ! FormatCalendarAnnotation(calendarID, + // showCalendar). + Handle 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-builtintimezonegetoffsetstringfor MaybeHandle BuiltinTimeZoneGetOffsetStringFor( Isolate* isolate, Handle time_zone, @@ -6657,7 +6706,10 @@ MaybeHandle JSTemporalCalendar::MonthCode( Handle::cast(temporal_date_like)->iso_month(); } IncrementalStringBuilder builder(isolate); - builder.AppendCString((month < 10) ? "M0" : "M"); + builder.AppendCharacter('M'); + if (month < 10) { + builder.AppendCharacter('0'); + } builder.AppendInt(month); return builder.Finish(); @@ -7896,6 +7948,12 @@ MaybeHandle JSTemporalPlainMonthDay::GetISOFields( return fields; } +// #sec-temporal.plainmonthday.prototype.tojson +MaybeHandle JSTemporalPlainMonthDay::ToJSON( + Isolate* isolate, Handle month_day) { + return TemporalMonthDayToString(isolate, month_day, ShowCalendar::kAuto); +} + MaybeHandle JSTemporalPlainYearMonth::Constructor( Isolate* isolate, Handle target, Handle new_target, Handle iso_year_obj, Handle iso_month_obj, diff --git a/src/objects/js-temporal-objects.h b/src/objects/js-temporal-objects.h index 2c237db182..a76a55b36b 100644 --- a/src/objects/js-temporal-objects.h +++ b/src/objects/js-temporal-objects.h @@ -338,6 +338,10 @@ class JSTemporalPlainMonthDay V8_WARN_UNUSED_RESULT static MaybeHandle GetISOFields( Isolate* isolate, Handle month_day); + // #sec-temporal.plainmonthday.prototype.tojson + V8_WARN_UNUSED_RESULT static MaybeHandle ToJSON( + Isolate* isolate, Handle month_day); + DECL_PRINTER(JSTemporalPlainMonthDay) DEFINE_TORQUE_GENERATED_JS_TEMPORAL_YEAR_MONTH_DAY() diff --git a/test/mjsunit/mjsunit.status b/test/mjsunit/mjsunit.status index ee0e04a75a..d317d54f18 100644 --- a/test/mjsunit/mjsunit.status +++ b/test/mjsunit/mjsunit.status @@ -46,7 +46,6 @@ # Temporal tests to be implemented # https://crbug.com/v8/11544 'temporal/calendar-date-until': [FAIL], - 'temporal/calendar-month-day-from-fields': [FAIL], 'temporal/calendar-week-of-year': [FAIL], 'temporal/calendar-year-month-from-fields': [FAIL], 'temporal/duration-add': [FAIL], diff --git a/test/test262/test262.status b/test/test262/test262.status index d631fc9bbd..af075f80cd 100644 --- a/test/test262/test262.status +++ b/test/test262/test262.status @@ -1155,9 +1155,6 @@ 'built-ins/Temporal/PlainMonthDay/prototype/equals/calendars': [FAIL], 'built-ins/Temporal/PlainMonthDay/prototype/equals/calendar-temporal-object': [FAIL], 'built-ins/Temporal/PlainMonthDay/prototype/equals/infinity-throws-rangeerror': [FAIL], - 'built-ins/Temporal/PlainMonthDay/prototype/toJSON/branding': [FAIL], - 'built-ins/Temporal/PlainMonthDay/prototype/toJSON/calendarname': [FAIL], - 'built-ins/Temporal/PlainMonthDay/prototype/toJSON/year-format': [FAIL], 'built-ins/Temporal/PlainMonthDay/prototype/toLocaleString/branding': [FAIL], 'built-ins/Temporal/PlainMonthDay/prototype/toPlainDate/argument-not-object': [FAIL], 'built-ins/Temporal/PlainMonthDay/prototype/toPlainDate/branding': [FAIL],