// Copyright 2018 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_INTL_SUPPORT #error Internationalization is expected to be enabled. #endif // V8_INTL_SUPPORT #include "src/objects/js-list-format.h" #include #include #include "src/execution/isolate.h" #include "src/heap/factory.h" #include "src/objects/elements-inl.h" #include "src/objects/elements.h" #include "src/objects/intl-objects.h" #include "src/objects/js-array-inl.h" #include "src/objects/js-list-format-inl.h" #include "src/objects/managed-inl.h" #include "src/objects/objects-inl.h" #include "src/objects/option-utils.h" #include "unicode/fieldpos.h" #include "unicode/fpositer.h" #include "unicode/listformatter.h" #include "unicode/ulistformatter.h" namespace v8 { namespace internal { namespace { UListFormatterWidth GetIcuWidth(JSListFormat::Style style) { switch (style) { case JSListFormat::Style::LONG: return ULISTFMT_WIDTH_WIDE; case JSListFormat::Style::SHORT: return ULISTFMT_WIDTH_SHORT; case JSListFormat::Style::NARROW: return ULISTFMT_WIDTH_NARROW; } UNREACHABLE(); } UListFormatterType GetIcuType(JSListFormat::Type type) { switch (type) { case JSListFormat::Type::CONJUNCTION: return ULISTFMT_TYPE_AND; case JSListFormat::Type::DISJUNCTION: return ULISTFMT_TYPE_OR; case JSListFormat::Type::UNIT: return ULISTFMT_TYPE_UNITS; } UNREACHABLE(); } } // namespace MaybeHandle JSListFormat::New(Isolate* isolate, Handle map, Handle locales, Handle input_options) { // 3. Let requestedLocales be ? CanonicalizeLocaleList(locales). Maybe> maybe_requested_locales = Intl::CanonicalizeLocaleList(isolate, locales); MAYBE_RETURN(maybe_requested_locales, Handle()); std::vector requested_locales = maybe_requested_locales.FromJust(); Handle options; const char* service = "Intl.ListFormat"; // 4. Let options be GetOptionsObject(_options_). ASSIGN_RETURN_ON_EXCEPTION(isolate, options, GetOptionsObject(isolate, input_options, service), JSListFormat); // Note: No need to create a record. It's not observable. // 6. Let opt be a new Record. // 7. Let matcher be ? GetOption(options, "localeMatcher", "string", « // "lookup", "best fit" », "best fit"). Maybe maybe_locale_matcher = Intl::GetLocaleMatcher(isolate, options, service); MAYBE_RETURN(maybe_locale_matcher, MaybeHandle()); // 8. Set opt.[[localeMatcher]] to matcher. Intl::MatcherOption matcher = maybe_locale_matcher.FromJust(); // 10. Let r be ResolveLocale(%ListFormat%.[[AvailableLocales]], // requestedLocales, opt, undefined, localeData). Maybe maybe_resolve_locale = Intl::ResolveLocale(isolate, JSListFormat::GetAvailableLocales(), requested_locales, matcher, {}); if (maybe_resolve_locale.IsNothing()) { THROW_NEW_ERROR(isolate, NewRangeError(MessageTemplate::kIcuError), JSListFormat); } Intl::ResolvedLocale r = maybe_resolve_locale.FromJust(); Handle locale_str = isolate->factory()->NewStringFromAsciiChecked(r.locale.c_str()); // 12. Let t be GetOption(options, "type", "string", «"conjunction", // "disjunction", "unit"», "conjunction"). Maybe maybe_type = GetStringOption( isolate, options, "type", service, {"conjunction", "disjunction", "unit"}, {Type::CONJUNCTION, Type::DISJUNCTION, Type::UNIT}, Type::CONJUNCTION); MAYBE_RETURN(maybe_type, MaybeHandle()); Type type_enum = maybe_type.FromJust(); // 14. Let s be ? GetOption(options, "style", "string", // «"long", "short", "narrow"», "long"). Maybe