LibJS: Convert Temporal.PlainMonthDay functions to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-10-21 20:12:16 +01:00
parent c043b818ad
commit c8e359c25e
Notes: sideshowbarker 2024-07-18 02:04:24 +09:00
4 changed files with 64 additions and 66 deletions

View file

@ -31,7 +31,7 @@ void PlainMonthDayConstructor::initialize(GlobalObject& global_object)
define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_old_native_function(vm.names.from, from, 1, attr);
define_native_function(vm.names.from, from, 1, attr);
}
// 10.1.1 Temporal.PlainMonthDay ( isoMonth, isoDay [ , calendarLike [ , referenceISOYear ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday
@ -83,26 +83,26 @@ ThrowCompletionOr<Object*> PlainMonthDayConstructor::construct(FunctionObject& n
}
// 10.2.2 Temporal.PlainMonthDay.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.from
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainMonthDayConstructor::from)
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayConstructor::from)
{
auto item = vm.argument(0);
// 1. Set options to ? GetOptionsObject(options).
auto* options = TRY_OR_DISCARD(get_options_object(global_object, vm.argument(1)));
auto* options = TRY(get_options_object(global_object, vm.argument(1)));
// 2. If Type(item) is Object and item has an [[InitializedTemporalMonthDay]] internal slot, then
if (item.is_object() && is<PlainMonthDay>(item.as_object())) {
// a. Perform ? ToTemporalOverflow(options).
(void)TRY_OR_DISCARD(to_temporal_overflow(global_object, *options));
(void)TRY(to_temporal_overflow(global_object, *options));
auto& plain_month_day_object = static_cast<PlainMonthDay&>(item.as_object());
// b. Return ? CreateTemporalMonthDay(item.[[ISOMonth]], item.[[ISODay]], item.[[Calendar]], item.[[ISOYear]]).
return TRY_OR_DISCARD(create_temporal_month_day(global_object, plain_month_day_object.iso_month(), plain_month_day_object.iso_day(), plain_month_day_object.calendar(), plain_month_day_object.iso_year()));
return TRY(create_temporal_month_day(global_object, plain_month_day_object.iso_month(), plain_month_day_object.iso_day(), plain_month_day_object.calendar(), plain_month_day_object.iso_year()));
}
// 3. Return ? ToTemporalMonthDay(item, options).
return TRY_OR_DISCARD(to_temporal_month_day(global_object, item, options));
return TRY(to_temporal_month_day(global_object, item, options));
}
}

View file

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

View file

