• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/base/bit-field.h"
16 #include "src/execution/isolate.h"
17 #include "src/heap/factory.h"
18 #include "src/objects/intl-objects.h"
19 #include "src/objects/managed.h"
20 #include "src/objects/objects.h"
21 
22 // Has to be the last include (doesn't have include guards):
23 #include "src/objects/object-macros.h"
24 
25 namespace U_ICU_NAMESPACE {
26 class UnicodeString;
27 namespace number {
28 class LocalizedNumberFormatter;
29 class UnlocalizedNumberFormatter;
30 class LocalizedNumberRangeFormatter;
31 }  //  namespace number
32 }  //  namespace U_ICU_NAMESPACE
33 
34 namespace v8 {
35 namespace internal {
36 
37 #include "torque-generated/src/objects/js-number-format-tq.inc"
38 
39 class JSNumberFormat
40     : public TorqueGeneratedJSNumberFormat<JSNumberFormat, JSObject> {
41  public:
42   // ecma402/#sec-initializenumberformat
43   V8_WARN_UNUSED_RESULT static MaybeHandle<JSNumberFormat> New(
44       Isolate* isolate, Handle<Map> map, Handle<Object> locales,
45       Handle<Object> options, const char* service);
46 
47   // ecma402/#sec-unwrapnumberformat
48   V8_WARN_UNUSED_RESULT static MaybeHandle<JSNumberFormat> UnwrapNumberFormat(
49       Isolate* isolate, Handle<JSReceiver> format_holder);
50 
51   // ecma402/#sec-intl.numberformat.prototype.resolvedoptions
52   static Handle<JSObject> ResolvedOptions(Isolate* isolate,
53                                           Handle<JSNumberFormat> number_format);
54 
55   V8_WARN_UNUSED_RESULT static MaybeHandle<JSArray> FormatToParts(
56       Isolate* isolate, Handle<JSNumberFormat> number_format,
57       Handle<Object> numeric_obj);
58 
59   // ecma402/#sec-formatnumericrange
60   V8_WARN_UNUSED_RESULT static MaybeHandle<String> FormatNumericRange(
61       Isolate* isolate, Handle<JSNumberFormat> number_format, Handle<Object> x,
62       Handle<Object> y);
63 
64   // ecma402/#sec-formatnumericrangetoparts
65   V8_WARN_UNUSED_RESULT static MaybeHandle<JSArray> FormatNumericRangeToParts(
66       Isolate* isolate, Handle<JSNumberFormat> number_format, Handle<Object> x,
67       Handle<Object> y);
68 
69   V8_WARN_UNUSED_RESULT static MaybeHandle<String> FormatNumeric(
70       Isolate* isolate,
71       const icu::number::LocalizedNumberFormatter& number_format,
72       Handle<Object> numeric_obj);
73 
74   V8_EXPORT_PRIVATE static const std::set<std::string>& GetAvailableLocales();
75 
76   // Helper functions shared with JSPluralRules.
77   static int32_t MinimumIntegerDigitsFromSkeleton(
78       const icu::UnicodeString& skeleton);
79   static bool FractionDigitsFromSkeleton(const icu::UnicodeString& skeleton,
80                                          int32_t* minimum, int32_t* maximum);
81   static bool SignificantDigitsFromSkeleton(const icu::UnicodeString& skeleton,
82                                             int32_t* minimum, int32_t* maximum);
83 
84   enum class ShowTrailingZeros { kShow, kHide };
85 
86   static icu::number::UnlocalizedNumberFormatter SetDigitOptionsToFormatter(
87       const icu::number::UnlocalizedNumberFormatter& settings,
88       const Intl::NumberFormatDigitOptions& digit_options,
89       int rounding_increment, ShowTrailingZeros show);
90 
91   DECL_PRINTER(JSNumberFormat)
92 
93   DECL_ACCESSORS(icu_number_formatter,
94                  Managed<icu::number::LocalizedNumberFormatter>)
95   DECL_ACCESSORS(icu_number_range_formatter,
96                  Managed<icu::number::LocalizedNumberRangeFormatter>)
97 
98   TQ_OBJECT_CONSTRUCTORS(JSNumberFormat)
99 };
100 
101 }  // namespace internal
102 }  // namespace v8
103 
104 #include "src/objects/object-macros-undef.h"
105 
106 #endif  // V8_OBJECTS_JS_NUMBER_FORMAT_H_
107