V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
js-relative-time-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_RELATIVE_TIME_FORMAT_H_
10 #define V8_OBJECTS_JS_RELATIVE_TIME_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/managed.h"
19 #include "unicode/uversion.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 RelativeDateTimeFormatter;
26 }
27 
28 namespace v8 {
29 namespace internal {
30 
32  public:
33  // Initializes relative time format object with properties derived from input
34  // locales and options.
35  V8_WARN_UNUSED_RESULT static MaybeHandle<JSRelativeTimeFormat> Initialize(
36  Isolate* isolate,
37  Handle<JSRelativeTimeFormat> relative_time_format_holder,
38  Handle<Object> locales, Handle<Object> options);
39 
40  V8_WARN_UNUSED_RESULT static Handle<JSObject> ResolvedOptions(
41  Isolate* isolate, Handle<JSRelativeTimeFormat> format_holder);
42 
43  Handle<String> StyleAsString() const;
44  Handle<String> NumericAsString() const;
45 
46  // ecma402/#sec-Intl.RelativeTimeFormat.prototype.format
47  // ecma402/#sec-Intl.RelativeTimeFormat.prototype.formatToParts
48  V8_WARN_UNUSED_RESULT static MaybeHandle<Object> Format(
49  Isolate* isolate, Handle<Object> value_obj, Handle<Object> unit_obj,
50  Handle<JSRelativeTimeFormat> format_holder, const char* func_name,
51  bool to_parts);
52 
53  static std::set<std::string> GetAvailableLocales();
54 
55  DECL_CAST(JSRelativeTimeFormat)
56 
57  // RelativeTimeFormat accessors.
58  DECL_ACCESSORS2(locale, String)
59 
60  DECL_ACCESSORS(icu_formatter, Managed<icu::RelativeDateTimeFormatter>)
61 
62  // Style: identifying the relative time format style used.
63  //
64  // ecma402/#sec-properties-of-intl-relativetimeformat-instances
65 
66  enum class Style {
67  LONG, // Everything spelled out.
68  SHORT, // Abbreviations used when possible.
69  NARROW, // Use the shortest possible form.
70  COUNT
71  };
72  inline void set_style(Style style);
73  inline Style style() const;
74 
75  // Numeric: identifying whether numerical descriptions are always used, or
76  // used only when no more specific version is available (e.g., "1 day ago" vs
77  // "yesterday").
78  //
79  // ecma402/#sec-properties-of-intl-relativetimeformat-instances
80  enum class Numeric {
81  ALWAYS, // numerical descriptions are always used ("1 day ago")
82  AUTO, // numerical descriptions are used only when no more specific
83  // version is available ("yesterday")
84  COUNT
85  };
86  inline void set_numeric(Numeric numeric);
87  inline Numeric numeric() const;
88 
89 // Bit positions in |flags|.
90 #define FLAGS_BIT_FIELDS(V, _) \
91  V(StyleBits, Style, 2, _) \
92  V(NumericBits, Numeric, 1, _)
93  DEFINE_BIT_FIELDS(FLAGS_BIT_FIELDS)
94 #undef FLAGS_BIT_FIELDS
95 
96  STATIC_ASSERT(Style::LONG <= StyleBits::kMax);
97  STATIC_ASSERT(Style::SHORT <= StyleBits::kMax);
98  STATIC_ASSERT(Style::NARROW <= StyleBits::kMax);
99  STATIC_ASSERT(Numeric::AUTO <= NumericBits::kMax);
100  STATIC_ASSERT(Numeric::ALWAYS <= NumericBits::kMax);
101 
102  // [flags] Bit field containing various flags about the function.
103  DECL_INT_ACCESSORS(flags)
104 
105  DECL_PRINTER(JSRelativeTimeFormat)
106  DECL_VERIFIER(JSRelativeTimeFormat)
107 
108  // Layout description.
109 #define JS_RELATIVE_TIME_FORMAT_FIELDS(V) \
110  V(kJSRelativeTimeFormatOffset, kTaggedSize) \
111  V(kLocaleOffset, kTaggedSize) \
112  V(kICUFormatterOffset, kTaggedSize) \
113  V(kFlagsOffset, kTaggedSize) \
114  /* Header size. */ \
115  V(kSize, 0)
116 
117  DEFINE_FIELD_OFFSET_CONSTANTS(JSObject::kHeaderSize,
118  JS_RELATIVE_TIME_FORMAT_FIELDS)
119 #undef JS_RELATIVE_TIME_FORMAT_FIELDS
120 
121  private:
122  static Style getStyle(const char* str);
123  static Numeric getNumeric(const char* str);
124 
125  DISALLOW_IMPLICIT_CONSTRUCTORS(JSRelativeTimeFormat);
126 };
127 
128 } // namespace internal
129 } // namespace v8
130 
131 #include "src/objects/object-macros-undef.h"
132 
133 #endif // V8_OBJECTS_JS_RELATIVE_TIME_FORMAT_H_
Definition: libplatform.h:13