14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_JS_RELATIVE_TIME_FORMAT_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_JS_RELATIVE_TIME_FORMAT_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "unicode/reldatefmt.h"
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_ci#include "ecmascript/intl/locale_helper.h"
224514f5e3Sopenharmony_ci#include "ecmascript/base/number_helper.h"
234514f5e3Sopenharmony_ci#include "ecmascript/common.h"
244514f5e3Sopenharmony_ci#include "ecmascript/ecma_macros.h"
254514f5e3Sopenharmony_ci#include "ecmascript/ecma_string.h"
264514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h"
274514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
284514f5e3Sopenharmony_ci#include "ecmascript/js_array.h"
294514f5e3Sopenharmony_ci#include "ecmascript/js_date.h"
304514f5e3Sopenharmony_ci#include "ecmascript/js_intl.h"
314514f5e3Sopenharmony_ci#include "ecmascript/js_locale.h"
324514f5e3Sopenharmony_ci#include "ecmascript/js_object.h"
334514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_number.h"
344514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h"
354514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h"
364514f5e3Sopenharmony_ci
374514f5e3Sopenharmony_cinamespace panda::ecmascript {
384514f5e3Sopenharmony_cienum class RelativeStyleOption : uint8_t { LONG = 0x01, SHORT, NARROW, EXCEPTION };
394514f5e3Sopenharmony_cienum class NumericOption : uint8_t { ALWAYS = 0x01, AUTO, EXCEPTION };
404514f5e3Sopenharmony_ci
414514f5e3Sopenharmony_ciclass JSRelativeTimeFormat : public JSObject {
424514f5e3Sopenharmony_cipublic:
434514f5e3Sopenharmony_ci    CAST_CHECK(JSRelativeTimeFormat, IsJSRelativeTimeFormat);
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_ci    static constexpr size_t LOCALE_OFFSET = JSObject::SIZE;
464514f5e3Sopenharmony_ci    ACCESSORS(Locale, LOCALE_OFFSET, NUMBERING_SYSTEM_OFFSET)
474514f5e3Sopenharmony_ci    ACCESSORS(NumberingSystem, NUMBERING_SYSTEM_OFFSET, ICU_FIELD_OFFSET)
484514f5e3Sopenharmony_ci    ACCESSORS(IcuField, ICU_FIELD_OFFSET, BIT_FIELD_OFFSET)  // icu field
494514f5e3Sopenharmony_ci    ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
504514f5e3Sopenharmony_ci    DEFINE_ALIGN_SIZE(LAST_OFFSET);
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_ci    // define BitField
534514f5e3Sopenharmony_ci    static constexpr size_t STYLE_BITS = 3;
544514f5e3Sopenharmony_ci    static constexpr size_t NUMERIC_BITS = 2;
554514f5e3Sopenharmony_ci    FIRST_BIT_FIELD(BitField, Style, RelativeStyleOption, STYLE_BITS)
564514f5e3Sopenharmony_ci    NEXT_BIT_FIELD(BitField, Numeric, NumericOption, NUMERIC_BITS, Style)
574514f5e3Sopenharmony_ci
584514f5e3Sopenharmony_ci    DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LOCALE_OFFSET, BIT_FIELD_OFFSET)
594514f5e3Sopenharmony_ci    DECL_DUMP()
604514f5e3Sopenharmony_ci
614514f5e3Sopenharmony_ci    // 14.1.1 InitializeRelativeTimeFormat ( relativeTimeFormat, locales, options )
624514f5e3Sopenharmony_ci    static JSHandle<JSRelativeTimeFormat> InitializeRelativeTimeFormat(
634514f5e3Sopenharmony_ci        JSThread *thread, const JSHandle<JSRelativeTimeFormat> &relativeTimeFormat,
644514f5e3Sopenharmony_ci        const JSHandle<JSTaggedValue> &locales, const JSHandle<JSTaggedValue> &options);
654514f5e3Sopenharmony_ci
664514f5e3Sopenharmony_ci    // UnwrapRelativeTimeFormat
674514f5e3Sopenharmony_ci    static JSHandle<JSTaggedValue> UnwrapRelativeTimeFormat(JSThread *thread, const JSHandle<JSTaggedValue> &rtf);
684514f5e3Sopenharmony_ci
694514f5e3Sopenharmony_ci    // Get icu formatter from icu field
704514f5e3Sopenharmony_ci    icu::RelativeDateTimeFormatter *GetIcuRTFFormatter() const
714514f5e3Sopenharmony_ci    {
724514f5e3Sopenharmony_ci        ASSERT(GetIcuField().IsJSNativePointer());
734514f5e3Sopenharmony_ci        auto result = JSNativePointer::Cast(GetIcuField().GetTaggedObject())->GetExternalPointer();
744514f5e3Sopenharmony_ci        return reinterpret_cast<icu::RelativeDateTimeFormatter *>(result);
754514f5e3Sopenharmony_ci    }
764514f5e3Sopenharmony_ci
774514f5e3Sopenharmony_ci    static void FreeIcuRTFFormatter([[maybe_unused]] void *env, void *pointer, void *data)
784514f5e3Sopenharmony_ci    {
794514f5e3Sopenharmony_ci        if (pointer == nullptr) {
804514f5e3Sopenharmony_ci            return;
814514f5e3Sopenharmony_ci        }
824514f5e3Sopenharmony_ci        auto icuFormatter = reinterpret_cast<icu::RelativeDateTimeFormatter *>(pointer);
834514f5e3Sopenharmony_ci        icuFormatter->~RelativeDateTimeFormatter();
844514f5e3Sopenharmony_ci        if (data != nullptr) {
854514f5e3Sopenharmony_ci            reinterpret_cast<EcmaVM *>(data)->GetNativeAreaAllocator()->FreeBuffer(pointer);
864514f5e3Sopenharmony_ci        }
874514f5e3Sopenharmony_ci    }
884514f5e3Sopenharmony_ci
894514f5e3Sopenharmony_ci    static void ResolvedOptions(JSThread *thread, const JSHandle<JSRelativeTimeFormat> &relativeTimeFormat,
904514f5e3Sopenharmony_ci                                const JSHandle<JSObject> &options);
914514f5e3Sopenharmony_ci
924514f5e3Sopenharmony_ci    static JSHandle<EcmaString> Format(JSThread *thread, double value, const JSHandle<EcmaString> &unit,
934514f5e3Sopenharmony_ci                                       const JSHandle<JSRelativeTimeFormat> &relativeTimeFormat);
944514f5e3Sopenharmony_ci
954514f5e3Sopenharmony_ci    static JSHandle<JSArray> FormatToParts(JSThread *thread, double value, const JSHandle<EcmaString> &unit,
964514f5e3Sopenharmony_ci                                           const JSHandle<JSRelativeTimeFormat> &relativeTimeFormat);
974514f5e3Sopenharmony_ci};
984514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
994514f5e3Sopenharmony_ci
1004514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_JS_RELATIVE_TIME_FORMAT_H