diff options
author | Linus Groh <mail@linusgroh.de> | 2021-08-14 23:59:43 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-08-15 10:36:36 +0100 |
commit | 1382be17072bb119173c95b70d8e424983d26dad (patch) | |
tree | b5e59097146f713a67a7f4bdf4d498958eaf10d6 /Userland/Libraries/LibJS | |
parent | 37b5a0715e2edfa1dcf28ae1970c0b618c50798d (diff) | |
download | serenity-1382be17072bb119173c95b70d8e424983d26dad.zip |
LibJS: Implement Temporal.PlainMonthDay.prototype.calendar
Diffstat (limited to 'Userland/Libraries/LibJS')
3 files changed, 48 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp index 96ac820b89..cab057394b 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp @@ -4,7 +4,9 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include <AK/TypeCasts.h> #include <LibJS/Runtime/GlobalObject.h> +#include <LibJS/Runtime/Temporal/PlainMonthDay.h> #include <LibJS/Runtime/Temporal/PlainMonthDayPrototype.h> namespace JS::Temporal { @@ -23,6 +25,34 @@ void PlainMonthDayPrototype::initialize(GlobalObject& global_object) // 10.3.2 Temporal.PlainMonthDay.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype-@@tostringtag define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Temporal.PlainMonthDay"), Attribute::Configurable); + + define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable); +} + +static PlainMonthDay* 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<PlainMonthDay>(this_object)) { + vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Temporal.PlainMonthDay"); + return {}; + } + return static_cast<PlainMonthDay*>(this_object); +} + +// 10.3.3 get Temporal.PlainMonthDay.prototype.calendar, https://tc39.es/proposal-temporal/#sec-get-temporal.plainmonthday.prototype.calendar +JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::calendar_getter) +{ + // 1. Let plainMonthDay be the this value. + // 2. Perform ? RequireInternalSlot(plainMonthDay, [[InitializedTemporalMonthDay]]). + auto* plain_month_day = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Return plainMonthDay.[[Calendar]]. + return Value(&plain_month_day->calendar()); } } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h index fe7d73f8ab..6d6c3759dd 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h @@ -17,6 +17,9 @@ public: explicit PlainMonthDayPrototype(GlobalObject&); virtual void initialize(GlobalObject&) override; virtual ~PlainMonthDayPrototype() override = default; + +private: + JS_DECLARE_NATIVE_FUNCTION(calendar_getter); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.calendar.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.calendar.js new file mode 100644 index 0000000000..e699dcf4e6 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.calendar.js @@ -0,0 +1,15 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const calendar = new Temporal.Calendar("iso8601"); + const plainMonthDay = new Temporal.PlainMonthDay(7, 6, calendar); + expect(plainMonthDay.calendar).toBe(calendar); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainMonthDay object", () => { + expect(() => { + Reflect.get(Temporal.PlainMonthDay.prototype, "calendar", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainMonthDay"); + }); +}); |