Debian Codegraph @

js/src/builtin/intl/RelativeTimeFormat.h

raw
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
19 namespace mozilla::intl {
20 class RelativeTimeFormat;
21 }
22
23 namespace js {
24
25 class RelativeTimeFormatObject : public NativeObject {
26 public:
27 static const JSClass class_;
28 static const JSClass& protoClass_;
29
30 static constexpr uint32_t INTERNALS_SLOT = 0;
31 static constexpr uint32_t URELATIVE_TIME_FORMAT_SLOT = 1;
32 static constexpr uint32_t SLOT_COUNT = 2;
33
34 static_assert(INTERNALS_SLOT == INTL_INTERNALS_OBJECT_SLOT,
35 "INTERNALS_SLOT must match self-hosting define for internals "
36 "object slot");
37
38 // Estimated memory use for URelativeDateTimeFormatter (see IcuMemoryUsage).
39 static constexpr size_t EstimatedMemoryUse = 8188;
40
41 mozilla::intl::RelativeTimeFormat* getRelativeTimeFormatter() const {
42 const auto& slot = getFixedSlot(URELATIVE_TIME_FORMAT_SLOT);
43 if (slot.isUndefined()) {
44 return nullptr;
45 }
46 return static_cast<mozilla::intl::RelativeTimeFormat*>(slot.toPrivate());
47 }
48
49 void setRelativeTimeFormatter(mozilla::intl::RelativeTimeFormat* rtf) {
50 setFixedSlot(URELATIVE_TIME_FORMAT_SLOT, PrivateValue(rtf));
51 }
52
53 private:
54 static const JSClassOps classOps_;
55 static const ClassSpec classSpec_;
56
57 static void finalize(JS::GCContext* gcx, JSObject* obj);
58 };
59
60 /**
61 * Returns a relative time as a string formatted according to the effective
62 * locale and the formatting options of the given RelativeTimeFormat.
63 *
64 * |t| should be a number representing a number to be formatted.
65 * |unit| should be "second", "minute", "hour", "day", "week", "month",
66 * "quarter", or "year".
67 * |numeric| should be "always" or "auto".
68 *
69 * Usage: formatted = intl_FormatRelativeTime(relativeTimeFormat, t,
70 * unit, numeric, formatToParts)
71 */
72 [[nodiscard]] extern bool intl_FormatRelativeTime(JSContext* cx, unsigned argc,
73 JS::Value* vp);
74
75 namespace intl {
76
77 using FieldType = js::ImmutableTenuredPtr<PropertyName*> JSAtomState::*;
78
79 [[nodiscard]] bool FormattedRelativeTimeToParts(
80 JSContext* cx, HandleString str,
81 const mozilla::intl::NumberPartVector& parts, FieldType relativeTimeUnit,
82 MutableHandleValue result);
83
84 } // namespace intl
85 } // namespace js
86
87 #endif /* builtin_intl_RelativeTimeFormat_h */