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_NumberFormat_h
8 #define builtin_intl_NumberFormat_h
9 
10 #include "mozilla/Attributes.h"
11 
12 #include <stdint.h>
13 
14 #include "builtin/SelfHostingDefines.h"
15 #include "js/Class.h"
16 #include "vm/NativeObject.h"
17 
18 namespace mozilla::intl {
19 class NumberFormat;
20 class NumberRangeFormat;
21 }  // namespace mozilla::intl
22 
23 namespace js {
24 
25 class NumberFormatObject : 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 UNUMBER_FORMATTER_SLOT = 1;
32   static constexpr uint32_t UNUMBER_RANGE_FORMATTER_SLOT = 2;
33   static constexpr uint32_t SLOT_COUNT = 3;
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 UNumberFormatter and UFormattedNumber
40   // (see IcuMemoryUsage).
41   static constexpr size_t EstimatedMemoryUse = 972;
42 
43   // Estimated memory use for UNumberRangeFormatter and UFormattedNumberRange
44   // (see IcuMemoryUsage).
45   static constexpr size_t EstimatedRangeFormatterMemoryUse = 14143;
46 
getNumberFormatter()47   mozilla::intl::NumberFormat* getNumberFormatter() const {
48     const auto& slot = getFixedSlot(UNUMBER_FORMATTER_SLOT);
49     if (slot.isUndefined()) {
50       return nullptr;
51     }
52     return static_cast<mozilla::intl::NumberFormat*>(slot.toPrivate());
53   }
54 
setNumberFormatter(mozilla::intl::NumberFormat * formatter)55   void setNumberFormatter(mozilla::intl::NumberFormat* formatter) {
56     setFixedSlot(UNUMBER_FORMATTER_SLOT, PrivateValue(formatter));
57   }
58 
getNumberRangeFormatter()59   mozilla::intl::NumberRangeFormat* getNumberRangeFormatter() const {
60     const auto& slot = getFixedSlot(UNUMBER_RANGE_FORMATTER_SLOT);
61     if (slot.isUndefined()) {
62       return nullptr;
63     }
64     return static_cast<mozilla::intl::NumberRangeFormat*>(slot.toPrivate());
65   }
66 
setNumberRangeFormatter(mozilla::intl::NumberRangeFormat * formatter)67   void setNumberRangeFormatter(mozilla::intl::NumberRangeFormat* formatter) {
68     setFixedSlot(UNUMBER_RANGE_FORMATTER_SLOT, PrivateValue(formatter));
69   }
70 
71  private:
72   static const JSClassOps classOps_;
73   static const ClassSpec classSpec_;
74 
75   static void finalize(JSFreeOp* fop, JSObject* obj);
76 };
77 
78 /**
79  * Returns a new instance of the standard built-in NumberFormat constructor.
80  * Self-hosted code cannot cache this constructor (as it does for others in
81  * Utilities.js) because it is initialized after self-hosted code is compiled.
82  *
83  * Usage: numberFormat = intl_NumberFormat(locales, options)
84  */
85 [[nodiscard]] extern bool intl_NumberFormat(JSContext* cx, unsigned argc,
86                                             Value* vp);
87 
88 /**
89  * Returns the numbering system type identifier per Unicode
90  * Technical Standard 35, Unicode Locale Data Markup Language, for the
91  * default numbering system for the given locale.
92  *
93  * Usage: defaultNumberingSystem = intl_numberingSystem(locale)
94  */
95 [[nodiscard]] extern bool intl_numberingSystem(JSContext* cx, unsigned argc,
96                                                Value* vp);
97 
98 /**
99  * Returns a string representing the number x according to the effective
100  * locale and the formatting options of the given NumberFormat.
101  *
102  * Spec: ECMAScript Internationalization API Specification, 11.3.2.
103  *
104  * Usage: formatted = intl_FormatNumber(numberFormat, x, formatToParts)
105  */
106 [[nodiscard]] extern bool intl_FormatNumber(JSContext* cx, unsigned argc,
107                                             Value* vp);
108 
109 /**
110  * Returns a string representing the number range «x - y» according to the
111  * effective locale and the formatting options of the given NumberFormat.
112  *
113  * Usage: formatted = intl_FormatNumberRange(numberFormat, x, y, formatToParts)
114  */
115 [[nodiscard]] extern bool intl_FormatNumberRange(JSContext* cx, unsigned argc,
116                                                  Value* vp);
117 
118 #if DEBUG || MOZ_SYSTEM_ICU
119 /**
120  * Returns an object with all available measurement units.
121  *
122  * Usage: units = intl_availableMeasurementUnits()
123  */
124 [[nodiscard]] extern bool intl_availableMeasurementUnits(JSContext* cx,
125                                                          unsigned argc,
126                                                          Value* vp);
127 #endif
128 
129 }  // namespace js
130 
131 #endif /* builtin_intl_NumberFormat_h */
132