14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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#ifndef ECMASCRIPT_JS_SEGMENTER_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_JS_SEGMENTER_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/ecma_string.h"
204514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h"
214514f5e3Sopenharmony_ci#include "ecmascript/ecma_macros.h"
224514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
234514f5e3Sopenharmony_ci#include "ecmascript/js_hclass.h"
244514f5e3Sopenharmony_ci#include "ecmascript/js_intl.h"
254514f5e3Sopenharmony_ci#include "ecmascript/js_locale.h"
264514f5e3Sopenharmony_ci#include "ecmascript/js_object.h"
274514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h"
284514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h"
294514f5e3Sopenharmony_ci
304514f5e3Sopenharmony_ci#include "unicode/locdspnm.h"
314514f5e3Sopenharmony_ci
324514f5e3Sopenharmony_cinamespace panda::ecmascript {
334514f5e3Sopenharmony_cienum class GranularityOption : uint8_t {
344514f5e3Sopenharmony_ci    GRAPHEME = 0x01,
354514f5e3Sopenharmony_ci    WORD,
364514f5e3Sopenharmony_ci    SENTENCE,
374514f5e3Sopenharmony_ci    EXCEPTION
384514f5e3Sopenharmony_ci};
394514f5e3Sopenharmony_ci
404514f5e3Sopenharmony_ciclass JSSegmenter : public JSObject {
414514f5e3Sopenharmony_cipublic:
424514f5e3Sopenharmony_ci    CAST_CHECK(JSSegmenter, IsJSSegmenter);
434514f5e3Sopenharmony_ci
444514f5e3Sopenharmony_ci    static constexpr size_t LOCALE_OFFSET = JSObject::SIZE;
454514f5e3Sopenharmony_ci    ACCESSORS(Locale, LOCALE_OFFSET, ICU_FIELD_OFFSET)
464514f5e3Sopenharmony_ci    ACCESSORS(IcuField, ICU_FIELD_OFFSET, BIT_FIELD_OFFSET)
474514f5e3Sopenharmony_ci    ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
484514f5e3Sopenharmony_ci    DEFINE_ALIGN_SIZE(LAST_OFFSET);
494514f5e3Sopenharmony_ci
504514f5e3Sopenharmony_ci    // define BitField
514514f5e3Sopenharmony_ci    static constexpr size_t GRANULARITY_BITS = 3;
524514f5e3Sopenharmony_ci    FIRST_BIT_FIELD(BitField, Granularity, GranularityOption, GRANULARITY_BITS)
534514f5e3Sopenharmony_ci
544514f5e3Sopenharmony_ci    DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LOCALE_OFFSET, BIT_FIELD_OFFSET)
554514f5e3Sopenharmony_ci    DECL_DUMP()
564514f5e3Sopenharmony_ci
574514f5e3Sopenharmony_ci    static JSHandle<JSSegmenter> InitializeSegmenter(JSThread *thread,
584514f5e3Sopenharmony_ci                                                     const JSHandle<JSSegmenter> &segmenter,
594514f5e3Sopenharmony_ci                                                     const JSHandle<JSTaggedValue> &locales,
604514f5e3Sopenharmony_ci                                                     const JSHandle<JSTaggedValue> &options);
614514f5e3Sopenharmony_ci    // Get icu break iterator from icu field
624514f5e3Sopenharmony_ci    icu::BreakIterator *GetIcuBreakIterator() const
634514f5e3Sopenharmony_ci    {
644514f5e3Sopenharmony_ci        ASSERT(GetIcuField().IsJSNativePointer());
654514f5e3Sopenharmony_ci        auto result = JSNativePointer::Cast(GetIcuField().GetTaggedObject())->GetExternalPointer();
664514f5e3Sopenharmony_ci        return reinterpret_cast<icu::BreakIterator *>(result);
674514f5e3Sopenharmony_ci    }
684514f5e3Sopenharmony_ci
694514f5e3Sopenharmony_ci    static void FreeIcuBreakIterator([[maybe_unused]] void *env, void *pointer, [[maybe_unused]] void* hint)
704514f5e3Sopenharmony_ci    {
714514f5e3Sopenharmony_ci        if (pointer == nullptr) {
724514f5e3Sopenharmony_ci            return;
734514f5e3Sopenharmony_ci        }
744514f5e3Sopenharmony_ci        auto icuBreakIterator = reinterpret_cast<icu::BreakIterator *>(pointer);
754514f5e3Sopenharmony_ci        delete icuBreakIterator;
764514f5e3Sopenharmony_ci    }
774514f5e3Sopenharmony_ci
784514f5e3Sopenharmony_ci    static void SetIcuBreakIterator(JSThread *thread, const JSHandle<JSSegmenter> &segmenter,
794514f5e3Sopenharmony_ci                                    icu::BreakIterator* icuBreakIterator, const NativePointerCallback &callback);
804514f5e3Sopenharmony_ci
814514f5e3Sopenharmony_ci    static JSHandle<TaggedArray> GetAvailableLocales(JSThread *thread);
824514f5e3Sopenharmony_ci
834514f5e3Sopenharmony_ci    static void ResolvedOptions(JSThread *thread, const JSHandle<JSSegmenter> &segmenter,
844514f5e3Sopenharmony_ci                                const JSHandle<JSObject> &options);
854514f5e3Sopenharmony_ci
864514f5e3Sopenharmony_ci    static JSHandle<JSTaggedValue> GranularityOptionToEcmaString(JSThread *thread, GranularityOption granularity);
874514f5e3Sopenharmony_ci
884514f5e3Sopenharmony_ciprivate:
894514f5e3Sopenharmony_ci    static void InitializeIcuBreakIterator(JSThread *thread, const JSHandle<JSSegmenter> &segmenter,
904514f5e3Sopenharmony_ci                                           const icu::Locale &icuLocale, GranularityOption granularity);
914514f5e3Sopenharmony_ci};
924514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
934514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_JS_SEGMENTER_H