1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:27:45 +00:00

LibJS: Implement Temporal.ZonedDateTime.prototype.calendar

This commit is contained in:
Linus Groh 2021-08-01 18:08:45 +01:00
parent bc416ab802
commit d022b74d33
3 changed files with 49 additions and 0 deletions

View file

@ -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());
}
}

View file

@ -17,6 +17,9 @@ public:
explicit ZonedDateTimePrototype(GlobalObject&);
virtual void initialize(GlobalObject&) override;
virtual ~ZonedDateTimePrototype() override = default;
private:
JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
};
}

View file

@ -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");
});
});