mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 09:37:34 +00:00
LibJS: Implement Temporal.PlainDateTime.prototype.getISOFields()
This commit is contained in:
parent
18fd0d4011
commit
080112eb82
4 changed files with 106 additions and 0 deletions
|
@ -173,6 +173,7 @@ namespace JS {
|
||||||
P(getInt8) \
|
P(getInt8) \
|
||||||
P(getInt16) \
|
P(getInt16) \
|
||||||
P(getInt32) \
|
P(getInt32) \
|
||||||
|
P(getISOFields) \
|
||||||
P(getMilliseconds) \
|
P(getMilliseconds) \
|
||||||
P(getMinutes) \
|
P(getMinutes) \
|
||||||
P(getMonth) \
|
P(getMonth) \
|
||||||
|
@ -227,6 +228,15 @@ namespace JS {
|
||||||
P(isSafeInteger) \
|
P(isSafeInteger) \
|
||||||
P(isSealed) \
|
P(isSealed) \
|
||||||
P(isView) \
|
P(isView) \
|
||||||
|
P(isoDay) \
|
||||||
|
P(isoHour) \
|
||||||
|
P(isoMicrosecond) \
|
||||||
|
P(isoMillisecond) \
|
||||||
|
P(isoMinute) \
|
||||||
|
P(isoMonth) \
|
||||||
|
P(isoNanosecond) \
|
||||||
|
P(isoSecond) \
|
||||||
|
P(isoYear) \
|
||||||
P(italics) \
|
P(italics) \
|
||||||
P(join) \
|
P(join) \
|
||||||
P(keyFor) \
|
P(keyFor) \
|
||||||
|
|
|
@ -31,6 +31,7 @@ void PlainDateTimePrototype::initialize(GlobalObject& global_object)
|
||||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||||
define_native_function(vm.names.valueOf, value_of, 0, attr);
|
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.toPlainDate, to_plain_date, 0, attr);
|
||||||
|
define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
static PlainDateTime* typed_this(GlobalObject& global_object)
|
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());
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ private:
|
||||||
JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
|
JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(to_plain_date);
|
JS_DECLARE_NATIVE_FUNCTION(to_plain_date);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
describe("normal behavior", () => {
|
||||||
|
test("length is 0", () => {
|
||||||
|
expect(Temporal.PlainDateTime.prototype.getISOFields).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("basic functionality", () => {
|
||||||
|
const calendar = new Temporal.Calendar("iso8601");
|
||||||
|
const plainDateTime = new Temporal.PlainDateTime(
|
||||||
|
2021,
|
||||||
|
7,
|
||||||
|
23,
|
||||||
|
0,
|
||||||
|
42,
|
||||||
|
18,
|
||||||
|
123,
|
||||||
|
456,
|
||||||
|
789,
|
||||||
|
calendar
|
||||||
|
);
|
||||||
|
const fields = plainDateTime.getISOFields();
|
||||||
|
expect(fields).toEqual({
|
||||||
|
calendar: calendar,
|
||||||
|
isoDay: 23,
|
||||||
|
isoHour: 0,
|
||||||
|
isoMicrosecond: 200,
|
||||||
|
isoMillisecond: 123,
|
||||||
|
isoMinute: 42,
|
||||||
|
isoMonth: 7,
|
||||||
|
isoNanosecond: 21,
|
||||||
|
isoSecond: 18,
|
||||||
|
isoYear: 2021,
|
||||||
|
});
|
||||||
|
// Test field order
|
||||||
|
expect(Object.getOwnPropertyNames(fields)).toEqual([
|
||||||
|
"calendar",
|
||||||
|
"isoDay",
|
||||||
|
"isoHour",
|
||||||
|
"isoMicrosecond",
|
||||||
|
"isoMillisecond",
|
||||||
|
"isoMinute",
|
||||||
|
"isoMonth",
|
||||||
|
"isoNanosecond",
|
||||||
|
"isoSecond",
|
||||||
|
"isoYear",
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue