summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-08-01 18:08:45 +0100
committerLinus Groh <mail@linusgroh.de>2021-08-01 20:31:31 +0100
commitd022b74d3365323395b2697aee5bcbceffe54057 (patch)
tree884b2382b39f92f3ca57350c614ad48ea01a3d31 /Userland
parentbc416ab80275f1c01b8b8fff0010c802e3487d9f (diff)
downloadserenity-d022b74d3365323395b2697aee5bcbceffe54057.zip
LibJS: Implement Temporal.ZonedDateTime.prototype.calendar
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp30
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h3
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.calendar.js16
3 files changed, 49 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
index b1f71335fe..0322846dd1 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
@@ -4,7 +4,9 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/TypeCasts.h>
#include <LibJS/Runtime/GlobalObject.h>
+#include <LibJS/Runtime/Temporal/ZonedDateTime.h>
#include <LibJS/Runtime/Temporal/ZonedDateTimePrototype.h>
namespace JS::Temporal {
@@ -23,6 +25,34 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object)
// 6.3.2 Temporal.ZonedDateTime.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype-@@tostringtag
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.ZonedDateTime"), Attribute::Configurable);
+
+ define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable);
+}
+
+static ZonedDateTime* 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<ZonedDateTime>(this_object)) {
+ vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Temporal.ZonedDateTime");
+ return {};
+ }
+ return static_cast<ZonedDateTime*>(this_object);
+}
+
+// 6.3.3 get Temporal.ZonedDateTime.prototype.calendar, https://tc39.es/proposal-temporal/#sec-get-temporal.zoneddatetime.prototype.calendar
+JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::calendar_getter)
+{
+ // 1. Let zonedDateTime be the this value.
+ // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]).
+ auto* zoned_date_time = typed_this(global_object);
+ if (vm.exception())
+ return {};
+
+ // 3. Return zonedDateTime.[[Calendar]].
+ return Value(&zoned_date_time->calendar());
}
}
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
index 64be549401..dcfcaf392b 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
@@ -17,6 +17,9 @@ public:
explicit ZonedDateTimePrototype(GlobalObject&);
virtual void initialize(GlobalObject&) override;
virtual ~ZonedDateTimePrototype() override = default;
+
+private:
+ JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
};
}
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.calendar.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.calendar.js
new file mode 100644
index 0000000000..b8655eb7d9
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.calendar.js
@@ -0,0 +1,16 @@
+describe("correct behavior", () => {
+ test("basic functionality", () => {
+ const timeZone = new Temporal.TimeZone("UTC");
+ const calendar = new Temporal.Calendar("iso8601");
+ const zonedDateTime = new Temporal.ZonedDateTime(0n, timeZone, calendar);
+ expect(zonedDateTime.calendar).toBe(calendar);
+ });
+});
+
+test("errors", () => {
+ test("this value must be a Temporal.ZonedDateTime object", () => {
+ expect(() => {
+ Reflect.get(Temporal.ZonedDateTime.prototype, "calendar", "foo");
+ }).toThrowWithMessage(TypeError, "Not a Temporal.ZonedDateTime");
+ });
+});