tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

NumberFormat.h (6175B)


      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 #include <string_view>
     12 
     13 #include "builtin/SelfHostingDefines.h"
     14 #include "js/Class.h"
     15 #include "vm/NativeObject.h"
     16 
     17 class JSString;
     18 class JSLinearString;
     19 
     20 namespace mozilla::intl {
     21 class NumberFormat;
     22 class NumberRangeFormat;
     23 }  // namespace mozilla::intl
     24 
     25 namespace js {
     26 
     27 class ArrayObject;
     28 
     29 class NumberFormatObject : public NativeObject {
     30 public:
     31  static const JSClass class_;
     32  static const JSClass& protoClass_;
     33 
     34  static constexpr uint32_t INTERNALS_SLOT = 0;
     35  static constexpr uint32_t UNUMBER_FORMATTER_SLOT = 1;
     36  static constexpr uint32_t UNUMBER_RANGE_FORMATTER_SLOT = 2;
     37  static constexpr uint32_t SLOT_COUNT = 3;
     38 
     39  static_assert(INTERNALS_SLOT == INTL_INTERNALS_OBJECT_SLOT,
     40                "INTERNALS_SLOT must match self-hosting define for internals "
     41                "object slot");
     42 
     43  // Estimated memory use for UNumberFormatter and UFormattedNumber
     44  // (see IcuMemoryUsage).
     45  static constexpr size_t EstimatedMemoryUse = 972;
     46 
     47  // Estimated memory use for UNumberRangeFormatter and UFormattedNumberRange
     48  // (see IcuMemoryUsage).
     49  static constexpr size_t EstimatedRangeFormatterMemoryUse = 19894;
     50 
     51  mozilla::intl::NumberFormat* getNumberFormatter() const {
     52    const auto& slot = getFixedSlot(UNUMBER_FORMATTER_SLOT);
     53    if (slot.isUndefined()) {
     54      return nullptr;
     55    }
     56    return static_cast<mozilla::intl::NumberFormat*>(slot.toPrivate());
     57  }
     58 
     59  void setNumberFormatter(mozilla::intl::NumberFormat* formatter) {
     60    setFixedSlot(UNUMBER_FORMATTER_SLOT, PrivateValue(formatter));
     61  }
     62 
     63  mozilla::intl::NumberRangeFormat* getNumberRangeFormatter() const {
     64    const auto& slot = getFixedSlot(UNUMBER_RANGE_FORMATTER_SLOT);
     65    if (slot.isUndefined()) {
     66      return nullptr;
     67    }
     68    return static_cast<mozilla::intl::NumberRangeFormat*>(slot.toPrivate());
     69  }
     70 
     71  void setNumberRangeFormatter(mozilla::intl::NumberRangeFormat* formatter) {
     72    setFixedSlot(UNUMBER_RANGE_FORMATTER_SLOT, PrivateValue(formatter));
     73  }
     74 
     75 private:
     76  static const JSClassOps classOps_;
     77  static const ClassSpec classSpec_;
     78 
     79  static void finalize(JS::GCContext* gcx, JSObject* obj);
     80 };
     81 
     82 /**
     83 * Returns a new instance of the standard built-in NumberFormat constructor.
     84 *
     85 * Usage: numberFormat = intl_NumberFormat(locales, options)
     86 */
     87 [[nodiscard]] extern bool intl_NumberFormat(JSContext* cx, unsigned argc,
     88                                            Value* vp);
     89 
     90 /**
     91 * Returns the numbering system type identifier per Unicode
     92 * Technical Standard 35, Unicode Locale Data Markup Language, for the
     93 * default numbering system for the given locale.
     94 *
     95 * Usage: defaultNumberingSystem = intl_numberingSystem(locale)
     96 */
     97 [[nodiscard]] extern bool intl_numberingSystem(JSContext* cx, unsigned argc,
     98                                               Value* vp);
     99 
    100 /**
    101 * Returns a string representing the number x according to the effective
    102 * locale and the formatting options of the given NumberFormat.
    103 *
    104 * Spec: ECMAScript Internationalization API Specification, 11.3.2.
    105 *
    106 * Usage: formatted = intl_FormatNumber(numberFormat, x, formatToParts)
    107 */
    108 [[nodiscard]] extern bool intl_FormatNumber(JSContext* cx, unsigned argc,
    109                                            Value* vp);
    110 
    111 /**
    112 * Returns a string representing the number range «x - y» according to the
    113 * effective locale and the formatting options of the given NumberFormat.
    114 *
    115 * Usage: formatted = intl_FormatNumberRange(numberFormat, x, y, formatToParts)
    116 */
    117 [[nodiscard]] extern bool intl_FormatNumberRange(JSContext* cx, unsigned argc,
    118                                                 Value* vp);
    119 
    120 #if DEBUG || MOZ_SYSTEM_ICU
    121 /**
    122 * Returns an object with all available measurement units.
    123 *
    124 * Usage: units = intl_availableMeasurementUnits()
    125 */
    126 [[nodiscard]] extern bool intl_availableMeasurementUnits(JSContext* cx,
    127                                                         unsigned argc,
    128                                                         Value* vp);
    129 #endif
    130 
    131 namespace intl {
    132 
    133 /**
    134 * Returns a new instance of the standard built-in NumberFormat constructor.
    135 */
    136 [[nodiscard]] extern NumberFormatObject* CreateNumberFormat(
    137    JSContext* cx, JS::Handle<JS::Value> locales,
    138    JS::Handle<JS::Value> options);
    139 
    140 /**
    141 * Returns a possibly cached instance of the standard built-in NumberFormat
    142 * constructor.
    143 */
    144 [[nodiscard]] extern NumberFormatObject* GetOrCreateNumberFormat(
    145    JSContext* cx, JS::Handle<JS::Value> locales,
    146    JS::Handle<JS::Value> options);
    147 
    148 /**
    149 * Returns a string representing the number x according to the effective locale
    150 * and the formatting options of the given NumberFormat.
    151 */
    152 [[nodiscard]] extern JSString* FormatNumber(
    153    JSContext* cx, Handle<NumberFormatObject*> numberFormat, double x);
    154 
    155 /**
    156 * Returns a string representing the BigInt x according to the effective locale
    157 * and the formatting options of the given NumberFormat.
    158 */
    159 [[nodiscard]] extern JSString* FormatBigInt(
    160    JSContext* cx, Handle<NumberFormatObject*> numberFormat, Handle<BigInt*> x);
    161 
    162 using NumberFormatUnit = js::ImmutableTenuredPtr<PropertyName*> JSAtomState::*;
    163 
    164 [[nodiscard]] extern JSLinearString* FormatNumber(
    165    JSContext* cx, mozilla::intl::NumberFormat* numberFormat, double x);
    166 
    167 [[nodiscard]] extern JSLinearString* FormatNumber(
    168    JSContext* cx, mozilla::intl::NumberFormat* numberFormat,
    169    std::string_view x);
    170 
    171 [[nodiscard]] extern ArrayObject* FormatNumberToParts(
    172    JSContext* cx, mozilla::intl::NumberFormat* numberFormat, double x,
    173    NumberFormatUnit unit = nullptr);
    174 
    175 [[nodiscard]] extern ArrayObject* FormatNumberToParts(
    176    JSContext* cx, mozilla::intl::NumberFormat* numberFormat,
    177    std::string_view x, NumberFormatUnit unit = nullptr);
    178 
    179 }  // namespace intl
    180 
    181 }  // namespace js
    182 
    183 #endif /* builtin_intl_NumberFormat_h */