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_SEGMENT_ITERATOR_H 17 #define ECMASCRIPT_JS_SEGMENT_ITERATOR_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_segmenter.h" 28 #include "ecmascript/js_tagged_value.h" 29 #include "ecmascript/object_factory.h" 30 31 #include "unicode/locdspnm.h" 32 33 namespace panda::ecmascript { 34 class JSSegmentIterator : public JSObject { 35 public: 36 CAST_CHECK(JSSegmentIterator, IsJSSegmentIterator); 37 38 static constexpr size_t ICU_FIELD_OFFSET = JSObject::SIZE; 39 ACCESSORS(IcuField, ICU_FIELD_OFFSET, SEGMENTS_STRING_OFFSET) 40 ACCESSORS(IteratedString, SEGMENTS_STRING_OFFSET, UNICODE_STRING_OFFSET) 41 ACCESSORS(UnicodeString, UNICODE_STRING_OFFSET, BIT_FIELD_OFFSET) 42 ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET) 43 DEFINE_ALIGN_SIZE(LAST_OFFSET); 44 45 // define BitField 46 static constexpr size_t GRANULARITY_BITS = 3; 47 FIRST_BIT_FIELD(BitField, Granularity, GranularityOption, GRANULARITY_BITS) 48 49 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, ICU_FIELD_OFFSET, BIT_FIELD_OFFSET) 50 DECL_DUMP() 51 52 // 18.6.1 CreateSegmentIterator ( segmenter, string ) 53 static JSHandle<JSSegmentIterator> CreateSegmentIterator(JSThread *thread, icu::BreakIterator* icuBreakIterator, 54 const JSHandle<EcmaString> &string, 55 GranularityOption granularity); 56 57 // 18.6.2(1) SegmentIteratorPrototype.next ( ) 58 static JSTaggedValue Next(JSThread *thread, const JSHandle<JSSegmentIterator> &iterator); 59 60 // Get icu break iterator from icu field GetIcuBreakIterator()61 icu::BreakIterator *GetIcuBreakIterator() const 62 { 63 ASSERT(GetIcuField().IsJSNativePointer()); 64 auto result = JSNativePointer::Cast(GetIcuField().GetTaggedObject())->GetExternalPointer(); 65 return reinterpret_cast<icu::BreakIterator *>(result); 66 } 67 FreeIcuBreakIterator(void * env,void * pointer,void * hint)68 static void FreeIcuBreakIterator([[maybe_unused]] void *env, void *pointer, [[maybe_unused]] void* hint) 69 { 70 if (pointer == nullptr) { 71 return; 72 } 73 auto icuBreakIterator = reinterpret_cast<icu::BreakIterator *>(pointer); 74 delete icuBreakIterator; 75 } 76 77 static void SetIcuBreakIterator(JSThread *thread, const JSHandle<JSSegmentIterator> &iterator, 78 icu::BreakIterator* icuBreakIterator, const NativePointerCallback &callback); 79 FreeUString(void * env,void * pointer,void * hint)80 static void FreeUString([[maybe_unused]] void *env, void *pointer, [[maybe_unused]] void* hint) 81 { 82 if (pointer == nullptr) { 83 return; 84 } 85 auto unicodeString = reinterpret_cast<icu::UnicodeString *>(pointer); 86 delete unicodeString; 87 } 88 89 static void SetUString(JSThread *thread, const JSHandle<JSSegmentIterator> &iterator, 90 icu::UnicodeString* icuUnicodeString, const NativePointerCallback &callback); 91 GetUString()92 icu::UnicodeString *GetUString() const 93 { 94 ASSERT(GetUnicodeString().IsJSNativePointer()); 95 auto result = JSNativePointer::Cast(GetUnicodeString().GetTaggedObject())->GetExternalPointer(); 96 return reinterpret_cast<icu::UnicodeString *>(result); 97 } 98 }; 99 } // namespace panda::ecmascript 100 #endif // ECMASCRIPT_JS_SEGMENT_ITERATOR_H