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 } // namespace number 30 } // namespace U_ICU_NAMESPACE 31 32 namespace v8 { 33 namespace internal { 34 35 #include "torque-generated/src/objects/js-number-format-tq.inc" 36 37 class JSNumberFormat 38 : public TorqueGeneratedJSNumberFormat<JSNumberFormat, JSObject> { 39 public: 40 // ecma402/#sec-initializenumberformat 41 V8_WARN_UNUSED_RESULT static MaybeHandle<JSNumberFormat> New( 42 Isolate* isolate, Handle<Map> map, Handle<Object> locales, 43 Handle<Object> options, const char* service); 44 45 // ecma402/#sec-unwrapnumberformat 46 V8_WARN_UNUSED_RESULT static MaybeHandle<JSNumberFormat> UnwrapNumberFormat( 47 Isolate* isolate, Handle<JSReceiver> format_holder); 48 49 // ecma402/#sec-intl.numberformat.prototype.resolvedoptions 50 static Handle<JSObject> ResolvedOptions(Isolate* isolate, 51 Handle<JSNumberFormat> number_format); 52 53 V8_WARN_UNUSED_RESULT static MaybeHandle<JSArray> FormatToParts( 54 Isolate* isolate, Handle<JSNumberFormat> number_format, 55 Handle<Object> numeric_obj); 56 57 V8_WARN_UNUSED_RESULT static MaybeHandle<String> FormatNumeric( 58 Isolate* isolate, 59 const icu::number::LocalizedNumberFormatter& number_format, 60 Handle<Object> numeric_obj); 61 62 V8_EXPORT_PRIVATE static const std::set<std::string>& GetAvailableLocales(); 63 64 // Helper functions shared with JSPluralRules. 65 static int32_t MinimumIntegerDigitsFromSkeleton( 66 const icu::UnicodeString& skeleton); 67 static bool FractionDigitsFromSkeleton(const icu::UnicodeString& skeleton, 68 int32_t* minimum, int32_t* maximum); 69 static bool SignificantDigitsFromSkeleton(const icu::UnicodeString& skeleton, 70 int32_t* minimum, int32_t* maximum); 71 static icu::number::LocalizedNumberFormatter SetDigitOptionsToFormatter( 72 const icu::number::LocalizedNumberFormatter& icu_number_formatter, 73 const Intl::NumberFormatDigitOptions& digit_options); 74 75 DECL_PRINTER(JSNumberFormat) 76 77 DECL_ACCESSORS(icu_number_formatter, 78 Managed<icu::number::LocalizedNumberFormatter>) 79 80 TQ_OBJECT_CONSTRUCTORS(JSNumberFormat) 81 }; 82 83 struct NumberFormatSpan { 84 int32_t field_id; 85 int32_t begin_pos; 86 int32_t end_pos; 87 88 NumberFormatSpan() = default; NumberFormatSpanNumberFormatSpan89 NumberFormatSpan(int32_t field_id, int32_t begin_pos, int32_t end_pos) 90 : field_id(field_id), begin_pos(begin_pos), end_pos(end_pos) {} 91 }; 92 93 V8_EXPORT_PRIVATE std::vector<NumberFormatSpan> FlattenRegionsToParts( 94 std::vector<NumberFormatSpan>* regions); 95 96 } // namespace internal 97 } // namespace v8 98 99 #include "src/objects/object-macros-undef.h" 100 101 #endif // V8_OBJECTS_JS_NUMBER_FORMAT_H_ 102