LibJS: Implement Temporal.PlainDateTime.prototype.hour

This commit is contained in:
Idan Horowitz 2021-07-30 00:02:50 +03:00 committed by Linus Groh
parent f93b6ea58c
commit f553ab3104
Notes: sideshowbarker 2024-07-18 07:46:22 +09:00
3 changed files with 29 additions and 0 deletions

View file

@ -32,6 +32,7 @@ void PlainDateTimePrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.month, month_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.monthCode, month_code_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.day, day_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.hour, hour_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.valueOf, value_of, 0, attr);
@ -129,6 +130,19 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::day_getter)
return Value(calendar_day(global_object, calendar, *date_time));
}
// 5.3.8 get Temporal.PlainDateTime.prototype.hour, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindatetime.prototype.hour
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::hour_getter)
{
// 1. Let dateTime be the this value.
// 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]).
auto* date_time = typed_this(global_object);
if (vm.exception())
return {};
// 3. Return 𝔽(dateTime.[[ISOHour]]).
return Value(date_time->iso_hour());
}
// 5.3.35 Temporal.PlainDateTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.valueof
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::value_of)
{

View file

@ -24,6 +24,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(month_getter);
JS_DECLARE_NATIVE_FUNCTION(month_code_getter);
JS_DECLARE_NATIVE_FUNCTION(day_getter);
JS_DECLARE_NATIVE_FUNCTION(hour_getter);
JS_DECLARE_NATIVE_FUNCTION(value_of);
JS_DECLARE_NATIVE_FUNCTION(to_plain_date);
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);

View file

@ -0,0 +1,14 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const plainDateTime = new Temporal.PlainDateTime(2021, 7, 30, 1);
expect(plainDateTime.hour).toBe(1);
});
});
test("errors", () => {
test("this value must be a Temporal.PlainDateTime object", () => {
expect(() => {
Reflect.get(Temporal.PlainDateTime.prototype, "hour", "foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.PlainDateTime");
});
});