14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include "ecmascript/js_list_format.h"
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ci#include <cstring>
194514f5e3Sopenharmony_ci#include <vector>
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_ci#include "ecmascript/intl/locale_helper.h"
224514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
234514f5e3Sopenharmony_ci#include "ecmascript/js_iterator.h"
244514f5e3Sopenharmony_ci#include "ecmascript/object_factory-inl.h"
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_ci
274514f5e3Sopenharmony_cinamespace panda::ecmascript {
284514f5e3Sopenharmony_ciicu::ListFormatter *JSListFormat::GetIcuListFormatter() const
294514f5e3Sopenharmony_ci{
304514f5e3Sopenharmony_ci    ASSERT(GetIcuLF().IsJSNativePointer());
314514f5e3Sopenharmony_ci    auto result = JSNativePointer::Cast(GetIcuLF().GetTaggedObject())->GetExternalPointer();
324514f5e3Sopenharmony_ci    return reinterpret_cast<icu::ListFormatter *>(result);
334514f5e3Sopenharmony_ci}
344514f5e3Sopenharmony_ci
354514f5e3Sopenharmony_civoid JSListFormat::FreeIcuListFormatter([[maybe_unused]] void *env, void *pointer, [[maybe_unused]] void* hint)
364514f5e3Sopenharmony_ci{
374514f5e3Sopenharmony_ci    if (pointer == nullptr) {
384514f5e3Sopenharmony_ci        return;
394514f5e3Sopenharmony_ci    }
404514f5e3Sopenharmony_ci    auto icuListFormat = reinterpret_cast<icu::ListFormatter *>(pointer);
414514f5e3Sopenharmony_ci    icuListFormat->~ListFormatter();
424514f5e3Sopenharmony_ci    delete icuListFormat;
434514f5e3Sopenharmony_ci}
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_civoid JSListFormat::SetIcuListFormatter(JSThread *thread, const JSHandle<JSListFormat> listFormat,
464514f5e3Sopenharmony_ci                                       icu::ListFormatter *icuListFormatter, const NativePointerCallback &callback)
474514f5e3Sopenharmony_ci{
484514f5e3Sopenharmony_ci    EcmaVM *ecmaVm = thread->GetEcmaVM();
494514f5e3Sopenharmony_ci    ObjectFactory *factory = ecmaVm->GetFactory();
504514f5e3Sopenharmony_ci    ASSERT(icuListFormatter != nullptr);
514514f5e3Sopenharmony_ci    JSTaggedValue data = listFormat->GetIcuLF();
524514f5e3Sopenharmony_ci    if (data.IsJSNativePointer()) {
534514f5e3Sopenharmony_ci        JSNativePointer *native = JSNativePointer::Cast(data.GetTaggedObject());
544514f5e3Sopenharmony_ci        native->ResetExternalPointer(thread, icuListFormatter);
554514f5e3Sopenharmony_ci        return;
564514f5e3Sopenharmony_ci    }
574514f5e3Sopenharmony_ci    JSHandle<JSNativePointer> pointer = factory->NewJSNativePointer(icuListFormatter, callback);
584514f5e3Sopenharmony_ci    listFormat->SetIcuLF(thread, pointer.GetTaggedValue());
594514f5e3Sopenharmony_ci}
604514f5e3Sopenharmony_ci
614514f5e3Sopenharmony_ciJSHandle<TaggedArray> JSListFormat::GetAvailableLocales(JSThread *thread)
624514f5e3Sopenharmony_ci{
634514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
644514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> listFormatLocales = env->GetListFormatLocales();
654514f5e3Sopenharmony_ci    if (!listFormatLocales->IsUndefined()) {
664514f5e3Sopenharmony_ci        return JSHandle<TaggedArray>::Cast(listFormatLocales);
674514f5e3Sopenharmony_ci    }
684514f5e3Sopenharmony_ci    const char *key = "listPattern";
694514f5e3Sopenharmony_ci    const char *path = nullptr;
704514f5e3Sopenharmony_ci    std::vector<std::string> availableStringLocales = intl::LocaleHelper::GetAvailableLocales(thread, key, path);
714514f5e3Sopenharmony_ci    JSHandle<TaggedArray> availableLocales = JSLocale::ConstructLocaleList(thread, availableStringLocales);
724514f5e3Sopenharmony_ci    env->SetListFormatLocales(thread, availableLocales);
734514f5e3Sopenharmony_ci    return availableLocales;
744514f5e3Sopenharmony_ci}
754514f5e3Sopenharmony_ci
764514f5e3Sopenharmony_ci// 13. InitializeListFormat ( listformat, locales, options )
774514f5e3Sopenharmony_ciJSHandle<JSListFormat> JSListFormat::InitializeListFormat(JSThread *thread,
784514f5e3Sopenharmony_ci                                                          const JSHandle<JSListFormat> &listFormat,
794514f5e3Sopenharmony_ci                                                          const JSHandle<JSTaggedValue> &locales,
804514f5e3Sopenharmony_ci                                                          const JSHandle<JSTaggedValue> &options)
814514f5e3Sopenharmony_ci{
824514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope scope(thread);
834514f5e3Sopenharmony_ci    EcmaVM *ecmaVm = thread->GetEcmaVM();
844514f5e3Sopenharmony_ci    ObjectFactory *factory = ecmaVm->GetFactory();
854514f5e3Sopenharmony_ci    auto globalConst = thread->GlobalConstants();
864514f5e3Sopenharmony_ci
874514f5e3Sopenharmony_ci    // 3. Let requestedLocales be ? CanonicalizeLocaleList(locales).
884514f5e3Sopenharmony_ci    JSHandle<TaggedArray> requestedLocales = intl::LocaleHelper::CanonicalizeLocaleList(thread, locales);
894514f5e3Sopenharmony_ci    RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSListFormat, thread);
904514f5e3Sopenharmony_ci
914514f5e3Sopenharmony_ci    // 4. Let options be ? GetOptionsObject(options).
924514f5e3Sopenharmony_ci    JSHandle<JSObject> optionsObject;
934514f5e3Sopenharmony_ci    if (options->IsUndefined()) {
944514f5e3Sopenharmony_ci        optionsObject = factory->CreateNullJSObject();
954514f5e3Sopenharmony_ci    } else if (!options->IsJSObject()) {
964514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "options is not Object", listFormat);
974514f5e3Sopenharmony_ci    } else {
984514f5e3Sopenharmony_ci        optionsObject = JSTaggedValue::ToObject(thread, options);
994514f5e3Sopenharmony_ci        RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSListFormat, thread);
1004514f5e3Sopenharmony_ci    }
1014514f5e3Sopenharmony_ci
1024514f5e3Sopenharmony_ci    // 5. Let opt be a new Record.
1034514f5e3Sopenharmony_ci    // 6. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
1044514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> property = globalConst->GetHandledLocaleMatcherString();
1054514f5e3Sopenharmony_ci    auto matcher = JSLocale::GetOptionOfString<LocaleMatcherOption>(
1064514f5e3Sopenharmony_ci        thread, optionsObject, property, {LocaleMatcherOption::LOOKUP, LocaleMatcherOption::BEST_FIT},
1074514f5e3Sopenharmony_ci        {"lookup", "best fit"}, LocaleMatcherOption::BEST_FIT);
1084514f5e3Sopenharmony_ci    RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSListFormat, thread);
1094514f5e3Sopenharmony_ci
1104514f5e3Sopenharmony_ci    // 8. Let localeData be %ListFormat%.[[LocaleData]].
1114514f5e3Sopenharmony_ci    JSHandle<TaggedArray> availableLocales;
1124514f5e3Sopenharmony_ci    if (requestedLocales->GetLength() == 0) {
1134514f5e3Sopenharmony_ci        availableLocales = factory->EmptyArray();
1144514f5e3Sopenharmony_ci    } else {
1154514f5e3Sopenharmony_ci        availableLocales = GetAvailableLocales(thread);
1164514f5e3Sopenharmony_ci    }
1174514f5e3Sopenharmony_ci
1184514f5e3Sopenharmony_ci    // 9. Let r be ResolveLocale(%ListFormat%.[[AvailableLocales]], requestedLocales,
1194514f5e3Sopenharmony_ci    // opt, %ListFormat%.[[RelevantExtensionKeys]], localeData).
1204514f5e3Sopenharmony_ci    std::set<std::string> relevantExtensionKeys {""};
1214514f5e3Sopenharmony_ci    ResolvedLocale r =
1224514f5e3Sopenharmony_ci        JSLocale::ResolveLocale(thread, availableLocales, requestedLocales, matcher, relevantExtensionKeys);
1234514f5e3Sopenharmony_ci    RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSListFormat, thread);
1244514f5e3Sopenharmony_ci
1254514f5e3Sopenharmony_ci    // 10. Set listFormat.[[Locale]] to r.[[locale]].
1264514f5e3Sopenharmony_ci    icu::Locale icuLocale = r.localeData;
1274514f5e3Sopenharmony_ci    JSHandle<EcmaString> localeStr = intl::LocaleHelper::ToLanguageTag(thread, icuLocale);
1284514f5e3Sopenharmony_ci    RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSListFormat, thread);
1294514f5e3Sopenharmony_ci    listFormat->SetLocale(thread, localeStr.GetTaggedValue());
1304514f5e3Sopenharmony_ci
1314514f5e3Sopenharmony_ci    // 11. Let type be ? GetOption(options, "type", "string", « "conjunction", "disjunction", "unit" », "conjunction").
1324514f5e3Sopenharmony_ci    property = globalConst->GetHandledTypeString();
1334514f5e3Sopenharmony_ci    auto type = JSLocale::GetOptionOfString<ListTypeOption>(thread, optionsObject, property,
1344514f5e3Sopenharmony_ci                                                            {ListTypeOption::CONJUNCTION, ListTypeOption::DISJUNCTION,
1354514f5e3Sopenharmony_ci                                                            ListTypeOption::UNIT},
1364514f5e3Sopenharmony_ci                                                            {"conjunction", "disjunction", "unit"},
1374514f5e3Sopenharmony_ci                                                            ListTypeOption::CONJUNCTION);
1384514f5e3Sopenharmony_ci    RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSListFormat, thread);
1394514f5e3Sopenharmony_ci
1404514f5e3Sopenharmony_ci    // 12. Set listFormat.[[Type]] to type.
1414514f5e3Sopenharmony_ci    listFormat->SetType(type);
1424514f5e3Sopenharmony_ci
1434514f5e3Sopenharmony_ci    // 13. Let style be ? GetOption(options, "style", "string", « "long", "short", "narrow" », "long").
1444514f5e3Sopenharmony_ci    property = globalConst->GetHandledStyleString();
1454514f5e3Sopenharmony_ci    auto style = JSLocale::GetOptionOfString<ListStyleOption>(thread, optionsObject, property,
1464514f5e3Sopenharmony_ci                                                              {ListStyleOption::LONG, ListStyleOption::SHORT,
1474514f5e3Sopenharmony_ci                                                              ListStyleOption::NARROW},
1484514f5e3Sopenharmony_ci                                                              {"long", "short", "narrow"}, ListStyleOption::LONG);
1494514f5e3Sopenharmony_ci    RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSListFormat, thread);
1504514f5e3Sopenharmony_ci
1514514f5e3Sopenharmony_ci    // 14. Set listFormat.[[Style]] to style.
1524514f5e3Sopenharmony_ci    listFormat->SetStyle(style);
1534514f5e3Sopenharmony_ci
1544514f5e3Sopenharmony_ci    // 15. Let dataLocale be r.[[dataLocale]].
1554514f5e3Sopenharmony_ci    // 16. Let dataLocaleData be localeData.[[<dataLocale>]].
1564514f5e3Sopenharmony_ci    // 17. Let dataLocaleTypes be dataLocaleData.[[<type>]].
1574514f5e3Sopenharmony_ci    // 18. Set listFormat.[[Templates]] to dataLocaleTypes.[[<style>]].
1584514f5e3Sopenharmony_ci    // 19. Return listFormat.
1594514f5e3Sopenharmony_ci
1604514f5e3Sopenharmony_ci    // Trans typeOption to ICU type
1614514f5e3Sopenharmony_ci    UListFormatterType uType;
1624514f5e3Sopenharmony_ci    switch (type) {
1634514f5e3Sopenharmony_ci        case ListTypeOption::CONJUNCTION:
1644514f5e3Sopenharmony_ci            uType = ULISTFMT_TYPE_AND;
1654514f5e3Sopenharmony_ci            break;
1664514f5e3Sopenharmony_ci        case ListTypeOption::DISJUNCTION:
1674514f5e3Sopenharmony_ci            uType = ULISTFMT_TYPE_OR;
1684514f5e3Sopenharmony_ci            break;
1694514f5e3Sopenharmony_ci        case ListTypeOption::UNIT:
1704514f5e3Sopenharmony_ci            uType = ULISTFMT_TYPE_UNITS;
1714514f5e3Sopenharmony_ci            break;
1724514f5e3Sopenharmony_ci        default:
1734514f5e3Sopenharmony_ci            LOG_ECMA(FATAL) << "this branch is unreachable";
1744514f5e3Sopenharmony_ci            UNREACHABLE();
1754514f5e3Sopenharmony_ci    }
1764514f5e3Sopenharmony_ci
1774514f5e3Sopenharmony_ci    // Trans StyleOption to ICU Style
1784514f5e3Sopenharmony_ci    UListFormatterWidth uStyle;
1794514f5e3Sopenharmony_ci    switch (style) {
1804514f5e3Sopenharmony_ci        case ListStyleOption::LONG:
1814514f5e3Sopenharmony_ci            uStyle = ULISTFMT_WIDTH_WIDE;
1824514f5e3Sopenharmony_ci            break;
1834514f5e3Sopenharmony_ci        case ListStyleOption::SHORT:
1844514f5e3Sopenharmony_ci            uStyle = ULISTFMT_WIDTH_SHORT;
1854514f5e3Sopenharmony_ci            break;
1864514f5e3Sopenharmony_ci        case ListStyleOption::NARROW:
1874514f5e3Sopenharmony_ci            uStyle = ULISTFMT_WIDTH_NARROW;
1884514f5e3Sopenharmony_ci            break;
1894514f5e3Sopenharmony_ci        default:
1904514f5e3Sopenharmony_ci            LOG_ECMA(FATAL) << "this branch is unreachable";
1914514f5e3Sopenharmony_ci            UNREACHABLE();
1924514f5e3Sopenharmony_ci    }
1934514f5e3Sopenharmony_ci    UErrorCode status = U_ZERO_ERROR;
1944514f5e3Sopenharmony_ci    icu::ListFormatter *icuListFormatter = icu::ListFormatter::createInstance(icuLocale, uType, uStyle, status);
1954514f5e3Sopenharmony_ci    if (U_FAILURE(status) || icuListFormatter == nullptr) {
1964514f5e3Sopenharmony_ci        delete icuListFormatter;
1974514f5e3Sopenharmony_ci        if (status == UErrorCode::U_MISSING_RESOURCE_ERROR) {
1984514f5e3Sopenharmony_ci            THROW_REFERENCE_ERROR_AND_RETURN(thread, "can not find icu data resources", listFormat);
1994514f5e3Sopenharmony_ci        }
2004514f5e3Sopenharmony_ci        THROW_RANGE_ERROR_AND_RETURN(thread, "create icu::ListFormatter failed", listFormat);
2014514f5e3Sopenharmony_ci    }
2024514f5e3Sopenharmony_ci    SetIcuListFormatter(thread, listFormat, icuListFormatter, JSListFormat::FreeIcuListFormatter);
2034514f5e3Sopenharmony_ci    return listFormat;
2044514f5e3Sopenharmony_ci}
2054514f5e3Sopenharmony_ci
2064514f5e3Sopenharmony_ci// 13.1.5 StringListFromIterable ( iterable )
2074514f5e3Sopenharmony_ciJSHandle<JSTaggedValue> JSListFormat::StringListFromIterable(JSThread *thread, const JSHandle<JSTaggedValue> &iterable)
2084514f5e3Sopenharmony_ci{
2094514f5e3Sopenharmony_ci    JSHandle<JSArray> array = JSHandle<JSArray>::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)));
2104514f5e3Sopenharmony_ci    RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
2114514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> arrayList = JSHandle<JSTaggedValue>::Cast(array);
2124514f5e3Sopenharmony_ci    // 1. If iterable is undefined, then
2134514f5e3Sopenharmony_ci    // a. Return a new empty List.
2144514f5e3Sopenharmony_ci    if (iterable->IsUndefined()) {
2154514f5e3Sopenharmony_ci        return arrayList;
2164514f5e3Sopenharmony_ci    }
2174514f5e3Sopenharmony_ci    // 2. Let iteratorRecord be ? GetIterator(iterable).
2184514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> iteratorRecord(JSIterator::GetIterator(thread, iterable));
2194514f5e3Sopenharmony_ci    RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
2204514f5e3Sopenharmony_ci    // 3. Let list be a new empty List.
2214514f5e3Sopenharmony_ci    // 4. Let next be true.
2224514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> next(thread, JSTaggedValue::True());
2234514f5e3Sopenharmony_ci    // 5. Repeat, while next is not false,
2244514f5e3Sopenharmony_ci    // a. Set next to ? IteratorStep(iteratorRecord).
2254514f5e3Sopenharmony_ci    // b. If next is not false, then
2264514f5e3Sopenharmony_ci    // i. Let nextValue be ? IteratorValue(next).
2274514f5e3Sopenharmony_ci    // ii. If Type(nextValue) is not String, then
2284514f5e3Sopenharmony_ci    // 1. Let error be ThrowCompletion(a newly created TypeError object).
2294514f5e3Sopenharmony_ci    // 2. Return ? IteratorClose(iteratorRecord, error).
2304514f5e3Sopenharmony_ci    // iii. Append nextValue to the end of the List list.
2314514f5e3Sopenharmony_ci    uint32_t k = 0;
2324514f5e3Sopenharmony_ci    while (!next->IsFalse()) {
2334514f5e3Sopenharmony_ci        next = JSIterator::IteratorStep(thread, iteratorRecord);
2344514f5e3Sopenharmony_ci        RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
2354514f5e3Sopenharmony_ci        if (!next->IsFalse()) {
2364514f5e3Sopenharmony_ci            JSHandle<JSTaggedValue> nextValue(JSIterator::IteratorValue(thread, next));
2374514f5e3Sopenharmony_ci            RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
2384514f5e3Sopenharmony_ci            if (!nextValue->IsString()) {
2394514f5e3Sopenharmony_ci                ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2404514f5e3Sopenharmony_ci                JSHandle<JSObject> typeError =
2414514f5e3Sopenharmony_ci                    factory->GetJSError(ErrorType::TYPE_ERROR, "nextValue is not string", StackCheck::NO);
2424514f5e3Sopenharmony_ci                JSHandle<JSTaggedValue> error(
2434514f5e3Sopenharmony_ci                    factory->NewCompletionRecord(CompletionRecordType::THROW, JSHandle<JSTaggedValue>(typeError)));
2444514f5e3Sopenharmony_ci                JSTaggedValue result = JSIterator::IteratorClose(thread, iteratorRecord, error).GetTaggedValue();
2454514f5e3Sopenharmony_ci                THROW_TYPE_ERROR_AND_RETURN(thread, "type error", JSHandle<JSTaggedValue>(thread, result));
2464514f5e3Sopenharmony_ci            }
2474514f5e3Sopenharmony_ci            JSArray::FastSetPropertyByValue(thread, arrayList, k, nextValue);
2484514f5e3Sopenharmony_ci            k++;
2494514f5e3Sopenharmony_ci        }
2504514f5e3Sopenharmony_ci    }
2514514f5e3Sopenharmony_ci    // 6. Return list.
2524514f5e3Sopenharmony_ci    return arrayList;
2534514f5e3Sopenharmony_ci}
2544514f5e3Sopenharmony_ci
2554514f5e3Sopenharmony_cinamespace {
2564514f5e3Sopenharmony_ci    std::vector<icu::UnicodeString> ToUnicodeStringArray(JSThread *thread, const JSHandle<JSArray> &array)
2574514f5e3Sopenharmony_ci    {
2584514f5e3Sopenharmony_ci        uint32_t length = array->GetArrayLength();
2594514f5e3Sopenharmony_ci        std::vector<icu::UnicodeString> result;
2604514f5e3Sopenharmony_ci        for (uint32_t k = 0; k < length; k++) {
2614514f5e3Sopenharmony_ci            JSHandle<JSTaggedValue> listArray = JSHandle<JSTaggedValue>::Cast(array);
2624514f5e3Sopenharmony_ci            JSHandle<JSTaggedValue> kValue = JSArray::FastGetPropertyByValue(thread, listArray, k);
2634514f5e3Sopenharmony_ci            ASSERT(kValue->IsString());
2644514f5e3Sopenharmony_ci            JSHandle<EcmaString> kValueString = JSTaggedValue::ToString(thread, kValue);
2654514f5e3Sopenharmony_ci            std::string stdString = intl::LocaleHelper::ConvertToStdString(kValueString);
2664514f5e3Sopenharmony_ci            icu::StringPiece sp(stdString);
2674514f5e3Sopenharmony_ci            icu::UnicodeString uString = icu::UnicodeString::fromUTF8(sp);
2684514f5e3Sopenharmony_ci            result.push_back(uString);
2694514f5e3Sopenharmony_ci        }
2704514f5e3Sopenharmony_ci        return result;
2714514f5e3Sopenharmony_ci    }
2724514f5e3Sopenharmony_ci
2734514f5e3Sopenharmony_ci    icu::FormattedList GetIcuFormatted(JSThread *thread, const JSHandle<JSListFormat> &listFormat,
2744514f5e3Sopenharmony_ci                                       const JSHandle<JSArray> &listArray)
2754514f5e3Sopenharmony_ci    {
2764514f5e3Sopenharmony_ci        icu::ListFormatter *icuListFormat = listFormat->GetIcuListFormatter();
2774514f5e3Sopenharmony_ci        ASSERT(icuListFormat != nullptr);
2784514f5e3Sopenharmony_ci        std::vector<icu::UnicodeString> usArray = ToUnicodeStringArray(thread, listArray);
2794514f5e3Sopenharmony_ci        UErrorCode status = U_ZERO_ERROR;
2804514f5e3Sopenharmony_ci        icu::FormattedList formatted = icuListFormat->formatStringsToValue(usArray.data(),
2814514f5e3Sopenharmony_ci                                                                           static_cast<int32_t>(usArray.size()),
2824514f5e3Sopenharmony_ci                                                                           status);
2834514f5e3Sopenharmony_ci        return formatted;
2844514f5e3Sopenharmony_ci    }
2854514f5e3Sopenharmony_ci
2864514f5e3Sopenharmony_ci    void FormatListToArray(JSThread *thread, const icu::FormattedList &formatted, const JSHandle<JSArray> &receiver,
2874514f5e3Sopenharmony_ci                           UErrorCode &status, icu::UnicodeString &listString)
2884514f5e3Sopenharmony_ci    {
2894514f5e3Sopenharmony_ci        icu::ConstrainedFieldPosition cfpo;
2904514f5e3Sopenharmony_ci        cfpo.constrainCategory(UFIELD_CATEGORY_LIST);
2914514f5e3Sopenharmony_ci        auto globalConst = thread->GlobalConstants();
2924514f5e3Sopenharmony_ci        JSMutableHandle<JSTaggedValue> typeString(thread, JSTaggedValue::Undefined());
2934514f5e3Sopenharmony_ci        int index = 0;
2944514f5e3Sopenharmony_ci        while (formatted.nextPosition(cfpo, status) && U_SUCCESS(status)) {
2954514f5e3Sopenharmony_ci            int32_t fieldId = cfpo.getField();
2964514f5e3Sopenharmony_ci            int32_t start = cfpo.getStart();
2974514f5e3Sopenharmony_ci            int32_t limit = cfpo.getLimit();
2984514f5e3Sopenharmony_ci            if (static_cast<UListFormatterField>(fieldId) == ULISTFMT_ELEMENT_FIELD) {
2994514f5e3Sopenharmony_ci                JSHandle<EcmaString> substring = intl::LocaleHelper::UStringToString(thread, listString, start, limit);
3004514f5e3Sopenharmony_ci                typeString.Update(globalConst->GetElementString());
3014514f5e3Sopenharmony_ci                JSLocale::PutElement(thread, index, receiver, typeString, JSHandle<JSTaggedValue>::Cast(substring));
3024514f5e3Sopenharmony_ci                RETURN_IF_ABRUPT_COMPLETION(thread);
3034514f5e3Sopenharmony_ci                index++;
3044514f5e3Sopenharmony_ci            } else {
3054514f5e3Sopenharmony_ci                JSHandle<EcmaString> substring = intl::LocaleHelper::UStringToString(thread, listString, start, limit);
3064514f5e3Sopenharmony_ci                typeString.Update(globalConst->GetLiteralString());
3074514f5e3Sopenharmony_ci                JSLocale::PutElement(thread, index, receiver, typeString, JSHandle<JSTaggedValue>::Cast(substring));
3084514f5e3Sopenharmony_ci                RETURN_IF_ABRUPT_COMPLETION(thread);
3094514f5e3Sopenharmony_ci                index++;
3104514f5e3Sopenharmony_ci            }
3114514f5e3Sopenharmony_ci        }
3124514f5e3Sopenharmony_ci    }
3134514f5e3Sopenharmony_ci
3144514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> ListOptionStyleToEcmaString(JSThread *thread, ListStyleOption style)
3154514f5e3Sopenharmony_ci    {
3164514f5e3Sopenharmony_ci        JSMutableHandle<JSTaggedValue> result(thread, JSTaggedValue::Undefined());
3174514f5e3Sopenharmony_ci        auto globalConst = thread->GlobalConstants();
3184514f5e3Sopenharmony_ci        switch (style) {
3194514f5e3Sopenharmony_ci            case ListStyleOption::LONG:
3204514f5e3Sopenharmony_ci                result.Update(globalConst->GetHandledLongString().GetTaggedValue());
3214514f5e3Sopenharmony_ci                break;
3224514f5e3Sopenharmony_ci            case ListStyleOption::SHORT:
3234514f5e3Sopenharmony_ci                result.Update(globalConst->GetHandledShortString().GetTaggedValue());
3244514f5e3Sopenharmony_ci                break;
3254514f5e3Sopenharmony_ci            case ListStyleOption::NARROW:
3264514f5e3Sopenharmony_ci                result.Update(globalConst->GetHandledNarrowString().GetTaggedValue());
3274514f5e3Sopenharmony_ci                break;
3284514f5e3Sopenharmony_ci            default:
3294514f5e3Sopenharmony_ci                LOG_ECMA(FATAL) << "this branch is unreachable";
3304514f5e3Sopenharmony_ci                UNREACHABLE();
3314514f5e3Sopenharmony_ci        }
3324514f5e3Sopenharmony_ci        return result;
3334514f5e3Sopenharmony_ci    }
3344514f5e3Sopenharmony_ci
3354514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> ListOptionTypeToEcmaString(JSThread *thread, ListTypeOption type)
3364514f5e3Sopenharmony_ci    {
3374514f5e3Sopenharmony_ci        JSMutableHandle<JSTaggedValue> result(thread, JSTaggedValue::Undefined());
3384514f5e3Sopenharmony_ci        auto globalConst = thread->GlobalConstants();
3394514f5e3Sopenharmony_ci        switch (type) {
3404514f5e3Sopenharmony_ci            case ListTypeOption::CONJUNCTION:
3414514f5e3Sopenharmony_ci                result.Update(globalConst->GetHandledConjunctionString().GetTaggedValue());
3424514f5e3Sopenharmony_ci                break;
3434514f5e3Sopenharmony_ci            case ListTypeOption::DISJUNCTION:
3444514f5e3Sopenharmony_ci                result.Update(globalConst->GetHandledDisjunctionString().GetTaggedValue());
3454514f5e3Sopenharmony_ci                break;
3464514f5e3Sopenharmony_ci            case ListTypeOption::UNIT:
3474514f5e3Sopenharmony_ci                result.Update(globalConst->GetHandledUnitString().GetTaggedValue());
3484514f5e3Sopenharmony_ci                break;
3494514f5e3Sopenharmony_ci            default:
3504514f5e3Sopenharmony_ci                LOG_ECMA(FATAL) << "this branch is unreachable";
3514514f5e3Sopenharmony_ci                UNREACHABLE();
3524514f5e3Sopenharmony_ci        }
3534514f5e3Sopenharmony_ci        return result;
3544514f5e3Sopenharmony_ci    }
3554514f5e3Sopenharmony_ci}
3564514f5e3Sopenharmony_ci
3574514f5e3Sopenharmony_ci// 13.1.3 FormatList ( listFormat, list )
3584514f5e3Sopenharmony_ciJSHandle<EcmaString> JSListFormat::FormatList(JSThread *thread, const JSHandle<JSListFormat> &listFormat,
3594514f5e3Sopenharmony_ci                                              const JSHandle<JSArray> &listArray)
3604514f5e3Sopenharmony_ci{
3614514f5e3Sopenharmony_ci    JSHandle<EcmaString> stringValue;
3624514f5e3Sopenharmony_ci    UErrorCode status = U_ZERO_ERROR;
3634514f5e3Sopenharmony_ci    icu::FormattedList formatted = GetIcuFormatted(thread, listFormat, listArray);
3644514f5e3Sopenharmony_ci    if (U_FAILURE(status)) {
3654514f5e3Sopenharmony_ci        THROW_RANGE_ERROR_AND_RETURN(thread, "icu listformat failed", stringValue);
3664514f5e3Sopenharmony_ci    }
3674514f5e3Sopenharmony_ci    icu::UnicodeString result = formatted.toString(status);
3684514f5e3Sopenharmony_ci    if (U_FAILURE(status)) {
3694514f5e3Sopenharmony_ci        THROW_RANGE_ERROR_AND_RETURN(thread, "formatted list toString failed", stringValue);
3704514f5e3Sopenharmony_ci    }
3714514f5e3Sopenharmony_ci    stringValue = intl::LocaleHelper::UStringToString(thread, result);
3724514f5e3Sopenharmony_ci    RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, stringValue);
3734514f5e3Sopenharmony_ci    // 4. Return result
3744514f5e3Sopenharmony_ci    return stringValue;
3754514f5e3Sopenharmony_ci}
3764514f5e3Sopenharmony_ci
3774514f5e3Sopenharmony_ci// 13.1.4 FormatListToParts ( listFormat, list )
3784514f5e3Sopenharmony_ciJSHandle<JSArray> JSListFormat::FormatListToParts(JSThread *thread, const JSHandle<JSListFormat> &listFormat,
3794514f5e3Sopenharmony_ci                                                  const JSHandle<JSArray> &listArray)
3804514f5e3Sopenharmony_ci{
3814514f5e3Sopenharmony_ci    UErrorCode status = U_ZERO_ERROR;
3824514f5e3Sopenharmony_ci    icu::FormattedList formatted = GetIcuFormatted(thread, listFormat, listArray);
3834514f5e3Sopenharmony_ci    if (U_FAILURE(status)) {
3844514f5e3Sopenharmony_ci        THROW_RANGE_ERROR_AND_RETURN(thread, "icu listformat failed", listArray);
3854514f5e3Sopenharmony_ci    }
3864514f5e3Sopenharmony_ci    icu::UnicodeString result = formatted.toString(status);
3874514f5e3Sopenharmony_ci    if (U_FAILURE(status)) {
3884514f5e3Sopenharmony_ci        THROW_RANGE_ERROR_AND_RETURN(thread, "formatted list toString failed", listArray);
3894514f5e3Sopenharmony_ci    }
3904514f5e3Sopenharmony_ci    JSHandle<JSArray> array = JSHandle<JSArray>::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)));
3914514f5e3Sopenharmony_ci    RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSArray, thread);
3924514f5e3Sopenharmony_ci    FormatListToArray(thread, formatted, array, status, result);
3934514f5e3Sopenharmony_ci    RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSArray, thread);
3944514f5e3Sopenharmony_ci    return array;
3954514f5e3Sopenharmony_ci}
3964514f5e3Sopenharmony_ci
3974514f5e3Sopenharmony_civoid JSListFormat::ResolvedOptions(JSThread *thread, const JSHandle<JSListFormat> &listFormat,
3984514f5e3Sopenharmony_ci                                   const JSHandle<JSObject> &options)
3994514f5e3Sopenharmony_ci{
4004514f5e3Sopenharmony_ci    auto globalConst = thread->GlobalConstants();
4014514f5e3Sopenharmony_ci
4024514f5e3Sopenharmony_ci    // [[Locale]]
4034514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> propertyKey = globalConst->GetHandledLocaleString();
4044514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> locale(thread, listFormat->GetLocale());
4054514f5e3Sopenharmony_ci    JSObject::CreateDataPropertyOrThrow(thread, options, propertyKey, locale);
4064514f5e3Sopenharmony_ci    RETURN_IF_ABRUPT_COMPLETION(thread);
4074514f5e3Sopenharmony_ci
4084514f5e3Sopenharmony_ci    // [[type]]
4094514f5e3Sopenharmony_ci    ListTypeOption type = listFormat->GetType();
4104514f5e3Sopenharmony_ci    propertyKey = globalConst->GetHandledTypeString();
4114514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> typeString = ListOptionTypeToEcmaString(thread, type);
4124514f5e3Sopenharmony_ci    JSObject::CreateDataPropertyOrThrow(thread, options, propertyKey, typeString);
4134514f5e3Sopenharmony_ci    RETURN_IF_ABRUPT_COMPLETION(thread);
4144514f5e3Sopenharmony_ci
4154514f5e3Sopenharmony_ci    // [[Style]]
4164514f5e3Sopenharmony_ci    ListStyleOption style = listFormat->GetStyle();
4174514f5e3Sopenharmony_ci    propertyKey = globalConst->GetHandledStyleString();
4184514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> styleString = ListOptionStyleToEcmaString(thread, style);
4194514f5e3Sopenharmony_ci    JSObject::CreateDataPropertyOrThrow(thread, options, propertyKey, styleString);
4204514f5e3Sopenharmony_ci    RETURN_IF_ABRUPT_COMPLETION(thread);
4214514f5e3Sopenharmony_ci}
4224514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
423