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