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_SEGMENTS_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_JS_SEGMENTS_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_segmenter.h"
284514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h"
294514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h"
304514f5e3Sopenharmony_ci
314514f5e3Sopenharmony_ci#include "unicode/locdspnm.h"
324514f5e3Sopenharmony_ci
334514f5e3Sopenharmony_cinamespace panda::ecmascript {
344514f5e3Sopenharmony_ciclass JSSegments : public JSObject {
354514f5e3Sopenharmony_cipublic:
364514f5e3Sopenharmony_ci    CAST_CHECK(JSSegments, IsJSSegments);
374514f5e3Sopenharmony_ci
384514f5e3Sopenharmony_ci    static constexpr size_t ICU_FIELD_OFFSET = JSObject::SIZE;
394514f5e3Sopenharmony_ci    ACCESSORS(IcuField, ICU_FIELD_OFFSET, SEGMENTS_STRING_OFFSET)
404514f5e3Sopenharmony_ci    ACCESSORS(SegmentsString, SEGMENTS_STRING_OFFSET, UNICODE_STRING_OFFSET)
414514f5e3Sopenharmony_ci    ACCESSORS(UnicodeString, UNICODE_STRING_OFFSET, BIT_FIELD_OFFSET)
424514f5e3Sopenharmony_ci    ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
434514f5e3Sopenharmony_ci    DEFINE_ALIGN_SIZE(LAST_OFFSET);
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_ci    // define BitField
464514f5e3Sopenharmony_ci    static constexpr size_t GRANULARITY_BITS = 3;
474514f5e3Sopenharmony_ci    FIRST_BIT_FIELD(BitField, Granularity, GranularityOption, GRANULARITY_BITS)
484514f5e3Sopenharmony_ci
494514f5e3Sopenharmony_ci    DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, ICU_FIELD_OFFSET, BIT_FIELD_OFFSET)
504514f5e3Sopenharmony_ci    DECL_DUMP()
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_ci    // 18.5.1 CreateSegmentsObject ( segmenter, string )
534514f5e3Sopenharmony_ci    static JSHandle<JSSegments> CreateSegmentsObject(JSThread *thread,
544514f5e3Sopenharmony_ci                                                     const JSHandle<JSSegmenter> &segmenter,
554514f5e3Sopenharmony_ci                                                     const JSHandle<EcmaString> &string);
564514f5e3Sopenharmony_ci    // Get icu break iterator from icu field
574514f5e3Sopenharmony_ci    icu::BreakIterator *GetIcuBreakIterator() const
584514f5e3Sopenharmony_ci    {
594514f5e3Sopenharmony_ci        ASSERT(GetIcuField().IsJSNativePointer());
604514f5e3Sopenharmony_ci        auto result = JSNativePointer::Cast(GetIcuField().GetTaggedObject())->GetExternalPointer();
614514f5e3Sopenharmony_ci        return reinterpret_cast<icu::BreakIterator *>(result);
624514f5e3Sopenharmony_ci    }
634514f5e3Sopenharmony_ci
644514f5e3Sopenharmony_ci    static void FreeIcuBreakIterator([[maybe_unused]] void *env, void *pointer, [[maybe_unused]] void* hint)
654514f5e3Sopenharmony_ci    {
664514f5e3Sopenharmony_ci        if (pointer == nullptr) {
674514f5e3Sopenharmony_ci            return;
684514f5e3Sopenharmony_ci        }
694514f5e3Sopenharmony_ci        auto icuBreakIterator = reinterpret_cast<icu::BreakIterator *>(pointer);
704514f5e3Sopenharmony_ci        delete icuBreakIterator;
714514f5e3Sopenharmony_ci    }
724514f5e3Sopenharmony_ci
734514f5e3Sopenharmony_ci    static void SetIcuBreakIterator(JSThread *thread, const JSHandle<JSSegments> &segments,
744514f5e3Sopenharmony_ci                                    icu::BreakIterator* icuBreakIterator, const NativePointerCallback &callback);
754514f5e3Sopenharmony_ci
764514f5e3Sopenharmony_ci    static void FreeUString([[maybe_unused]] void *env, void *pointer, [[maybe_unused]] void* hint)
774514f5e3Sopenharmony_ci    {
784514f5e3Sopenharmony_ci        if (pointer == nullptr) {
794514f5e3Sopenharmony_ci            return;
804514f5e3Sopenharmony_ci        }
814514f5e3Sopenharmony_ci        auto unicodeString = reinterpret_cast<icu::UnicodeString *>(pointer);
824514f5e3Sopenharmony_ci        delete unicodeString;
834514f5e3Sopenharmony_ci    }
844514f5e3Sopenharmony_ci
854514f5e3Sopenharmony_ci    static void SetUString(JSThread *thread, const JSHandle<JSSegments> &segments,
864514f5e3Sopenharmony_ci                           icu::UnicodeString* icuUnicodeString, const NativePointerCallback &callback);
874514f5e3Sopenharmony_ci
884514f5e3Sopenharmony_ci    icu::UnicodeString *GetUString() const
894514f5e3Sopenharmony_ci    {
904514f5e3Sopenharmony_ci        ASSERT(GetUnicodeString().IsJSNativePointer());
914514f5e3Sopenharmony_ci        auto result = JSNativePointer::Cast(GetUnicodeString().GetTaggedObject())->GetExternalPointer();
924514f5e3Sopenharmony_ci        return reinterpret_cast<icu::UnicodeString *>(result);
934514f5e3Sopenharmony_ci    }
944514f5e3Sopenharmony_ci
954514f5e3Sopenharmony_ci    static JSTaggedValue Containing(JSThread *thread, const JSHandle<JSSegments> &segments, double index);
964514f5e3Sopenharmony_ci
974514f5e3Sopenharmony_ci    // 18.7.1 CreateSegmentDataObject ( segmenter, string, startIndex, endIndex )
984514f5e3Sopenharmony_ci    static JSHandle<JSObject> CreateSegmentDataObject(JSThread *thread, GranularityOption granularity,
994514f5e3Sopenharmony_ci        icu::BreakIterator* breakIterator, const JSHandle<EcmaString> &inputString,
1004514f5e3Sopenharmony_ci        const icu::UnicodeString& unicodeString, int32_t startIndex, int32_t endIndex);
1014514f5e3Sopenharmony_ci};
1024514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
1034514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_JS_SEGMENTS_H