1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef builtin_intl_RelativeTimeFormat_h
8 #define builtin_intl_RelativeTimeFormat_h
9 
10 #include "mozilla/intl/NumberPart.h"
11 
12 #include <stdint.h>
13 
14 #include "builtin/SelfHostingDefines.h"
15 #include "gc/Barrier.h"
16 #include "js/Class.h"
17 #include "vm/NativeObject.h"
18 #include "vm/Runtime.h"
19 
20 namespace mozilla::intl {
21 class RelativeTimeFormat;
22 }
23 
24 namespace js {
25 
26 class RelativeTimeFormatObject : public NativeObject {
27  public:
28   static const JSClass class_;
29   static const JSClass& protoClass_;
30 
31   static constexpr uint32_t INTERNALS_SLOT = 0;
32   static constexpr uint32_t URELATIVE_TIME_FORMAT_SLOT = 1;
33   static constexpr uint32_t SLOT_COUNT = 2;
34 
35   static_assert(INTERNALS_SLOT == INTL_INTERNALS_OBJECT_SLOT,
36                 "INTERNALS_SLOT must match self-hosting define for internals "
37                 "object slot");
38 
39   // Estimated memory use for URelativeDateTimeFormatter (see IcuMemoryUsage).
40   static constexpr size_t EstimatedMemoryUse = 10896;
41 
getRelativeTimeFormatter()42   mozilla::intl::RelativeTimeFormat* getRelativeTimeFormatter() const {
43     const auto& slot = getFixedSlot(URELATIVE_TIME_FORMAT_SLOT);
44     if (slot.isUndefined()) {
45       return nullptr;
46     }
47     return static_cast<mozilla::intl::RelativeTimeFormat*>(slot.toPrivate());
48   }
49 
setRelativeTimeFormatter(mozilla::intl::RelativeTimeFormat * rtf)50   void setRelativeTimeFormatter(mozilla::intl::RelativeTimeFormat* rtf) {
51     setFixedSlot(URELATIVE_TIME_FORMAT_SLOT, PrivateValue(rtf));
52   }
53 
54  private:
55   static const JSClassOps classOps_;
56   static const ClassSpec classSpec_;
57 
58   static void finalize(JSFreeOp* fop, JSObject* obj);
59 };
60 
61 /**
62  * Returns a relative time as a string formatted according to the effective
63  * locale and the formatting options of the given RelativeTimeFormat.
64  *
65  * |t| should be a number representing a number to be formatted.
66  * |unit| should be "second", "minute", "hour", "day", "week", "month",
67  *                  "quarter", or "year".
68  * |numeric| should be "always" or "auto".
69  *
70  * Usage: formatted = intl_FormatRelativeTime(relativeTimeFormat, t,
71  *                                            unit, numeric, formatToParts)
72  */
73 [[nodiscard]] extern bool intl_FormatRelativeTime(JSContext* cx, unsigned argc,
74                                                   JS::Value* vp);
75 
76 namespace intl {
77 
78 using FieldType = js::ImmutablePropertyNamePtr JSAtomState::*;
79 
80 [[nodiscard]] bool FormattedRelativeTimeToParts(
81     JSContext* cx, HandleString str,
82     const mozilla::intl::NumberPartVector& parts, FieldType relativeTimeUnit,
83     MutableHandleValue result);
84 
85 }  // namespace intl
86 }  // namespace js
87 
88 #endif /* builtin_intl_RelativeTimeFormat_h */
89