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

LibJS: Convert NativeFunction::{call,construct}() to ThrowCompletionOr

Both at the same time because many of them call construct() in call()
and I'm not keen on adding a bunch of temporary plumbing to turn
exceptions into throw completions.
Also changes the return value of construct() to Object* instead of Value
as it always needs to return an object; allowing an arbitrary Value is a
massive foot gun.
This commit is contained in:
Linus Groh 2021-10-20 21:16:30 +01:00
parent 0881f8160f
commit 5832de62fe
99 changed files with 597 additions and 669 deletions

View file

@ -37,15 +37,14 @@ void DisplayNamesConstructor::initialize(GlobalObject& global_object)
}
// 12.2.1 Intl.DisplayNames ( locales, options ), https://tc39.es/ecma402/#sec-Intl.DisplayNames
Value DisplayNamesConstructor::call()
ThrowCompletionOr<Value> DisplayNamesConstructor::call()
{
// 1. If NewTarget is undefined, throw a TypeError exception.
vm().throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Intl.DisplayNames");
return {};
return vm().throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Intl.DisplayNames");
}
// 12.2.1 Intl.DisplayNames ( locales, options ), https://tc39.es/ecma402/#sec-Intl.DisplayNames
Value DisplayNamesConstructor::construct(FunctionObject& new_target)
ThrowCompletionOr<Object*> DisplayNamesConstructor::construct(FunctionObject& new_target)
{
auto& vm = this->vm();
auto& global_object = this->global_object();
@ -54,19 +53,17 @@ Value DisplayNamesConstructor::construct(FunctionObject& new_target)
auto options_value = vm.argument(1);
// 2. Let displayNames be ? OrdinaryCreateFromConstructor(NewTarget, "%DisplayNames.prototype%", « [[InitializedDisplayNames]], [[Locale]], [[Style]], [[Type]], [[Fallback]], [[Fields]] »).
auto* display_names = TRY_OR_DISCARD(ordinary_create_from_constructor<DisplayNames>(global_object, new_target, &GlobalObject::intl_display_names_prototype));
auto* display_names = TRY(ordinary_create_from_constructor<DisplayNames>(global_object, new_target, &GlobalObject::intl_display_names_prototype));
// 3. Let requestedLocales be ? CanonicalizeLocaleList(locales).
auto requested_locales = TRY_OR_DISCARD(canonicalize_locale_list(global_object, locale_value));
auto requested_locales = TRY(canonicalize_locale_list(global_object, locale_value));
// 4. If options is undefined, throw a TypeError exception.
if (options_value.is_undefined()) {
vm.throw_exception<TypeError>(global_object, ErrorType::IsUndefined, "options"sv);
return {};
}
if (options_value.is_undefined())
return vm.throw_completion<TypeError>(global_object, ErrorType::IsUndefined, "options"sv);
// 5. Set options to ? GetOptionsObject(options).
auto* options = TRY_OR_DISCARD(Temporal::get_options_object(global_object, options_value));
auto* options = TRY(Temporal::get_options_object(global_object, options_value));
// 6. Let opt be a new Record.
LocaleOptions opt {};
@ -74,7 +71,7 @@ Value DisplayNamesConstructor::construct(FunctionObject& new_target)
// 7. Let localeData be %DisplayNames%.[[LocaleData]].
// 8. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
auto matcher = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, { "lookup"sv, "best fit"sv }, "best fit"sv));
auto matcher = TRY(get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, { "lookup"sv, "best fit"sv }, "best fit"sv));
// 9. Set opt.[[localeMatcher]] to matcher.
opt.locale_matcher = matcher;
@ -83,25 +80,23 @@ Value DisplayNamesConstructor::construct(FunctionObject& new_target)
auto result = resolve_locale(requested_locales, opt, {});
// 11. Let style be ? GetOption(options, "style", "string", « "narrow", "short", "long" », "long").
auto style = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.style, Value::Type::String, { "narrow"sv, "short"sv, "long"sv }, "long"sv));
auto style = TRY(get_option(global_object, *options, vm.names.style, Value::Type::String, { "narrow"sv, "short"sv, "long"sv }, "long"sv));
// 12. Set displayNames.[[Style]] to style.
display_names->set_style(style.as_string().string());
// 13. Let type be ? GetOption(options, "type", "string", « "language", "region", "script", "currency" », undefined).
auto type = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.type, Value::Type::String, { "language"sv, "region"sv, "script"sv, "currency"sv }, Empty {}));
auto type = TRY(get_option(global_object, *options, vm.names.type, Value::Type::String, { "language"sv, "region"sv, "script"sv, "currency"sv }, Empty {}));
// 14. If type is undefined, throw a TypeError exception.
if (type.is_undefined()) {
vm.throw_exception<TypeError>(global_object, ErrorType::IsUndefined, "options.type"sv);
return {};
}
if (type.is_undefined())
return vm.throw_completion<TypeError>(global_object, ErrorType::IsUndefined, "options.type"sv);
// 15. Set displayNames.[[Type]] to type.
display_names->set_type(type.as_string().string());
// 16. Let fallback be ? GetOption(options, "fallback", "string", « "code", "none" », "code").
auto fallback = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.fallback, Value::Type::String, { "code"sv, "none"sv }, "code"sv));
auto fallback = TRY(get_option(global_object, *options, vm.names.fallback, Value::Type::String, { "code"sv, "none"sv }, "code"sv));
// 17. Set displayNames.[[Fallback]] to fallback.
display_names->set_fallback(fallback.as_string().string());