LibJS: Implement Temporal.PlainYearMonth.prototype.calendar
This commit is contained in:
parent
d8e835d22f
commit
71eca69d7c
Notes:
sideshowbarker
2024-07-18 07:13:20 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/71eca69d7ce Pull-request: https://github.com/SerenityOS/serenity/pull/9273 Reviewed-by: https://github.com/IdanHo ✅
3 changed files with 48 additions and 0 deletions
|
@ -4,7 +4,9 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/TypeCasts.h>
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
#include <LibJS/Runtime/Temporal/PlainYearMonth.h>
|
||||
#include <LibJS/Runtime/Temporal/PlainYearMonthPrototype.h>
|
||||
|
||||
namespace JS::Temporal {
|
||||
|
@ -23,6 +25,34 @@ void PlainYearMonthPrototype::initialize(GlobalObject& global_object)
|
|||
|
||||
// 9.3.2 Temporal.PlainYearMonth.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype-@@tostringtag
|
||||
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);
|
||||
}
|
||||
|
||||
static PlainYearMonth* typed_this(GlobalObject& global_object)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
auto* this_object = vm.this_value(global_object).to_object(global_object);
|
||||
if (!this_object)
|
||||
return {};
|
||||
if (!is<PlainYearMonth>(this_object)) {
|
||||
vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Temporal.PlainYearMonth");
|
||||
return {};
|
||||
}
|
||||
return static_cast<PlainYearMonth*>(this_object);
|
||||
}
|
||||
|
||||
// 9.3.3 get Temporal.PlainYearMonth.prototype.calendar, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.calendar
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::calendar_getter)
|
||||
{
|
||||
// 1. Let plainYearMonth be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(plainYearMonth, [[InitializedTemporalYearMonth]]).
|
||||
auto* plain_year_month = typed_this(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
// 3. Return plainYearMonth.[[Calendar]].
|
||||
return Value(&plain_year_month->calendar());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,6 +17,9 @@ public:
|
|||
explicit PlainYearMonthPrototype(GlobalObject&);
|
||||
virtual void initialize(GlobalObject&) override;
|
||||
virtual ~PlainYearMonthPrototype() override = default;
|
||||
|
||||
private:
|
||||
JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
describe("correct behavior", () => {
|
||||
test("basic functionality", () => {
|
||||
const calendar = new Temporal.Calendar("iso8601");
|
||||
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7, calendar);
|
||||
expect(plainYearMonth.calendar).toBe(calendar);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.PlainYearMonth object", () => {
|
||||
expect(() => {
|
||||
Reflect.get(Temporal.PlainYearMonth.prototype, "calendar", "foo");
|
||||
}).toThrowWithMessage(TypeError, "Not a Temporal.PlainYearMonth");
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue