1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:47:44 +00:00

LibJS: Implement Temporal.PlainTime.prototype.calendar

This commit is contained in:
Linus Groh 2021-07-28 18:48:35 +01:00
parent ad89a205bc
commit a8dd1b9480
3 changed files with 46 additions and 0 deletions

View file

@ -5,6 +5,8 @@
*/
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/PlainTime.h>
#include <LibJS/Runtime/Temporal/PlainTimePrototype.h>
namespace JS::Temporal {
@ -24,10 +26,38 @@ void PlainTimePrototype::initialize(GlobalObject& global_object)
// 4.3.2 Temporal.PlainTime.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype-@@tostringtag
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.PlainTime"), Attribute::Configurable);
define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.valueOf, value_of, 0, attr);
}
static PlainTime* 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<PlainTime>(this_object)) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Temporal.PlainTime");
return {};
}
return static_cast<PlainTime*>(this_object);
}
// 4.3.3 get Temporal.PlainTime.prototype.calendar, https://tc39.es/proposal-temporal/#sec-get-temporal.plaintime.prototype.calendar
JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::calendar_getter)
{
// 1. Let temporalTime be the this value.
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
auto* temporal_time = typed_this(global_object);
if (vm.exception())
return {};
// 3. Return temporalTime.[[Calendar]].
return Value(&temporal_time->calendar());
}
// 4.3.23 Temporal.PlainTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.valueof
JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::value_of)
{

View file

@ -19,6 +19,7 @@ public:
virtual ~PlainTimePrototype() override = default;
private:
JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
JS_DECLARE_NATIVE_FUNCTION(value_of);
};

View file

@ -0,0 +1,15 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const plainTime = new Temporal.PlainTime();
expect(plainTime.calendar).toBeInstanceOf(Temporal.Calendar);
expect(plainTime.calendar.id).toBe("iso8601");
});
});
test("errors", () => {
test("this value must be a Temporal.PlainTime object", () => {
expect(() => {
Reflect.get(Temporal.PlainTime.prototype, "calendar", "foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.PlainTime");
});
});