summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-08-07 22:48:29 +0100
committerLinus Groh <mail@linusgroh.de>2021-08-08 17:45:06 +0100
commit71eca69d7ce2169b703801e16eb4836fd92bba4d (patch)
tree961738322e32c8cad24fe7b047e31b426bfa72a2 /Userland
parentd8e835d22f1b2e753d33021313f543a2b8d6ca01 (diff)
downloadserenity-71eca69d7ce2169b703801e16eb4836fd92bba4d.zip
LibJS: Implement Temporal.PlainYearMonth.prototype.calendar
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp30
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h3
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.calendar.js15
3 files changed, 48 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp
index 7af2cdf406..d63a32767c 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp
@@ -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());
}
}
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h
index 5d1f0f5193..39b3d19ba7 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h
@@ -17,6 +17,9 @@ public:
explicit PlainYearMonthPrototype(GlobalObject&);
virtual void initialize(GlobalObject&) override;
virtual ~PlainYearMonthPrototype() override = default;
+
+private:
+ JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
};
}
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.calendar.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.calendar.js
new file mode 100644
index 0000000000..9b80816de8
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.calendar.js
@@ -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");
+ });
+});