• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_RELATIVE_TIME_FORMAT_H
17 #define ECMASCRIPT_JS_RELATIVE_TIME_FORMAT_H
18 
19 #include "unicode/reldatefmt.h"
20 
21 #include "ecmascript/base/number_helper.h"
22 #include "ecmascript/common.h"
23 #include "ecmascript/ecma_macros.h"
24 #include "ecmascript/ecma_string.h"
25 #include "ecmascript/ecma_vm.h"
26 #include "ecmascript/global_env.h"
27 #include "ecmascript/js_array.h"
28 #include "ecmascript/js_date.h"
29 #include "ecmascript/js_intl.h"
30 #include "ecmascript/js_locale.h"
31 #include "ecmascript/js_object.h"
32 #include "ecmascript/js_tagged_number.h"
33 #include "ecmascript/js_tagged_value.h"
34 #include "ecmascript/object_factory.h"
35 
36 namespace panda::ecmascript {
37 enum class RelativeStyleOption : uint8_t { LONG = 0x01, SHORT, NARROW, EXCEPTION };
38 enum class NumericOption : uint8_t { ALWAYS = 0x01, AUTO, EXCEPTION };
39 
40 class JSRelativeTimeFormat : public JSObject {
41 public:
42     CAST_CHECK(JSRelativeTimeFormat, IsJSRelativeTimeFormat);
43 
44     static constexpr size_t LOCALE_OFFSET = JSObject::SIZE;
45     ACCESSORS(Locale, LOCALE_OFFSET, NUMBERING_SYSTEM_OFFSET)
46     ACCESSORS(NumberingSystem, NUMBERING_SYSTEM_OFFSET, ICU_FIELD_OFFSET)
47     ACCESSORS(IcuField, ICU_FIELD_OFFSET, BIT_FIELD_OFFSET)  // icu field
48     ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
49     DEFINE_ALIGN_SIZE(LAST_OFFSET);
50 
51     // define BitField
52     static constexpr size_t STYLE_BITS = 3;
53     static constexpr size_t NUMERIC_BITS = 2;
54     FIRST_BIT_FIELD(BitField, Style, RelativeStyleOption, STYLE_BITS)
55     NEXT_BIT_FIELD(BitField, Numeric, NumericOption, NUMERIC_BITS, Style)
56 
57     DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LOCALE_OFFSET, BIT_FIELD_OFFSET)
58     DECL_DUMP()
59 
60     // 14.1.1 InitializeRelativeTimeFormat ( relativeTimeFormat, locales, options )
61     static JSHandle<JSRelativeTimeFormat> InitializeRelativeTimeFormat(
62         JSThread *thread, const JSHandle<JSRelativeTimeFormat> &relativeTimeFormat,
63         const JSHandle<JSTaggedValue> &locales, const JSHandle<JSTaggedValue> &options);
64 
65     // UnwrapRelativeTimeFormat
66     static JSHandle<JSTaggedValue> UnwrapRelativeTimeFormat(JSThread *thread, const JSHandle<JSTaggedValue> &rtf);
67 
68     // Get icu formatter from icu field
GetIcuRTFFormatter()69     icu::RelativeDateTimeFormatter *GetIcuRTFFormatter() const
70     {
71         ASSERT(GetIcuField().IsJSNativePointer());
72         auto result = JSNativePointer::Cast(GetIcuField().GetTaggedObject())->GetExternalPointer();
73         return reinterpret_cast<icu::RelativeDateTimeFormatter *>(result);
74     }
75 
FreeIcuRTFFormatter(void * pointer,void * data)76     static void FreeIcuRTFFormatter(void *pointer, void *data)
77     {
78         if (pointer == nullptr) {
79             return;
80         }
81         auto icuFormatter = reinterpret_cast<icu::RelativeDateTimeFormatter *>(pointer);
82         icuFormatter->~RelativeDateTimeFormatter();
83         if (data != nullptr) {
84             reinterpret_cast<EcmaVM *>(data)->GetNativeAreaAllocator()->FreeBuffer(pointer);
85         }
86     }
87 
88     static void ResolvedOptions(JSThread *thread, const JSHandle<JSRelativeTimeFormat> &relativeTimeFormat,
89                                 const JSHandle<JSObject> &options);
90 
91     static JSHandle<EcmaString> Format(JSThread *thread, double value, const JSHandle<EcmaString> &unit,
92                                        const JSHandle<JSRelativeTimeFormat> &relativeTimeFormat);
93 
94     static JSHandle<JSArray> FormatToParts(JSThread *thread, double value, const JSHandle<EcmaString> &unit,
95                                            const JSHandle<JSRelativeTimeFormat> &relativeTimeFormat);
96 };
97 }  // namespace panda::ecmascript
98 
99 #endif  // ECMASCRIPT_JS_RELATIVE_TIME_FORMAT_H