LibJS: Implement Temporal.PlainDate.from

This commit is contained in:
Idan Horowitz 2021-07-26 17:07:59 +03:00 committed by Linus Groh
parent 07485802c6
commit 67b3255fe8
3 changed files with 47 additions and 0 deletions

View file

@ -28,6 +28,7 @@ void PlainDateConstructor::initialize(GlobalObject& global_object)
define_direct_property(vm.names.prototype, global_object.temporal_plain_date_prototype(), 0);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.from, from, 1, attr);
define_native_function(vm.names.compare, compare, 2, attr);
define_direct_property(vm.names.length, Value(3), Attribute::Configurable);
@ -96,6 +97,30 @@ Value PlainDateConstructor::construct(FunctionObject& new_target)
return create_temporal_date(global_object, y, m, d, *calendar, &new_target);
}
// 3.2.2 Temporal.PlainDate.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.from
JS_DEFINE_NATIVE_FUNCTION(PlainDateConstructor::from)
{
// 1. Set options to ? GetOptionsObject(options).
auto* options = get_options_object(global_object, vm.argument(1));
if (vm.exception())
return {};
auto item = vm.argument(0);
// 2. If Type(item) is Object and item has an [[InitializedTemporalDate]] internal slot, then
if (item.is_object() && is<PlainDate>(item.as_object())) {
auto& plain_date_item = static_cast<PlainDate&>(item.as_object());
// a. Perform ? ToTemporalOverflow(options).
(void)to_temporal_overflow(global_object, *options);
if (vm.exception())
return {};
// b. Return ? CreateTemporalDate(item.[[ISOYear]], item.[[ISOMonth]], item.[[ISODay]], item.[[Calendar]]).
return create_temporal_date(global_object, plain_date_item.iso_year(), plain_date_item.iso_month(), plain_date_item.iso_day(), plain_date_item.calendar());
}
// 3. Return ? ToTemporalDate(item, options).
return to_temporal_date(global_object, item, options);
}
// 3.2.3 Temporal.PlainDate.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-properties-of-the-temporal-plaindate-constructor
JS_DEFINE_NATIVE_FUNCTION(PlainDateConstructor::compare)
{

View file

@ -24,6 +24,7 @@ public:
private:
virtual bool has_constructor() const override { return true; }
JS_DECLARE_NATIVE_FUNCTION(from);
JS_DECLARE_NATIVE_FUNCTION(compare);
};

View file

@ -0,0 +1,21 @@
describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.PlainDate.from).toHaveLength(1);
});
test("PlainDate instance argument", () => {
const plainDate = new Temporal.PlainDate(2021, 7, 26);
const createdPlainDate = Temporal.PlainDate.from(plainDate);
expect(createdPlainDate.year).toBe(2021);
expect(createdPlainDate.month).toBe(7);
expect(createdPlainDate.day).toBe(26);
});
// Un-skip once ParseISODateTime & ParseTemporalDateString are implemented
test.skip("PlainDate string argument", () => {
const createdPlainDate = Temporal.PlainDate.from("2021-07-26");
expect(createdPlainDate.year).toBe(2021);
expect(createdPlainDate.month).toBe(7);
expect(createdPlainDate.day).toBe(26);
});
});