LibJS: Implement Temporal.PlainYearMonth.prototype.year

This commit is contained in:
Linus Groh 2021-08-07 22:53:18 +01:00
parent 71eca69d7c
commit c947ba9ed9
3 changed files with 33 additions and 0 deletions

View file

@ -6,6 +6,7 @@
#include <AK/TypeCasts.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/PlainYearMonth.h>
#include <LibJS/Runtime/Temporal/PlainYearMonthPrototype.h>
@ -27,6 +28,7 @@ void PlainYearMonthPrototype::initialize(GlobalObject& global_object)
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.PlainYearMonth"), Attribute::Configurable);
define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.year, year_getter, {}, Attribute::Configurable);
}
static PlainYearMonth* typed_this(GlobalObject& global_object)
@ -55,4 +57,20 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::calendar_getter)
return Value(&plain_year_month->calendar());
}
// 9.3.4 get Temporal.PlainYearMonth.prototype.year, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.year
JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::year_getter)
{
// 1. Let yearMonth be the this value.
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
auto* year_month = typed_this(global_object);
if (vm.exception())
return {};
// 3. Let calendar be yearMonth.[[Calendar]].
auto& calendar = year_month->calendar();
// 4. Return 𝔽(? CalendarYear(calendar, yearMonth)).
return Value(calendar_year(global_object, calendar, *year_month));
}
}

View file

@ -20,6 +20,7 @@ public:
private:
JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
JS_DECLARE_NATIVE_FUNCTION(year_getter);
};
}

View file

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