• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class JSDisplayNames : public JSObject {
59 public:
60     CAST_CHECK(JSDisplayNames, IsJSDisplayNames);
61 
62     static constexpr size_t LOCALE_OFFSET = JSObject::SIZE;
63     ACCESSORS(Locale, LOCALE_OFFSET, ICU_LOCALE_DISPPLAY_NAMES)
64     ACCESSORS(IcuLDN, ICU_LOCALE_DISPPLAY_NAMES, BIT_FIELD_OFFSET)
65     ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
66     DEFINE_ALIGN_SIZE(LAST_OFFSET);
67 
68     // define BitField
69     static constexpr size_t TYPE_BITS = 4;
70     static constexpr size_t STYLE_BITS = 3;
71     static constexpr size_t FALL_BACK_BITS = 2;
72     FIRST_BIT_FIELD(BitField, Type, TypednsOption, TYPE_BITS)
73     NEXT_BIT_FIELD(BitField, Style, StyOption, STYLE_BITS, Type)
74     NEXT_BIT_FIELD(BitField, Fallback, FallbackOption, FALL_BACK_BITS, Style)
75 
76     DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LOCALE_OFFSET, BIT_FIELD_OFFSET)
77     DECL_DUMP()
78 
79     icu::LocaleDisplayNames *GetIcuLocaleDisplayNames() const;
80 
81     static void FreeIcuLocaleDisplayNames(void *pointer, [[maybe_unused]] void* hint);
82 
83     static JSHandle<TaggedArray> GetAvailableLocales(JSThread *thread);
84 
85     static void SetIcuLocaleDisplayNames(JSThread *thread, const JSHandle<JSDisplayNames> &displayNames,
86                                              icu::LocaleDisplayNames* icuPointer, const DeleteEntryPoint &callback);
87 
88     // 12.1.1  CanonicalCodeForDisplayNames ( type, code )
89     static JSHandle<EcmaString> CanonicalCodeForDisplayNames(JSThread *thread,
90                                                              const JSHandle<JSDisplayNames> &displayNames,
91                                                              const TypednsOption &typeOpt,
92                                                              const JSHandle<EcmaString> &code);
93 
94     // 12.1.2 InitializeDisplayNames ( displaynames, locales, options )
95     static JSHandle<JSDisplayNames> InitializeDisplayNames(JSThread *thread,
96                                                              const JSHandle<JSDisplayNames> &displayNames,
97                                                              const JSHandle<JSTaggedValue> &locales,
98                                                              const JSHandle<JSTaggedValue> &options);
99 
100     static void ResolvedOptions(JSThread *thread, const JSHandle<JSDisplayNames> &displayNames,
101                                 const JSHandle<JSObject> &options);
102 };
103 }  // namespace panda::ecmascript
104 #endif  // ECMASCRIPT_JS_DISPLAYNAMES_H