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

LibJS: Implement Temporal.PlainYearMonth.from

This commit is contained in:
Luke Wilde 2021-09-09 05:06:46 +01:00 committed by Linus Groh
parent 58e2597dc2
commit 092ec0cecf
3 changed files with 115 additions and 0 deletions

View file

@ -28,6 +28,9 @@ void PlainYearMonthConstructor::initialize(GlobalObject& global_object)
define_direct_property(vm.names.prototype, global_object.temporal_plain_year_month_prototype(), 0);
define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.from, from, 1, attr);
}
// 9.1.1 Temporal.PlainYearMonth ( isoYear, isoMonth [ , calendarLike [ , referenceISODay ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth
@ -89,4 +92,31 @@ Value PlainYearMonthConstructor::construct(FunctionObject& new_target)
return create_temporal_year_month(global_object, y, m, *calendar, ref, &new_target);
}
// 9.2.2 Temporal.PlainYearMonth.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.from
JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthConstructor::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 [[InitializedTemporalYearMonth]] internal slot, then
if (item.is_object() && is<PlainYearMonth>(item.as_object())) {
// a. Perform ? ToTemporalOverflow(options).
(void)to_temporal_overflow(global_object, *options);
if (vm.exception())
return {};
auto& plain_year_month_object = static_cast<PlainYearMonth&>(item.as_object());
// b. Return ? CreateTemporalYearMonth(item.[[ISOYear]], item.[[ISOMonth]], item.[[Calendar]], item.[[ISODay]]).
return create_temporal_year_month(global_object, plain_year_month_object.iso_year(), plain_year_month_object.iso_month(), plain_year_month_object.calendar(), plain_year_month_object.iso_day());
}
// 3. Return ? ToTemporalYearMonth(item, options).
return to_temporal_year_month(global_object, item, options);
}
}