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

LibJS: Implement Temporal.PlainDate.prototype.calendar

This commit is contained in:
Idan Horowitz 2021-07-19 00:40:14 +03:00 committed by Linus Groh
parent 94e1324a67
commit 94322ea985
3 changed files with 47 additions and 0 deletions

View file

@ -5,6 +5,7 @@
*/
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/PlainDate.h>
#include <LibJS/Runtime/Temporal/PlainDatePrototype.h>
namespace JS::Temporal {
@ -23,6 +24,34 @@ void PlainDatePrototype::initialize(GlobalObject& global_object)
// 3.3.2 Temporal.PlainDate.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype-@@tostringtag
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.PlainDate"), Attribute::Configurable);
define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable);
}
static PlainDate* 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<PlainDate>(this_object)) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Temporal.PlainDate");
return {};
}
return static_cast<PlainDate*>(this_object);
}
// 3.3.3 get Temporal.PlainDate.prototype.calendar, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.calendar
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::calendar_getter)
{
// 1. Let temporalDate be the this value.
// Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = typed_this(global_object);
if (vm.exception())
return {};
// 3. Return temporalDate.[[Calendar]].
return Value(&temporal_date->calendar());
}
}

View file

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

View file

@ -0,0 +1,15 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const calendar = { hello: "friends" };
const plain_date = new Temporal.PlainDate(1, 1, 1, calendar);
expect(plain_date.calendar).toBe(calendar);
});
});
test("errors", () => {
test("this value must be a Temporal.Duration object", () => {
expect(() => {
Reflect.get(Temporal.PlainDate.prototype, "calendar", "foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.PlainDate");
});
});