LibJS: Implement Temporal.PlainDateTime.prototype.calendar
This commit is contained in:
parent
78acc976a6
commit
aa2c8b6b91
Notes:
sideshowbarker
2024-07-18 08:33:20 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/aa2c8b6b911 Pull-request: https://github.com/SerenityOS/serenity/pull/8940 Reviewed-by: https://github.com/IdanHo ✅
3 changed files with 45 additions and 0 deletions
Userland/Libraries/LibJS
Runtime/Temporal
Tests/builtins/Temporal/PlainDateTime
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
#include <LibJS/Runtime/Temporal/PlainDateTime.h>
|
||||
#include <LibJS/Runtime/Temporal/PlainDateTimePrototype.h>
|
||||
|
||||
namespace JS::Temporal {
|
||||
|
@ -24,10 +25,38 @@ void PlainDateTimePrototype::initialize(GlobalObject& global_object)
|
|||
// 5.3.2 Temporal.PlainDateTime.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype-@@tostringtag
|
||||
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.PlainDateTime"), 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 PlainDateTime* 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<PlainDateTime>(this_object)) {
|
||||
vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Temporal.PlainDateTime");
|
||||
return {};
|
||||
}
|
||||
return static_cast<PlainDateTime*>(this_object);
|
||||
}
|
||||
|
||||
// 5.3.3 get Temporal.PlainDateTime.prototype.calendar, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindatetime.prototype.calendar
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::calendar_getter)
|
||||
{
|
||||
// 1. Let dateTime be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]).
|
||||
auto* date_time = typed_this(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
// 3. Return dateTime.[[Calendar]].
|
||||
return Value(&date_time->calendar());
|
||||
}
|
||||
|
||||
// 5.3.35 Temporal.PlainDateTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.valueof
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::value_of)
|
||||
{
|
||||
|
|
|
@ -19,6 +19,7 @@ public:
|
|||
virtual ~PlainDateTimePrototype() override = default;
|
||||
|
||||
private:
|
||||
JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
||||
};
|
||||
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
describe("correct behavior", () => {
|
||||
test("basic functionality", () => {
|
||||
const calendar = { hello: "friends" };
|
||||
const plainDateTime = new Temporal.PlainDateTime(0, 1, 1, 0, 0, 0, 0, 0, 0, calendar);
|
||||
expect(plainDateTime.calendar).toBe(calendar);
|
||||
});
|
||||
});
|
||||
|
||||
test("errors", () => {
|
||||
test("this value must be a Temporal.PlainDateTime object", () => {
|
||||
expect(() => {
|
||||
Reflect.get(Temporal.PlainDateTime.prototype, "calendar", "foo");
|
||||
}).toThrowWithMessage(TypeError, "Not a Temporal.PlainDateTime");
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue