1 /* 2 * Copyright (c) 2021-2022 Huawei Device 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_DISPLAYNAMES_H 17 #define ECMASCRIPT_JS_DISPLAYNAMES_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_object.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 enum class TypednsOption : uint8_t { 35 LANGUAGE = 0x01, 36 CALENDAR, 37 CURRENCY, 38 DATETIMEFIELD, 39 REGION, 40 SCRIPT, 41 UNDEFINED, 42 EXCEPTION 43 }; 44 45 enum class StyOption : uint8_t { 46 LONG = 0x01, 47 SHORT, 48 NARROW, 49 EXCEPTION 50 }; 51 52 enum class FallbackOption : uint8_t { 53 CODE = 0x01, 54 NONE, 55 EXCEPTION 56 }; 57 58 enum class LanguageDisplayOption : uint8_t { 59 DIALECT = 0x01, 60 STANDARD, 61 EXCEPTION 62 }; 63 64 class JSDisplayNames : public JSObject { 65 public: 66 CAST_CHECK(JSDisplayNames, IsJSDisplayNames); 67 68 static constexpr size_t LOCALE_OFFSET = JSObject::SIZE; 69 ACCESSORS(Locale, LOCALE_OFFSET, ICU_LOCALE_DISPPLAY_NAMES) 70 ACCESSORS(IcuLDN, ICU_LOCALE_DISPPLAY_NAMES, BIT_FIELD_OFFSET) 71 ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET) 72 DEFINE_ALIGN_SIZE(LAST_OFFSET); 73 74 // define BitField 75 static constexpr size_t TYPE_BITS = 4; 76 static constexpr size_t STYLE_BITS = 3; 77 static constexpr size_t FALL_BACK_BITS = 2; 78 static constexpr size_t LANGUAGE_DISPLAY_BITS = 2; 79 FIRST_BIT_FIELD(BitField, Type, TypednsOption, TYPE_BITS) 80 NEXT_BIT_FIELD(BitField, Style, StyOption, STYLE_BITS, Type) 81 NEXT_BIT_FIELD(BitField, Fallback, FallbackOption, FALL_BACK_BITS, Style) 82 NEXT_BIT_FIELD(BitField, LanguageDisplay, LanguageDisplayOption, LANGUAGE_DISPLAY_BITS, Fallback) 83 84 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LOCALE_OFFSET, BIT_FIELD_OFFSET) 85 DECL_DUMP() 86 87 icu::LocaleDisplayNames *GetIcuLocaleDisplayNames() const; 88 89 static void FreeIcuLocaleDisplayNames(void *env, void *pointer, void* hint); 90 91 static JSHandle<TaggedArray> GetAvailableLocales(JSThread *thread); 92 93 static void SetIcuLocaleDisplayNames(JSThread *thread, const JSHandle<JSDisplayNames> &displayNames, 94 icu::LocaleDisplayNames* icuPointer, const NativePointerCallback &callback); 95 96 // 12.1.1 CanonicalCodeForDisplayNames ( type, code ) 97 static JSHandle<EcmaString> CanonicalCodeForDisplayNames(JSThread *thread, 98 const JSHandle<JSDisplayNames> &displayNames, 99 const TypednsOption &typeOpt, 100 const JSHandle<EcmaString> &code); 101 102 // 12.1.2 InitializeDisplayNames ( displaynames, locales, options ) 103 static JSHandle<JSDisplayNames> InitializeDisplayNames(JSThread *thread, 104 const JSHandle<JSDisplayNames> &displayNames, 105 const JSHandle<JSTaggedValue> &locales, 106 const JSHandle<JSTaggedValue> &options); 107 108 static void ResolvedOptions(JSThread *thread, const JSHandle<JSDisplayNames> &displayNames, 109 const JSHandle<JSObject> &options); 110 111 private: 112 static UDateTimePatternField StringToUDateTimePatternField(const char* code); 113 }; 114 } // namespace panda::ecmascript 115 #endif // ECMASCRIPT_JS_DISPLAYNAMES_H