@ -28,68 +28,68 @@ void PlainMonthDayPrototype::initialize(GlobalObject& global_object)
// 10.3.2 Temporal.PlainMonthDay.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype-@@tostringtag
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Temporal.PlainMonthDay"), Attribute::Configurable);
define_old_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.monthCode, month_code_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.day, day_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.monthCode, month_code_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.day, day_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_old_native_function(vm.names.equals, equals, 1, attr);
define_old_native_function(vm.names.toString, to_string, 0, attr);
define_old_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
define_old_native_function(vm.names.toJSON, to_json, 0, attr);
define_old_native_function(vm.names.valueOf, value_of, 0, attr);
define_old_native_function(vm.names.toPlainDate, to_plain_date, 1, attr);
define_old_native_function(vm.names.getISOFields, get_iso_fields, 0, attr);
define_native_function(vm.names.equals, equals, 1, attr);
define_native_function(vm.names.toString, to_string, 0, attr);
define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
define_native_function(vm.names.toJSON, to_json, 0, attr);
define_native_function(vm.names.valueOf, value_of, 0, attr);
define_native_function(vm.names.toPlainDate, to_plain_date, 1, attr);
define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr);
}
// 10.3.3 get Temporal.PlainMonthDay.prototype.calendar, https://tc39.es/proposal-temporal/#sec-get-temporal.plainmonthday.prototype.calendar
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainMonthDayPrototype::calendar_getter)
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::calendar_getter)
{
// 1. Let monthDay be the this value.
// 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]).
auto* month_day = TRY_OR_DISCARD(typed_this_object(global_object));
auto* month_day = TRY(typed_this_object(global_object));
// 3. Return monthDay.[[Calendar]].
return Value(&month_day->calendar());
}
// 10.3.4 get Temporal.PlainMonthDay.prototype.monthCode, https://tc39.es/proposal-temporal/#sec-get-temporal.plainmonthday.prototype.monthcode
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainMonthDayPrototype::month_code_getter)
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::month_code_getter)
{
// 1. Let monthDay be the this value.
// 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]).
auto* month_day = TRY_OR_DISCARD(typed_this_object(global_object));
auto* month_day = TRY(typed_this_object(global_object));
// 3. Let calendar be monthDay.[[Calendar]].
auto& calendar = month_day->calendar();
// 4. Return ? CalendarMonthCode(calendar, monthDay).
return js_string(vm, TRY_OR_DISCARD(calendar_month_code(global_object, calendar, *month_day)));
return js_string(vm, TRY(calendar_month_code(global_object, calendar, *month_day)));
}
// 10.3.5 get Temporal.PlainMonthDay.prototype.day, https://tc39.es/proposal-temporal/#sec-get-temporal.plainmonthday.prototype.day
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainMonthDayPrototype::day_getter)
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::day_getter)
{
// 1. Let monthDay be the this value.
// 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]).
auto* month_day = TRY_OR_DISCARD(typed_this_object(global_object));
auto* month_day = TRY(typed_this_object(global_object));
// 3. Let calendar be monthDay.[[Calendar]].
auto& calendar = month_day->calendar();
// 4. Return 𝔽(? CalendarDay(calendar, monthDay)).
return Value(TRY_OR_DISCARD(calendar_day(global_object, calendar, *month_day)));
return Value(TRY(calendar_day(global_object, calendar, *month_day)));
}
// 10.3.7 Temporal.PlainMonthDay.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.equals
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainMonthDayPrototype::equals)
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::equals)
{
// 1. Let monthDay be the this value.
// 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]).
auto* month_day = TRY_OR_DISCARD(typed_this_object(global_object));
auto* month_day = TRY(typed_this_object(global_object));
// 3. Set other to ? ToTemporalMonthDay(other).
auto* other = TRY_OR_DISCARD(to_temporal_month_day(global_object, vm.argument(0)));
auto* other = TRY(to_temporal_month_day(global_object, vm.argument(0)));
// 4. If monthDay.[[ISOMonth]] ≠ other.[[ISOMonth]], return false.
if (month_day->iso_month() != other->iso_month())
@ -104,90 +104,88 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(PlainMonthDayPrototype::equals)
return Value(false);
// 7. Return ? CalendarEquals(monthDay.[[Calendar]], other.[[Calendar]]).
return Value(TRY_OR_DISCARD(calendar_equals(global_object, month_day->calendar(), other->calendar())));
return Value(TRY(calendar_equals(global_object, month_day->calendar(), other->calendar())));
}
// 10.3.8 Temporal.PlainMonthDay.prototype.toString ( [ options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.tostring
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainMonthDayPrototype::to_string)
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_string)
{
// 1. Let monthDay be the this value.
// 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]).
auto* month_day = TRY_OR_DISCARD(typed_this_object(global_object));
auto* month_day = TRY(typed_this_object(global_object));
// 3. Set options to ? GetOptionsObject(options).
auto* options = TRY_OR_DISCARD(get_options_object(global_object, vm.argument(0)));
auto* options = TRY(get_options_object(global_object, vm.argument(0)));
// 4. Let showCalendar be ? ToShowCalendarOption(options).
auto show_calendar = TRY_OR_DISCARD(to_show_calendar_option(global_object, *options));
auto show_calendar = TRY(to_show_calendar_option(global_object, *options));
// 5. Return ? TemporalMonthDayToString(monthDay, showCalendar).
return js_string(vm, TRY_OR_DISCARD(temporal_month_day_to_string(global_object, *month_day, show_calendar)));
return js_string(vm, TRY(temporal_month_day_to_string(global_object, *month_day, show_calendar)));
}
// 10.3.9 Temporal.PlainMonthDay.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.tolocalestring
// NOTE: This is the minimum toLocaleString implementation for engines without ECMA-402.
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainMonthDayPrototype::to_locale_string)
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_locale_string)
{
// 1. Let monthDay be the this value.
// 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]).
auto* month_day = TRY_OR_DISCARD(typed_this_object(global_object));
auto* month_day = TRY(typed_this_object(global_object));
// 3. Return ? TemporalMonthDayToString(monthDay, "auto").
return js_string(vm, TRY_OR_DISCARD(temporal_month_day_to_string(global_object, *month_day, "auto"sv)));
return js_string(vm, TRY(temporal_month_day_to_string(global_object, *month_day, "auto"sv)));
}
// 10.3.10 Temporal.PlainMonthDay.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.tojson
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainMonthDayPrototype::to_json)
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_json)
{
// 1. Let monthDay be the this value.
// 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]).
auto* month_day = TRY_OR_DISCARD(typed_this_object(global_object));
auto* month_day = TRY(typed_this_object(global_object));
// 3. Return ? TemporalMonthDayToString(monthDay, "auto").
return js_string(vm, TRY_OR_DISCARD(temporal_month_day_to_string(global_object, *month_day, "auto"sv)));
return js_string(vm, TRY(temporal_month_day_to_string(global_object, *month_day, "auto"sv)));
}
// 10.3.11 Temporal.PlainMonthDay.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.valueof
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainMonthDayPrototype::value_of)
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::value_of)
{
// 1. Throw a TypeError exception.
vm.throw_exception<TypeError>(global_object, ErrorType::Convert, "Temporal.PlainMonthDay", "a primitive value");
return {};
return vm.throw_completion<TypeError>(global_object, ErrorType::Convert, "Temporal.PlainMonthDay", "a primitive value");
}
// 10.3.12 Temporal.PlainMonthDay.prototype.toPlainDate ( item ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.toplaindate
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainMonthDayPrototype::to_plain_date)
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_plain_date)
{
auto item = vm.argument(0);
// 1. Let monthDay be the this value.
// 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]).
auto* month_day = TRY_OR_DISCARD(typed_this_object(global_object));
auto* month_day = TRY(typed_this_object(global_object));
// 3. If Type(item) is not Object, then
if (!item.is_object()) {
// a. Throw a TypeError exception.
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, item);
return {};
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, item);
}
// 4. Let calendar be monthDay.[[Calendar]].
auto& calendar = month_day->calendar();
// 5. Let receiverFieldNames be ? CalendarFields(calendar, « "day", "monthCode" »).
auto receiver_field_names = TRY_OR_DISCARD(calendar_fields(global_object, calendar, { "day"sv, "monthCode"sv }));
auto receiver_field_names = TRY(calendar_fields(global_object, calendar, { "day"sv, "monthCode"sv }));
// 6. Let fields be ? PrepareTemporalFields(monthDay, receiverFieldNames, «»).
auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *month_day, receiver_field_names, {}));
auto* fields = TRY(prepare_temporal_fields(global_object, *month_day, receiver_field_names, {}));
// 7. Let inputFieldNames be ? CalendarFields(calendar, « "year" »).
auto input_field_names = TRY_OR_DISCARD(calendar_fields(global_object, calendar, { "year"sv }));
auto input_field_names = TRY(calendar_fields(global_object, calendar, { "year"sv }));
// 8. Let inputFields be ? PrepareTemporalFields(item, inputFieldNames, «»).
auto* input_fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, item.as_object(), input_field_names, {}));
auto* input_fields = TRY(prepare_temporal_fields(global_object, item.as_object(), input_field_names, {}));
// 9. Let mergedFields be ? CalendarMergeFields(calendar, fields, inputFields).
auto* merged_fields = TRY_OR_DISCARD(calendar_merge_fields(global_object, calendar, *fields, *input_fields));
auto* merged_fields = TRY(calendar_merge_fields(global_object, calendar, *fields, *input_fields));
// 10. Let mergedFieldNames be the List containing all the elements of receiverFieldNames followed by all the elements of inputFieldNames, with duplicate elements removed.
Vector<String> merged_field_names;
@ -201,7 +199,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(PlainMonthDayPrototype::to_plain_date)
}
// 11. Set mergedFields to ? PrepareTemporalFields(mergedFields, mergedFieldNames, «»).
merged_fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *merged_fields, merged_field_names, {}));
merged_fields = TRY(prepare_temporal_fields(global_object, *merged_fields, merged_field_names, {}));
// 12. Let options be ! OrdinaryObjectCreate(null).
auto* options = Object::create(global_object, nullptr);
@ -210,15 +208,15 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(PlainMonthDayPrototype::to_plain_date)
MUST(options->create_data_property_or_throw(vm.names.overflow, js_string(vm, vm.names.reject.as_string())));
// 14. Return ? DateFromFields(calendar, mergedFields, options).
return TRY_OR_DISCARD(date_from_fields(global_object, calendar, *merged_fields, *options));
return TRY(date_from_fields(global_object, calendar, *merged_fields, *options));
}
// 10.3.13 Temporal.PlainMonthDay.prototype.getISOFields ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.getisofields
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainMonthDayPrototype::get_iso_fields)
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::get_iso_fields)
{
// 1. Let monthDay be the this value.
// 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]).
auto* month_day = TRY_OR_DISCARD(typed_this_object(global_object));
auto* month_day = TRY(typed_this_object(global_object));
// 3. Let fields be ! OrdinaryObjectCreate(%Object.prototype%).
auto* fields = Object::create(global_object, global_object.object_prototype());

View file

@ -20,16 +20,16 @@ public:
virtual ~PlainMonthDayPrototype() override = default;
private:
JS_DECLARE_OLD_NATIVE_FUNCTION(calendar_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(month_code_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(day_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(equals);
JS_DECLARE_OLD_NATIVE_FUNCTION(to_string);
JS_DECLARE_OLD_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_OLD_NATIVE_FUNCTION(to_json);
JS_DECLARE_OLD_NATIVE_FUNCTION(value_of);
JS_DECLARE_OLD_NATIVE_FUNCTION(to_plain_date);
JS_DECLARE_OLD_NATIVE_FUNCTION(get_iso_fields);
JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
JS_DECLARE_NATIVE_FUNCTION(month_code_getter);
JS_DECLARE_NATIVE_FUNCTION(day_getter);
JS_DECLARE_NATIVE_FUNCTION(equals);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(to_json);
JS_DECLARE_NATIVE_FUNCTION(value_of);
JS_DECLARE_NATIVE_FUNCTION(to_plain_date);
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
};
}