V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
js-number-format.h
1 // Copyright 2018 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_INTL_SUPPORT
6 #error Internationalization is expected to be enabled.
7 #endif // V8_INTL_SUPPORT
8 
9 #ifndef V8_OBJECTS_JS_NUMBER_FORMAT_H_
10 #define V8_OBJECTS_JS_NUMBER_FORMAT_H_
11 
12 #include <set>
13 #include <string>
14 
15 #include "src/heap/factory.h"
16 #include "src/isolate.h"
17 #include "src/objects.h"
18 #include "src/objects/intl-objects.h"
19 #include "src/objects/managed.h"
20 
21 // Has to be the last include (doesn't have include guards):
22 #include "src/objects/object-macros.h"
23 
24 namespace U_ICU_NAMESPACE {
25 class NumberFormat;
26 } // namespace U_ICU_NAMESPACE
27 
28 namespace v8 {
29 namespace internal {
30 
31 class JSNumberFormat : public JSObject {
32  public:
33  // ecma402/#sec-initializenumberformat
34  V8_WARN_UNUSED_RESULT static MaybeHandle<JSNumberFormat> Initialize(
35  Isolate* isolate, Handle<JSNumberFormat> number_format,
36  Handle<Object> locales, Handle<Object> options);
37 
38  // ecma402/#sec-unwrapnumberformat
39  V8_WARN_UNUSED_RESULT static MaybeHandle<JSNumberFormat> UnwrapNumberFormat(
40  Isolate* isolate, Handle<JSReceiver> format_holder);
41 
42  // ecma402/#sec-intl.numberformat.prototype.resolvedoptions
43  static Handle<JSObject> ResolvedOptions(Isolate* isolate,
44  Handle<JSNumberFormat> number_format);
45 
46  V8_WARN_UNUSED_RESULT static MaybeHandle<JSArray> FormatToParts(
47  Isolate* isolate, Handle<JSNumberFormat> number_format, double number);
48 
49  // A utility function used by the above JSNumberFormat::FormatToParts()
50  // and JSRelativeTimeFormat::FormatToParts().
51  // Format the number by using the icu::NumberFormat to get the field
52  // information. It add an object into the result array, starting from the
53  // start_index and return the total number of elements in the result array.
54  // For each object added as element, it set the substring of the field as
55  // "value", the field type as "type". If the unit is not null, it also set
56  // unit as "unit" to each added object.
57  V8_WARN_UNUSED_RESULT static Maybe<int> FormatToParts(
58  Isolate* isolate, Handle<JSArray> result, int start_index,
59  const icu::NumberFormat& fmt, double number, Handle<String> unit);
60 
61  V8_WARN_UNUSED_RESULT static MaybeHandle<String> FormatNumber(
62  Isolate* isolate, const icu::NumberFormat& number_format, double number);
63 
64  static std::set<std::string> GetAvailableLocales();
65 
66  Handle<String> StyleAsString() const;
67  Handle<String> CurrencyDisplayAsString() const;
68 
69  DECL_CAST(JSNumberFormat)
70  DECL_PRINTER(JSNumberFormat)
71  DECL_VERIFIER(JSNumberFormat)
72 
73  // [[Style]] is one of the values "decimal", "percent" or "currency",
74  // identifying the style of the number format.
75  enum class Style {
76  DECIMAL,
77  PERCENT,
78  CURRENCY,
79 
80  COUNT
81  };
82  inline void set_style(Style style);
83  inline Style style() const;
84 
85  // [[CurrencyDisplay]] is one of the values "code", "symbol" or "name",
86  // identifying the display of the currency number format.
87  enum class CurrencyDisplay {
88  CODE,
89  SYMBOL,
90  NAME,
91 
92  COUNT
93  };
94  inline void set_currency_display(CurrencyDisplay currency_display);
95  inline CurrencyDisplay currency_display() const;
96 
97 // Layout description.
98 #define JS_NUMBER_FORMAT_FIELDS(V) \
99  V(kLocaleOffset, kTaggedSize) \
100  V(kICUNumberFormatOffset, kTaggedSize) \
101  V(kBoundFormatOffset, kTaggedSize) \
102  V(kFlagsOffset, kTaggedSize) \
103  /* Total size. */ \
104  V(kSize, 0)
105 
106  DEFINE_FIELD_OFFSET_CONSTANTS(JSObject::kHeaderSize, JS_NUMBER_FORMAT_FIELDS)
107 #undef JS_NUMBER_FORMAT_FIELDS
108 
109 // Bit positions in |flags|.
110 #define FLAGS_BIT_FIELDS(V, _) \
111  V(StyleBits, Style, 2, _) \
112  V(CurrencyDisplayBits, CurrencyDisplay, 2, _)
113 
114  DEFINE_BIT_FIELDS(FLAGS_BIT_FIELDS)
115 #undef FLAGS_BIT_FIELDS
116 
117  STATIC_ASSERT(Style::DECIMAL <= StyleBits::kMax);
118  STATIC_ASSERT(Style::PERCENT <= StyleBits::kMax);
119  STATIC_ASSERT(Style::CURRENCY <= StyleBits::kMax);
120 
121  STATIC_ASSERT(CurrencyDisplay::CODE <= CurrencyDisplayBits::kMax);
122  STATIC_ASSERT(CurrencyDisplay::SYMBOL <= CurrencyDisplayBits::kMax);
123  STATIC_ASSERT(CurrencyDisplay::NAME <= CurrencyDisplayBits::kMax);
124 
125  DECL_ACCESSORS2(locale, String)
126  DECL_ACCESSORS(icu_number_format, Managed<icu::NumberFormat>)
127  DECL_ACCESSORS(bound_format, Object)
128  DECL_INT_ACCESSORS(flags)
129 
130  private:
131  DISALLOW_IMPLICIT_CONSTRUCTORS(JSNumberFormat);
132 };
133 
135  int32_t field_id;
136  int32_t begin_pos;
137  int32_t end_pos;
138 
139  NumberFormatSpan() = default;
140  NumberFormatSpan(int32_t field_id, int32_t begin_pos, int32_t end_pos)
141  : field_id(field_id), begin_pos(begin_pos), end_pos(end_pos) {}
142 };
143 
144 std::vector<NumberFormatSpan> FlattenRegionsToParts(
145  std::vector<NumberFormatSpan>* regions);
146 
147 } // namespace internal
148 } // namespace v8
149 
150 #include "src/objects/object-macros-undef.h"
151 
152 #endif // V8_OBJECTS_JS_NUMBER_FORMAT_H_
Definition: v8.h:56
Definition: libplatform.h:13