1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:27:35 +00:00

LibJS: Implement Temporal.PlainDateTime.prototype.getISOFields()

This commit is contained in:
Linus Groh 2021-07-23 01:04:10 +01:00
parent 18fd0d4011
commit 080112eb82
4 changed files with 106 additions and 0 deletions

View file

@ -173,6 +173,7 @@ namespace JS {
P(getInt8) \
P(getInt16) \
P(getInt32) \
P(getISOFields) \
P(getMilliseconds) \
P(getMinutes) \
P(getMonth) \
@ -227,6 +228,15 @@ namespace JS {
P(isSafeInteger) \
P(isSealed) \
P(isView) \
P(isoDay) \
P(isoHour) \
P(isoMicrosecond) \
P(isoMillisecond) \
P(isoMinute) \
P(isoMonth) \
P(isoNanosecond) \
P(isoSecond) \
P(isoYear) \
P(italics) \
P(join) \
P(keyFor) \

View file

@ -31,6 +31,7 @@ void PlainDateTimePrototype::initialize(GlobalObject& global_object)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.valueOf, value_of, 0, attr);
define_native_function(vm.names.toPlainDate, to_plain_date, 0, attr);
define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr);
}
static PlainDateTime* typed_this(GlobalObject& global_object)
@ -80,4 +81,51 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_plain_date)
return create_temporal_date(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->calendar());
}
// 5.3.41 Temporal.PlainDateTime.prototype.getISOFields ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.getisofields
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::get_iso_fields)
{
// 1. Let dateTime be the this value.
// 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]).
auto* date_time = typed_this(global_object);
if (vm.exception())
return {};
// 3. Let fields be ! OrdinaryObjectCreate(%Object.prototype%).
auto* fields = Object::create(global_object, global_object.object_prototype());
// 4. Perform ! CreateDataPropertyOrThrow(fields, "calendar", dateTime.[[Calendar]]).
fields->create_data_property_or_throw(vm.names.calendar, Value(&date_time->calendar()));
// 5. Perform ! CreateDataPropertyOrThrow(fields, "isoDay", 𝔽(dateTime.[[ISODay]])).
fields->create_data_property_or_throw(vm.names.isoDay, Value(date_time->iso_day()));
// 6. Perform ! CreateDataPropertyOrThrow(fields, "isoHour", 𝔽(dateTime.[[ISOHour]])).
fields->create_data_property_or_throw(vm.names.isoHour, Value(date_time->iso_hour()));
// 7. Perform ! CreateDataPropertyOrThrow(fields, "isoMicrosecond", 𝔽(dateTime.[[ISOMicrosecond]])).
fields->create_data_property_or_throw(vm.names.isoMicrosecond, Value(date_time->iso_microsecond()));
// 8. Perform ! CreateDataPropertyOrThrow(fields, "isoMillisecond", 𝔽(dateTime.[[ISOMillisecond]])).
fields->create_data_property_or_throw(vm.names.isoMillisecond, Value(date_time->iso_millisecond()));
// 9. Perform ! CreateDataPropertyOrThrow(fields, "isoMinute", 𝔽(dateTime.[[ISOMinute]])).
fields->create_data_property_or_throw(vm.names.isoMinute, Value(date_time->iso_minute()));
// 10. Perform ! CreateDataPropertyOrThrow(fields, "isoMonth", 𝔽(dateTime.[[ISOMonth]])).
fields->create_data_property_or_throw(vm.names.isoMonth, Value(date_time->iso_month()));
// 11. Perform ! CreateDataPropertyOrThrow(fields, "isoNanosecond", 𝔽(dateTime.[[ISONanosecond]])).
fields->create_data_property_or_throw(vm.names.isoNanosecond, Value(date_time->iso_nanosecond()));
// TODO: Typo in the spec? ([[Second]] -> [[ISOSecond]])
// 12. Perform ! CreateDataPropertyOrThrow(fields, "isoSecond", 𝔽(dateTime.[[Second]])).
fields->create_data_property_or_throw(vm.names.isoSecond, Value(date_time->iso_second()));
// 13. Perform ! CreateDataPropertyOrThrow(fields, "isoYear", 𝔽(dateTime.[[ISOYear]])).
fields->create_data_property_or_throw(vm.names.isoYear, Value(date_time->iso_year()));
// 14. Return fields.
return fields;
}
}

View file

@ -22,6 +22,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
JS_DECLARE_NATIVE_FUNCTION(value_of);
JS_DECLARE_NATIVE_FUNCTION(to_plain_date);
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
};
}