• 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_LIST_FORMAT_H_
10 #define V8_OBJECTS_JS_LIST_FORMAT_H_
11 
12 #include "src/heap/factory.h"
13 #include "src/isolate.h"
14 #include "src/objects.h"
15 #include "unicode/uversion.h"
16 
17 // Has to be the last include (doesn't have include guards):
18 #include "src/objects/object-macros.h"
19 
20 namespace U_ICU_NAMESPACE {
21 class ListFormatter;
22 }
23 
24 namespace v8 {
25 namespace internal {
26 
27 class JSListFormat : public JSObject {
28  public:
29   // Initializes relative time format object with properties derived from input
30   // locales and options.
31   static MaybeHandle<JSListFormat> InitializeListFormat(
32       Isolate* isolate, Handle<JSListFormat> list_format_holder,
33       Handle<Object> locales, Handle<Object> options);
34 
35   static Handle<JSObject> ResolvedOptions(Isolate* isolate,
36                                           Handle<JSListFormat> format_holder);
37 
38   // Unpacks formatter object from corresponding JavaScript object.
39   static icu::ListFormatter* UnpackFormatter(
40       Isolate* isolate, Handle<JSListFormat> list_format_holder);
41 
42   // ecma402 #sec-formatlist
43   V8_WARN_UNUSED_RESULT static MaybeHandle<String> FormatList(
44       Isolate* isolate, Handle<JSListFormat> format_holder,
45       Handle<JSArray> list);
46 
47   // ecma42 #sec-formatlisttoparts
48   V8_WARN_UNUSED_RESULT static MaybeHandle<JSArray> FormatListToParts(
49       Isolate* isolate, Handle<JSListFormat> format_holder,
50       Handle<JSArray> list);
51 
52   Handle<String> StyleAsString() const;
53   Handle<String> TypeAsString() const;
54 
55   DECL_CAST(JSListFormat)
56 
57   // ListFormat accessors.
58   DECL_ACCESSORS(locale, String)
59   DECL_ACCESSORS(formatter, Foreign)
60 
61   // Style: identifying the relative time format style used.
62   //
63   // ecma402/#sec-properties-of-intl-listformat-instances
64   enum class Style {
65     LONG,    // Everything spelled out.
66     SHORT,   // Abbreviations used when possible.
67     NARROW,  // Use the shortest possible form.
68     COUNT
69   };
70   inline void set_style(Style style);
71   inline Style style() const;
72 
73   // Type: identifying the list of types used.
74   //
75   // ecma402/#sec-properties-of-intl-listformat-instances
76   enum class Type {
77     CONJUNCTION,  // for "and"-based lists (e.g., "A, B and C")
78     DISJUNCTION,  // for "or"-based lists (e.g., "A, B or C"),
79     UNIT,  // for lists of values with units (e.g., "5 pounds, 12 ounces").
80     COUNT
81   };
82   inline void set_type(Type type);
83   inline Type type() const;
84 
85 // Bit positions in |flags|.
86 #define FLAGS_BIT_FIELDS(V, _) \
87   V(StyleBits, Style, 2, _)    \
88   V(TypeBits, Type, 2, _)
89   DEFINE_BIT_FIELDS(FLAGS_BIT_FIELDS)
90 #undef FLAGS_BIT_FIELDS
91 
92   STATIC_ASSERT(Style::LONG <= StyleBits::kMax);
93   STATIC_ASSERT(Style::SHORT <= StyleBits::kMax);
94   STATIC_ASSERT(Style::NARROW <= StyleBits::kMax);
95   STATIC_ASSERT(Type::CONJUNCTION <= TypeBits::kMax);
96   STATIC_ASSERT(Type::DISJUNCTION <= TypeBits::kMax);
97   STATIC_ASSERT(Type::UNIT <= TypeBits::kMax);
98 
99   // [flags] Bit field containing various flags about the function.
100   DECL_INT_ACCESSORS(flags)
101 
102   DECL_PRINTER(JSListFormat)
103   DECL_VERIFIER(JSListFormat)
104 
105   // Layout description.
106   static const int kJSListFormatOffset = JSObject::kHeaderSize;
107   static const int kLocaleOffset = kJSListFormatOffset + kPointerSize;
108   static const int kFormatterOffset = kLocaleOffset + kPointerSize;
109   static const int kFlagsOffset = kFormatterOffset + kPointerSize;
110   static const int kSize = kFlagsOffset + kPointerSize;
111 
112  private:
113   DISALLOW_IMPLICIT_CONSTRUCTORS(JSListFormat);
114 };
115 
116 }  // namespace internal
117 }  // namespace v8
118 
119 #include "src/objects/object-macros-undef.h"
120 
121 #endif  // V8_OBJECTS_JS_LIST_FORMAT_H_
122