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     static const std::vector<LocaleMatcherOption> LOCALE_MATCHER_OPTION;
67     static const std::vector<std::string> LOCALE_MATCHER_OPTION_NAME;
68 
69     static const std::vector<StyOption> STY_OPTION;
70     static const std::vector<std::string> STY_OPTION_NAME;
71 
72     static const std::vector<TypednsOption> TYPED_NS_OPTION;
73     static const std::vector<std::string> TYPED_NS_OPTION_NAME;
74 
75     static const std::vector<FallbackOption> FALLBACK_OPTION;
76     static const std::vector<std::string> FALLBACK_OPTION_OPTION_NAME;
77 
78     static const std::vector<LanguageDisplayOption> LANGUAGE_DISPLAY_OPTION;
79     static const std::vector<std::string> LANGUAGE_DISPLAY_OPTION_NAME;
80     CAST_CHECK(JSDisplayNames, IsJSDisplayNames);
81 
82     static constexpr size_t LOCALE_OFFSET = JSObject::SIZE;
83     ACCESSORS(Locale, LOCALE_OFFSET, ICU_LOCALE_DISPPLAY_NAMES)
84     ACCESSORS(IcuLDN, ICU_LOCALE_DISPPLAY_NAMES, BIT_FIELD_OFFSET)
85     ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
86     DEFINE_ALIGN_SIZE(LAST_OFFSET);
87 
88     // define BitField
89     static constexpr size_t TYPE_BITS = 4;
90     static constexpr size_t STYLE_BITS = 3;
91     static constexpr size_t FALL_BACK_BITS = 2;
92     static constexpr size_t LANGUAGE_DISPLAY_BITS = 2;
93     FIRST_BIT_FIELD(BitField, Type, TypednsOption, TYPE_BITS)
94     NEXT_BIT_FIELD(BitField, Style, StyOption, STYLE_BITS, Type)
95     NEXT_BIT_FIELD(BitField, Fallback, FallbackOption, FALL_BACK_BITS, Style)
96     NEXT_BIT_FIELD(BitField, LanguageDisplay, LanguageDisplayOption, LANGUAGE_DISPLAY_BITS, Fallback)
97 
98     DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LOCALE_OFFSET, BIT_FIELD_OFFSET)
99     DECL_DUMP()
100 
101     icu::LocaleDisplayNames *GetIcuLocaleDisplayNames() const;
102 
103     static void FreeIcuLocaleDisplayNames(void *env, void *pointer, void* hint);
104 
105     static JSHandle<TaggedArray> GetAvailableLocales(JSThread *thread);
106 
107     static void SetIcuLocaleDisplayNames(JSThread *thread, const JSHandle<JSDisplayNames> &displayNames,
108                                          icu::LocaleDisplayNames* icuPointer, const NativePointerCallback &callback);
109 
110     // 12.1.1  CanonicalCodeForDisplayNames ( type, code )
111     static JSHandle<EcmaString> CanonicalCodeForDisplayNames(JSThread *thread,
112                                                              const JSHandle<JSDisplayNames> &displayNames,
113                                                              const TypednsOption &typeOpt,
114                                                              const JSHandle<EcmaString> &code);
115 
116     // 12.1.2 InitializeDisplayNames ( displaynames, locales, options )
117     static JSHandle<JSDisplayNames> InitializeDisplayNames(JSThread *thread,
118                                                              const JSHandle<JSDisplayNames> &displayNames,
119                                                              const JSHandle<JSTaggedValue> &locales,
120                                                              const JSHandle<JSTaggedValue> &options);
121 
122     static void ResolvedOptions(JSThread *thread, const JSHandle<JSDisplayNames> &displayNames,
123                                 const JSHandle<JSObject> &options);
124 
125 private:
126     static UDateTimePatternField StringToUDateTimePatternField(const char* code);
127 };
128 }  // namespace panda::ecmascript
129 #endif  // ECMASCRIPT_JS_DISPLAYNAMES_H