LibJS: Implement Temporal.PlainMonthDay.prototype.day

This commit is contained in:
Linus Groh 2021-08-15 01:07:49 +01:00
parent 9551aa17d3
commit c2ed3ad66b
Notes: sideshowbarker 2024-07-18 05:41:47 +09:00
3 changed files with 32 additions and 0 deletions

View File

@ -29,6 +29,7 @@ void PlainMonthDayPrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.monthCode, month_code_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.day, day_getter, {}, Attribute::Configurable);
}
static PlainMonthDay* typed_this(GlobalObject& global_object)
@ -73,4 +74,20 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::month_code_getter)
return js_string(vm, calendar_month_code(global_object, calendar, *month_day));
}
// 10.3.5 get Temporal.PlainMonthDay.prototype.day, https://tc39.es/proposal-temporal/#sec-get-temporal.plainmonthday.prototype.day
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::day_getter)
{
// 1. Let monthDay be the this value.
// 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]).
auto* month_day = typed_this(global_object);
if (vm.exception())
return {};
// 3. Let calendar be monthDay.[[Calendar]].
auto& calendar = month_day->calendar();
// 4. Return 𝔽(? CalendarDay(calendar, monthDay)).
return Value(calendar_day(global_object, calendar, *month_day));
}
}

View File

@ -21,6 +21,7 @@ public:
private:
JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
JS_DECLARE_NATIVE_FUNCTION(month_code_getter);
JS_DECLARE_NATIVE_FUNCTION(day_getter);
};
}

View File

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