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

[Temporal] Add Plain(YearMonth|MonthDay).prototype.toPlainDate

Spec Text:
https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.toplaindate
https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.toplaindate

Bug: v8:11544
Change-Id: I506efe3fa3685f621596fa34301a73d8c57ded38
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3566114
Commit-Queue: Frank Tang <ftang@chromium.org>
Reviewed-by: 's avatarAdam Klein <adamk@chromium.org>
Cr-Commit-Position: refs/heads/main@{#80783}
parent 68a7736b
......@@ -176,16 +176,12 @@ TO_BE_IMPLEMENTED(TemporalPlainYearMonthPrototypeUntil)
TO_BE_IMPLEMENTED(TemporalPlainYearMonthPrototypeSince)
/* Temporal #sec-temporal.plainyearmonth.prototype.equals */
TO_BE_IMPLEMENTED(TemporalPlainYearMonthPrototypeEquals)
/* Temporal #sec-temporal.plainyearmonth.prototype.toplaindate */
TO_BE_IMPLEMENTED(TemporalPlainYearMonthPrototypeToPlainDate)
/* Temporal.PlainMonthDay */
/* There is no compare for PlainMonthDay. See
* https://github.com/tc39/proposal-temporal/issues/1547 */
/* Temporal #sec-temporal.plainmonthday.prototype.equals */
TO_BE_IMPLEMENTED(TemporalPlainMonthDayPrototypeEquals)
/* Temporal #sec-temporal.plainmonthday.prototype.toplaindate */
TO_BE_IMPLEMENTED(TemporalPlainMonthDayPrototypeToPlainDate)
/* Temporal.Calendar */
/* Temporal #sec-temporal.calendar.prototype.weekofyear */
......@@ -499,6 +495,7 @@ TEMPORAL_GET_BY_INVOKE_CALENDAR_METHOD(PlainYearMonth, MonthsInYear,
TEMPORAL_GET_BY_INVOKE_CALENDAR_METHOD(PlainYearMonth, InLeapYear, inLeapYear)
TEMPORAL_METHOD2(PlainYearMonth, From)
TEMPORAL_PROTOTYPE_METHOD2(PlainYearMonth, With, with)
TEMPORAL_PROTOTYPE_METHOD1(PlainYearMonth, ToPlainDate, toPlainDate)
TEMPORAL_PROTOTYPE_METHOD0(PlainYearMonth, GetISOFields, getISOFields)
TEMPORAL_VALUE_OF(PlainYearMonth)
TEMPORAL_PROTOTYPE_METHOD2(PlainYearMonth, ToLocaleString, toLocaleString)
......@@ -521,6 +518,7 @@ TEMPORAL_GET_BY_FORWARD_CALENDAR(PlainMonthDay, MonthCode, monthCode)
TEMPORAL_GET_BY_FORWARD_CALENDAR(PlainMonthDay, Day, day)
TEMPORAL_METHOD2(PlainMonthDay, From)
TEMPORAL_PROTOTYPE_METHOD2(PlainMonthDay, With, with)
TEMPORAL_PROTOTYPE_METHOD1(PlainMonthDay, ToPlainDate, toPlainDate)
TEMPORAL_PROTOTYPE_METHOD0(PlainMonthDay, GetISOFields, getISOFields)
TEMPORAL_VALUE_OF(PlainMonthDay)
TEMPORAL_PROTOTYPE_METHOD0(PlainMonthDay, ToJSON, toJSON)
......
......@@ -25,6 +25,7 @@
#include "src/objects/objects-inl.h"
#include "src/objects/option-utils.h"
#include "src/objects/property-descriptor.h"
#include "src/objects/string-set.h"
#include "src/strings/string-builder-inl.h"
#include "src/temporal/temporal-parser.h"
#ifdef V8_INTL_SUPPORT
......@@ -8038,8 +8039,6 @@ MaybeHandle<JSReceiver> CalendarMergeFields(
return Handle<JSReceiver>::cast(result);
}
} // namespace
// Common code shared by Temporal.Plain(Date|YearMonth|MonthDay).prototype.with
template <typename T,
MaybeHandle<T> (*from_fields_func)(
......@@ -8099,6 +8098,8 @@ MaybeHandle<T> PlainDateOrYearMonthOrMonthDayWith(
return from_fields_func(isolate, calendar, fields, options);
}
} // namespace
// #sec-temporal.plaindate.prototype.with
MaybeHandle<JSTemporalPlainDate> JSTemporalPlainDate::With(
Isolate* isolate, Handle<JSTemporalPlainDate> temporal_date,
......@@ -9360,6 +9361,124 @@ MaybeHandle<JSTemporalPlainMonthDay> JSTemporalPlainMonthDay::With(
field_names, "Temporal.PlainMonthDay.prototype.with");
}
namespace {
// Common code shared by PlainMonthDay and PlainYearMonth.prototype.toPlainDate
template <typename T>
MaybeHandle<JSTemporalPlainDate> PlainMonthDayOrYearMonthToPlainDate(
Isolate* isolate, Handle<T> temporal, Handle<Object> item_obj,
Handle<String> receiver_field_name_1, Handle<String> receiver_field_name_2,
Handle<String> input_field_name) {
Factory* factory = isolate->factory();
// 1. Let monthDay be the this value.
// 2. Perform ? RequireInternalSlot(monthDay,
// [[InitializedTemporalXXX]]).
// 3. If Type(item) is not Object, then
if (!item_obj->IsJSReceiver()) {
// a. Throw a TypeError exception.
THROW_NEW_ERROR(isolate, NEW_TEMPORAL_INVALID_ARG_TYPE_ERROR(),
JSTemporalPlainDate);
}
Handle<JSReceiver> item = Handle<JSReceiver>::cast(item_obj);
// 4. Let calendar be Xxx.[[Calendar]].
Handle<JSReceiver> calendar(temporal->calendar(), isolate);
// 5. Let receiverFieldNames be ? CalendarFields(calendar, «
// receiverFieldName1, receiverFieldName2 »).
Handle<FixedArray> receiver_field_names = factory->NewFixedArray(2);
receiver_field_names->set(0, *receiver_field_name_1);
receiver_field_names->set(1, *receiver_field_name_2);
ASSIGN_RETURN_ON_EXCEPTION(
isolate, receiver_field_names,
CalendarFields(isolate, calendar, receiver_field_names),
JSTemporalPlainDate);
// 6. Let fields be ? PrepareTemporalFields(temporal, receiverFieldNames, «»).
Handle<JSReceiver> fields;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, fields,
PrepareTemporalFields(isolate, temporal, receiver_field_names,
RequiredFields::kNone),
JSTemporalPlainDate);
// 7. Let inputFieldNames be ? CalendarFields(calendar, « inputFieldName »).
Handle<FixedArray> input_field_names = factory->NewFixedArray(1);
input_field_names->set(0, *input_field_name);
ASSIGN_RETURN_ON_EXCEPTION(
isolate, input_field_names,
CalendarFields(isolate, calendar, input_field_names),
JSTemporalPlainDate);
// 8. Let inputFields be ? PrepareTemporalFields(item, inputFieldNames, «»).
Handle<JSReceiver> input_fields;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, input_fields,
PrepareTemporalFields(isolate, item, input_field_names,
RequiredFields::kNone),
JSTemporalPlainDate);
// 9. Let mergedFields be ? CalendarMergeFields(calendar, fields,
// inputFields).
Handle<JSReceiver> merged_fields;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, merged_fields,
CalendarMergeFields(isolate, calendar, fields, input_fields),
JSTemporalPlainDate);
// 10. Let mergedFieldNames be the List containing all the elements of
// receiverFieldNames followed by all the elements of inputFieldNames, with
// duplicate elements removed.
Handle<FixedArray> merged_field_names = factory->NewFixedArray(
receiver_field_names->length() + input_field_names->length());
Handle<StringSet> added = StringSet::New(isolate);
for (int i = 0; i < receiver_field_names->length(); i++) {
Handle<Object> item(receiver_field_names->get(i), isolate);
DCHECK(item->IsString());
Handle<String> string = Handle<String>::cast(item);
if (!added->Has(isolate, string)) {
merged_field_names->set(added->NumberOfElements(), *item);
added = StringSet::Add(isolate, added, string);
}
}
for (int i = 0; i < input_field_names->length(); i++) {
Handle<Object> item(input_field_names->get(i), isolate);
DCHECK(item->IsString());
Handle<String> string = Handle<String>::cast(item);
if (!added->Has(isolate, string)) {
merged_field_names->set(added->NumberOfElements(), *item);
added = StringSet::Add(isolate, added, string);
}
}
merged_field_names = FixedArray::ShrinkOrEmpty(isolate, merged_field_names,
added->NumberOfElements());
// 11. Set mergedFields to ? PrepareTemporalFields(mergedFields,
// mergedFieldNames, «»).
ASSIGN_RETURN_ON_EXCEPTION(
isolate, merged_fields,
PrepareTemporalFields(isolate, merged_fields, merged_field_names,
RequiredFields::kNone),
JSTemporalPlainDate);
// 12. Let options be ! OrdinaryObjectCreate(null).
Handle<JSObject> options = factory->NewJSObjectWithNullProto();
// 13. Perform ! CreateDataPropertyOrThrow(options, "overflow", "reject").
CHECK(JSReceiver::CreateDataProperty(
isolate, options, factory->overflow_string(),
factory->reject_string(), Just(kThrowOnError))
.FromJust());
// 14. Return ? DateFromFields(calendar, mergedFields, options).
return DateFromFields(isolate, calendar, merged_fields, options);
}
} // namespace
// #sec-temporal.plainmonthday.prototype.toplaindate
MaybeHandle<JSTemporalPlainDate> JSTemporalPlainMonthDay::ToPlainDate(
Isolate* isolate, Handle<JSTemporalPlainMonthDay> month_day,
Handle<Object> item_obj) {
Factory* factory = isolate->factory();
// 5. Let receiverFieldNames be ? CalendarFields(calendar, « "day",
// "monthCode" »).
// 7. Let inputFieldNames be ? CalendarFields(calendar, « "year" »).
return PlainMonthDayOrYearMonthToPlainDate<JSTemporalPlainMonthDay>(
isolate, month_day, item_obj, factory->day_string(),
factory->monthCode_string(), factory->year_string());
}
// #sec-temporal.plainmonthday.prototype.getisofields
MaybeHandle<JSReceiver> JSTemporalPlainMonthDay::GetISOFields(
Isolate* isolate, Handle<JSTemporalPlainMonthDay> month_day) {
......@@ -9616,6 +9735,19 @@ MaybeHandle<JSTemporalPlainYearMonth> JSTemporalPlainYearMonth::With(
field_names, "Temporal.PlainYearMonth.prototype.with");
}
// #sec-temporal.plainyearmonth.prototype.toplaindate
MaybeHandle<JSTemporalPlainDate> JSTemporalPlainYearMonth::ToPlainDate(
Isolate* isolate, Handle<JSTemporalPlainYearMonth> year_month,
Handle<Object> item_obj) {
Factory* factory = isolate->factory();
// 5. Let receiverFieldNames be ? CalendarFields(calendar, « "monthCode",
// "year" »).
// 7. Let inputFieldNames be ? CalendarFields(calendar, « "day" »).
return PlainMonthDayOrYearMonthToPlainDate<JSTemporalPlainYearMonth>(
isolate, year_month, item_obj, factory->monthCode_string(),
factory->year_string(), factory->day_string());
}
// #sec-temporal.plainyearmonth.prototype.getisofields
MaybeHandle<JSReceiver> JSTemporalPlainYearMonth::GetISOFields(
Isolate* isolate, Handle<JSTemporalPlainYearMonth> year_month) {
......
......@@ -433,6 +433,11 @@ class JSTemporalPlainMonthDay
Isolate* isolate, Handle<JSTemporalPlainMonthDay> month_day,
Handle<Object> temporal_month_day_like, Handle<Object> options);
// #sec-temporal.plainmonthday.prototype.toplaindate
V8_WARN_UNUSED_RESULT static MaybeHandle<JSTemporalPlainDate> ToPlainDate(
Isolate* isolate, Handle<JSTemporalPlainMonthDay> month_day,
Handle<Object> item);
// #sec-temporal.plainmonthday.prototype.getisofields
V8_WARN_UNUSED_RESULT static MaybeHandle<JSReceiver> GetISOFields(
Isolate* isolate, Handle<JSTemporalPlainMonthDay> month_day);
......@@ -541,6 +546,11 @@ class JSTemporalPlainYearMonth
Isolate* isolate, Handle<JSTemporalPlainYearMonth> year_month,
Handle<Object> temporal_year_month_like, Handle<Object> options);
// #sec-temporal.plainyearmonth.prototype.toplaindate
V8_WARN_UNUSED_RESULT static MaybeHandle<JSTemporalPlainDate> ToPlainDate(
Isolate* isolate, Handle<JSTemporalPlainYearMonth> year_month,
Handle<Object> item);
// #sec-temporal.plainyearmonth.prototype.getisofields
V8_WARN_UNUSED_RESULT static MaybeHandle<JSReceiver> GetISOFields(
Isolate* isolate, Handle<JSTemporalPlainYearMonth> year_month);
......
......@@ -297,10 +297,6 @@
'built-ins/RegExp/property-escapes/generated/strings/RGI_Emoji_Tag_Sequence': [FAIL],
'built-ins/RegExp/property-escapes/generated/strings/RGI_Emoji_ZWJ_Sequence': [FAIL],
# Reject invalid date
# https://github.com/tc39/test262/issues/3252
'built-ins/Temporal/PlainMonthDay/prototype/toPlainDate/basic': [FAIL],
# -0
'built-ins/Temporal/PlainDate/prototype/subtract/balance-smaller-units': [FAIL],
'built-ins/Temporal/PlainDate/prototype/since/roundingmode-undefined': [FAIL],
......@@ -1010,12 +1006,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/toPlainDate/argument-not-object': [FAIL],
'built-ins/Temporal/PlainMonthDay/prototype/toPlainDate/branding': [FAIL],
'built-ins/Temporal/PlainMonthDay/prototype/toPlainDate/calendar-fields-iterable': [FAIL],
'built-ins/Temporal/PlainMonthDay/prototype/toPlainDate/calendar-merge-fields-returns-primitive': [FAIL],
'built-ins/Temporal/PlainMonthDay/prototype/toPlainDate/copies-merge-fields-object': [FAIL],
'built-ins/Temporal/PlainMonthDay/prototype/toPlainDate/infinity-throws-rangeerror': [FAIL],
'built-ins/Temporal/PlainMonthDay/prototype/toPlainDate/limits': [FAIL],
'built-ins/Temporal/PlainMonthDay/prototype/with/basic': [FAIL],
'built-ins/Temporal/PlainMonthDay/prototype/with/calendar-arguments': [FAIL],
......@@ -1219,12 +1209,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/toPlainDate/argument-not-object': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/toPlainDate/branding': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/toPlainDate/calendar-fields-iterable': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/toPlainDate/calendar-merge-fields-returns-primitive': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/toPlainDate/copies-merge-fields-object': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/toPlainDate/infinity-throws-rangeerror': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/toPlainDate/limits': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/until/argument-string-with-utc-designator': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/until/branding': [FAIL],
......@@ -1607,7 +1591,6 @@
'intl402/Temporal/PlainMonthDay/prototype/toLocaleString/options-undefined': [FAIL],
'intl402/Temporal/PlainMonthDay/prototype/toLocaleString/resolved-time-zone': [FAIL],
'intl402/Temporal/PlainMonthDay/prototype/toLocaleString/timezone-getoffsetnanosecondsfor-not-callable': [FAIL],
'intl402/Temporal/PlainMonthDay/prototype/toPlainDate/infinity-throws-rangeerror': [FAIL],
'intl402/Temporal/PlainTime/prototype/toLocaleString/locales-undefined': [FAIL],
'intl402/Temporal/PlainTime/prototype/toLocaleString/options-conflict': [FAIL],
'intl402/Temporal/PlainTime/prototype/toLocaleString/options-undefined': [FAIL],
......@@ -1848,7 +1831,6 @@
'built-ins/Temporal/PlainYearMonth/prototype/subtract/limits': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/subtract/month-length': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/subtract/options-invalid': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/toPlainDate/basic': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/until/argument-casting': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/until/arguments-missing-throws': [FAIL],
'built-ins/Temporal/PlainYearMonth/prototype/until/largestunit-auto': [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