• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ECMASCRIPT_JS_SEGMENTER_H
17 #define ECMASCRIPT_JS_SEGMENTER_H
18 
19 #include "ecmascript/ecma_string.h"
20 #include "ecmascript/ecma_vm.h"
21 #include "ecmascript/ecma_macros.h"
22 #include "ecmascript/global_env.h"
23 #include "ecmascript/js_hclass.h"
24 #include "ecmascript/js_intl.h"
25 #include "ecmascript/js_locale.h"
26 #include "ecmascript/js_object.h"
27 #include "ecmascript/js_tagged_value.h"
28 #include "ecmascript/object_factory.h"
29 
30 #include "unicode/locdspnm.h"
31 
32 namespace panda::ecmascript {
33 enum class GranularityOption : uint8_t {
34     GRAPHEME = 0x01,
35     WORD,
36     SENTENCE,
37     EXCEPTION
38 };
39 
40 class JSSegmenter : public JSObject {
41 public:
42     CAST_CHECK(JSSegmenter, IsJSSegmenter);
43 
44     static constexpr size_t LOCALE_OFFSET = JSObject::SIZE;
45     ACCESSORS(Locale, LOCALE_OFFSET, ICU_FIELD_OFFSET)
46     ACCESSORS(IcuField, ICU_FIELD_OFFSET, BIT_FIELD_OFFSET)
47     ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
48     DEFINE_ALIGN_SIZE(LAST_OFFSET);
49 
50     // define BitField
51     static constexpr size_t GRANULARITY_BITS = 3;
52     FIRST_BIT_FIELD(BitField, Granularity, GranularityOption, GRANULARITY_BITS)
53 
54     DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LOCALE_OFFSET, BIT_FIELD_OFFSET)
55     DECL_DUMP()
56 
57     static JSHandle<JSSegmenter> InitializeSegmenter(JSThread *thread,
58                                                      const JSHandle<JSSegmenter> &segmenter,
59                                                      const JSHandle<JSTaggedValue> &locales,
60                                                      const JSHandle<JSTaggedValue> &options);
61     // Get icu break iterator from icu field
GetIcuBreakIterator()62     icu::BreakIterator *GetIcuBreakIterator() const
63     {
64         ASSERT(GetIcuField().IsJSNativePointer());
65         auto result = JSNativePointer::Cast(GetIcuField().GetTaggedObject())->GetExternalPointer();
66         return reinterpret_cast<icu::BreakIterator *>(result);
67     }
68 
FreeIcuBreakIterator(void * env,void * pointer,void * hint)69     static void FreeIcuBreakIterator([[maybe_unused]] void *env, void *pointer, [[maybe_unused]] void* hint)
70     {
71         if (pointer == nullptr) {
72             return;
73         }
74         auto icuBreakIterator = reinterpret_cast<icu::BreakIterator *>(pointer);
75         delete icuBreakIterator;
76     }
77 
78     static void SetIcuBreakIterator(JSThread *thread, const JSHandle<JSSegmenter> &segmenter,
79                                     icu::BreakIterator* icuBreakIterator, const NativePointerCallback &callback);
80 
81     static JSHandle<TaggedArray> GetAvailableLocales(JSThread *thread);
82 
83     static void ResolvedOptions(JSThread *thread, const JSHandle<JSSegmenter> &segmenter,
84                                 const JSHandle<JSObject> &options);
85 
86     static JSHandle<JSTaggedValue> GranularityOptionToEcmaString(JSThread *thread, GranularityOption granularity);
87 
88 private:
89     static void InitializeIcuBreakIterator(JSThread *thread, const JSHandle<JSSegmenter> &segmenter,
90                                            const icu::Locale &icuLocale, GranularityOption granularity);
91 };
92 }  // namespace panda::ecmascript
93 #endif  // ECMASCRIPT_JS_SEGMENTER_H