LibJS: Make PrimitiveString::utf8_string() infallible

Work towards #20449.
This commit is contained in:
Andreas Kling 2023-08-08 19:17:55 +02:00
parent 7849950383
commit c084269e5f
Notes: sideshowbarker 2024-07-16 22:11:09 +09:00
29 changed files with 79 additions and 93 deletions

View File

@ -59,7 +59,7 @@ ThrowCompletionOr<Value> Console::assert_()
// 3. Otherwise:
else {
// 1. Let concat be the concatenation of message, U+003A (:), U+0020 SPACE, and first.
auto concat = TRY_OR_THROW_OOM(vm, String::formatted("{}: {}", TRY(message->utf8_string()), MUST(first.to_string(vm))));
auto concat = TRY_OR_THROW_OOM(vm, String::formatted("{}: {}", message->utf8_string(), MUST(first.to_string(vm))));
// 2. Set data[0] to concat.
data[0] = PrimitiveString::create(vm, move(concat));
}

View File

@ -384,7 +384,7 @@ ThrowCompletionOr<LocaleResult> resolve_locale(VM& vm, Vector<String> const& req
MatcherResult matcher_result;
// 2. If matcher is "lookup", then
if (matcher.is_string() && (TRY(matcher.as_string().utf8_string_view()) == "lookup"sv)) {
if (matcher.is_string() && (matcher.as_string().utf8_string_view()) == "lookup"sv) {
// a. Let r be ! LookupMatcher(availableLocales, requestedLocales).
matcher_result = MUST_OR_THROW_OOM(lookup_matcher(vm, requested_locales));
}
@ -576,7 +576,7 @@ ThrowCompletionOr<Array*> supported_locales(VM& vm, Vector<String> const& reques
Vector<String> supported_locales;
// 3. If matcher is "best fit", then
if (TRY(matcher.as_string().utf8_string_view()) == "best fit"sv) {
if (matcher.as_string().utf8_string_view() == "best fit"sv) {
// a. Let supportedLocales be BestFitSupportedLocales(availableLocales, requestedLocales).
supported_locales = TRY(best_fit_supported_locales(vm, requested_locales));
}

View File

@ -27,7 +27,7 @@ static ThrowCompletionOr<Collator*> initialize_collator(VM& vm, Collator& collat
auto usage = TRY(get_option(vm, *options, vm.names.usage, OptionType::String, { "sort"sv, "search"sv }, "sort"sv));
// 4. Set collator.[[Usage]] to usage.
collator.set_usage(TRY(usage.as_string().utf8_string_view()));
collator.set_usage(usage.as_string().utf8_string_view());
// 5. If usage is "sort", then
// a. Let localeData be %Collator%.[[SortLocaleData]].
@ -49,11 +49,11 @@ static ThrowCompletionOr<Collator*> initialize_collator(VM& vm, Collator& collat
// 11. If collation is not undefined, then
if (!collation.is_undefined()) {
// a. If collation does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
if (!::Locale::is_type_identifier(TRY(collation.as_string().utf8_string_view())))
if (!::Locale::is_type_identifier(collation.as_string().utf8_string_view()))
return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, collation, "collation"sv);
// 12. Set opt.[[co]] to collation.
opt.co = TRY(collation.as_string().utf8_string());
opt.co = collation.as_string().utf8_string();
}
// 13. Let numeric be ? GetOption(options, "numeric", boolean, empty, undefined).
@ -69,7 +69,7 @@ static ThrowCompletionOr<Collator*> initialize_collator(VM& vm, Collator& collat
// 17. Set opt.[[kf]] to caseFirst.
auto case_first = TRY(get_option(vm, *options, vm.names.caseFirst, OptionType::String, { "upper"sv, "lower"sv, "false"sv }, Empty {}));
if (!case_first.is_undefined())
opt.kf = TRY(case_first.as_string().utf8_string());
opt.kf = case_first.as_string().utf8_string();
// 18. Let relevantExtensionKeys be %Collator%.[[RelevantExtensionKeys]].
auto relevant_extension_keys = Collator::relevant_extension_keys();
@ -117,7 +117,7 @@ static ThrowCompletionOr<Collator*> initialize_collator(VM& vm, Collator& collat
}
// 28. Set collator.[[Sensitivity]] to sensitivity.
collator.set_sensitivity(TRY(sensitivity.as_string().utf8_string_view()));
collator.set_sensitivity(sensitivity.as_string().utf8_string_view());
// 29. Let ignorePunctuation be ? GetOption(options, "ignorePunctuation", boolean, empty, false).
auto ignore_punctuation = TRY(get_option(vm, *options, vm.names.ignorePunctuation, OptionType::Boolean, {}, false));

View File

@ -106,11 +106,11 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
// 8. If calendar is not undefined, then
if (!calendar.is_undefined()) {
// a. If calendar cannot be matched by the type Unicode locale nonterminal, throw a RangeError exception.
if (!::Locale::is_type_identifier(TRY(calendar.as_string().utf8_string_view())))
if (!::Locale::is_type_identifier(calendar.as_string().utf8_string_view()))
return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, calendar, "calendar"sv);
// 9. Set opt.[[ca]] to calendar.
opt.ca = TRY(calendar.as_string().utf8_string());
opt.ca = calendar.as_string().utf8_string();
}
// 10. Let numberingSystem be ? GetOption(options, "numberingSystem", string, empty, undefined).
@ -119,11 +119,11 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
// 11. If numberingSystem is not undefined, then
if (!numbering_system.is_undefined()) {
// a. If numberingSystem cannot be matched by the type Unicode locale nonterminal, throw a RangeError exception.
if (!::Locale::is_type_identifier(TRY(numbering_system.as_string().utf8_string_view())))
if (!::Locale::is_type_identifier(numbering_system.as_string().utf8_string_view()))
return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv);
// 12. Set opt.[[nu]] to numberingSystem.
opt.nu = TRY(numbering_system.as_string().utf8_string());
opt.nu = numbering_system.as_string().utf8_string();
}
// 13. Let hour12 be ? GetOption(options, "hour12", boolean, empty, undefined).
@ -140,7 +140,7 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
// 16. Set opt.[[hc]] to hourCycle.
if (!hour_cycle.is_nullish())
opt.hc = TRY(hour_cycle.as_string().utf8_string());
opt.hc = hour_cycle.as_string().utf8_string();
// 17. Let localeData be %DateTimeFormat%.[[LocaleData]].
// 18. Let r be ResolveLocale(%DateTimeFormat%.[[AvailableLocales]], requestedLocales, opt, %DateTimeFormat%.[[RelevantExtensionKeys]], localeData).
@ -275,7 +275,7 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
// d. Set formatOptions.[[<prop>]] to value.
if (!value.is_undefined()) {
option = ::Locale::calendar_pattern_style_from_string(TRY(value.as_string().utf8_string_view()));
option = ::Locale::calendar_pattern_style_from_string(value.as_string().utf8_string_view());
// e. If value is not undefined, then
// i. Set hasExplicitFormatComponents to true.
@ -294,14 +294,14 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
// 40. Set dateTimeFormat.[[DateStyle]] to dateStyle.
if (!date_style.is_undefined())
date_time_format->set_date_style(TRY(date_style.as_string().utf8_string_view()));
date_time_format->set_date_style(date_style.as_string().utf8_string_view());
// 41. Let timeStyle be ? GetOption(options, "timeStyle", string, « "full", "long", "medium", "short" », undefined).
auto time_style = TRY(get_option(vm, *options, vm.names.timeStyle, OptionType::String, AK::Array { "full"sv, "long"sv, "medium"sv, "short"sv }, Empty {}));
// 42. Set dateTimeFormat.[[TimeStyle]] to timeStyle.
if (!time_style.is_undefined())
date_time_format->set_time_style(TRY(time_style.as_string().utf8_string_view()));
date_time_format->set_time_style(time_style.as_string().utf8_string_view());
Optional<::Locale::CalendarPattern> best_format {};
@ -397,7 +397,7 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
auto formats = TRY_OR_THROW_OOM(vm, ::Locale::get_calendar_available_formats(data_locale, date_time_format->calendar()));
// g. If matcher is "basic", then
if (TRY(matcher.as_string().utf8_string_view()) == "basic"sv) {
if (matcher.as_string().utf8_string_view() == "basic"sv) {
// i. Let bestFormat be BasicFormatMatcher(formatOptions, formats).
best_format = basic_format_matcher(format_options, move(formats));
}

View File

@ -82,7 +82,7 @@ ThrowCompletionOr<NonnullGCPtr<Object>> DisplayNamesConstructor::construct(Funct
auto style = TRY(get_option(vm, *options, vm.names.style, OptionType::String, { "narrow"sv, "short"sv, "long"sv }, "long"sv));
// 12. Set displayNames.[[Style]] to style.
display_names->set_style(TRY(style.as_string().utf8_string_view()));
display_names->set_style(style.as_string().utf8_string_view());
// 13. Let type be ? GetOption(options, "type", string, « "language", "region", "script", "currency", "calendar", "dateTimeField" », undefined).
auto type = TRY(get_option(vm, *options, vm.names.type, OptionType::String, { "language"sv, "region"sv, "script"sv, "currency"sv, "calendar"sv, "dateTimeField"sv }, Empty {}));
@ -92,13 +92,13 @@ ThrowCompletionOr<NonnullGCPtr<Object>> DisplayNamesConstructor::construct(Funct
return vm.throw_completion<TypeError>(ErrorType::IsUndefined, "options.type"sv);
// 15. Set displayNames.[[Type]] to type.
display_names->set_type(TRY(type.as_string().utf8_string_view()));
display_names->set_type(type.as_string().utf8_string_view());
// 16. Let fallback be ? GetOption(options, "fallback", string, « "code", "none" », "code").
auto fallback = TRY(get_option(vm, *options, vm.names.fallback, OptionType::String, { "code"sv, "none"sv }, "code"sv));
// 17. Set displayNames.[[Fallback]] to fallback.
display_names->set_fallback(TRY(fallback.as_string().utf8_string_view()));
display_names->set_fallback(fallback.as_string().utf8_string_view());
// 18. Set displayNames.[[Locale]] to r.[[locale]].
display_names->set_locale(move(result.locale));
@ -119,7 +119,7 @@ ThrowCompletionOr<NonnullGCPtr<Object>> DisplayNamesConstructor::construct(Funct
// 26. If type is "language", then
if (display_names->type() == DisplayNames::Type::Language) {
// a. Set displayNames.[[LanguageDisplay]] to languageDisplay.
display_names->set_language_display(TRY(language_display.as_string().utf8_string_view()));
display_names->set_language_display(language_display.as_string().utf8_string_view());
// b. Let typeFields be typeFields.[[<languageDisplay>]].
// c. Assert: typeFields is a Record (see 12.4.3).

View File

@ -46,8 +46,8 @@ JS_DEFINE_NATIVE_FUNCTION(DisplayNamesPrototype::of)
code = PrimitiveString::create(vm, TRY(code.to_string(vm)));
// 4. Let code be ? CanonicalCodeForDisplayNames(displayNames.[[Type]], code).
code = TRY(canonical_code_for_display_names(vm, display_names->type(), TRY(code.as_string().utf8_string_view())));
auto code_string = TRY(code.as_string().utf8_string_view());
code = TRY(canonical_code_for_display_names(vm, display_names->type(), code.as_string().utf8_string_view()));
auto code_string = code.as_string().utf8_string_view();
// 5. Let fields be displayNames.[[Fields]].
// 6. If fields has a field [[<code>]], return fields.[[<code>]].

View File

@ -309,7 +309,7 @@ ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(VM& vm, String
}
}
} else {
style = TRY(style_value.as_string().utf8_string_view());
style = style_value.as_string().utf8_string_view();
}
// 4. Let displayField be the string-concatenation of unit and "Display".
@ -333,7 +333,7 @@ ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(VM& vm, String
}
// 7. Return the Record { [[Style]]: style, [[Display]]: display }.
return DurationUnitOptions { .style = TRY_OR_THROW_OOM(vm, String::from_utf8(style)), .display = TRY(display.as_string().utf8_string()) };
return DurationUnitOptions { .style = TRY_OR_THROW_OOM(vm, String::from_utf8(style)), .display = display.as_string().utf8_string() };
}
// 1.1.7 PartitionDurationFormatPattern ( durationFormat, duration ), https://tc39.es/proposal-intl-duration-format/#sec-partitiondurationformatpattern

View File

@ -67,14 +67,14 @@ ThrowCompletionOr<NonnullGCPtr<Object>> DurationFormatConstructor::construct(Fun
// 7. If numberingSystem is not undefined, then
if (!numbering_system.is_undefined()) {
// a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
if (!::Locale::is_type_identifier(TRY(numbering_system.as_string().utf8_string_view())))
if (!::Locale::is_type_identifier(numbering_system.as_string().utf8_string_view()))
return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv);
}
// 8. Let opt be the Record { [[localeMatcher]]: matcher, [[nu]]: numberingSystem }.
LocaleOptions opt {};
opt.locale_matcher = matcher;
opt.nu = numbering_system.is_undefined() ? Optional<String>() : TRY(numbering_system.as_string().utf8_string());
opt.nu = numbering_system.is_undefined() ? Optional<String>() : numbering_system.as_string().utf8_string();
// 9. Let r be ResolveLocale(%DurationFormat%.[[AvailableLocales]], requestedLocales, opt, %DurationFormat%.[[RelevantExtensionKeys]], %DurationFormat%.[[LocaleData]]).
auto result = MUST_OR_THROW_OOM(resolve_locale(vm, requested_locales, opt, DurationFormat::relevant_extension_keys()));
@ -93,7 +93,7 @@ ThrowCompletionOr<NonnullGCPtr<Object>> DurationFormatConstructor::construct(Fun
auto style = TRY(get_option(vm, *options, vm.names.style, OptionType::String, { "long"sv, "short"sv, "narrow"sv, "digital"sv }, "short"sv));
// 14. Set durationFormat.[[Style]] to style.
duration_format->set_style(TRY(style.as_string().utf8_string_view()));
duration_format->set_style(style.as_string().utf8_string_view());
// 15. Set durationFormat.[[DataLocale]] to r.[[dataLocale]].
duration_format->set_data_locale(move(result.data_locale));
@ -119,7 +119,7 @@ ThrowCompletionOr<NonnullGCPtr<Object>> DurationFormatConstructor::construct(Fun
auto digital_base = duration_instances_component.digital_default;
// f. Let unitOptions be ? GetDurationUnitOptions(unit, options, style, valueList, digitalBase, prevStyle).
auto unit_options = TRY(get_duration_unit_options(vm, unit, *options, TRY(style.as_string().utf8_string_view()), value_list, digital_base, previous_style));
auto unit_options = TRY(get_duration_unit_options(vm, unit, *options, style.as_string().utf8_string_view(), value_list, digital_base, previous_style));
// g. Set the value of the styleSlot slot of durationFormat to unitOptions.[[Style]].
(duration_format->*style_slot)(unit_options.style);

View File

@ -276,7 +276,7 @@ ThrowCompletionOr<Vector<String>> string_list_from_iterable(VM& vm, Value iterab
}
// iii. Append nextValue to the end of the List list.
TRY_OR_THROW_OOM(vm, list.try_append(TRY(next_value.as_string().utf8_string())));
TRY_OR_THROW_OOM(vm, list.try_append(next_value.as_string().utf8_string()));
}
} while (next != nullptr);

View File

@ -80,13 +80,13 @@ ThrowCompletionOr<NonnullGCPtr<Object>> ListFormatConstructor::construct(Functio
auto type = TRY(get_option(vm, *options, vm.names.type, OptionType::String, { "conjunction"sv, "disjunction"sv, "unit"sv }, "conjunction"sv));
// 12. Set listFormat.[[Type]] to type.
list_format->set_type(TRY(type.as_string().utf8_string_view()));
list_format->set_type(type.as_string().utf8_string_view());
// 13. Let style be ? GetOption(options, "style", string, « "long", "short", "narrow" », "long").
auto style = TRY(get_option(vm, *options, vm.names.style, OptionType::String, { "long"sv, "short"sv, "narrow"sv }, "long"sv));
// 14. Set listFormat.[[Style]] to style.
list_format->set_style(TRY(style.as_string().utf8_string_view()));
list_format->set_style(style.as_string().utf8_string_view());
// Note: The remaining steps are skipped in favor of deferring to LibUnicode.

View File

@ -33,10 +33,10 @@ static ThrowCompletionOr<Optional<String>> get_string_option(VM& vm, Object cons
if (option.is_undefined())
return OptionalNone {};
if (validator && !validator(TRY(option.as_string().utf8_string_view())))
if (validator && !validator(option.as_string().utf8_string_view()))
return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, option, property);
return TRY(option.as_string().utf8_string());
return option.as_string().utf8_string();
}
// 14.1.2 ApplyOptionsToTag ( tag, options ), https://tc39.es/ecma402/#sec-apply-options-to-tag

View File

@ -1669,7 +1669,7 @@ ThrowCompletionOr<MathematicalValue> to_intl_mathematical_value(VM& vm, Value va
// 3. If Type(primValue) is String,
// a. Let str be primValue.
auto string = TRY(primitive_value.as_string().utf8_string());
auto string = primitive_value.as_string().utf8_string();
// Step 4 handled separately by the FIXME above.

View File

@ -102,11 +102,11 @@ ThrowCompletionOr<NumberFormat*> initialize_number_format(VM& vm, NumberFormat&
// 7. If numberingSystem is not undefined, then
if (!numbering_system.is_undefined()) {
// a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
if (!::Locale::is_type_identifier(TRY(numbering_system.as_string().utf8_string_view())))
if (!::Locale::is_type_identifier(numbering_system.as_string().utf8_string_view()))
return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv);
// 8. Set opt.[[nu]] to numberingSystem.
opt.nu = TRY(numbering_system.as_string().utf8_string());
opt.nu = numbering_system.as_string().utf8_string();
}
// 9. Let localeData be %NumberFormat%.[[LocaleData]].
@ -162,7 +162,7 @@ ThrowCompletionOr<NumberFormat*> initialize_number_format(VM& vm, NumberFormat&
auto notation = TRY(get_option(vm, *options, vm.names.notation, OptionType::String, { "standard"sv, "scientific"sv, "engineering"sv, "compact"sv }, "standard"sv));
// 19. Set numberFormat.[[Notation]] to notation.
number_format.set_notation(TRY(notation.as_string().utf8_string_view()));
number_format.set_notation(notation.as_string().utf8_string_view());
// 20. Perform ? SetNumberFormatDigitOptions(numberFormat, options, mnfdDefault, mxfdDefault, notation).
TRY(set_number_format_digit_options(vm, number_format, *options, default_min_fraction_digits, default_max_fraction_digits, number_format.notation()));
@ -176,7 +176,7 @@ ThrowCompletionOr<NumberFormat*> initialize_number_format(VM& vm, NumberFormat&
// 23. If notation is "compact", then
if (number_format.notation() == NumberFormat::Notation::Compact) {
// a. Set numberFormat.[[CompactDisplay]] to compactDisplay.
number_format.set_compact_display(TRY(compact_display.as_string().utf8_string_view()));
number_format.set_compact_display(compact_display.as_string().utf8_string_view());
// b. Set defaultUseGrouping to "min2".
default_use_grouping = "min2"sv;
@ -205,7 +205,7 @@ ThrowCompletionOr<NumberFormat*> initialize_number_format(VM& vm, NumberFormat&
auto sign_display = TRY(get_option(vm, *options, vm.names.signDisplay, OptionType::String, { "auto"sv, "never"sv, "always"sv, "exceptZero"sv, "negative"sv }, "auto"sv));
// 30. Set numberFormat.[[SignDisplay]] to signDisplay.
number_format.set_sign_display(TRY(sign_display.as_string().utf8_string_view()));
number_format.set_sign_display(sign_display.as_string().utf8_string_view());
// 31. Return numberFormat.
return &number_format;
@ -234,7 +234,7 @@ ThrowCompletionOr<void> set_number_format_digit_options(VM& vm, NumberFormatBase
// 7. Let roundingPriority be ? GetOption(options, "roundingPriority", string, « "auto", "morePrecision", "lessPrecision" », "auto").
auto rounding_priority_option = TRY(get_option(vm, options, vm.names.roundingPriority, OptionType::String, { "auto"sv, "morePrecision"sv, "lessPrecision"sv }, "auto"sv));
auto rounding_priority = MUST_OR_THROW_OOM(rounding_priority_option.as_string().utf8_string_view());
auto rounding_priority = rounding_priority_option.as_string().utf8_string_view();
// 8. Let roundingIncrement be ? GetNumberOption(options, "roundingIncrement", 1, 5000, 1).
auto rounding_increment = TRY(get_number_option(vm, options, vm.names.roundingIncrement, 1, 5000, 1));
@ -261,10 +261,10 @@ ThrowCompletionOr<void> set_number_format_digit_options(VM& vm, NumberFormatBase
intl_object.set_rounding_increment(*rounding_increment);
// 15. Set intlObj.[[RoundingMode]] to roundingMode.
intl_object.set_rounding_mode(TRY(rounding_mode.as_string().utf8_string_view()));
intl_object.set_rounding_mode(rounding_mode.as_string().utf8_string_view());
// 16. Set intlObj.[[TrailingZeroDisplay]] to trailingZeroDisplay.
intl_object.set_trailing_zero_display(TRY(trailing_zero_display.as_string().utf8_string_view()));
intl_object.set_trailing_zero_display(trailing_zero_display.as_string().utf8_string_view());
// 17. If mnsd is not undefined or mxsd is not undefined, then
// a. Let hasSd be true.
@ -417,7 +417,7 @@ ThrowCompletionOr<void> set_number_format_unit_options(VM& vm, NumberFormat& int
auto style = TRY(get_option(vm, options, vm.names.style, OptionType::String, { "decimal"sv, "percent"sv, "currency"sv, "unit"sv }, "decimal"sv));
// 4. Set intlObj.[[Style]] to style.
intl_object.set_style(TRY(style.as_string().utf8_string_view()));
intl_object.set_style(style.as_string().utf8_string_view());
// 5. Let currency be ? GetOption(options, "currency", string, empty, undefined).
auto currency = TRY(get_option(vm, options, vm.names.currency, OptionType::String, {}, Empty {}));
@ -430,7 +430,7 @@ ThrowCompletionOr<void> set_number_format_unit_options(VM& vm, NumberFormat& int
}
// 7. Else,
// a. If IsWellFormedCurrencyCode(currency) is false, throw a RangeError exception.
else if (!is_well_formed_currency_code(TRY(currency.as_string().utf8_string_view())))
else if (!is_well_formed_currency_code(currency.as_string().utf8_string_view()))
return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, currency, "currency"sv);
// 8. Let currencyDisplay be ? GetOption(options, "currencyDisplay", string, « "code", "symbol", "narrowSymbol", "name" », "symbol").
@ -450,7 +450,7 @@ ThrowCompletionOr<void> set_number_format_unit_options(VM& vm, NumberFormat& int
}
// 12. Else,
// a. If ! IsWellFormedUnitIdentifier(unit) is false, throw a RangeError exception.
else if (!is_well_formed_unit_identifier(TRY(unit.as_string().utf8_string_view())))
else if (!is_well_formed_unit_identifier(unit.as_string().utf8_string_view()))
return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, unit, "unit"sv);
// 13. Let unitDisplay be ? GetOption(options, "unitDisplay", string, « "short", "narrow", "long" », "short").
@ -459,22 +459,22 @@ ThrowCompletionOr<void> set_number_format_unit_options(VM& vm, NumberFormat& int
// 14. If style is "currency", then
if (intl_object.style() == NumberFormat::Style::Currency) {
// a. Set intlObj.[[Currency]] to the ASCII-uppercase of currency.
intl_object.set_currency(TRY_OR_THROW_OOM(vm, TRY(currency.as_string().utf8_string()).to_uppercase()));
intl_object.set_currency(TRY_OR_THROW_OOM(vm, currency.as_string().utf8_string().to_uppercase()));
// c. Set intlObj.[[CurrencyDisplay]] to currencyDisplay.
intl_object.set_currency_display(TRY(currency_display.as_string().utf8_string_view()));
intl_object.set_currency_display(currency_display.as_string().utf8_string_view());
// d. Set intlObj.[[CurrencySign]] to currencySign.
intl_object.set_currency_sign(TRY(currency_sign.as_string().utf8_string_view()));
intl_object.set_currency_sign(currency_sign.as_string().utf8_string_view());
}
// 15. If style is "unit", then
if (intl_object.style() == NumberFormat::Style::Unit) {
// a. Set intlObj.[[Unit]] to unit.
intl_object.set_unit(TRY(unit.as_string().utf8_string()));
intl_object.set_unit(unit.as_string().utf8_string());
// b. Set intlObj.[[UnitDisplay]] to unitDisplay.
intl_object.set_unit_display(TRY(unit_display.as_string().utf8_string_view()));
intl_object.set_unit_display(unit_display.as_string().utf8_string_view());
}
return {};

View File

@ -94,7 +94,7 @@ ThrowCompletionOr<PluralRules*> initialize_plural_rules(VM& vm, PluralRules& plu
auto type = TRY(get_option(vm, *options, vm.names.type, OptionType::String, AK::Array { "cardinal"sv, "ordinal"sv }, "cardinal"sv));
// 7. Set pluralRules.[[Type]] to t.
plural_rules.set_type(TRY(type.as_string().utf8_string_view()));
plural_rules.set_type(type.as_string().utf8_string_view());
// 8. Perform ? SetNumberFormatDigitOptions(pluralRules, options, +0𝔽, 3𝔽, "standard").
TRY(set_number_format_digit_options(vm, plural_rules, *options, 0, 3, NumberFormat::Notation::Standard));

View File

@ -101,11 +101,11 @@ ThrowCompletionOr<RelativeTimeFormat*> initialize_relative_time_format(VM& vm, R
// 7. If numberingSystem is not undefined, then
if (!numbering_system.is_undefined()) {
// a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
if (!::Locale::is_type_identifier(TRY(numbering_system.as_string().utf8_string_view())))
if (!::Locale::is_type_identifier(numbering_system.as_string().utf8_string_view()))
return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv);
// 8. Set opt.[[nu]] to numberingSystem.
opt.nu = TRY(numbering_system.as_string().utf8_string());
opt.nu = numbering_system.as_string().utf8_string();
}
// 9. Let localeData be %RelativeTimeFormat%.[[LocaleData]].
@ -129,13 +129,13 @@ ThrowCompletionOr<RelativeTimeFormat*> initialize_relative_time_format(VM& vm, R
auto style = TRY(get_option(vm, *options, vm.names.style, OptionType::String, { "long"sv, "short"sv, "narrow"sv }, "long"sv));
// 16. Set relativeTimeFormat.[[Style]] to style.
relative_time_format.set_style(TRY(style.as_string().utf8_string_view()));
relative_time_format.set_style(style.as_string().utf8_string_view());
// 17. Let numeric be ? GetOption(options, "numeric", string, « "always", "auto" », "always").
auto numeric = TRY(get_option(vm, *options, vm.names.numeric, OptionType::String, { "always"sv, "auto"sv }, "always"sv));
// 18. Set relativeTimeFormat.[[Numeric]] to numeric.
relative_time_format.set_numeric(TRY(numeric.as_string().utf8_string_view()));
relative_time_format.set_numeric(numeric.as_string().utf8_string_view());
// 19. Let relativeTimeFormat.[[NumberFormat]] be ! Construct(%NumberFormat%, « locale »).
auto number_format = MUST(construct(vm, realm.intrinsics().intl_number_format_constructor(), PrimitiveString::create(vm, locale)));

View File

@ -81,7 +81,7 @@ ThrowCompletionOr<NonnullGCPtr<Object>> SegmenterConstructor::construct(Function
auto granularity = TRY(get_option(vm, *options, vm.names.granularity, OptionType::String, { "grapheme"sv, "word"sv, "sentence"sv }, "grapheme"sv));
// 13. Set segmenter.[[SegmenterGranularity]] to granularity.
segmenter->set_segmenter_granularity(TRY(granularity.as_string().utf8_string_view()));
segmenter->set_segmenter_granularity(granularity.as_string().utf8_string_view());
// 14. Return segmenter.
return segmenter;

View File

@ -74,14 +74,13 @@ bool PrimitiveString::is_empty() const
VERIFY_NOT_REACHED();
}
ThrowCompletionOr<String> PrimitiveString::utf8_string() const
String PrimitiveString::utf8_string() const
{
auto& vm = this->vm();
resolve_rope_if_needed(EncodingPreference::UTF8);
if (!has_utf8_string()) {
if (has_deprecated_string())
m_utf8_string = TRY_OR_THROW_OOM(vm, String::from_deprecated_string(*m_deprecated_string));
m_utf8_string = MUST(String::from_deprecated_string(*m_deprecated_string));
else if (has_utf16_string())
m_utf8_string = m_utf16_string->to_utf8();
else
@ -91,9 +90,9 @@ ThrowCompletionOr<String> PrimitiveString::utf8_string() const
return *m_utf8_string;
}
ThrowCompletionOr<StringView> PrimitiveString::utf8_string_view() const
StringView PrimitiveString::utf8_string_view() const
{
(void)TRY(utf8_string());
(void)utf8_string();
return m_utf8_string->bytes_as_string_view();
}
@ -292,14 +291,14 @@ void PrimitiveString::resolve_rope_if_needed(EncodingPreference preference) cons
for (auto const* current : pieces) {
if (!previous) {
// This is the very first piece, just append it and continue.
builder.append(MUST(current->utf8_string()));
builder.append(current->utf8_string());
previous = current;
continue;
}
// Get the UTF-8 representations for both strings.
auto current_string_as_utf8 = MUST(current->utf8_string_view());
auto previous_string_as_utf8 = MUST(previous->utf8_string_view());
auto current_string_as_utf8 = current->utf8_string_view();
auto previous_string_as_utf8 = previous->utf8_string_view();
// NOTE: Now we need to look at the end of the previous string and the start
// of the current string, to see if they should be combined into a surrogate.

View File

@ -38,8 +38,8 @@ public:
bool is_empty() const;
ThrowCompletionOr<String> utf8_string() const;
ThrowCompletionOr<StringView> utf8_string_view() const;
[[nodiscard]] String utf8_string() const;
[[nodiscard]] StringView utf8_string_view() const;
bool has_utf8_string() const { return m_utf8_string.has_value(); }
ThrowCompletionOr<DeprecatedString> deprecated_string() const;

View File

@ -282,7 +282,7 @@ ThrowCompletionOr<Value> shadow_realm_import_value(VM& vm, DeprecatedString spec
// NOTE: Even though the spec tells us to use %ThrowTypeError%, it's not observable if we actually do.
// Throw a nicer TypeError forwarding the import error message instead (we know the argument is an Error object).
auto throw_type_error = NativeFunction::create(realm, {}, [](auto& vm) -> ThrowCompletionOr<Value> {
return vm.template throw_completion<TypeError>(TRY(vm.argument(0).as_object().get_without_side_effects(vm.names.message).as_string().utf8_string()));
return vm.template throw_completion<TypeError>(vm.argument(0).as_object().get_without_side_effects(vm.names.message).as_string().utf8_string());
});
// 13. Return PerformPromiseThen(innerCapability.[[Promise]], onFulfilled, callerRealm.[[Intrinsics]].[[%ThrowTypeError%]], promiseCapability).

View File

@ -146,7 +146,7 @@ ThrowCompletionOr<Value> get_option(VM& vm, Object const& options, PropertyKey c
if (!values.is_empty()) {
// NOTE: Every location in the spec that invokes GetOption with type=boolean also has values=undefined.
VERIFY(value.is_string());
if (auto value_string = TRY(value.as_string().utf8_string()); !values.contains_slow(value_string))
if (auto value_string = value.as_string().utf8_string(); !values.contains_slow(value_string))
return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, value_string, property.as_string());
}
@ -530,7 +530,7 @@ ThrowCompletionOr<Optional<String>> get_temporal_unit(VM& vm, Object const& norm
auto value = option_value.is_undefined()
? Optional<String> {}
: TRY(option_value.as_string().utf8_string());
: option_value.as_string().utf8_string();
// 11. If value is listed in the Plural column of Table 13, then
for (auto const& row : temporal_units) {

View File

@ -131,7 +131,7 @@ ThrowCompletionOr<Vector<String>> calendar_fields(VM& vm, Object& calendar, Vect
Vector<String> result;
TRY_OR_THROW_OOM(vm, result.try_ensure_capacity(list.size()));
for (auto& value : list)
result.unchecked_append(TRY(value.as_string().utf8_string()));
result.unchecked_append(value.as_string().utf8_string());
return result;
}

View File

@ -559,7 +559,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::fields)
return *TRY(iterator_close(vm, iterator_record, move(completion)));
}
auto next_value_string = TRY(next_value.as_string().utf8_string());
auto next_value_string = next_value.as_string().utf8_string();
// iii. If fieldNames contains nextValue, then
if (field_names.contains_slow(next_value)) {

View File

@ -176,7 +176,7 @@ ThrowCompletionOr<ZonedDateTime*> to_temporal_zoned_date_time(VM& vm, Value item
offset_behavior = OffsetBehavior::Wall;
} else {
// NOTE: Not in the spec, since it directly assigns to offsetString in step i, but we can't do it there as it's a type mismatch.
offset_string = TRY(offset_string_value.as_string().utf8_string());
offset_string = offset_string_value.as_string().utf8_string();
}
// l. Let result be ? InterpretTemporalDateTimeFields(calendar, fields, options).

View File

@ -801,7 +801,7 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::with)
// 18. Assert: Type(offsetString) is String.
VERIFY(offset_string_value.is_string());
auto offset_string = TRY(offset_string_value.as_string().utf8_string());
auto offset_string = offset_string_value.as_string().utf8_string();
// 19. Let dateTimeResult be ? InterpretTemporalDateTimeFields(calendar, fields, options).
auto date_time_result = TRY(interpret_temporal_date_time_fields(vm, calendar, *fields, options));

View File

@ -370,18 +370,7 @@ ErrorOr<String> Value::to_string_without_side_effects() const
case INT32_TAG:
return String::number(as_i32());
case STRING_TAG:
if (auto string = as_string().utf8_string(); string.is_throw_completion()) {
auto completion = string.release_error();
// We can't explicitly check for OOM because InternalError does not store the ErrorType
VERIFY(completion.value().has_value());
VERIFY(completion.value()->is_object());
VERIFY(is<InternalError>(completion.value()->as_object()));
return AK::Error::from_errno(ENOMEM);
} else {
return string.release_value();
}
return as_string().utf8_string();
case SYMBOL_TAG:
return as_symbol().descriptive_string();
case BIGINT_TAG:
@ -414,7 +403,7 @@ ThrowCompletionOr<String> Value::to_string(VM& vm) const
switch (m_value.tag) {
// 1. If argument is a String, return argument.
case STRING_TAG:
return TRY(as_string().utf8_string());
return as_string().utf8_string();
// 2. If argument is a Symbol, throw a TypeError exception.
case SYMBOL_TAG:
return vm.throw_completion<TypeError>(ErrorType::Convert, "symbol", "string");

View File

@ -1573,7 +1573,7 @@ void Element::enqueue_a_custom_element_callback_reaction(FlyString const& callba
VERIFY(!arguments.is_empty());
auto& attribute_name_value = arguments.first();
VERIFY(attribute_name_value.is_string());
auto attribute_name = attribute_name_value.as_string().utf8_string().release_allocated_value_but_fixme_should_propagate_errors();
auto attribute_name = attribute_name_value.as_string().utf8_string();
// 2. If definition's observed attributes does not contain attributeName, then return.
if (!definition->observed_attributes().contains_slow(attribute_name))

View File

@ -166,9 +166,7 @@ private:
WebIDL::ExceptionOr<void> serialize_string(Vector<u32>& vector, JS::PrimitiveString const& primitive_string)
{
auto string = TRY(Bindings::throw_dom_exception_if_needed(m_vm, [&primitive_string]() {
return primitive_string.utf8_string();
}));
auto string = primitive_string.utf8_string();
TRY(serialize_string(vector, string));
return {};
}

View File

@ -52,7 +52,7 @@ WebIDL::ExceptionOr<String> serialize_javascript_value_to_json_string(JS::VM& vm
VERIFY(result.is_string());
// 4. Return result.
return TRY(result.as_string().utf8_string());
return result.as_string().utf8_string();
}
// https://infra.spec.whatwg.org/#serialize-a-javascript-value-to-json-bytes

View File

@ -3131,7 +3131,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> transform_stream_default_
TRY(transform_stream_error(stream, reason));
// 2. Throw readable.[[storedError]].
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, TRY(readable->stored_error().as_string().utf8_string_view()) };
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, readable->stored_error().as_string().utf8_string() };
});
return WebIDL::create_resolved_promise(realm, react_result);