Line data Source code
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,
48 : Handle<Object> numeric_obj);
49 :
50 : // A utility function used by the above JSNumberFormat::FormatToParts()
51 : // and JSRelativeTimeFormat::FormatToParts().
52 : // Format the number by using the icu::NumberFormat to get the field
53 : // information. It add an object into the result array, starting from the
54 : // start_index and return the total number of elements in the result array.
55 : // For each object added as element, it set the substring of the field as
56 : // "value", the field type as "type". If the unit is not null, it also set
57 : // unit as "unit" to each added object.
58 : V8_WARN_UNUSED_RESULT static Maybe<int> FormatToParts(
59 : Isolate* isolate, Handle<JSArray> result, int start_index,
60 : const icu::NumberFormat& fmt, Handle<Object> numeric_obj,
61 : Handle<String> unit);
62 :
63 : V8_WARN_UNUSED_RESULT static MaybeHandle<String> FormatNumeric(
64 : Isolate* isolate, const icu::NumberFormat& number_format,
65 : Handle<Object> numeric_obj);
66 :
67 : static const std::set<std::string>& GetAvailableLocales();
68 :
69 : Handle<String> StyleAsString() const;
70 : Handle<String> CurrencyDisplayAsString() const;
71 :
72 : DECL_CAST(JSNumberFormat)
73 : DECL_PRINTER(JSNumberFormat)
74 : DECL_VERIFIER(JSNumberFormat)
75 :
76 : // [[Style]] is one of the values "decimal", "percent" or "currency",
77 : // identifying the style of the number format.
78 : enum class Style {
79 : DECIMAL,
80 : PERCENT,
81 : CURRENCY,
82 :
83 : COUNT
84 : };
85 : inline void set_style(Style style);
86 : inline Style style() const;
87 :
88 : // [[CurrencyDisplay]] is one of the values "code", "symbol" or "name",
89 : // identifying the display of the currency number format.
90 : enum class CurrencyDisplay {
91 : CODE,
92 : SYMBOL,
93 : NAME,
94 :
95 : COUNT
96 : };
97 : inline void set_currency_display(CurrencyDisplay currency_display);
98 : inline CurrencyDisplay currency_display() const;
99 :
100 : // Layout description.
101 : #define JS_NUMBER_FORMAT_FIELDS(V) \
102 : V(kLocaleOffset, kTaggedSize) \
103 : V(kICUNumberFormatOffset, kTaggedSize) \
104 : V(kBoundFormatOffset, kTaggedSize) \
105 : V(kFlagsOffset, kTaggedSize) \
106 : /* Total size. */ \
107 : V(kSize, 0)
108 :
109 : DEFINE_FIELD_OFFSET_CONSTANTS(JSObject::kHeaderSize, JS_NUMBER_FORMAT_FIELDS)
110 : #undef JS_NUMBER_FORMAT_FIELDS
111 :
112 : // Bit positions in |flags|.
113 : #define FLAGS_BIT_FIELDS(V, _) \
114 : V(StyleBits, Style, 2, _) \
115 : V(CurrencyDisplayBits, CurrencyDisplay, 2, _)
116 :
117 : DEFINE_BIT_FIELDS(FLAGS_BIT_FIELDS)
118 : #undef FLAGS_BIT_FIELDS
119 :
120 : STATIC_ASSERT(Style::DECIMAL <= StyleBits::kMax);
121 : STATIC_ASSERT(Style::PERCENT <= StyleBits::kMax);
122 : STATIC_ASSERT(Style::CURRENCY <= StyleBits::kMax);
123 :
124 : STATIC_ASSERT(CurrencyDisplay::CODE <= CurrencyDisplayBits::kMax);
125 : STATIC_ASSERT(CurrencyDisplay::SYMBOL <= CurrencyDisplayBits::kMax);
126 : STATIC_ASSERT(CurrencyDisplay::NAME <= CurrencyDisplayBits::kMax);
127 :
128 : DECL_ACCESSORS(locale, String)
129 : DECL_ACCESSORS(icu_number_format, Managed<icu::NumberFormat>)
130 : DECL_ACCESSORS(bound_format, Object)
131 : DECL_INT_ACCESSORS(flags)
132 :
133 : OBJECT_CONSTRUCTORS(JSNumberFormat, JSObject);
134 : };
135 :
136 : struct NumberFormatSpan {
137 : int32_t field_id;
138 : int32_t begin_pos;
139 : int32_t end_pos;
140 :
141 : NumberFormatSpan() = default;
142 : NumberFormatSpan(int32_t field_id, int32_t begin_pos, int32_t end_pos)
143 18317 : : field_id(field_id), begin_pos(begin_pos), end_pos(end_pos) {}
144 : };
145 :
146 : std::vector<NumberFormatSpan> FlattenRegionsToParts(
147 : std::vector<NumberFormatSpan>* regions);
148 :
149 : } // namespace internal
150 : } // namespace v8
151 :
152 : #include "src/objects/object-macros-undef.h"
153 :
154 : #endif // V8_OBJECTS_JS_NUMBER_FORMAT_H_
|